Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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).
    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by DJ888, 04-16-2024, 06:09 PM
    6 responses
    18 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by Jon17, Today, 04:33 PM
    0 responses
    1 view
    0 likes
    Last Post Jon17
    by Jon17
     
    Started by Javierw.ok, Today, 04:12 PM
    0 responses
    6 views
    0 likes
    Last Post Javierw.ok  
    Started by timmbbo, Today, 08:59 AM
    2 responses
    10 views
    0 likes
    Last Post bltdavid  
    Started by alifarahani, Today, 09:40 AM
    6 responses
    41 views
    0 likes
    Last Post alifarahani  
    Working...
    X