View Full Version : Easy Indicator do not plot....
xenayoo
06-04-2010, 02:55 AM
Hello,
i need a little help with this Indicator:
protected override void Initialize()
{
Add(new Plot(new Pen(Color.DarkGreen, 5), PlotStyle.Bar, "UpVol"));
Add(new Plot(new Pen(Color.Red, 5), PlotStyle.Bar, "DnVol"));
Add(new Plot(new Pen(Color.Black,1), PlotStyle.Bar, "Lower"));
Add(new Plot(new Pen(Color.Yellow,3), PlotStyle.Bar, "Greater"));
CalculateOnBarClose = true;
Overlay = false;
PriceTypeSupported = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
if(Close[0] > Open[0])
UpVol.Set(Volume[0]);
else
DnVol.Set(Volume[0]);
HighVolume = MAX(Volume, Period)[1];
if(Volume[0] > HighVolume)
Lower.Set(Volume[0]);
else
Greater.Set(Volume[0]);
}
I am just blind at the moment - i don't see the reason, why this indicator just plot nothing. A little more infos: The panel-scale shows 0 0,5 and 1, so I suppose, that there is an issue with my calculation with Volume[0]. Second: I can compile this indicator without any problems, but i can't export: compiling error.....
Please help.
NinjaTrader_Bertrand
06-04-2010, 02:59 AM
You likely run into this error explained in the tip below, as you do not have a check for enough CurrentBars to be present before continuing in your OnBarUpdate() - http://www.ninjatrader.com/support/forum/showthread.php?t=3170
xenayoo
06-04-2010, 03:20 AM
Hello Bertrand. Great and fast help. That's it.... Thx a lot.
xenayoo
06-28-2010, 09:26 AM
Ok, now next problem: I want to change the System DMI-Indicator. I just combined the system DMI and the custom plot color sample for the ADX line. I changed nothing in the calculations. But it doesn't work: Error message in Log: "Error calling the 'OnBarUpdate' method for indicator 'ADXmc' on bar1....
Why does the original indicator work an my indicator not? I copied the whole code from DMI to the new Indicator and add the "AddNewPlot"....
edit: Here is my code
public class ADXmc : Indicator
{
#region Variables
private int period = 14;
private DataSeries dmPlus;
private DataSeries dmMinus;
private DataSeries sumDmPlus;
private DataSeries sumDmMinus;
private DataSeries sumTr;
private DataSeries tr;
private DataSeries adx;
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(new Pen(Color.Blue, 2), "ADXmed"));
Add(new Plot(new Pen(Color.Violet, 2), "ADXdown"));
Add(new Plot(new Pen(Color.Cyan, 2), "ADXup"));
Add(new Plot(Color.Green, "+DI"));
Add(new Plot(Color.Red, "-DI"));
Add(new Line(Color.DarkViolet, 25, "Lower"));
Add(new Line(Color.YellowGreen, 75, "Upper"));
dmPlus = new DataSeries(this);
dmMinus = new DataSeries(this);
sumDmPlus = new DataSeries(this);
sumDmMinus = new DataSeries(this);
sumTr = new DataSeries(this);
tr = new DataSeries(this);
adx = new DataSeries(this);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
double trueRange = High[0] - Low[0];
if (CurrentBar <= 10)
{
tr.Set(trueRange);
dmPlus.Set(0);
dmMinus.Set(0);
sumTr.Set(tr[0]);
sumDmPlus.Set(dmPlus[0]);
sumDmMinus.Set(dmMinus[0]);
Adx.Set(50);
ADXmed.Set(50);
}
else
{
tr.Set(Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(trueRange, Math.Abs(High[0] - Close[1]))));
dmPlus.Set(High[0] - High[1] > Low[1] - Low[0] ? Math.Max(High[0] - High[1], 0) : 0);
dmMinus.Set(Low[1] - Low[0] > High[0] - High[1] ? Math.Max(Low[1] - Low[0], 0) : 0);
if (CurrentBar < Period)
{
sumTr.Set(sumTr[1] + tr[0]);
sumDmPlus.Set(sumDmPlus[1] + dmPlus[0]);
sumDmMinus.Set(sumDmMinus[1] + dmMinus[0]);
}
else
{
sumTr.Set(sumTr[1] - sumTr[1] / Period + tr[0]);
sumDmPlus.Set(sumDmPlus[1] - sumDmPlus[1] / Period + dmPlus[0]);
sumDmMinus.Set(sumDmMinus[1] - sumDmMinus[1] / Period + dmMinus[0]);
}
double diPlus = 100 * (sumTr[0] == 0 ? 0 : sumDmPlus[0] / sumTr[0]);
double diMinus = 100 * (sumTr[0] == 0 ? 0 : sumDmMinus[0] / sumTr[0]);
double diff = Math.Abs(diPlus - diMinus);
double sum = diPlus + diMinus;
Adx.Set(sum == 0 ? 50 : ((Period - 1) * Value[1] + 100 * diff / sum) / Period);
if (Adx[0] == Adx[1])
{
ADXmed.Set(1,Adx[1]);
ADXmed.Set(Adx[0]);
}
else if (Adx[0] < Adx[1])
{
ADXdown.Set(1,Adx[1]);
ADXdown.Set(Adx[0]);
}
else
{
ADXup.Set(1,Adx[1]);
ADXup.Set(Adx[0]);
}
DiPlus.Set(diPlus);
DiMinus.Set(diMinus);
}
}
Thank you for help....
NinjaTrader_Bertrand
06-28-2010, 09:39 AM
Did you port over the CurrentBars check from the reference sample into your custom indicator as well? The error suggests this was not done...
xenayoo
06-28-2010, 09:46 AM
Hi Bertrand,
i add the code in my previous posting. And yes, you will see, i checked the current bar. In the original Indicator the checking was:
if (CurrentBar == 0)
but it doesn't work at all....
NinjaTrader_Bertrand
06-28-2010, 09:49 AM
xenayoo, please try adding something like this to the OnBarUpdate() start -
if (CurrentBar < 1) return;
Your snippet would not resolve the issue of trying to access a series value where it does not exist...reaching one bar back at the first bar is just not possible.
xenayoo
06-28-2010, 10:05 AM
Doesn't work....
NinjaTrader_Bertrand
06-28-2010, 10:08 AM
Please post the code you use so we could take a look - thanks.
xenayoo
06-29-2010, 03:54 AM
Please post the code you use so we could take a look - thanks.
Hm, i posted the whole code in my previous posting. But here is the code with latest changes...
Again: It'sexactly the same code from NT-DMI-indicator, wich works fine. I just want to change the color of the ADX-line to multicolor....
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
[Description("Enter the description of your new custom indicator here")]
public class ADXmc : Indicator
{
#region Variables
private int period = 14;
private DataSeries dmPlus;
private DataSeries dmMinus;
private DataSeries sumDmPlus;
private DataSeries sumDmMinus;
private DataSeries sumTr;
private DataSeries tr;
private DataSeries adx;
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(new Pen(Color.Blue, 2), "ADXmed"));
Add(new Plot(new Pen(Color.Violet, 2), "ADXdown"));
Add(new Plot(new Pen(Color.Cyan, 2), "ADXup"));
Add(new Plot(Color.Green, "+DI"));
Add(new Plot(Color.Red, "-DI"));
Add(new Line(Color.DarkViolet, 25, "Lower"));
Add(new Line(Color.YellowGreen, 75, "Upper"));
dmPlus = new DataSeries(this);
dmMinus = new DataSeries(this);
sumDmPlus = new DataSeries(this);
sumDmMinus = new DataSeries(this);
sumTr = new DataSeries(this);
tr = new DataSeries(this);
adx = new DataSeries(this);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
double trueRange = High[0] - Low[0];
if (CurrentBar == 0)
{
tr.Set(trueRange);
dmPlus.Set(0);
dmMinus.Set(0);
sumTr.Set(tr[0]);
sumDmPlus.Set(dmPlus[0]);
sumDmMinus.Set(dmMinus[0]);
Adx.Set(50);
ADXmed.Set(50);
ADXup.Set(50);
ADXdown.Set(50);
return;
}
else
{
tr.Set(Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(trueRange, Math.Abs(High[0] - Close[1]))));
dmPlus.Set(High[0] - High[1] > Low[1] - Low[0] ? Math.Max(High[0] - High[1], 0) : 0);
dmMinus.Set(Low[1] - Low[0] > High[0] - High[1] ? Math.Max(Low[1] - Low[0], 0) : 0);
if (CurrentBar < Period)
{
sumTr.Set(sumTr[1] + tr[0]);
sumDmPlus.Set(sumDmPlus[1] + dmPlus[0]);
sumDmMinus.Set(sumDmMinus[1] + dmMinus[0]);
}
else
{
sumTr.Set(sumTr[1] - sumTr[1] / Period + tr[0]);
sumDmPlus.Set(sumDmPlus[1] - sumDmPlus[1] / Period + dmPlus[0]);
sumDmMinus.Set(sumDmMinus[1] - sumDmMinus[1] / Period + dmMinus[0]);
}
double diPlus = 100 * (sumTr[0] == 0 ? 0 : sumDmPlus[0] / sumTr[0]);
double diMinus = 100 * (sumTr[0] == 0 ? 0 : sumDmMinus[0] / sumTr[0]);
double diff = Math.Abs(diPlus - diMinus);
double sum = diPlus + diMinus;
Adx.Set(sum == 0 ? 50 : ((Period - 1) * Value[1] + 100 * diff / sum) / Period);
if (Adx[0] == Adx[1])
{
ADXmed.Set(1,Adx[1]);
ADXmed.Set(Adx[0]);
}
else if (Adx[0] < Adx[1])
{
ADXdown.Set(1,Adx[1]);
ADXdown.Set(Adx[0]);
}
else
{
ADXup.Set(1,Adx[1]);
ADXup.Set(Adx[0]);
}
DiPlus.Set(diPlus);
DiMinus.Set(diMinus);
}
}
#region Properties
/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries ADXmed
{
get { return Values[0]; }
}
[Browsable(false)]
[XmlIgnore()]
public DataSeries ADXdown
{
get { return Values[1]; }
}
[Browsable(false)]
[XmlIgnore()]
public DataSeries ADXup
{
get { return Values[2]; }
}
[Browsable(false)]
[XmlIgnore()]
public DataSeries DiPlus
{
get { return Values[3]; }
}
[Browsable(false)]
[XmlIgnore()]
public DataSeries DiMinus
{
get { return Values[4]; }
}
[Browsable(false)]
[XmlIgnore()]
public DataSeries Adx
{
get { return Values[5]; }
}
/// <summary>
/// </summary>
[Description("Numbers of bars used for calculations")]
[Category("Parameters")]
public int Period
{
get { return period; }
set { period = Math.Max(1, value); }
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private ADXmc[] cacheADXmc = null;
private static ADXmc checkADXmc = new ADXmc();
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public ADXmc ADXmc(int period)
{
return ADXmc(Input, period);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public ADXmc ADXmc(Data.IDataSeries input, int period)
{
checkADXmc.Period = period;
period = checkADXmc.Period;
if (cacheADXmc != null)
for (int idx = 0; idx < cacheADXmc.Length; idx++)
if (cacheADXmc[idx].Period == period && cacheADXmc[idx].EqualsInput(input))
return cacheADXmc[idx];
ADXmc indicator = new ADXmc();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
indicator.Input = input;
indicator.Period = period;
indicator.SetUp();
ADXmc[] tmp = new ADXmc[cacheADXmc == null ? 1 : cacheADXmc.Length + 1];
if (cacheADXmc != null)
cacheADXmc.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheADXmc = tmp;
Indicators.Add(indicator);
return indicator;
}
}
}
// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.ADXmc ADXmc(int period)
{
return _indicator.ADXmc(Input, period);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.ADXmc ADXmc(Data.IDataSeries input, int period)
{
return _indicator.ADXmc(input, period);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.ADXmc ADXmc(int period)
{
return _indicator.ADXmc(Input, period);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.ADXmc ADXmc(Data.IDataSeries input, int period)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
return _indicator.ADXmc(input, period);
}
}
}
#endregion
NinjaTrader_Bertrand
06-29-2010, 04:24 AM
Thanks, it seems you missed adding the ADX plot to the Initialize() section - if I add this to it, compile and reload works just fine here -
Add(new Plot(new Pen(Color.Cyan, 2), "ADX"));
xenayoo
06-29-2010, 04:46 AM
Thanks, it seems you missed adding the ADX plot to the Initialize() section - if I add this to it, compile and reload works just fine here -
Add(new Plot(new Pen(Color.Cyan, 2), "ADX"));
;) Well, that's the original ONE-COLOR line. I want this line with 3 colors: falling, flat and rising... I just use Adx as Dataseries. For ploting, I use ADXup, ADXdown and ADXmed. I can compile my code without error. But the indicator doesn't plot and leave a message in the Log.
NinjaTrader_Bertrand
06-29-2010, 04:53 AM
You would then need to use the DataSeries you defined in the code for this and not the public property capitalize (Adx vs adx) - please note NinjaScript is case sensitive.
If you replace references to Adx with adx and comment out delete you not needed public property it should work as well.
xenayoo
06-30-2010, 08:08 AM
Thank you, Bertrand. That's it. And additionaly a little error in the calculation-Logic... Thank you very much...
:D
NinjaTrader_Bertrand
06-30-2010, 08:50 AM
Great to hear thanks for reporting back.
Kstan
03-05-2012, 08:05 PM
Thank you, Bertrand. That's it. And additionaly a little error in the calculation-Logic... Thank you very much...
:DGood day Bertrand. In my search for an ADX to changes color for up/down, I came across this link. Can this code be modified to take the ADX indicator that comes standard in Ninja be able to change the line color?
Your assistance is greatly appreciated. -K
koganam
03-06-2012, 12:13 AM
Hi Bertrand,
i add the code in my previous posting. And yes, you will see, i checked the current bar. In the original Indicator the checking was:
if (CurrentBar == 0)
but it doesn't work at all....
What are the public properties of your Plots?
xenayoo
03-06-2012, 01:50 AM
Good day Bertrand. In my search for an ADX to changes color for up/down, I came across this link. Can this code be modified to take the ADX indicator that comes standard in Ninja be able to change the line color?
Your assistance is greatly appreciated. -K
Hello Kstan,
there is something, you should know: My Posting is from 2008(!). This way of changing colors with defining seperate plots is used in NinjaTrader 6.x! I suppose, you are using NinjaTrader7. Here you define 1 plot for the Line and change the Color by using PlotColors.
Example:
...
PlotColors[PLOTNUMBER][BARSBACK] = Color.Green;
...
Jens
Kstan
03-06-2012, 12:06 PM
Good day Jens,
Thank you for the prompt reply.
I am not a coder -- by a long shot. I am simply looking for the basic ADX which is in the Ninja 7 platform to have the color change as it pivots.
Is there a place to copy that code config and then compile it; I have learned that much :-)
Appreciated -K
NinjaTrader_RyanM
03-06-2012, 01:46 PM
Hi Kstan,
You can edit system indicators through the Tools > Edit NinjaScript menu. Save copies of your own by right clicking > Save as and providing a new name.
If you have limited time or programming experience to create this script, consider working with one of our 3rd party NinjaScript consultants to create the indicator to your specifications:
http://www.ninjatrader.com/partners#NinjaScript-Consultants
xenayoo
03-07-2012, 02:14 AM
Hello KSten,
here is my actuall adxmc (ADXmulticolor). It shows the DI+ and DI- lines and the ADX-Lines with color-changing if it rise or fall. Additionaly it changes the Backgroundcolor, if ADX start rising between the DI-Lines after the are crossing.
Jens