Zero-Lag MA Trend Following

PakunFX · strategy · 62 行 · 点赞 764 · 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/
// © PakunFX
 
//@version=5
strategy("Zero-Lag MA Trend Following", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
 
// ユーザー入力
length = input.int(34, title="Length")
rr_ratio = input.float(2.0, title="Risk-Reward Ratio", step=0.1)
 
// カラー設定
up = input.color(color.green, "Up Label Color")
dn = input.color(color.red, "Down Label Color")
 
var box box1 = na
atr = ta.atr(14)  // ATRでボックスの範囲を計算
 
emaValue = ta.ema(close, length)
correction = close + (close - emaValue)
zlma = ta.ema(correction, length)
 
signalUp = ta.crossover(zlma, emaValue)  // 上昇シグナル
signalDn = ta.crossunder(zlma, emaValue)  // 下降シグナル
 
// フラグでエントリー条件を管理
var bool longConditionMet = false
var bool shortConditionMet = false
 
// ボックス生成ロジック
if signalUp
    box1 := box.new(bar_index, zlma, bar_index, zlma - atr, bgcolor=color.new(up, 90))
    longConditionMet := true  // 上昇シグナル後、ロング条件を満たす
 
if signalDn
    box1 := box.new(bar_index, zlma + atr, bar_index, zlma, bgcolor=color.new(dn, 90))
    shortConditionMet := true  // 下降シグナル後、ショート条件を満たす
 
if na(box1) == false
    box.set_right(box1, bar_index + 4)
 
// エントリー条件:価格が再度ボックスを抜けた場合
if longConditionMet and ta.crossover(low, box1.get_top()) and barstate.isconfirmed
    sl = box1.get_bottom()
    tp = close + (close - sl) * rr_ratio
    strategy.entry("Long", strategy.long)
    strategy.exit("TP/SL Long", from_entry="Long", limit=tp, stop=sl)
    longConditionMet := false  // 条件をリセット
 
if shortConditionMet and ta.crossunder(high, box1.get_bottom()) and barstate.isconfirmed
    sl = box1.get_top()
    tp = close - (sl - close) * rr_ratio
    strategy.entry("Short", strategy.short)
    strategy.exit("TP/SL Short", from_entry="Short", limit=tp, stop=sl)
    shortConditionMet := false  // 条件をリセット
 
// 補助プロット
plot(zlma, color=color.new(color.blue, 0), linewidth=2)
plot(emaValue, color=color.new(color.orange, 0), linewidth=2)
 
// ダイヤモンドシグナルプロット(視覚補助)
plotshape(series=signalUp, style=shape.diamond, location=location.abovebar, color=color.new(color.green, 50), size=size.small, title="Bullish Signal")
plotshape(series=signalDn, style=shape.diamond, location=location.belowbar, color=color.new(color.red, 50), size=size.small, title="Bearish Signal")

← 返回列表