Reputation: 27
I'd hoped to do something without for loops so either what I'm about to inquire about is impossible or requires multiple var commands and on and off switches
study("On/Off condition", "", true)
upBar = close > open
// On/off conditions.
triggerOn = upBar and upBar[1] and upBar[2]
triggerOff = not upBar and not upBar[1]
// Switch state is implicitly saved across bars thanks to initialize-only-once keyword "var".
var onOffSwitch = false
// Turn the switch on when triggerOn is true. If it is already on,
// keep it on unless triggerOff occurs.
onOffSwitch := triggerOn or (onOffSwitch and not triggerOff)
bgcolor(onOffSwitch ? color.green : na)
plotchar(triggerOn, "triggerOn", "▲", location.belowbar, color.lime, 0, size = size.tiny, text = "On")
plotchar(triggerOff, "triggerOff", "▼", location.abovebar, color.red, 0, size = size.tiny, text = "Off")
Basically, if you have a moving average cross over, I'm looking at how I could post facto change the source of that moving average to low OR high instead of close, depending on if it crosses over or under, respectively. This behaves as a crude volatility stop more or less, using the low/high makes it behave like a slower period moving average.
For example, the cross over happens, the switch state is turned on, and it knows that this cross over happened the previous 3 bars as in the example above from the pinecode faq, but it then redefines the source variable. Now the issue with this is that the cross over state has to occur before the switch state can change using close as source, and I'm trying to "jump back" and change it, but that type of jump back feature absent for loops isn't readily apparent in pine code.
Is there any way to redefine the source variable 'after' the alert condition, the event, the switch state has occurred?
Let me try to clarify why an on and off switch is probably better than the following, why var may need to be used; as you can see in the following, even this method needs the event to occur before the volatility stop line is plotted. You can change the source variable based on various conditions but not the condition that it has gone long or short itself, as that is not yet known information in the code. I.e how do you change the a variable at the beginning of the script -- at the end of the script?
Factor = slot5a2
Up = ma - Factor * calcS4()
Dn = ma + Factor * calcS4()
TrendUp = 0.0
TrendUp := alttstep[1] > TrendUp[1] ? max(Up, TrendUp[1]) : Up
TrendDown = 0.0
TrendDown := alttstep[1] < TrendDown[1] ? min(Dn, TrendDown[1]) : Dn
Trend = 0.0
Trend := (src555) > TrendDown[1] ? 1 : (src555) < TrendUp[1] ? -1 : nz(Trend[1], 1)
TslMain = Trend == 1 ? TrendUp : TrendDown
linecolor = Trend == 1 ? color.green : color.red
barcolor(Trend == 1 ? color.green : color.red)
//======================================================================================//
//===== PLOTS ===========================================================================//
plot(TslMain, color=linecolor, style=plot.style_line, linewidth=2, title="SuperTrend")
plotarrow(Trend == 1 and Trend[1] == -1 ? Trend : na, title="Up Entry Arrow", colorup=color.lime, maxheight=60, minheight=50, transp=0)
plotarrow(Trend == -1 and Trend[1] == 1 ? Trend : na, title="Down Entry Arrow", colordown=color.red, maxheight=60, minheight=50, transp=0)
//===== ALERTS ==========================================================================//
//--- alerts using variables ---//
sLong = Trend == 1 and Trend[1] == -1
alertcondition(sLong, title='Long', message='SuperTrend Alert Long')
sShort = Trend == -1 and Trend[1] == 1
alertcondition(sShort, title='Short', message='SuperTrend Alert Short')
Upvotes: 0
Views: 2754
Reputation: 8779
You don't say much about the conditions defining your transitions. This supposes transitions on crosses of 2 MAs on close
of different periods. Since the transition condition is detected after one bar, that's when we switch between 2 other MAs on high/low:
//@version=4
study("Hi/Lo MAs", "", true)
emaHi = sma(high, 30)
emaLo = sma(low, 30)
bull = sma(close, 20) > sma(close, 100)
stop = bull ? emaLo : emaHi
stopChg = bull != bull[1]
c_stop = bull ? color.green : color.fuchsia
plot(stop, "Stop", stopChg ? na : c_stop)
plotchar(stopChg ? stop : na, "stopChg", "•", location.absolute, c_stop, size = size.tiny)
plot(emaLo, "emaLo", color.green, 4, transp = 90)
plot(emaHi, "emaHi", color.fuchsia, 4, transp = 90)
Upvotes: 1
Reputation: 1043
You should make your question more clear, but from what I have understood you are looking for something like this:
os = 0
os := on ? 1 : off ? 0 : os[1]
Which for the trailing stop case give:
os = 0
mah = sma(high,14)
mal = sma(low,14)
os := cross(close,mah) ? 1 : cross(close,mal) ? 0 : os[1]
stop = os*mal+(1-os)*mah
Upvotes: 0