This was interesting… I played around and smaller data sets looked fairly promising, but then implementing is on a full year, the results didn’t pan out so hot… fun exercise and figuring out how to be better with stop-loss could potentially improve this!
//@version=4
strategy(title="MA Crossing Strategy", overlay=true, initial_capital=1000, commission_type=strategy.commission.percent, commission_value=0.2)
//sma inputs
shortVal = input(defval=20, title="Short SMA", minval=1)
longVal = input(defval=80, title="Long SMA", minval=1)
//Date and Time
fromMonth = input(defval=2, title="From month", minval=1)
fromDay= input(defval=15, title="From day", minval=1)
fromYear= input(defval=2021, title="From year", minval=2014)
toMonth = input(defval=9, title="To month", minval=1)
toDay= input(defval=15, title="To day", minval=1)
toYear= input(defval=2021, title="To year", minval=2014)
tsiLong = input(title="TSI Long Length", type=input.integer, defval=25)
tsiShort = input(title="TSI Short Length", type=input.integer, defval=13)
tsiSignal = input(title="TSI Signal Length", type=input.integer, defval=13)
//Definitions
short=sma(close, shortVal)
long=sma(close, longVal)
price = close
double_smooth(src, tsiLong, tsiShort) =>
fist_smooth = ema(src, tsiLong)
ema(fist_smooth, tsiLong)
pc = change(price)
double_smoothed_pc = double_smooth(pc, tsiLong, tsiShort)
double_smoothed_abs_pc = double_smooth(abs(pc), tsiLong, tsiShort)
tsi_value = 100 * (double_smoothed_pc / double_smoothed_abs_pc)
//Logic
timeInRange=(time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay,23,59))
//buy=short >= long
longSignal = (crossover(short, long) or crossover(tsi_value, ema(tsi_value, tsiSignal))) and timeInRange
//sell=long >= short
shortSignal = crossover(long, short) and timeInRange
//Position
if(longSignal)
strategy.entry(id="longPosition", long=true, qty=0.1, comment="buy")
strategy.exit("longSignalExit", "longPosition", comment="longOut") //, profit = 5000000
if(shortSignal)
strategy.entry(id="shortPosition", long=false, qty=0.1, comment="sell" )
strategy.exit("exitLong", "longPosition", profit = 500000)
//Stop-Loss
strategy.exit("exitLong", "longPosition", profit = 500000) //, loss = 100000
strategy.exit("exitShort", "shortPosition", profit = 250000) //, loss = 100000
On 1 hr BTC I got:
58% NP, 45% profitable, 1.1 profit factor, 80% Max Drawdown
1hr ETH yielded: 4.83% NP, 35% profitable, 1.1 factor, 22% max drawdown