Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GuntharDeNiro/bfca4112fda0e47065892179e18399b0 to your computer and use it in GitHub Desktop.
Save GuntharDeNiro/bfca4112fda0e47065892179e18399b0 to your computer and use it in GitHub Desktop.
//@version=4
study("MM_Trend_Test", overlay=true)
start = input(0.02)
increment = input(0.02)
maximum = input(0.2)
psar = sar(start, increment, maximum)
plot(psar, title="SAR", linewidth=1, style=6, color=color.white)
conversionPeriods = input(9, minval=1, title="Conversion Line Periods"),
basePeriods = input(26, minval=1, title="Base Line Periods")
laggingSpan2Periods = input(52, minval=1, title="Lagging Span 2 Periods"),
displacement = input(1, minval=1, title="Displacement")
donchian(len) => avg(lowest(len), highest(len))
conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)
leadLine1 = avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)
plot(conversionLine, color=#0496ff, title="Conversion Line")
plot(baseLine, color=#991515, title="Base Line")
plot(close, offset = -displacement, color=#459915, title="Lagging Span")
p1 = plot(leadLine1, offset = displacement, color=color.green,
title="Lead 1")
p2 = plot(leadLine2, offset = displacement, color=color.red,
title="Lead 2")
fill(p1, p2, color = leadLine1 > leadLine2 ? color.green : color.red)
lookback = input(2,minval=1)
cciLong = cci(close,14) > 100
cciShort = cci(close,14) < -100
longCondition = leadLine1 > leadLine2 and low > leadLine1 and psar < low and cciLong
shortCondition = leadLine1 < leadLine2 and high < leadLine1 and psar > high and cciShort
inLongPosition = false
inLongPosition := longCondition[1] ? true : shortCondition[1] ? false : inLongPosition[1]
inShortPosition = false
inShortPosition:= shortCondition[1] ? true : longCondition[1] ? false : inShortPosition[1]
flat = false
flat := not inLongPosition and not inShortPosition
alertcondition(inLongPosition and psar < high, "Long", "LONG")
alertcondition(inShortPosition and psar > low, "Short", "SHORT")
alertcondition(inLongPosition and psar > high, "ROE close LONG", "ROE close LONG")
alertcondition(inShortPosition and psar < low, "ROE close SHORT", "ROE close SHORT")
fillDefault = input(defval=true, title="Colour Neutral Position?")
backgroundColour = (inShortPosition and psar > low) or (inShortPosition and psar < low) ? color.red : (inLongPosition and psar < high) or (inLongPosition and psar > high) ? color.green : (fillDefault) ? #000000 : na
bgcolor(color=backgroundColour, transp=95, title="Conditionally coloured background")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment