CryptOli 3 MAs long/short Backtest
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Crypto-Oli

//@version=4
strategy("CryptOli 3 MAs long/short Backtest", initial_capital=5000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, overlay=true)

// this is an educational Script - basicly its very simple - you can see how minimal changes impact results, thats why i posted it
// Credits to Quantnomad to publish tons of free educational script
// this Script is based on https://www.tradingview.com/script/0NgUadGr-Ultimate-MA-Cross-Indicator/ Quantnomads Ultimate MA Indicator 
// HA - Option for calcucaltion based on HA-Candles (very famous recently)
// Source Input - Option (Candletype for calculation, close, ohlc4 ect.) --- there are huge differences --- try it by your own

////////////////////////////////////////////////////////////////////////////////
// BACKTESTING RANGE

// From Date Inputs
fromDay = input(defval=1, title="From Day", minval=1, maxval=31)
fromMonth = input(defval=1, title="From Month", minval=1, maxval=12)
fromYear = input(defval=2015, title="From Year", minval=1970)

// To Date Inputs
toDay = input(defval=1, title="To Day", minval=1, maxval=31)
toMonth = input(defval=1, title="To Month", minval=1, maxval=12)
toYear = input(defval=2020, title="To Year", minval=1970)

// Calculate start/end date and time condition
startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond = time >= startDate and time <= finishDate

////////////////////////////////////////////////////////////////////////////////

h = input(false, title = "Signals from Heikin Ashi Candles")

ma_type      = input(title = "MA Type",         type = input.string,  defval = "SMMA", options = ['SMA', 'EMA', 'WMA', 'VWMA', 'HMA', 'SMMA', 'DEMA'])
src = input(ohlc4)

short_ma_len = input(title = "Short MA Length", type = input.integer, defval = 7,     minval = 1)
short_ma_src = h ? security(heikinashi(syminfo.tickerid), timeframe.period, src, lookahead = false) : close
middle_ma_len  = input(title = "Middle MA Length",  type = input.integer, defval = 13,    minval = 2)
middle_ma_src = h ? security(heikinashi(syminfo.tickerid), timeframe.period, src, lookahead = false) : close
long_ma_len  = input(title = "Long MA Length",  type = input.integer, defval = 21,    minval = 2)
long_ma_src = h ? security(heikinashi(syminfo.tickerid), timeframe.period, src, lookahead = false) : close


tick_round(x) => 
    round(x / syminfo.mintick) * syminfo.mintick

// Set initial values to 0
short_ma = 0.0
middle_ma = 0.0
long_ma  = 0.0

// Simple Moving Average (SMA)
if ma_type == 'SMA' 
    short_ma := sma(short_ma_src, short_ma_len)
    middle_ma := sma(middle_ma_src, middle_ma_len)
    long_ma  := sma(long_ma_src,  long_ma_len)

// Exponential Moving Average (EMA)
if ma_type == 'EMA'
    short_ma := ema(short_ma_src, short_ma_len)
    middle_ma := ema(middle_ma_src, middle_ma_len)
    long_ma  := ema(long_ma_src,  long_ma_len)

// Weighted Moving Average (WMA)
if ma_type == 'WMA'
    short_ma := wma(short_ma_src, short_ma_len)
    middle_ma := wma(middle_ma_src, middle_ma_len)
    long_ma  := wma(long_ma_src,  long_ma_len)

// Hull Moving Average (HMA)
if ma_type == 'HMA'
    short_ma := wma(2*wma(short_ma_src, short_ma_len/2)-wma(short_ma_src, short_ma_len), round(sqrt(short_ma_len)))
    middle_ma := wma(2*wma(middle_ma_src, middle_ma_len/2)-wma(middle_ma_src, middle_ma_len), round(sqrt(middle_ma_len)))
    long_ma  := wma(2*wma(long_ma_src,  long_ma_len /2)-wma(long_ma_src,  long_ma_len),  round(sqrt(long_ma_len)))

// Volume-weighted Moving Average (VWMA)
if ma_type == 'VWMA'
    short_ma := vwma(short_ma_src, short_ma_len)
    middle_ma := vwma(middle_ma_src, middle_ma_len)
    long_ma  := vwma(long_ma_src,  long_ma_len)


// Smoothed Moving Average (SMMA)    
if ma_type == 'SMMA'
    short_ma := na(short_ma[1]) ? sma(short_ma_src, short_ma_len) : (short_ma[1] * (short_ma_len - 1) + short_ma_src) / short_ma_len
    middle_ma := na(middle_ma[1]) ? sma(middle_ma_src, middle_ma_len) : (middle_ma[1] * (middle_ma_len - 1) + middle_ma_src) / middle_ma_len
    long_ma  := na(long_ma[1])  ? sma(long_ma_src,  long_ma_len)  : (long_ma[1]  * (long_ma_len  - 1) + long_ma_src)  / long_ma_len

// Double Exponential Moving Average (DEMA)
if ma_type == 'DEMA'
    e1_short = ema(short_ma_src, short_ma_len)
    e1_middle = ema(middle_ma_src, middle_ma_len)
    e1_long  = ema(long_ma_src,  long_ma_len)
    
    short_ma := 2 * e1_short - ema(e1_short, short_ma_len)
    middle_ma := 2 * e1_middle - ema(e1_middle, middle_ma_len)
    long_ma  := 2 * e1_long  - ema(e1_long,  long_ma_len)

// Plot MAs
plot(short_ma, color = color.green,   linewidth = 1)
plot(middle_ma, color = color.yellow,   linewidth = 1)
plot(long_ma,  color = color.red, linewidth = 1)

if close>long_ma and short_ma>middle_ma and time_cond
    strategy.entry("Long", strategy.long)


if close<long_ma and short_ma<middle_ma and time_cond
    strategy.entry("Short", strategy.short)
