Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Custom indicator code doesn't seem to calculate SMA correctly

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Custom indicator code doesn't seem to calculate SMA correctly

    New to NinjaScript, translating some of my custom indicators over from Pine Script.

    Bare bones indicator function:
    WR = WilliamsR(Close,7)
    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!!!

    Click image for larger version  Name:	image.png Views:	0 Size:	85.0 KB ID:	1296614

    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​
    Last edited by gravenjp; 03-21-2024, 06:16 AM.

    #2
    Hello gravenjp,

    In this situation because a different platform is being compared you may see differences in how the indicator processes or also what underlying data was used for the calculation. To find the specific difference you would need to make a copy of the pinescript sma and add debugging into it and also make a copy of the NinjaScript sma and add debugging into it. You would need to print the bar time, bar price and the sma calculated value for each bar from both scripts and then compare them. That would let you know where the deviation is in either processing calculation or data.

    In NinjaScript you can use the Print command and the output window for debugging, I am not sure what the equivelent would be in pinescript. https://ninjatrader.com/support/help...lightsub=print
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Balage0922, Today, 07:38 AM
    0 responses
    5 views
    0 likes
    Last Post Balage0922  
    Started by JoMoon2024, Today, 06:56 AM
    0 responses
    6 views
    0 likes
    Last Post JoMoon2024  
    Started by Haiasi, 04-25-2024, 06:53 PM
    2 responses
    19 views
    0 likes
    Last Post Massinisa  
    Started by Creamers, Today, 05:32 AM
    0 responses
    6 views
    0 likes
    Last Post Creamers  
    Started by Segwin, 05-07-2018, 02:15 PM
    12 responses
    1,786 views
    0 likes
    Last Post Leafcutter  
    Working...
    X