Bare bones indicator function:
Value [0] = SMA(WR,2)
When I plot these two separately in Pine Script and compare them to what Ninja Script draws, the WR plot is identical but shifted one post late (green line in picture), and the SMA plot is not drawing correctly (orange line in picture). I've tried at three different ways of calculating the SMA: calling the public SMA indicator, copying the SMA code into my custom indicator, and manually calculating the SMA output with Value[0] = CurrentBar == 0 ? WR[0]: (WR[0] + WR[1]) / 2;. All three draw the same plot, and it's not the same as the Pine Script display (which was built to copy an indicator I build in Sierra Chart and the math is the same between those two platforms). Interestingly, Value[0] = WR[0] will plot (although 1 candle shifted right), but Value[0] = WR[1] just displays a horizontal line that doesn't change with price. Makes me wonder if I'm not calling historical data correctly...
Yellow circles (and pink vertical lines) show green bottom peak at 10:10 in Pine Script (top chart), but at 10:15 in NinjaScript (bottom chart). WR (green line) values are identical, just shifted 1 candle right in NinjaScript.
Pink dots show WR[1] and WR[0] on the green line, with the Value[0] = SMA(WR,2) at the blue dot on the orange line. Blue dots are right at the average of the two pink dots in Pine Script (top chart), but in several cases they are outside the space between the two pink dots in NinjaScript (bottom chart), which is impossible when you take the average of two points.
My code is after the image. Would you please help me identify the root cause of these two issues? Any help will be immensely appreciated!!!
My Pine Script code:
PeriodWR = (7)
percentRange(PeriodWR) =>
max = ta.highest(high, PeriodWR)
min = ta.lowest(low, PeriodWR)
(-100*((max - close) / (max - min)))
WR = percentRange(PeriodWR)
plot(WR, color=color.green)
PeriodSMA = (2)
WRSMA= ta.sma(WR,PeriodSMA)
plot(WRSMA, color=color.orange, linewidth = 1, display = display.none+display.pane)
My NinjaScript code:
public class WRSMA: Indicator
{
private MAX max;
private MIN min;
private double priorSum;
private double sum;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"WRSMA";
Name = "WRSMA";
Calculate = Calculate.OnPriceChange;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = false;
AddPlot(Brushes.Orange, "Plot");
}
else if (State == State.Configure)
{
priorSum = 0;
sum = 0;
}
else if (State == State.DataLoaded)
{
max = MAX(High, 7);
min = MIN(Low, 7);
//WR = WilliamsR(Close, 7);
//WRSMA = SMA(WR, 2);
}
}
protected override void OnBarUpdate()
{
double max0 = max[0];
double min0 = min[0];
int Period = 2;
WR[0] = -100 * (max0 - Close[0]) / (max0 - min0 == 0 ? 1 : max0 - min0);
if (BarsArray[0].BarsType.IsRemoveLastBarSupported)
{
if (CurrentBar == 0)
Value[0] = WR[0];
else
{
double last = Value[1] * Math.Min(CurrentBar, Period);
if (CurrentBar >= Period)
Value[0] = (last + WR[0] - WR[Period]) / Math.Min(CurrentBar, Period);
else
Value[0] = ((last + WR[0]) / (Math.Min(CurrentBar, Period) + 1));
}
}
else
{
if (IsFirstTickOfBar)
priorSum = sum;
sum = priorSum + WR[0] - (CurrentBar >= Period ? WR[Period] : 0);
Value[0] = sum / (CurrentBar < Period ? CurrentBar + 1 : Period);
}
//SMA of WilliamsR
//Value[0] = CurrentBar == 0 ? WR[0]: (WR[0] + WR[1]) / 2;
//Value[0] = WR[0];
}
region Properties
[Browsable(false)]
[XmlIgnore()]
public Series<double> WR
{
get { return Values[0]; }
}
#endregion
Comment