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

Discrepancy between plot and calculation of bounds

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

    Discrepancy between plot and calculation of bounds

    I have a discrepancy between the calculated bands for TMA indicator and plot and the logic for crossover above and below the upper and lower bands. The plots are correct. The crossover logic for the upper and lower bands is comparing 2 bars ago for the bands to be broached and then bar 1 is waiting for the cross back over the band and the current bar then can potential execute a trade is what I'm trying to use. The bands that are detected using the center band and offset aren't the same as the plot. What do I need to do to fix this?

    code:


    amespace NinjaTrader.NinjaScript.Indicators
    {
    public class TmaZones : Indicator
    {
    private Series<double> trendBreak;

    private Series<double> upperBandOffset;
    private Series<double> lowerBandOffset;

    private bool stackedDown, stackedUp;

    private bool CrossLower = false;
    private bool CrossHigher = false;

    private double sumC = 0.0;
    private double sumW = 0.0;
    private double rngV = 0.0;

    private bool previousClosesAbove;
    private bool previousClosesBelow;

    protected override void OnStateChange()
    {
    if(State == State.SetDefaults)
    {
    Description = @"";
    Name = "TmaZones";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = false;
    DrawVerticalGridLines = false;
    PaintPriceMarkers = false;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    IsSuspendedWhileInactive = true;

    atrPeriod = 100;
    atrFactor = 2.618;
    halfLength = 20;

    AddPlot(new Stroke(Brushes.Sienna, 2), PlotStyle.Line, "UpperBand"); //Sienna
    AddPlot(new Stroke(Brushes.Gray, 1), PlotStyle.Line, "MiddlBand");
    AddPlot(new Stroke(Brushes.MediumSeaGreen, 2), PlotStyle.Line, "LowerBand"); //Medium Sea Green
    }
    else if(State == State.Configure)
    {
    if(ChartBars != null)
    {
    ZOrder = ChartBars.ZOrder - 1;
    }

    lowerBandOffset = new Series<double>(this);
    upperBandOffset = new Series<double>(this);

    CrossLower = false;
    CrossHigher = false;
    }
    else if (State == State.DataLoaded)
    {
    trendBreak = new Series<double>(this);
    }
    }

    protected override void OnBarUpdate()
    {

    if(CurrentBar < halfLength + 1) return;
    if(State != State.Realtime && !IsFirstTickOfBar) return;

    for(int i=halfLength; i>=0; i--)
    {
    sumC = (halfLength+1) * Close[i];
    sumW = (halfLength+1);

    int k = halfLength;

    for(int j=1; j<=halfLength; j++)
    {
    if(i+j > CurrentBar) break;

    sumC += k * Close[i+j];
    sumW += k;

    if(j<=i)
    {
    sumC += k * Close[i-j];
    sumW += k;
    }

    k--;
    }

    rngV = ATR(atrPeriod)[i] * atrFactor;

    MiddlBand[i] = sumC / sumW;
    UpperBand[i] = MiddlBand[i] + rngV;
    LowerBand[i] = MiddlBand[i] - rngV;


    LowerBandOffset[i] = (LowerBand[i] + (5 * TickSize));
    UpperBandOffset[i] = (UpperBand[i] - (5 * TickSize));
    }

    if (CrossBelow(Low, LowerBand, 2) && (CrossAbove(Low, LowerBand, 1)))
    {
    ctrendBreak[0] = 1;
    }

    if (CrossAbove(High, UpperBand, 2) && (CrossBelow(High, UpperBand, 1)))
    {
    trendBreak[0] = -1;
    }
    }
    Click image for larger version

Name:	TMABands.png
Views:	147
Size:	196.5 KB
ID:	1207505

    #2
    Hi Tonofit, thanks for posting. Use Print to print out data from this custom indicator to see why you are seeing a difference. We have a guide on debugging custom code here:



    Kind regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by ageeholdings, 05-01-2024, 05:22 AM
    6 responses
    42 views
    0 likes
    Last Post ageeholdings  
    Started by tony_28217, Today, 07:04 PM
    0 responses
    11 views
    0 likes
    Last Post tony_28217  
    Started by flybuzz, Today, 10:33 AM
    1 response
    9 views
    0 likes
    Last Post flybuzz
    by flybuzz
     
    Started by spencerp92, 10-10-2023, 09:56 AM
    4 responses
    309 views
    0 likes
    Last Post flybuzz
    by flybuzz
     
    Started by samish18, Yesterday, 10:13 AM
    1 response
    26 views
    0 likes
    Last Post NinjaTrader_Eduardo  
    Working...
    X