Trade Planner - Buy Sell Signals | SL | TP

JohnsonForexTrader · study · 330 行 · 点赞 128 · 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("Trade Planner - Buy Sell Signals | SL | TP", overlay=true, max_lines_count=50, max_labels_count=50)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// INPUTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
fastEMA = input.int(20, "Fast EMA")
slowEMA = input.int(50, "Slow EMA")
rsiLen  = input.int(14, "RSI Length")
 
atrLen = input.int(14, "ATR Length")
slATR  = input.float(1.5, "Stop Loss ATR Multiplier", step=0.1)
 
rr1 = input.float(1.0, "TP1 Risk/Reward", step=0.1)
rr2 = input.float(2.0, "TP2 Risk/Reward", step=0.1)
rr3 = input.float(3.0, "TP3 Risk/Reward", step=0.1)
 
showEMA    = input.bool(true, "Show EMAs")
showLevels = input.bool(true, "Show Trade Levels")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// COLORS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
entryColor = color.blue
slColor    = color.red
tpColor    = color.green
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// INDICATORS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
emaFast = ta.ema(close, fastEMA)
emaSlow = ta.ema(close, slowEMA)
rsi     = ta.rsi(close, rsiLen)
atr     = ta.atr(atrLen)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BUY / SELL CONDITIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
buySignal  = ta.crossover(emaFast, emaSlow) and rsi > 50
sellSignal = ta.crossunder(emaFast, emaSlow) and rsi < 50
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// EMA PLOTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plot(showEMA ? emaFast : na, title="Fast EMA", linewidth=2)
plot(showEMA ? emaSlow : na, title="Slow EMA", linewidth=2)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BUY / SELL SIGNALS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plotshape(
     buySignal,
     title="BUY",
     style=shape.labelup,
     location=location.belowbar,
     text="BUY",
     textcolor=color.white,
     color=color.green,
     size=size.small
)
 
plotshape(
     sellSignal,
     title="SELL",
     style=shape.labeldown,
     location=location.abovebar,
     text="SELL",
     textcolor=color.white,
     color=color.red,
     size=size.small
)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// VARIABLES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var float entry = na
var float sl    = na
var float tp1   = na
var float tp2   = na
var float tp3   = na
 
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
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DELETE OLD TRADE PLAN
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if buySignal or sellSignal
    line.delete(entryLine)
    line.delete(slLine)
    line.delete(tp1Line)
    line.delete(tp2Line)
    line.delete(tp3Line)
 
    label.delete(entryLabel)
    label.delete(slLabel)
    label.delete(tp1Label)
    label.delete(tp2Label)
    label.delete(tp3Label)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BUY TRADE PLAN
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if buySignal
    entry := close
    sl := entry - atr * slATR
 
    risk = entry - sl
 
    tp1 := entry + risk * rr1
    tp2 := entry + risk * rr2
    tp3 := entry + risk * rr3
 
    if showLevels
        entryLine := line.new(
             bar_index, entry,
             bar_index + 30, entry,
             extend=extend.right,
             color=entryColor,
             width=2
        )
 
        slLine := line.new(
             bar_index, sl,
             bar_index + 30, sl,
             extend=extend.right,
             color=slColor,
             width=2
        )
 
        tp1Line := line.new(
             bar_index, tp1,
             bar_index + 30, tp1,
             extend=extend.right,
             color=tpColor,
             width=2
        )
 
        tp2Line := line.new(
             bar_index, tp2,
             bar_index + 30, tp2,
             extend=extend.right,
             color=tpColor,
             width=2
        )
 
        tp3Line := line.new(
             bar_index, tp3,
             bar_index + 30, tp3,
             extend=extend.right,
             color=tpColor,
             width=2
        )
 
        // RIGHT-SIDE TP LABELS
        tp1Label := label.new(
             bar_index + 30,
             tp1,
             "TP1",
             style=label.style_label_left,
             color=tpColor,
             textcolor=color.white
        )
 
        tp2Label := label.new(
             bar_index + 30,
             tp2,
             "TP2",
             style=label.style_label_left,
             color=tpColor,
             textcolor=color.white
        )
 
        tp3Label := label.new(
             bar_index + 30,
             tp3,
             "TP3",
             style=label.style_label_left,
             color=tpColor,
             textcolor=color.white
        )
 
        // ENTRY LABEL
        entryLabel := label.new(
             bar_index + 30,
             entry,
             "ENTRY",
             style=label.style_label_left,
             color=entryColor,
             textcolor=color.white
        )
 
        // SL LABEL
        slLabel := label.new(
             bar_index + 30,
             sl,
             "SL",
             style=label.style_label_left,
             color=slColor,
             textcolor=color.white
        )
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SELL TRADE PLAN
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if sellSignal
    entry := close
    sl := entry + atr * slATR
 
    risk = sl - entry
 
    tp1 := entry - risk * rr1
    tp2 := entry - risk * rr2
    tp3 := entry - risk * rr3
 
    if showLevels
        entryLine := line.new(
             bar_index, entry,
             bar_index + 30, entry,
             extend=extend.right,
             color=entryColor,
             width=2
        )
 
        slLine := line.new(
             bar_index, sl,
             bar_index + 30, sl,
             extend=extend.right,
             color=slColor,
             width=2
        )
 
        tp1Line := line.new(
             bar_index, tp1,
             bar_index + 30, tp1,
             extend=extend.right,
             color=tpColor,
             width=2
        )
 
        tp2Line := line.new(
             bar_index, tp2,
             bar_index + 30, tp2,
             extend=extend.right,
             color=tpColor,
             width=2
        )
 
        tp3Line := line.new(
             bar_index, tp3,
             bar_index + 30, tp3,
             extend=extend.right,
             color=tpColor,
             width=2
        )
 
        // RIGHT-SIDE TP LABELS
        tp1Label := label.new(
             bar_index + 30,
             tp1,
             "TP1",
             style=label.style_label_left,
             color=tpColor,
             textcolor=color.white
        )
 
        tp2Label := label.new(
             bar_index + 30,
             tp2,
             "TP2",
             style=label.style_label_left,
             color=tpColor,
             textcolor=color.white
        )
 
        tp3Label := label.new(
             bar_index + 30,
             tp3,
             "TP3",
             style=label.style_label_left,
             color=tpColor,
             textcolor=color.white
        )
 
        // ENTRY LABEL
        entryLabel := label.new(
             bar_index + 30,
             entry,
             "ENTRY",
             style=label.style_label_left,
             color=entryColor,
             textcolor=color.white
        )
 
        // SL LABEL
        slLabel := label.new(
             bar_index + 30,
             sl,
             "SL",
             style=label.style_label_left,
             color=slColor,
             textcolor=color.white
        )
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
alertcondition(
     buySignal,
     title="BUY Signal",
     message="Trade Planner: BUY Signal"
)
 
alertcondition(
     sellSignal,
     title="SELL Signal",
     message="Trade Planner: SELL Signal"
)

← 返回列表