Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

History and realtime when Price crossing Upper or Lower bands for TMA with slope

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

    History and realtime when Price crossing Upper or Lower bands for TMA with slope

    For TMA I'm setting a upper or lower band blocking variable for crossing and re-crossing back through the upper or lower band. I also have a SMA using some bars to establish the slope for a trend to decided if a trade should be long or short.

    The code for establishing the bands is below here and I'm using and offset from the band as the crossover location. I'm only showing the code for the lower band here.

    Will this work as the band is repainted, but the crossing should work for the current bar an looking back at the previous bar for the comparison.


    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, LowerBandOffset[0], 1) && (CrossLower == false))
    {
    CrossLower = true;
    Print(Convert.ToString(Times[0][0].TimeOfDay) + "LOWER BAND START DOWN Close: " + Close[0] + "Low: " + Low[0] + "High: " + High[0] + "LowerBand: " + LowerBand[0] + "UpperBand" + UpperBand[0] + "LowerBandOffset: " + LowerBandOffset[0] + "UpperBandOffset: " + UpperBandOffset[0]);
    }
    if (CrossAbove(Low, LowerBandOffset[0], 1) && (CrossLower == true))
    {
    CrossLower = false;
    if ((Slope(SMA(20), 3, 0)) < 0)
    {
    trendBreak[0] = -1;
    }
    else
    {
    trendBreak[0] = 1;
    }
    Attached Files

    #2
    Hello Tonofit,


    Will this work as the band is repainted, but the crossing should work for the current bar an looking back at the previous bar for the comparison.

    The code you have shown wont execute for each repaint if by repaint you mean each time the bar price changes. You have the following line of code:

    Code:
    if (State != State.Realtime && !IsFirstTickOfBar) return;
    That will essentially run OnBarClose, it waits for the first tick of the bar in realtime otherwise it returns.

    If you wanted to do that calculation for each price change you would have to remove the IsFirstTickOfBar part of the condition however that may affect your other logic.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CaptainJack, 05-29-2026, 05:09 AM
    0 responses
    245 views
    0 likes
    Last Post CaptainJack  
    Started by CaptainJack, 05-29-2026, 12:02 AM
    0 responses
    157 views
    0 likes
    Last Post CaptainJack  
    Started by charlesugo_1, 05-26-2026, 05:03 PM
    0 responses
    165 views
    1 like
    Last Post charlesugo_1  
    Started by DannyP96, 05-18-2026, 02:38 PM
    1 response
    250 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    201 views
    0 likes
    Last Post CarlTrading  
    Working...
    X