![]() |
|
|||||||
| Indicator Development Support for the development of custom indicators using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Dec 2007
Posts: 310
Thanks: 0
Thanked 5 times in 3 posts
|
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; } |
|
|
|
|
|
#2 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
Awesome monpere. Thanks!
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Certified NinjaScript Consultant
Join Date: Oct 2007
Location: Northern Illinois
Posts: 256
Thanks: 0
Thanked 3 times in 3 posts
|
Excellent! Thank you!
|
|
|
|
|
|
#4 |
|
Certified NinjaScript Consultant
Join Date: Oct 2007
Location: Northern Illinois
Posts: 256
Thanks: 0
Thanked 3 times in 3 posts
|
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.
|
|
|
|
|
|
#5 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
It would go outside of the OnBarUpdate(), but inside the Indicator namespace.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#6 |
|
Certified NinjaScript Consultant
Join Date: Oct 2007
Location: Northern Illinois
Posts: 256
Thanks: 0
Thanked 3 times in 3 posts
|
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!
|
|
|
|
|
|
#7 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Member
Join Date: Jan 2007
Location: , ,
Posts: 44
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#9 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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
.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#10 |
|
Member
Join Date: Jan 2007
Location: , ,
Posts: 44
Thanks: 0
Thanked 0 times in 0 posts
|
Hi Josh,
Thanks cheers Michael B |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |