本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=3
//study(title="MA Crossover Strategy", overlay = true)
strategy("MA Crossover Strategy with MA Turning Point Exits", overlay=true)
src = input(close, title="Source")
price = security(tickerid, period, src)
ma1 = input(5, title="1st MA Length")
type1 = input("HMA", "1st MA Type", options=["SMA", "EMA", "HMA", "VWMA"])
ma2 = input(7, title="2nd MA Length")
type2 = input("HMA", "2nd MA Type", options=["SMA", "EMA", "HMA", "VWMA"])
f_hma(_src, _length)=>
_return = wma((2*wma(_src, _length/2))-wma(_src, _length), round(sqrt(_length)))
price1 = if (type1 == "SMA")
sma(price, ma1)
else
if (type1 == "EMA")
ema(price, ma1)
else
if (type1 == "VWMA")
vwma(price, ma1)
else
f_hma(price, ma1)
price2 = if (type2 == "SMA")
sma(price, ma2)
else
if (type2 == "EMA")
ema(price, ma2)
else
if (type2 == "VWMA")
vwma(price, ma2)
else
f_hma(price, ma2)
//plot(series=price, style=line, title="Price", color=black, linewidth=1, transp=0)
plot(series=price1, style=line, title="1st MA", color=blue, linewidth=2, transp=0)
plot(series=price2, style=line, title="2nd MA", color=green, linewidth=2, transp=0)
longCondition = crossover(price1, price2)
if (longCondition) // and time>timestamp(2018,6,1,9,30)
strategy.entry("Long", strategy.long)
shortCondition = crossunder(price1, price2)
if (shortCondition) // and time>timestamp(2018,6,1,9,30)
strategy.entry("Short", strategy.short)
lookback1 = input(1, "Lookback 1")
roc1 = roc(price1, lookback1)
ma1up = false
ma1down = false
ma2up = false
ma2down = false
ma1up := nz(ma1up[1])
ma1down := nz(ma1down[1])
ma2up := nz(ma2up[1])
ma2down := nz(ma2down[1])
trendStrength1 = input(2, title="Minimum slope magnitude * 100", type=float) * 0.01
if crossover(roc1, trendStrength1)
ma1up := true
ma1down := false
if crossunder(roc1, -trendStrength1)
ma1up := false
ma1down := true
shortexitCondition = ma1up and ma1down[1]
if (shortexitCondition)
strategy.close("Short")
longexitCondition = ma1down and ma1up[1]
if (longexitCondition)
strategy.close("Long")