Tradingview, Pine editor, SMA (6,70)

This code below has 2 interesting things:

  • Calculate the SMA for 6 days and 70 days.
  • Define a period of time for your backtesting

//@version=3

strategy(“SMA – 6d – 70d”, overlay=true, initial_capital=1000)

// === BACKTEST RANGE ===
FromMonth = input(defval = 1, title = “From Month”, minval = 1)
FromDay = input(defval = 1, title = “From Day”, minval = 1)
FromYear = input(defval = 2012, title = “From Year”, minval = 2014)
ToMonth = input(defval = 1, title = “To Month”, minval = 1)
ToDay = input(defval = 1, title = “To Day”, minval = 1)
ToYear = input(defval = 2018, title = “To Year”, minval = 2018)

// === SERIES SETUP ===
buy = crossover(sma(close, 6), sma(close, 70))
sell = crossunder(sma(close, 6), sma(close, 70))

// === ALERTS ===
strategy.entry(“L”, strategy.long, when=(buy and (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))))
strategy.close(“L”, when=(sell and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))))
plot(sma(close, 6),”SMA 6″, orange)
plot(sma(close, 70),”SMA 70″, navy)
plot(close,”price”, red)

Leave a Comment