NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Sunday May 26th at 12PM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 01-15-2007, 10:30 PM   #1
rt-trader
Senior Member
 
Join Date: Jan 2007
Location: , ,
Posts: 250
Thanks: 1
Thanked 2 times in 1 post
Post imported post

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




rt-trader is offline  
Reply With Quote
Old 01-17-2007, 02:34 AM   #2
tquinn
Senior Member
 
Join Date: Jan 2005
Location: , ,
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

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.

Quote:
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);
tquinn is offline  
Reply With Quote
Old 01-17-2007, 05:58 AM   #3
rt-trader
Senior Member
 
Join Date: Jan 2007
Location: , ,
Posts: 250
Thanks: 1
Thanked 2 times in 1 post
Post imported post

Thankyou for the post tquinn - will give what you suggest a try......


rt-trader is offline  
Reply With Quote
Old 01-18-2007, 06:26 PM   #4
Raditz
Junior Member
 
Join Date: May 2006
Location: Certified NinjaScript Consultant, ,
Posts: 8
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

Hi

Try this

Code:
SolidBrush brush = new SolidBrush(Color.Gray);
int barWidth = ChartControl.ChartStyle.GetBarPaintWidth(ChartControl.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/
Raditz is offline  
Reply With Quote
Old 01-19-2007, 02:25 PM   #5
tquinn
Senior Member
 
Join Date: Jan 2005
Location: , ,
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

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.
tquinn is offline  
Reply With Quote
Old 01-19-2007, 04:09 PM   #6
rt-trader
Senior Member
 
Join Date: Jan 2007
Location: , ,
Posts: 250
Thanks: 1
Thanked 2 times in 1 post
Post imported post

Wow,

Well beyond my skills too! Thanks heaps for posting this Muly.Iattempt to get it working...

Cheers


rt-trader is offline  
Reply With Quote
Old 01-19-2007, 05:24 PM   #7
Raditz
Junior Member
 
Join Date: May 2006
Location: Certified NinjaScript Consultant, ,
Posts: 8
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

Hi

the post bellow is the code for the Plot function

Code:
public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
Muly
Final
http://fin-alg.com/
Raditz is offline  
Reply With Quote
Old 01-20-2007, 02:16 AM   #8
tquinn
Senior Member
 
Join Date: Jan 2005
Location: , ,
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

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.

Quote:
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
}
tquinn is offline  
Reply With Quote
Old 01-20-2007, 04:16 AM   #9
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Post imported post

Please paste in error messages. They likely are not related to Muly's code. Likely your NT installation is screwed up.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 01-20-2007, 04:25 AM   #10
Raditz
Junior Member
 
Join Date: May 2006
Location: Certified NinjaScript Consultant, ,
Posts: 8
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

Probebly the using directive is missing some here is my

Code:
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/
Raditz is offline  
Reply With Quote
Old 01-20-2007, 05:18 AM   #11
tquinn
Senior Member
 
Join Date: Jan 2005
Location: , ,
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

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
tquinn is offline  
Reply With Quote
Old 01-20-2007, 05:24 AM   #12
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Post imported post

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


NinjaTrader_Dierk is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 03:02 AM.