I´m new to NT8 coding and facing basic programming issues.
I want to code a cumulative delta divergence indicator based on the crossing of SMI&SMIavg lines. I use the pjscumdelta (https://pjsmith.me.uk/index.php/2016...ninjatrader-7/) indicator to obtain the cumdelta value. Said indicator works based on tick series and my indicator will work on range or time based series.
I´ve the same indicator coded for NT7 based on the pjs NT7 version but I´m unable to make it work smoothly when running on NT8 and I guess it is due to the incorrect manner of calling the pjs indicator from my indicator code.
My current code is (I´ve tried many different alternatives to no avail):
namespace NinjaTrader.NinjaScript.Indicators
{
public class SMIdiver : Indicator
{
#region Variables
private int percentDLength = 3;
private int percentKLength = 5;
private Series<double> rel_diff;
private Series<double> diff;
private double dotactr = 1;
private double dotantr = 1;
private double preactr = 1;
private double preantr = 1;
private double dotactg = 1;
private double dotantg = 1;
private double preactg = 1;
private double preantg = 1;
private double cumdelactr = 1;
private double cumdelactg = 1;
private double cumdelantr = 1;
private double cumdelantg = 1;
private Series<double> myDataSerieSMIToS;
private Series<double> myDataSerieavgSMIToS;
private Series<double> signal;
private pjsCumDelta pjsCumDelta1;
#endregion
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Indicator here.";
Name = "SMIdiver";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = false;
myDataSerieSMIToS = new Series<double>(this);
myDataSerieavgSMIToS = new Series<double>(this);
AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.Line, "SMIToS");
AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.Line, "AvgSMIToS");
AddLine(Brushes.Green, -40, "Lower");
AddLine(Brushes.Green, 40, "Upper");
IsAutoScale = false;
rel_diff= new Series<double>(this);
diff = new Series<double>(this);
signal = new Series<double>(this);
}
else if (State == State.Configure)
{
AddDataSeries(BarsPeriodType.Tick, 1);
}
else if (State == State.DataLoaded)
{
pjsCumDelta1 = pjsCumDelta(BarsArray[1],1,1,1,true,true,true,true,false,true,false,0,fals e,false,false);
}
}
protected override void OnBarUpdate()
{
//Add your custom indicator logic here.
if (BarsInProgress == 1) return;
if (CurrentBars[0] < 30 || CurrentBars[1] < 30) return;
if(CurrentBars[0] < percentKLength || CurrentBars[0] < percentDLength) return;
double min_low = MIN(Low, percentKLength)[0];
double max_high = MAX(High, percentKLength)[0];
rel_diff[0] = Close[0] - (max_high + min_low)/2;
diff[0] = max_high - min_low;
signal[0] = 0;
double avgrel = EMA(EMA(rel_diff, percentDLength), percentDLength)[0];
double avgdiff = EMA(EMA(diff, percentDLength), percentDLength)[0];
myDataSerieSMIToS[0] = EMA(EMA(rel_diff, percentDLength), percentDLength)[0] / (EMA(EMA(diff, percentDLength), percentDLength)[0] /2)*100;
myDataSerieavgSMIToS[0] = EMA(myDataSerieSMIToS, percentDLength)[0];
if (myDataSerieSMIToS[1] > myDataSerieavgSMIToS[1] && myDataSerieSMIToS[0] < myDataSerieavgSMIToS[0] && myDataSerieavgSMIToS[0]> 40)
{
dotactr = myDataSerieavgSMIToS[0];
preactr = High[0];
cumdelactr = pjsCumDelta1.Plot0balance[0];
if (dotactr < dotantr && preactr > High[1] && preactr > preantr)
{
Draw.ArrowDown(this,CurrentBar.ToString() + Time[0], true, 0, myDataSerieavgSMIToS[0] + 15, Brushes.Red);
signal[0] = -1;
}
else if (cumdelactr < cumdelantr && preactr > High[1] && preactr > preantr)
{
Draw.ArrowDown(this,CurrentBar.ToString() + Time[0], true, 0, myDataSerieavgSMIToS[0] + 15, Brushes.Violet);
signal[0] = -1;
}
dotantr = dotactr;
preantr = preactr;
cumdelantr = cumdelactr;
}
if (myDataSerieSMIToS[1] < myDataSerieavgSMIToS[1] && myDataSerieSMIToS[0] > myDataSerieavgSMIToS[0] && myDataSerieavgSMIToS[0]< -40)
{
dotactg = myDataSerieavgSMIToS[0];
preactg = Low[0];
cumdelactg = pjsCumDelta1.Plot0balance[0];
if (dotactg > dotantg && preactg < Low[1] && preactg < preantg)
{
Draw.ArrowUp(this,CurrentBar.ToString() + Time[0],true, 0, myDataSerieavgSMIToS[0] - 15, Brushes.Green);
signal[0] = 1;
}
else if (cumdelactg > cumdelantg && preactg < Low[1] && preactg < preantg)
{
Draw.ArrowUp(this,CurrentBar.ToString() + Time[0], true, 0, myDataSerieavgSMIToS[0] - 15, Brushes.Lime);
signal[0] = 1;
}
dotantg = dotactg;
preantg = preactg;
cumdelantg = cumdelactg;
}
SMIToS[0] = avgrel/(avgdiff/2)*100;
AvgSMIToS[0] = EMA(SMIToS, percentDLength)[0];
}
This shouldn´t be too complicated!

Comment