Triple EMA + QQE Trend Following Strategy [TradeDots]

tradedots · strategy · 99 行 · 点赞 1,461 · TradingView 原页

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

Pine Script

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tradedots
 
//@version=5
strategy("Triple EMA + QQE Trend Following Strategy [TradeDots]", overlay = true, max_lines_count = 500, max_boxes_count = 100, initial_capital = 10000, currency = currency.USD, commission_type = strategy.commission.percent, commission_value = 0.01, default_qty_type = strategy.percent_of_equity, default_qty_value = 80)
 
//pipsize
GetPipSize() =>
    syminfo.mintick * (syminfo.type == "forex" ? 10 : 1)
 
pipSize = GetPipSize()
 
var ts = 0.
var float stopLoss = na
 
//qqe values
rsi_period = input(14, title='RSI Length', group = "QQE")
rsi_smoothing_period = input(5, title='RSI Smoothing Period', group = "QQE")
factor = input(4.238, title='QQE Factor', group = "QQE")
 
//tema
ma_source = input(close, "TEMA Source", inline="MA1", group="TEMA")
tema1_length = input.int(20, "TEMA #1 Length", minval=1,group="TEMA")
tema2_length = input.int(40, "TEMA #2 Length", minval=1,group="TEMA")
 
input_stop_loss = input.int(120, "Stop Loss (pip)", group = "Strategy")
 
tema(src, length) =>
    ema1 = ta.ema(src, length)
    ema2 = ta.ema(ema1, length)
    ema3 = ta.ema(ema2, length)
 
    tema = 3*ema1 - 3*ema2 + ema3
 
tema1 = tema(ma_source, tema1_length)
tema2 = tema(ma_source, tema2_length)
 
tema1Plot = plot(tema1, title = " TEMA #1", color = #c7f9cc, linewidth = 2)
tema2Plot = plot(tema2, title = " TEMA #2", color = #57cc99, linewidth = 2)
 
// calculating qqe
rsi = ta.rsi(close, rsi_period)
rsiMa = ta.ema(rsi, rsi_smoothing_period)
atrRsi = math.abs(rsiMa[1] - rsiMa)
 
final_smooth = rsi_period * 2 - 1
MaAtrRsi = ta.ema(atrRsi, final_smooth)
dar = ta.ema(MaAtrRsi, final_smooth) * factor
 
crossover = ta.crossover(rsiMa, ts)
crossunder = ta.crossunder(rsiMa, ts)
 
ts := nz(crossover ? rsiMa - MaAtrRsi * factor
  : crossunder ? rsiMa + MaAtrRsi * factor
  : rsiMa > ts ? math.max(rsiMa - MaAtrRsi * factor, ts)
  : math.min(rsiMa + MaAtrRsi * factor, ts), rsiMa)
 
// entry and exit conditions
long_condition = close > tema1 and tema1 > tema2 and tema2 > tema2[1] and ta.crossover(rsiMa, ts) and dayofweek != dayofweek.monday
short_condition = close < tema1 and tema1 < tema2 and tema2 < tema2[1] and ta.crossunder(rsiMa, ts) and dayofweek != dayofweek.monday
 
if long_condition and strategy.opentrades == 0
    strategy.entry("Long", strategy.long)
    label.new(bar_index, low, "🟢 Long", color = #9cff87, style = label.style_label_up)
    stopLoss := close - input_stop_loss * pipSize
 
else if short_condition and strategy.opentrades == 0
    strategy.entry("Short", strategy.short)
    label.new(bar_index, high, "🔴 Short", color = #f9396a, style = label.style_label_down, textcolor = color.white)
    stopLoss := close + input_stop_loss * pipSize
 
else if strategy.opentrades == 0
    stopLoss := na
 
if barstate.isconfirmed and strategy.opentrades > 0
    if strategy.opentrades.size(strategy.opentrades- 1) > 0
        if close < stopLoss
            strategy.close("Long")
        stopLoss := (close - input_stop_loss * pipSize) > stopLoss ? close - input_stop_loss * pipSize : stopLoss
    else if strategy.opentrades.size(strategy.opentrades - 1) < 0
        if close > stopLoss
            strategy.close("Short")
        stopLoss := (close + input_stop_loss * pipSize) < stopLoss ? close + input_stop_loss * pipSize : stopLoss
 
// Check if position is closed (Print label if true)
is_pos_closed = (strategy.position_size[1] != 0 and strategy.position_size == 0)
is_short = strategy.position_size < 0
is_long = strategy.position_size > 0
 
if is_pos_closed and is_short[1]
    label.new(bar_index, low, "🟢 Close Short", color = #9cff87, style = label.style_label_up)
 
else if is_pos_closed and is_long[1]
    label.new(bar_index, high, "🔴 Close Long", color = #f9396a, style = label.style_label_down, textcolor = color.white)
 
fill(tema2Plot, tema1Plot, math.max(tema2,tema1), tema2,color.new(#9cff87, 85), color.new(#9cff87, 70))
fill(tema2Plot, tema1Plot, tema2, math.min(tema2,tema1),color.new(#f9396a, 70), color.new(#f9396a, 85))
 
plot(stopLoss, color=#f9396a, style = plot.style_cross, linewidth = 2, title = "Trail Stop")

← 返回列表