Reputation: 3192
I have 3 moving averages (we'll call them slow, medium and fast). When the medium crosses below the slow, I am looking for shorts. However, I want to wait until the fast one "peaks" (that is, is heading up, and then starts to move down) before generating a signal.
I have an arbitrary length of time I'm willing to wait for this fast MA move downward... eg within 10 bars.
How can I say "If fast MA turns downward within 10 bars after the medium MA crosses below the slow MA, plotshape"?
Eg here is where I would want my signal (happens to be 6 bars after the cross in this case):
Here is my current code:
medSlowCrossdown = crossunder(vmaVolMed,vmaVolSlow)
fastTurndown = rising(vmaVolFast[1],2) and falling(vmaVolFast,1)
nextFastTurndown = medSlowCrossdown[1-10] and fastTurndown
plotshape(nextFastTurndown, style=shape.circle, location=location.top)
Note: you will see I tried adding a range within the bar index medSlowCrossdown[1-10]
, as that seemed logical, but it produced an error of:
line 62: ./TVScriptAnalyzer.g: node from line undefined:-1 no viable alternative at input 'UP'
Upvotes: 0
Views: 2406
Reputation: 184
Here's a script that looks back 10 bars for medium to cross slow and fast peaking previous bar.
//@version=4
study("My Script")
_fast = ema(close, 6)
_medium = ema(close, 20)
_slow = ema(close, 50)
_medSlow = barssince(crossunder(_medium, _slow)) // bars since medium crossed slow
_fastPeaked = _fast<_fast[1] and _fast[1]>_fast[2] // true if fast peaked previous bar
_shortCondition = _medSlow < 10 and _fastPeaked // true if medium crossed slow less than 10 bars ago and fast just peaked
plotshape(_shortCondition?close:na, style=shape.triangledown, size=size.normal, color=color.red)
plot(_fast, color=color.orange)
plot(_medium, color=color.green)
plot(_slow, color=color.red)
Upvotes: 2