本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cyatophilum
//@version=6
strategy("Triple EMA Trend", overlay=false, fill_orders_on_standard_ohlc = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 5, commission_type = strategy.commission.percent, commission_value = 0.075)
//=== Trend Filters ===
adxGroup = "Trend Conditions"
adxLen = input.int(14, "ADX Length", group=adxGroup)
adxThresh = input.float(43.0, "ADX Threshold", group=adxGroup)
emaGroup = "EMA Conditions"
emaFastLen = input.int(30, "EMA Fast Length", group=emaGroup)
emaMidLen = input.int(46, "EMA Mid Length", group=emaGroup)
emaSlowLen = input.int(80, "EMA Slow Length", group=emaGroup)
//=== Risk Management ===
riskGroup = "Risk Management"
atrLen = input.int(14, "ATR Length", group=riskGroup)
slMult = input.float(1.8, "Stop Loss ATR Multiplier", group=riskGroup)
tpMult = input.float(3.3, "Take Profit ATR Multiplier", group=riskGroup)
backtestGroup = "Backtesting"
startDate = input.time(timestamp("01 Jan 2021 00:00 +0300"),'Start Date', group = backtestGroup)
endDate = input.time(timestamp("20 Jul 2030 00:00 +0300"),'End Date', group = backtestGroup)
enableLongs = input.bool(true,'Go Long', group = backtestGroup)
enableShorts = input.bool(false,'Go Short', group = backtestGroup)
dirmov(len) =>
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
truerange = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
[plus, minus]
adx(dilen, adxlen) =>
[plus, minus] = dirmov(dilen)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
var bool isLong = false
var bool isShort = false
var float entryPrice = na
var float stoplossPrice = na
var float takeprofitPrice = na
bool stoplossHit = false
bool takeprofitHit = false
emaFast = ta.ema(close, emaFastLen)
emaMid = ta.ema(close, emaMidLen)
emaSlow = ta.ema(close, emaSlowLen)
adx = adx(adxLen, adxLen)
atr = ta.atr(atrLen)
longCondition = adx > adxThresh and emaFast > emaMid and emaMid > emaSlow and close > emaFast
shortCondition = adx > adxThresh and emaFast < emaMid and emaMid < emaSlow and close < emaFast
entryLong = longCondition and not (isLong or isShort)
entryShort = shortCondition and not (isLong or isShort)
isLong := entryLong ? true : isLong
isShort := entryShort ? true : isShort
newTrade = entryLong or entryShort
crossoverTP = ta.crossover(high, takeprofitPrice)
crossunderSL = ta.crossunder(low, stoplossPrice)
crossunderTP = ta.crossunder(low, takeprofitPrice)
crossoverSL = ta.crossover(high, stoplossPrice)
if newTrade
entryPrice := close
if isLong
stoplossPrice := entryPrice - slMult * atr
takeprofitPrice := entryPrice + tpMult * atr
if isShort
stoplossPrice := entryPrice + slMult * atr
takeprofitPrice := entryPrice - tpMult * atr
else
if isLong
if crossoverTP
takeprofitHit := true
else if crossunderSL
stoplossHit := true
if isShort
if crossunderTP
takeprofitHit := true
else if crossoverSL
stoplossHit := true
if takeprofitHit or stoplossHit
isLong := false
isShort := false
if time > startDate and time < endDate
if (entryLong and enableLongs)
strategy.entry("Long Entry", strategy.long)
strategy.exit('Exit Long', 'Long Entry', limit = takeprofitPrice, stop = stoplossPrice)
if (entryShort and enableShorts)
strategy.entry("Short Entry", strategy.short)
strategy.exit('Exit Short', 'Short Entry', limit = takeprofitPrice, stop = stoplossPrice)
plot(adx, color=color.red, title="ADX")
hline(adxThresh,'ADX Threshold', color.white)
plot(emaFast,'EMA Fast', color.blue, force_overlay = true)
plot(emaMid,'EMA Mid', color.white, 2, force_overlay = true)
plot(emaSlow,'EMA Slow', color.yellow, 3, force_overlay = true)
plot(isLong or isShort ? takeprofitPrice : na, 'TP Price', color.lime, 1, plot.style_linebr, force_overlay = true)
plot(isLong or isShort ? stoplossPrice : na, 'SL Price', color.red, 1, plot.style_linebr, force_overlay = true)
plotshape(entryLong, 'Long Entry', shape.labelup, location.belowbar, color.lime, text='Long', textcolor = color.black, force_overlay = true)
plotshape(entryShort, 'Short Entry', shape.labeldown, location.abovebar, color.red, text='Short', textcolor = color.white, force_overlay = true)