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 SalmaTrader, 07-07-2026, 10:26 PM
    0 responses
    47 views
    0 likes
    Last Post SalmaTrader  
    Started by CarlTrading, 07-05-2026, 01:16 PM
    0 responses
    22 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 06-17-2026, 10:32 AM
    0 responses
    15 views
    0 likes
    Last Post CaptainJack  
    Started by kinfxhk, 06-17-2026, 04:15 AM
    0 responses
    21 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 06-17-2026, 04:06 AM
    0 responses
    23 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Working...
    X