EMA Momentum Trade Planner [Auto Target]

JohnsonForexTrader · study · 333 行 · 点赞 158 · 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/
// © JohnsonForexTrader
 
//@version=6
indicator("EMA Momentum Trade Planner [Auto Target]", shorttitle="EMA Auto TP", overlay=true, max_lines_count=20, max_labels_count=100)
 
//────────────────────────────────────
// SETTINGS
//────────────────────────────────────
grp1 = "EMA Settings"
emaFastLen = input.int(9, "Fast EMA", minval=1, group=grp1)
emaSlowLen = input.int(21, "Slow EMA", minval=1, group=grp1)
 
grp2 = "Auto Target Settings"
atrLen    = input.int(14, "ATR Length", minval=1, group=grp2)
slATR     = input.float(1.0, "SL ATR Multiplier", minval=0.1, step=0.1, group=grp2)
 
rr1 = input.float(1.0, "TP1 RR", minval=0.1, step=0.1, group=grp2)
rr2 = input.float(2.0, "TP2 RR", minval=0.1, step=0.1, group=grp2)
rr3 = input.float(3.0, "TP3 RR", minval=0.1, step=0.1, group=grp2)
 
grp3 = "Display"
showEMA = input.bool(true, "Show EMA", group=grp3)
showTargets = input.bool(true, "Show Auto Targets", group=grp3)
 
//────────────────────────────────────
// CALCULATIONS
//────────────────────────────────────
ema9  = ta.ema(close, emaFastLen)
ema21 = ta.ema(close, emaSlowLen)
atr   = ta.atr(atrLen)
 
//────────────────────────────────────
// TREND
//────────────────────────────────────
bullTrend = ema9 > ema21
bearTrend = ema9 < ema21
 
//────────────────────────────────────
// SIGNALS
//────────────────────────────────────
buySignal  = barstate.isconfirmed and ta.crossover(close, ema9) and bullTrend
sellSignal = barstate.isconfirmed and ta.crossunder(close, ema9) and bearTrend
 
//────────────────────────────────────
// TRADE VARIABLES
//────────────────────────────────────
var float entry = na
var float sl    = na
var float tp1   = na
var float tp2   = na
var float tp3   = na
 
var int direction = 0
 
var line entryLine = na
var line slLine = na
var line tp1Line = na
var line tp2Line = na
var line tp3Line = na
 
var label entryLabel = na
var label slLabel = na
var label tp1Label = na
var label tp2Label = na
var label tp3Label = na
 
//────────────────────────────────────
// REMOVE PREVIOUS PLAN
//────────────────────────────────────
if buySignal or sellSignal
 
    if not na(entryLine)
        line.delete(entryLine)
 
    if not na(slLine)
        line.delete(slLine)
 
    if not na(tp1Line)
        line.delete(tp1Line)
 
    if not na(tp2Line)
        line.delete(tp2Line)
 
    if not na(tp3Line)
        line.delete(tp3Line)
 
    if not na(entryLabel)
        label.delete(entryLabel)
 
    if not na(slLabel)
        label.delete(slLabel)
 
    if not na(tp1Label)
        label.delete(tp1Label)
 
    if not na(tp2Label)
        label.delete(tp2Label)
 
    if not na(tp3Label)
        label.delete(tp3Label)
 
//────────────────────────────────────
// BUY AUTO TARGETS
//────────────────────────────────────
if buySignal
 
    direction := 1
    entry := close
 
    risk = atr * slATR
 
    sl  := entry - risk
    tp1 := entry + risk * rr1
    tp2 := entry + risk * rr2
    tp3 := entry + risk * rr3
 
    if showTargets
 
        entryLine := line.new(
             bar_index, entry,
             bar_index + 1, entry,
             extend=extend.right,
             color=color.blue,
             width=2)
 
        slLine := line.new(
             bar_index, sl,
             bar_index + 1, sl,
             extend=extend.right,
             color=color.red,
             style=line.style_dashed)
 
        tp1Line := line.new(
             bar_index, tp1,
             bar_index + 1, tp1,
             extend=extend.right,
             color=color.green,
             style=line.style_dashed)
 
        tp2Line := line.new(
             bar_index, tp2,
             bar_index + 1, tp2,
             extend=extend.right,
             color=color.green,
             style=line.style_dashed)
 
        tp3Line := line.new(
             bar_index, tp3,
             bar_index + 1, tp3,
             extend=extend.right,
             color=color.green,
             width=2)
 
        entryLabel := label.new(
             bar_index + 1,
             entry,
             "ENTRY\n" + str.tostring(entry, format.mintick),
             style=label.style_label_left,
             color=color.blue,
             textcolor=color.white,
             size=size.small)
 
        slLabel := label.new(
             bar_index + 1,
             sl,
             "SL\n" + str.tostring(sl, format.mintick),
             style=label.style_label_left,
             color=color.red,
             textcolor=color.white,
             size=size.small)
 
        tp1Label := label.new(
             bar_index + 1,
             tp1,
             "TP1  " + str.tostring(tp1, format.mintick),
             style=label.style_label_left,
             color=color.green,
             textcolor=color.white,
             size=size.small)
 
        tp2Label := label.new(
             bar_index + 1,
             tp2,
             "TP2  " + str.tostring(tp2, format.mintick),
             style=label.style_label_left,
             color=color.green,
             textcolor=color.white,
             size=size.small)
 
        tp3Label := label.new(
             bar_index + 1,
             tp3,
             "TP3  " + str.tostring(tp3, format.mintick),
             style=label.style_label_left,
             color=color.green,
             textcolor=color.white,
             size=size.small)
 
//────────────────────────────────────
// SELL AUTO TARGETS
//────────────────────────────────────
if sellSignal
 
    direction := -1
    entry := close
 
    risk = atr * slATR
 
    sl  := entry + risk
    tp1 := entry - risk * rr1
    tp2 := entry - risk * rr2
    tp3 := entry - risk * rr3
 
    if showTargets
 
        entryLine := line.new(
             bar_index, entry,
             bar_index + 1, entry,
             extend=extend.right,
             color=color.blue,
             width=2)
 
        slLine := line.new(
             bar_index, sl,
             bar_index + 1, sl,
             extend=extend.right,
             color=color.red,
             style=line.style_dashed)
 
        tp1Line := line.new(
             bar_index, tp1,
             bar_index + 1, tp1,
             extend=extend.right,
             color=color.green,
             style=line.style_dashed)
 
        tp2Line := line.new(
             bar_index, tp2,
             bar_index + 1, tp2,
             extend=extend.right,
             color=color.green,
             style=line.style_dashed)
 
        tp3Line := line.new(
             bar_index, tp3,
             bar_index + 1, tp3,
             extend=extend.right,
             color=color.green,
             width=2)
 
        entryLabel := label.new(
             bar_index + 1,
             entry,
             "ENTRY\n" + str.tostring(entry, format.mintick),
             style=label.style_label_left,
             color=color.blue,
             textcolor=color.white,
             size=size.small)
 
        slLabel := label.new(
             bar_index + 1,
             sl,
             "SL\n" + str.tostring(sl, format.mintick),
             style=label.style_label_left,
             color=color.red,
             textcolor=color.white,
             size=size.small)
 
        tp1Label := label.new(
             bar_index + 1,
             tp1,
             "TP1  " + str.tostring(tp1, format.mintick),
             style=label.style_label_left,
             color=color.green,
             textcolor=color.white,
             size=size.small)
 
        tp2Label := label.new(
             bar_index + 1,
             tp2,
             "TP2  " + str.tostring(tp2, format.mintick),
             style=label.style_label_left,
             color=color.green,
             textcolor=color.white,
             size=size.small)
 
        tp3Label := label.new(
             bar_index + 1,
             tp3,
             "TP3  " + str.tostring(tp3, format.mintick),
             style=label.style_label_left,
             color=color.green,
             textcolor=color.white,
             size=size.small)
 
//────────────────────────────────────
// EMA
//────────────────────────────────────
plot(
     showEMA ? ema9 : na,
     title="EMA 9",
     color=color.blue,
     linewidth=2)
 
plot(
     showEMA ? ema21 : na,
     title="EMA 21",
     color=color.orange,
     linewidth=2)
 
//────────────────────────────────────
// BUY / SELL LABEL
//────────────────────────────────────
plotshape(
     buySignal,
     title="BUY Signal",
     text="BUY",
     style=shape.labelup,
     location=location.belowbar,
     color=color.green,
     textcolor=color.white,
     size=size.small)
 
plotshape(
     sellSignal,
     title="SELL Signal",
     text="SELL",
     style=shape.labeldown,
     location=location.abovebar,
     color=color.red,
     textcolor=color.white,
     size=size.small)

← 返回列表