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 Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    648 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    369 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    108 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    572 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    573 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X