NinjaTrader Support Forum  

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 12-25-2007, 07:45 AM   #1
monpere
Senior Member
 
Join Date: Dec 2007
Posts: 310
Thanks: 0
Thanked 5 times in 3 posts
Default Generic Indicator Overlay on price

I've altered the Plot method originally from user 'Josh' s Stochastic and RSI Overlay code, to be more generic. Just add the following code to your favorite indicator to overlay it on top of price

//[Gui.Design.DisplayName(".Test")]
//Add the following method under the 'NinjaTrader.Indicator' namespace
//to override the 'Plot' method to draw the indicator overlayed on the price chart
public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
{
// Add the following variables to Initialize() method
// AutoScale = false;
// Overlay = true;
//
// The following variables can be moved out to '#region Variables'
// and corresponding '#region Properties' entries added to make these
// variables user selectable from the indicator properties sheet
// Example: for scaleHigh:
//
// Delete the scaleHigh variable from this routine.
// In '#region Variables' add the following:
// private double scaleHigh = 100;
//
// In '#region Properties' add the following:
// [Description("Scale High")]
// [Category("Parameters")]
// [Gui.Design.DisplayName("Scale High")]
// public double Scale_High
// {
// get { return scaleHigh; }
// set { scaleHigh = value; }
// }
bool drawscale = true; // Limited scale marks
double scaleHigh = 200; // Max value of indicator (100 for Stochastic, +400 for CCI, etc.)
double scaleLow = -200; // Min value of indicator (0 for Stochastic, -400 for CCI, etc.)
double upperline = 175; // Optional horizontal line 1 (80 for Stochastic)
double lowerline = -175; // Optional horizontal line 2 (20 for Stochastic)
Color uppercolor = Color.Green; // Color for line 1
Color lowercolor = Color.Green; // Color for line 2

//Draw Scale
StringFormat stringFormat = new StringFormat();
SolidBrush textBrush = new SolidBrush(Color.Black);
System.Drawing.Font textFont = new Font("Arial", 8);
double scaleRange = (scaleHigh - scaleLow);
double scaleShift = ( scaleLow < 0 ) ? -scaleLow : 0;
if(drawscale)
{
//Upper line
double line = ((upperline+scaleShift)/scaleRange)*100;
graphics.DrawLine(new Pen(uppercolor,2),bounds.X,(float)((bounds.Bottom-bounds.Y)*(1-((double)line/100))),bounds.X+bounds.Width,(float)((bounds.Botto m-bounds.Y)*(1-((double)line/100))));
graphics.DrawString(upperline.ToString(), textFont, textBrush, bounds.X, (float)((bounds.Bottom-bounds.Y)*(1-((double)line/100))),stringFormat);

//1/4 up the scale
line = (scaleRange*.75)+scaleLow;
graphics.DrawString(line.ToString(), textFont, textBrush, bounds.X, (float)((bounds.Bottom-bounds.Y)*(1-0.75)),stringFormat);

//Center Line
line = ((scaleRange/2)+scaleLow)*100;
graphics.DrawLine(new Pen(Color.DarkGray,2),bounds.X,(float)((bounds.Bot tom-bounds.Y)*0.50),bounds.X+bounds.Width,(float)((bou nds.Bottom-bounds.Y)*0.50));
graphics.DrawString(((scaleRange/2)+scaleLow).ToString(), textFont, textBrush, bounds.X, (float)((bounds.Bottom-bounds.Y)*(1-0.50)),stringFormat);

//3/4 up the scale
line = (scaleRange*.25)+scaleLow;
graphics.DrawString(line.ToString(), textFont, textBrush, bounds.X, (float)((bounds.Bottom-bounds.Y)*(1-0.25)),stringFormat);

//Lower line
line = ((lowerline+scaleShift)/scaleRange)*100;
graphics.DrawLine(new Pen(lowercolor,2),bounds.X,(float)((bounds.Bottom-bounds.Y)*(1-((double)line/100))),bounds.X+bounds.Width,(float)((bounds.Botto m-bounds.Y)*(1-((double)line/100))));
graphics.DrawString(lowerline.ToString(), textFont, textBrush, bounds.X, (float)((bounds.Bottom-bounds.Y)*(1-((double)line/100))),stringFormat);
}

//Plot Indicator
int barWidth = ChartControl.ChartStyle.GetBarPaintWidth(ChartCont rol.BarWidth);
SmoothingMode oldSmoothingMode = graphics.SmoothingMode;
GraphicsPath dpath = new GraphicsPath();
int lastX = -1;
int lastY = -1;
DataSeries series = (DataSeries) Values[0];
double seriesvalue = 0;
Gui.Chart.Plot plot = Plots[0];
for(int count = 0; count < ChartControl.BarsPainted; count++)
{
int idx = ChartControl.LastBarPainted - ChartControl.BarsPainted + 1 + count;
if (idx < 0 || idx >= Input.Count || (!ChartControl.ShowBarsRequired && idx < BarsRequired))
continue;

seriesvalue = ((series.Get(idx))+scaleShift)/scaleRange;
if (seriesvalue == 0)
continue;

int x = (int) (ChartControl.CanvasRight - ChartControl.BarMarginRight - barWidth / 2 - (ChartControl.BarsPainted - 1) * ChartControl.BarSpace + count * ChartControl.BarSpace) + 1;
int y = (int) ((bounds.Bottom-bounds.Y)*(1-seriesvalue));

if (lastX >= 0)
{
dpath.AddLine(lastX - plot.Pen.Width / 2, lastY, x - plot.Pen.Width / 2, y);
}

lastX = x;
lastY = y;
}
dpath.Reverse();

graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawPath(Plots[0].Pen, dpath);
graphics.SmoothingMode = oldSmoothingMode;
}
monpere is offline  
Reply With Quote
Old 12-25-2007, 01:17 PM   #2
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Awesome monpere. Thanks!
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-29-2007, 04:57 PM   #3
sbgtrading
Certified NinjaScript Consultant
 
Join Date: Oct 2007
Location: Northern Illinois
Posts: 256
Thanks: 0
Thanked 3 times in 3 posts
Default

Excellent! Thank you!
sbgtrading is offline  
Reply With Quote
Old 12-29-2007, 05:06 PM   #4
sbgtrading
Certified NinjaScript Consultant
 
Join Date: Oct 2007
Location: Northern Illinois
Posts: 256
Thanks: 0
Thanked 3 times in 3 posts
Default

Hmmm...can you give us an example of exactly where to paste your code in? I've been unable to find the right location...keep getting compile errors.
sbgtrading is offline  
Reply With Quote
Old 12-29-2007, 05:29 PM   #5
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

It would go outside of the OnBarUpdate(), but inside the Indicator namespace.
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-29-2007, 07:14 PM   #6
sbgtrading
Certified NinjaScript Consultant
 
Join Date: Oct 2007
Location: Northern Illinois
Posts: 256
Thanks: 0
Thanked 3 times in 3 posts
Default

Hmmm...still having trouble. Can someone check the posted code to see if it's missing a "}"...or has too many of them perhaps? If not, then can someone post a test indicator that has this new code in there correctly? Thanks!


Quote:
Originally Posted by Josh View Post
It would go outside of the OnBarUpdate(), but inside the Indicator namespace.
sbgtrading is offline  
Reply With Quote
Old 12-29-2007, 09:27 PM   #7
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

What you probably experienced was some weirdness in the formatting when you copy and paste the code. Extra white space was added between variable names so that caused errors.
Attached Files
File Type: zip genericoverlay.zip (2.4 KB, 113 views)
NinjaTrader_Josh is offline  
Reply With Quote
Old 04-13-2008, 06:33 AM   #8
MichaelB
Member
 
Join Date: Jan 2007
Location: , ,
Posts: 44
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi Josh,
Is this a possible starting area to create a MACD Line overlay on price chart indicator?
ie, instead of having the "Diff" option set to Histogram , instead it is selected to Line to represent the value above and below the horizontal zero line ?
Thanks
Michael b
MichaelB is offline  
Reply With Quote
Old 04-13-2008, 02:02 PM   #9
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

For sure MichaelB, but this is beyond the level of support we can offer. Feel free to play around with the code though. I believe that is the reason why monpere released it .
NinjaTrader_Josh is offline  
Reply With Quote
Old 04-17-2008, 05:26 AM   #10
MichaelB
Member
 
Join Date: Jan 2007
Location: , ,
Posts: 44
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi Josh,
Thanks
cheers
Michael B
MichaelB 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stochastics Overlay NinjaTrader_Josh NinjaScript File Sharing Discussion 18 08-14-2009 01:42 PM
Using C#.Net 2.0 Generic Lists in NinjaScript whitmark Strategy Development 1 10-08-2007 07:08 AM
RSI Overlay NinjaTrader_Josh NinjaScript File Sharing Discussion 0 08-10-2007 08:58 PM
Overlay Stochastic on Price in NT6? pbastowski Charting 2 08-10-2007 05:01 AM
How to overlay indicator over price pane? Del Charting 1 11-03-2006 12:56 AM


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