Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Consecutive closes above or below moving average

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

    Consecutive closes above or below moving average

    Hi All.

    I'm looking for the most efficient way to code the following condition.

    1. Look for the first x number of closes above a moving average - print an arrow/bar colour etc - ignore all subsequent signals
    1.1 Signal can re-activate if price comes back to the ma (touches or closes below) and then x number of closes above - print arrow - ignore all subsequent signals
    2. Reverse of the above conditions for closes below

    If entry/pt/sl lines can be drawn above/below setup bar that would be ideal - these would be variables. i.e 2tick for entry, 16tick for sl and pt1/pt2/pt3

    The Tradingview code is below:


    -----------------------
    study("Indicator_ConsecutiveLH_SMA", overlay=true)

    //this is a very simple indicator that marks a candle if it has consecutive closes above or below the SMA

    sma_period = input(5,title="SMA length") //configure the SMA
    consecutive_lowhighmin = input(5,title="Consecutive LH Minimum") //configure the consecutive Low or High on one side of the SMA to trigger a trade
    consecutive_lowhighmax = input(6,title="Consecutive LH Maximum") //if there are too many consecutive Low or High we will not trade

    //key SMA
    keySMA = sma(close,sma_period)

    consecutiveBarsAbove = barssince(low<keySMA)
    consecutiveBarsBelow = barssince(high>keySMA)

    BullCloseAboveMinConsecutive = consecutiveBarsAbove >= consecutive_lowhighmin
    BullCloseBelowMaxConsecutive = consecutiveBarsAbove < consecutive_lowhighmax

    BearCloseAboveMinConsecutive = consecutiveBarsBelow >= consecutive_lowhighmin
    BearCloseBelowMaxConsecutive = consecutiveBarsBelow < consecutive_lowhighmax

    BullCloseIndicator = BullCloseAboveMinConsecutive and BullCloseBelowMaxConsecutive //consective closes between the min and max values
    BearCloseIndicator = BearCloseAboveMinConsecutive and BearCloseBelowMaxConsecutive

    barcolor(BullCloseIndicator ? color.yellow : BearCloseIndicator ? color.yellow : na)
    //barcolor(BearCloseIndicator ? color.yellow : color.green)
    //plotchar(BullCloseIndicator,title="BullIndicator", char="U",color=blue,size=size.tiny,location=locati on.abovebar)
    //plotchar(BearCloseIndicator,title="BearIndicator", char="D",color=red,size=size.tiny,location=locatio n.belowbar)



    Many thanks,
    Bernie

    #2
    Hello traderslab,

    Thanks for your post and welcome to the NinjaTrader forums!

    Please note that NinjaTrader does not provide coding or debugging services and we can leave this thread open for anyone that would like to create this for you.

    Another alternative is if you would like this created for you, we can provide a link to 3rd party programmers in the NinjaTrader ecosystem.

    If you would like to take on creating this yourself, we can provide you with links to get you started with Ninjascript.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    560 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    325 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    547 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X