Skip to content

Instantly share code, notes, and snippets.

@mkinney
Last active August 20, 2019 08:03
Show Gist options
  • Save mkinney/74e84d29cad4467958955bb56ae6c0d7 to your computer and use it in GitHub Desktop.
Save mkinney/74e84d29cad4467958955bb56ae6c0d7 to your computer and use it in GitHub Desktop.
//@version=3
// from user @\/ ethfinance 2019-08-19
// which modified from https://www.tradingview.com/script/zMrGet6h-Ultimate-Moving-Average-Strategy/
strategy(title = "Ultimate Moving Average Long Only Strategy", shorttitle = "UMA Strategy", overlay = true, pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, max_bars_back=2000)
// === Backtesting Period Selector ===
testStartYear = input(2017, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear, testStartMonth, testStartDay, 0, 0)
testStopYear = input(2020, "Backtest Stop Year")
testStopMonth = input(6, "Backtest Stop Month")
testStopDay = input(1, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear, testStopMonth, testStopDay, 0, 0)
closeYear = input(2019)
closeMonth = input(7)
closeDayOfMonth = input(1)
testPeriod() =>
time >= testPeriodStart and time <= testPeriodStop ? true : false
//inputs
src = close
useCurrentRes = input(true, title="Use Current Chart Resolution?")
resCustom = input(title="Use Different Timeframe? Uncheck Box Above", type=resolution, defval="D")
len = input(14, title="Moving Average Length - LookBack Period")
atype = input(2,minval=1,maxval=7,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA")
cc = input(true,title="Change Color Based On Direction?")
smoothe = input(2, minval=1, maxval=10, title="Color Smoothing - 1 = No Smoothing")
doma2 = input(false, title="Optional 2nd Moving Average")
len2 = input(50, title="Moving Average Length - Optional 2nd MA")
atype2 = input(1,minval=1,maxval=7,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA")
cc2 = input(true,title="Change Color Based On Direction 2nd MA?")
warn = input(false, title="***You Can Turn On The Show Dots Parameter Below Without Plotting 2nd MA to See Crosses***")
warn2 = input(false, title="***If Using Cross Feature W/O Plotting 2ndMA - Make Sure 2ndMA Parameters are Set Correctly***")
sd = input(false, title="Show Dots on Cross of Both MA's")
useStop = input(defval = true, title = "Use Trailing Stop?")
slPoints = input(defval = 200, title = "Stop Loss Trail Points", minval = 1)
slOffset = input(defval = 400, title = "Stop Loss Trail Offset", minval = 1)
res = useCurrentRes ? period : resCustom
//hull ma definition
hullma = wma(2*wma(src, len/2)-wma(src, len), round(sqrt(len)))
//TEMA definition
ema1 = ema(src, len)
ema2 = ema(ema1, len)
ema3 = ema(ema2, len)
tema = 3 * (ema1 - ema2) + ema3
avg = atype == 1 ? sma(src,len) : atype == 2 ? ema(src,len) : atype == 3 ? wma(src,len) : atype == 4 ? hullma : atype == 5 ? vwma(src, len) : atype == 6 ? rma(src,len) : tema
//2nd Ma - hull ma definition
hullma2 = wma(2*wma(src, len2/2)-wma(src, len2), round(sqrt(len2)))
//2nd MA TEMA definition
sema1 = ema(src, len2)
sema2 = ema(sema1, len2)
sema3 = ema(sema2, len2)
stema = 3 * (sema1 - sema2) + sema3
avg2 = atype2 == 1 ? sma(src,len2) : atype2 == 2 ? ema(src,len2) : atype2 == 3 ? wma(src,len2) : atype2 == 4 ? hullma2 : atype2 == 5 ? vwma(src, len2) : atype2 == 6 ? rma(src,len2) : tema
out = avg
out_two = avg2
ma_up = out >= out[smoothe]
ma_down = out < out[smoothe]
col = cc ? ma_up ? lime : ma_down ? red : aqua : aqua
col2 = cc2 ? ma_up ? lime : ma_down ? red : aqua : aqua
circleYPosition = out_two
plot(out, title="Multi-Timeframe Moving Avg", style=line, linewidth=4, color = col)
plot(doma2 and out_two ? out_two : na, title="2nd Multi-TimeFrame Moving Average", style=circles, linewidth=4, color=col2)
plot(sd and cross(out, out_two) ? circleYPosition : na,style=cross, linewidth=5, color=yellow)
// Strategy conditions
longCond = ma_up
shortCond = ma_down
// entries and base exit
if testPeriod()
strategy.entry(id = "Long", long = true, when = longCond)
strategy.close(id = "Long", when = shortCond)
if(year == closeYear and month == closeMonth and dayofmonth >= closeDayOfMonth)
strategy.close(id = "Long")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment