SuperRWI Strategy

DISOBAE · study · 179 行 · 点赞 1,417 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlphaOne28
 
//@version=5
indicator("Double Supertrend Entry and Single Supertrend Exit with ATR Filter", shorttitle="DSE-Counter", overlay=true)
plot(close)
 
 
// Input Parameters
Periods = input.int(title="ATR Period", defval=14)
src = input(close, title="Source")
showsignals = input.bool(title="Show Buy/Sell Signals?", defval=true)
Multiplier = input.float(title="ATR Multiplier", defval=2.0)
changeATR = input.bool(title="Change ATR Calculation Method?", defval=true)
highlighting = input.bool(title="Highlighter On/Off?", defval=false)
Periods2 = input.int(title="ATR Period 2", defval=21)
Multiplier2 = input.float(title="ATR Multiplier 2", defval=1.0)
changeATR2 = input.bool(title="Change ATR 2 Calculation Method?", defval=true)
highlighting2 = input.bool(title="Highlighter 2 On/Off?", defval=false)
Periods3 = input.int(title="Profit Target ATR Period", defval=14)
Periods4 = input.int(title="Stop Loss ATR Period", defval=14)
 
 
// Input Parameters for DMI/ADX
dmiLength = input.int(14, title="DMI Length", minval=1)
adxLevel = input.int(0, title="ADX Threshold", minval=0)
adxSmoothing = input.int(14, title="ADX Smoothing", minval=1)
 
// Input Parameters for profit target and stop loss
profitTargetMultiplier = input.float(title="Profit Target Multiplier", defval=2.0)
stopLossMultiplier = input.float(title="Stop Loss Multiplier", defval=2.0)
 
/// Supertrend Function
supertrend(src, factor, length, changeATR = true) =>
    atr2 = ta.sma(ta.atr(length), length)
    atr = changeATR ? ta.atr(length) : atr2
    up = src - (factor * atr)
    up1 = nz(up[1], up)
    up := close[1] > up1 ? math.max(up, up1) : up
    dn = src + (factor * atr)
    dn1 = nz(dn[1], dn)
    dn := close[1] < dn1 ? math.min(dn, dn1) : dn
    trend = 1
    trend := nz(trend[1], trend)
    trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
    [up, dn, trend]
 
// Calculate Supertrends
[up1, dn1, supertrend1] = supertrend(close, Multiplier, Periods, changeATR)
[up2, dn2, supertrend2] = supertrend(close, Multiplier2, Periods2, changeATR2)
 
// Calculate ATR for Profit target and Stop Losses
Profit_ATR = ta.atr(Periods3)
Stop_ATR = ta.atr(Periods4)
 
// Calculate DMI and ADX for the trade filtering
[diplus, diminus, adx] = ta.dmi(dmiLength, adxSmoothing)
 
// Plotting Supertrend lines
upPlot1 = plot(supertrend1 == 1 ? up1 : na, title="Up Trend 1", style=plot.style_linebr, linewidth=2, color=color.green)
dnPlot1 = plot(supertrend1 == -1 ? dn1 : na, title="Down Trend 1", style=plot.style_linebr, linewidth=2, color=color.red)
 
upPlot2 = plot(supertrend2 == 1 ? up2 : na, title="Up Trend 2", style=plot.style_linebr, linewidth=2, color=color.blue)
dnPlot2 = plot(supertrend2 == -1 ? dn2 : na, title="Down Trend 2", style=plot.style_linebr, linewidth=2, color=color.orange)
 
// Plotting the highlighters
//mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
//longFillColor = highlighting ? (supertrend1 == 1 ? color.green : color.white) : color.white
//shortFillColor = highlighting ? (supertrend1 == -1 ? color.red : color.white) : color.white
//fill(mPlot, upPlot1, title="UpTrend Highligter", color=longFillColor)
//fill(mPlot, dnPlot1, title="DownTrend Highligter", color=shortFillColor)
 
//mPlot2 = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
//longFillColor2 = highlighting2 ? (supertrend2 == 1 ? color.green : color.white) : color.white
//shortFillColor2 = highlighting2 ? (supertrend2 == -1 ? color.red : color.white) : color.white
//fill(mPlot2, upPlot2, title="UpTrend Highligter", color=longFillColor2)
//fill(mPlot2, dnPlot2, title="DownTrend Highligter", color=shortFillColor2)
 
//Take profit and Stop loss levels
var float long_tp_level = na
var float long_sl_level = na
var float short_tp_level = na
var float short_sl_level = na
 
//Are we in a trade?
var bool in_long_trade = false
var bool in_short_trade = false
 
//define entries and exits
long_entry = ta.crossover(close, dn1) and ta.crossover(close, dn2) and (adx >= adxLevel) and not in_long_trade and not in_short_trade
if long_entry
    long_tp_level := close + (Profit_ATR * profitTargetMultiplier)
    long_sl_level := close - (Stop_ATR * stopLossMultiplier)
    in_long_trade := true
else
    long_entry := false
 
short_entry = ta.crossunder(close, up1) and ta.crossunder(close, up2) and (adx >= adxLevel) and not in_short_trade and not in_long_trade
if short_entry
    short_tp_level := close - (Profit_ATR * profitTargetMultiplier)
    short_sl_level := close + (Stop_ATR * stopLossMultiplier)
    in_short_trade := true
 
else
    short_entry := false
 
long_tp_reached = in_long_trade and ta.crossover(close, long_tp_level)
long_sl_reached = in_long_trade and ta.crossunder(close, long_sl_level)
short_tp_reached = in_short_trade and ta.crossunder(close, short_tp_level)
short_sl_reached = in_short_trade and ta.crossover(close, short_sl_level) 
 
long_exit = long_tp_reached or long_sl_reached
if long_exit
    in_long_trade := false
 
short_exit = short_tp_reached or short_sl_reached
if short_exit
    in_short_trade := false
 
// Plotting Entry and Exit Signals
plotshape(long_entry and showsignals ? close : na, title="Buy", text="Buy", location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white)
plotshape(short_entry and showsignals ? close : na, title="Sell", text="Sell", location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white)
 
plotshape(long_exit and showsignals and long_tp_reached ? close : na, title="Exit", text="TP", location=location.abovebar, style=shape.circle, size=size.tiny, color=color.green, textcolor=color.white)
plotshape(long_exit and showsignals and long_sl_reached ? close : na, title="Exit", text="SL", location=location.abovebar, style=shape.xcross, size=size.tiny, color=color.red, textcolor=color.white)
plotshape(short_exit and showsignals and short_tp_reached ? close : na, title="Exit", text="TP", location=location.belowbar, style=shape.circle, size=size.tiny, color=color.red, textcolor=color.white)
plotshape(short_exit and showsignals and short_sl_reached ? close : na, title="Exit", text="SL", location=location.belowbar, style=shape.xcross, size=size.tiny, color=color.green, textcolor=color.white)
 
// Initialize trade counters and running profit/loss
var int total_wins = 0
var int total_losses = 0
var float running_profit = 0.0
var float running_loss = 0.0
 
// Update trade counters and running profit/loss
if (long_exit and showsignals)
    if (long_tp_reached)
        total_wins := total_wins + 1
        running_profit := running_profit + (close - close[1])
    else if (long_sl_reached)
        total_losses := total_losses + 1
        running_loss := running_loss + (close[1] - close)
 
if (short_exit and showsignals)
    if (short_tp_reached)
        total_wins := total_wins + 1
        running_profit := running_profit + (close[1] - close)
    else if (short_sl_reached)
        total_losses := total_losses + 1
        running_loss := running_loss + (close - close[1])
 
// Calculate win/loss percentage and average profit/loss
float win_percentage = total_wins + total_losses > 0 ? total_wins * 100 / (total_wins + total_losses) : na
float avg_profit = total_wins > 0 ? running_profit / total_wins : na
float avg_loss = total_losses > 0 ? running_loss / total_losses : na
 
// Table initialization and update functions
init_table() =>
    c_transparent = color.new(color.black, 100)
    table.new(position.top_right, columns=6, rows=2, frame_color=color.new(color.black, 100), frame_width=1, border_width=1, border_color=c_transparent)
 
update_table(tbl, totalWins, totalLosses, winPercentage, avgProfit, avgLoss) =>
    table.cell(tbl, 0, 0, "Winning Trades", bgcolor=color.gray, text_color=color.white)
    table.cell(tbl, 1, 0, str.tostring(totalWins), bgcolor=color.green, text_color=color.white)
 
    table.cell(tbl, 0, 1, "Losing Trades", bgcolor=color.gray, text_color=color.white)
    table.cell(tbl, 1, 1, str.tostring(totalLosses), bgcolor=color.red, text_color=color.white)
 
    table.cell(tbl, 2, 0, "% Profitable", bgcolor=color.gray, text_color=color.white)
    table.cell(tbl, 3, 0, not na(winPercentage) ? str.tostring(winPercentage, "0") + "%" : "N/A", bgcolor=color.gray, text_color=color.white)
 
    table.cell(tbl, 2, 1, "Avg Profit", bgcolor=color.gray, text_color=color.white)
    table.cell(tbl, 3, 1, not na(avgProfit) ? str.tostring(avgProfit, "0") : "N/A", bgcolor=color.green, text_color=color.white)
 
    table.cell(tbl, 4, 0, "Avg Loss", bgcolor=color.gray, text_color=color.white)
    table.cell(tbl, 5, 0, not na(avgLoss) ? str.tostring(avgLoss, "0") : "N/A", bgcolor=color.red, text_color=color.white)
 
mytable = init_table()
update_table(mytable, total_wins, total_losses, win_percentage, avg_profit, avg_loss)

← 返回列表