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
Comment