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 gemify, 11-11-2022, 11:52 AM
    6 responses
    803 views
    2 likes
    Last Post ultls
    by ultls
     
    Started by ScottWalsh, Today, 04:52 PM
    0 responses
    3 views
    0 likes
    Last Post ScottWalsh  
    Started by ScottWalsh, Today, 04:29 PM
    0 responses
    7 views
    0 likes
    Last Post ScottWalsh  
    Started by rtwave, 04-12-2024, 09:30 AM
    2 responses
    22 views
    0 likes
    Last Post rtwave
    by rtwave
     
    Started by tsantospinto, 04-12-2024, 07:04 PM
    5 responses
    70 views
    0 likes
    Last Post tsantospinto  
    Working...
    X