EMA Crossover

jordan.fan.basket · strategy · 75 行 · 点赞 299 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=2
strategy("EMA Crossover", overlay=true)
 
// Pull traditional Open / Close candle data
tClose = security(tickerid, period, close)
tOpen = security(tickerid, period, open)
 
// Inputs
mgPeriod = input(14, title = "Period length", minval=1)
mgEnvelopes = input(defval = true, title = "Use High/Low envelopes?", type = bool)
 
// Calculate McGinley MA
// Major change is to adjust the period length to 1/2 entered value. This is based on McGinleys research giving the last half of a data set the stronger weight
mgAvgClose = na(mgAvgClose[1]) ? ema(tClose, mgPeriod) : mgAvgClose[1] + (tClose - mgAvgClose[1]) / ((mgPeriod/2) * pow(tClose/mgAvgClose[1], 4))
mgAvgOpen = na(mgAvgOpen[1]) ? ema(tOpen, mgPeriod) : mgAvgOpen[1] + (tOpen - mgAvgOpen[1]) / ((mgPeriod/2) * pow(tOpen/mgAvgOpen[1], 4))
 
// Plot McGinley Moving Average
mgAvgOpenPlot = mgEnvelopes == true ? mgAvgOpen : na
mc_ginley = mgAvgOpenPlot ? mgAvgOpenPlot : na
mgOpenPlot = plot(mc_ginley, title = "McGinley Moving Average", color = yellow, transp = 0, style = cross, linewidth = 2, join = true)
 
ema_day = input(8, "EMA Day")
ema_val = ema(close, ema_day)
ema28 = ema(close, 28)
ema14 = ema(close, 14)
 
long = cross(mc_ginley, ema_val) and ema14 > ema28
short = cross(mc_ginley, ema_val) and ema14 < ema28
 
up = ema_val > ema_val[1]
down = ema_val < ema_val[1]
mycolor = up ? green : down ? red : blue
 
//exit_input = input("Exit Signal", defval="When EMA crosses price", options=["When EMA crosses price", "When EMA changes direction"])
exit_input = input(1, "Exit Signal (When EMA crosses price: 1, When EMA changes direction: 2)")
exit_signal = if exit_input == 1//"When EMA crosses price"
    cross(close, ema_val)
else
    tmp = if exit_input == 2//"When EMA changes direction"
        mycolor != mycolor[1]
    tmp
 
 
// plot(ema14, title="14", color=green, linewidth=2)
// plot(ema28, title="28", color=blue, linewidth=2)
plot(ema14, title="EMA (user)", color=mycolor, linewidth=2)
 
testStartYear = input(2014, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(2, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
 
testStopYear = input(2015, "Backtest Stop Year")
testStopMonth = input(12, "Backtest Stop Month")
testStopDay = input(30, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
 
testStartHour = input(0, "Backtest Start Hour")
testStartMinute = input(0, "Backtest Start Minute")
 
testStopHour = input(0, "Backtest Stop Hour")
testStopMinute = input(0, "Backtest Stop Minute")
 
stop_loss = input(1, "Stop loss (ticks)") * 12.5
 
 
if time >= testPeriodStart and time <= testPeriodStop
    if hour >= testStartHour and hour <= testStopHour and minute >= testStartMinute and minute <= testStopMinute
        strategy.entry("Long", strategy.long, 1.0, when=long)
        strategy.entry("Short", strategy.short, 1.0, when=short)
 
if time >= testPeriodStart and time <= testPeriodStop
    if hour >= testStartHour and hour <= testStopHour and minute >= testStartMinute and minute <= testStopMinute
	    strategy.exit("Close Long", "Long", loss=stop_loss, when=exit_signal)
	    strategy.exit("Close Short", "Short", loss=stop_loss, when=exit_signal)

← 返回列表