strategy(title="3.6_2019 parab. sar & mav", overlay=true, initial_capital=10000, currency='EUR')
// in this version of 3.6 trading system (old versione with psar and macd, for reference:
// https://saucius-finance.blogspot.com/2014/
// open (or close and reverse positions) only with a slow parabolic sar and
// close position if close < ma 20 and high > ma, then open again after throwback if close > ma and psar < close (viceversa
// for short).
// No action if close < low 1 or > high1 (yesterday's high or low), while in system 3.8 there is the "so called"
// maxmin and a close under or upon yesterday's highe or low brings to a new position
//psar lento 0.01, 0.2, 0.1
//************************************
// building psar
start = input(title="Start sar", type=float, step=0.005, defval=0.01)
increment = input(title="Increment sar", type=float, step=0.05, defval=0.2)
maximum = input(title="Maximum sar", type=float, step=0.01, defval=0.1)
period2 = input(title="Period ma", step=1, defval=20)
psar = sar(start, increment, maximum)
mav = sma(close, period2)
plot(psar, style = line , linewidth = 2,title = "parabolic sar")
plot(mav, style = line , linewidth = 2,title = "mov. avg.")
// ===ENTRY ==
strategy.entry("Long", true, when = crossover (close, psar))
strategy.entry("Short", false, when = crossunder (close, psar))
// entry after retracement to mav
entryrl = close [1] < mav and close > mav and close > psar
strategy.entry("Long", true, when = entryrl)
// entry short after retracement
entrysh = close [1] > mav and close < mav and close < psar
strategy.entry("Short", false, when = entrysh)
// === EXIT===
underma1 = high[1]< mav[1] and high > mav and close < mav
overma1 = low[1] > mav[1] and low < mav and close > mav
underclosema2 = close[1] > mav[1] and close < mav
overclosema2 = close[1] < mav[1] and close > mav
strategy.close("Long", when= crossunder (close, psar))
strategy.close("Short", when= crossover (close, psar))
strategy.close("Long", when= underma1)
strategy.close("Short", when= overma1)
strategy.close("Long", when= underclosema2)
strategy.close("Short", when= overclosema2)