View Full Version : Dynamic plot coloring
ThePatientOne
03-31-2007, 05:56 AM
I am trying to color my plot lines dynamically based on their direction. I found some code in the Help file about this and tried it. However, when I try running the code, the entire indicator is colored based on the value of the indicators on the last bar on the chart.
Here is my code:
if (Trigger[0] < TriggerAvg[0])
{
Plots[0].Pen = new Pen(Color.Magenta);
Plots[1].Pen = new Pen(Color.Magenta);
}
else if (Trigger[0] > TriggerAvg[0])
{
Plots[0].Pen = new Pen(Color.Lime);
Plots[1].Pen = new Pen(Color.Lime);
}
NinjaTrader_Ray
03-31-2007, 06:09 AM
See this post -
http://ninjatrader.mywowbb.com/forum14/1794.html
Ray
ThePatientOne
03-31-2007, 06:24 AM
Thanks Ray ...but ... does this mean I need to have four (4) plots to plot a two line indicator set? 2 plots for the up color and 2 for the down color?
That's what it looks like to me based on that code.
NinjaTrader_Ray
03-31-2007, 06:28 AM
If you want a 2 line indicator where both lines change color based on direction then yes, you need 4 plots, 2 up and 2 down.
Ray
ThePatientOne
03-31-2007, 06:32 AM
OK ... think I understand now.
If I wanted a two line indicator with three possible colors for each, I would then need 6 plots, correct?
NinjaTrader_Ray
03-31-2007, 06:50 AM
That is correct.
Ray
OUFan
03-31-2007, 02:09 PM
I am in need of help with some similar code. I have a moving average that I want to plot four colors on. Green for the major up move, darkgreen for a pullback against the major up move, Red for major down trend and darkred for pullback against major down trend. These major trends and pullbacks will be determined by my code.
I found this code exampleon the forums that I thought something similarwould work with my calculations, but when I put this type of code in the indicator, I get no plot at all. This is not my code, but this is code I found on the forum and I can't get it to work. My code needs only oneMoving Average with 4 color changes on the one moving average.
protected override void OnBarUpDate()
{
if(SMA(5)[0] > SMA(15)[0]
Plots[0].Pen.Color = Color.Green
else
Plots[0].Pen.Color = Color.Red;
}
I am an experienced TradeStation EL Programmer and this is very easy to do in their language, but I am stumped with Ninja Script.
Thanks, for any Help
whitmark
03-31-2007, 11:07 PM
OUFan,
Yes, this is one area that TS makesit much easier and perhaps in time it will be made more straightfoward in Ninja and reduce the number of plots required. If you want to usenative Ninja plot functionality (and not the plot override that uses C# graphics object) I think this is what you are looking for but I have not tested this specific code snippet:
// Determine Color forPlot1 Line
if (SMA(5)[0] > SMA(15)[0])
{
Plot1_Line_XUp.Set(1, Plot_Value[1]); //Needed for Line Plots
Plot1_Line_XUp.Set(Plot_Value[0]); // Needed for All Plots
}
else
{
Plot1_Line_XDn.Set(1, Plot_Value[1]); //Needed for Line Plots
Plot1_Line_XDn.Set(Plot_Value[0]); // Needed for All Plots
}
As you can see there is a separate plot for crossing up vs down, and it is a matter of "Setting" the plot line (with appropriate color) you want to display. If you are using only non-continuous plotstyles (e.g., dots, bars) you can get by with only " Plot1_Line_XUp.Set(Plot_Value[0]);" for example but if you want to ensure there is not a gap between different color plots, you will need to use an additional plot set to draw the from the previous bar as indicated.
Hope that helps
Regards,
Whitmark
OUFan
04-01-2007, 04:58 AM
Whitmark,
Thank you very much for your help, but could I ask one other favor of you. I am just now learning Ninja Script and am not exactly sure how and where to put the color changes in the code you just showed. If I was going to use Green for up and Red for down, could you show me howto code that into the code you posted.
Thank You for Your Help,
OUFan
NinjaTrader_Ray
04-01-2007, 08:32 AM
OUFan,
You have to add plots in the Initialize() method. The easiest way to do that if you are not just getting familiar with C# is to use the New Indicator wizard.
Start a new indicator, and within the wizard, add your four plots. When you are complete with the wizard, it will generate the code for you in the Initialize() method.
Ray
OUFan
04-01-2007, 02:03 PM
Ray,
Could you give a little more code example for changing colors on a moving average. I did as you said and made2 plots, but it only plots the color of the last calculation on all the historical data. I wanted to plot the color green when the fast moving average was above the slow moving average and red when the fast moving average was below the fast moving average. The fast moving average was above the slow moving average on my data on the last bar, so it made the slow moving average that I was plotting be all green, even though I could see plenty of times the fast moving average was below the fast moving average which should have made the moving average red.
I am wanting to only plot the slow moving average, but make it change colors when it met my above criteria. I would appreciate your help.
OUFan
NinjaTrader_Ray
04-02-2007, 01:06 AM
See attached sample indicator.
Unzip the attached file in My Documents\NinjaTrader 6\bin\Custom\Indicator
This will contain an indicator named DualColorLine.
Ray