Announcement

Collapse
No announcement yet.

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:	203
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

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    79 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    146 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    79 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    52 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    56 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X