View Full Version : Plotting background colors between bands
rt-trader
01-15-2007, 10:30 PM
I want toaddcode to a Ninja Script so that a different background color can be drawn between say an Upper and Lower Bollinger band. Apparently this can be done by over riding the Plot method with a custom drawing using native .Net graphics classes.
If anyone knows how to do this and is happy to share could you please post a few lines of sample code which shows the underlying instruction syntax necessary to achieve the desired result.
Many thanks
tquinn
01-17-2007, 02:34 AM
I'm a beginner with C#, and I wanted the same thing. I could not figure how to do it with .Net graphics so I settled for using NinjaScript to draw rectangles.
if (Middle[0]>Middle[1])
BandColor=Color.Blue;
else
BandColor=Color.Red;
DrawRectangle("R"+CurrentBar.ToString(),
1,Math.Max(Lower[0],Lower[1]),
0,Math.Min(Upper[0],Upper[1]),
Color.Transparent,BandColor,1);
rt-trader
01-17-2007, 05:58 AM
Thankyou for the post tquinn - will give what you suggest a try......
Raditz
01-18-2007, 06:26 PM
Hi
Try this
SolidBrush brush = new SolidBrush(Color.Gray);
int barWidth = ChartControl.ChartStyle.GetBarPaintWidth(ChartCont rol.BarWidth);
SmoothingMode oldSmoothingMode = graphics.SmoothingMode;
GraphicsPath path = new GraphicsPath();
for (int seriesIndex = 0; seriesIndex < 2; seriesIndex++)
{
int lastX = -1;
int lastY = -1;
DataSeries series = (DataSeries) Values[seriesIndex];
double val = 0;
Gui.Chart.Plot plot = Plots[seriesIndex];
for (int barIndex = 0; barIndex < ChartControl.BarsPainted; barIndex++)
{
int idx = ChartControl.LastBarPainted - ChartControl.BarsPainted + 1 + barIndex;
if (idx < 0 || idx >= Input.Count || (!ChartControl.ShowBarsRequired && idx < BarsRequired))
continue;
val = series.Get(idx);
int x = (int) (ChartControl.CanvasRight - ChartControl.BarMarginRight - barWidth / 2
- (ChartControl.BarsPainted - 1) * ChartControl.BarSpace + barIndex * ChartControl.BarSpace) + 1;
int y = (int) ((bounds.Y + bounds.Height) - ((val - min ) / (max - min)) * bounds.Height);
if (lastX >= 0)
{
path.AddLine(lastX - plot.Pen.Width / 2, lastY, x - plot.Pen.Width / 2, y);
}
lastX = x;
lastY = y;
}
path.Reverse();
}
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.FillPath(brush, path);
graphics.SmoothingMode = oldSmoothingMode;
Muly
Final
http://fin-alg.com/
tquinn
01-19-2007, 02:25 PM
Muly,
I'm not good enough with C# to use this. I placed it in OnBarUpdate() and it produced a bunch of compile errors. I'm sure it is my lack of C# skills.
rt-trader
01-19-2007, 04:09 PM
Wow,
Well beyond my skills too! Thanks heaps for posting this Muly.Iattempt to get it working...
Cheers
Raditz
01-19-2007, 05:24 PM
Hi
the post bellow is the code for the Plot function
public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
Muly
Final
http://fin-alg.com/
tquinn
01-20-2007, 02:16 AM
Muly,
It is good to get a lesson on how much there is to learn about a new programmimg language. If I understand your code, it is plotting vertical lines between Values[0]
and Values[1]. So I've created only those 2 Values.
This is what I have so far, but it still will not compile. Errors about "using directives" and "assemble references", things I know nothing about.
protected override void Initialize()
{
Add(new Plot(Color.Orange, "Upper band"));
Add(new Plot(Color.Orange, "Lower band"));
}
protected override void OnBarUpdate()
{
Upper.Set(SMA(14)[0] + 2 * StdDev(14)[0]);
Lower.Set(SMA(14)[0] - 2 * StdDev(14)[0]);
}
public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
{
// Your code from below
}
NinjaTrader_Dierk
01-20-2007, 04:16 AM
Please paste in error messages. They likely are not related to Muly's code. Likely your NT installation is screwed up.
Raditz
01-20-2007, 04:25 AM
Probebly the using directive is missing some here is my
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Xml.Serialization;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
make sure you include all
Muly
Final
http://fin-alg.com/
tquinn
01-20-2007, 05:18 AM
Thanks all,
I was missing " using System.Drawing.Drawing2D;", Muly's code runs fine now.
Now I need to learn how to put the Colored area behind the price bars, and maybe change the opacity. I'll save this lesson for a later day.
I need to get back to Mastering NinjaScript before I wander too far off track, into the world of C# and .NET.
Thanks again
NinjaTrader_Dierk
01-20-2007, 05:24 AM
Tom,
Just curious:
"using System.Drawing.Drawing2D;"
should be part of the code generated by the indicator wizard. Do you know why it was not there?
Thanks