Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator below a value for last XX consecutive bars

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

    Indicator below a value for last XX consecutive bars

    Morning

    I'm trying to enter a short trade only if a fast period SMA has been below the slow period SMA for, say, the last 50 bars in a row

    i.e. CrossAbove((Close,SMAFast),(Close,SMASlow),50,0)= false

    But I can't work out how to write this formula because you cannot use "=false" !!
    Any ideas would be greatly appreciated.

    I'm trying not to write it in this way below, because I want to be able to vary the bars in a row value

    SMA Fast[0]< SMA Slow[0]
    SMA Fast[1]< SMA Slow[1]
    SMA Fast[2]< SMA Slow[2]
    .........
    SMA Fast[50]< SMA Slow[50]

    Thanks
    STeve

    #2
    Hello lancasterstephen,

    Thanks for your post.

    You could use a bool and an int variable with your logic. For example, create a bool called crossFound and initialize it to false, create an int variable called myBarCount and set it to 0.

    if (CrossBelow (SMA(smaFast), SMA(smaSlow), 1)
    {

    crossFound = true; // set bool true when a cross below is found
    }

    if (CrossFound && SMA(smaFast)[0] < SMA(smaSlow[0]))
    {
    myBarCount++;
    // increment the bar counter
    }

    if (crossFound && myBarCount == 50) //Test that the cross was found and that 50 bars have passed.
    {
    // order entry here
    myBarCount = 0; // reset after order placed
    crossFound = false; // reset for next order
    }

    Note that you would have to establish reset criteria/logic so that crossFound is reset to false and myBarCount is reset to 0 if the conditions do not allow the bar count to reach 50 (for example a cross above).

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by charlesugo_1, 05-26-2026, 05:03 PM
    0 responses
    57 views
    0 likes
    Last Post charlesugo_1  
    Started by DannyP96, 05-18-2026, 02:38 PM
    1 response
    143 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    161 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 05-10-2026, 08:12 PM
    0 responses
    97 views
    0 likes
    Last Post CarlTrading  
    Started by Hwop38, 05-04-2026, 07:02 PM
    0 responses
    276 views
    0 likes
    Last Post Hwop38
    by Hwop38
     
    Working...
    X