Ultimate Strategy Template (Advanced Edition)

Hello traders

This script is an upgraded version of that one below

New features

- Upgraded to Pinescript version 5
- Added the exit SL/TP now in real-time
- Added text fields for the alerts - easier to send the commands to your trading bots

Step 1: Create your connector

Adapt your indicator with only 2 lines of code and then connect it to this strategy template.

For doing so:

1) Find in your indicator where are the conditions printing the long/buy and short/sell signals.
2) Create an additional plot as below

I'm giving an example with a Two moving averages cross.

Please replicate the same methodology for your indicator wether it's a MACD , ZigZag , Pivots , higher-highs, lower-lows or whatever indicator with clear buy and sell conditions.

indicator(title='Moving Average Cross', shorttitle='Moving Average Cross', overlay=true, precision=6, max_labels_count=500, max_lines_count=500)

type_ma1 = input.string(title='MA1 type', defval='SMA', options=['RMA', 'SMA', 'EMA'])
length_ma1 = input(10, title='[ALL but VWAP] MA1 length')

type_ma2 = input.string(title='MA2 type', defval='SMA', options=['RMA', 'SMA', 'EMA'])
length_ma2 = input(100, title='[ALL but VWAP] MA2 length')

// MA
f_ma(smoothing, src, length) =>
    rma_1 = ta.rma(src, length)
    sma_1 = ta.[__tag__=simplemovingaverage]sma[/__tag__](src, length)
    ema_1 = ta.[__tag__=ema]ema[/__tag__](src, length)
    iff_1 = smoothing == 'EMA' ? ema_1 : src
    iff_2 = smoothing == 'SMA' ? sma_1 : iff_1
    smoothing == 'RMA' ? rma_1 : iff_2

MA1 = f_ma(type_ma1, close, length_ma1)
MA2 = f_ma(type_ma2, close, length_ma2)

// buy and sell conditions
buy = ta.crossover(MA1, MA2)
sell = ta.crossunder(MA1, MA2)

plot(MA1, color=color.new(color.green, 0), title='Plot MA1', linewidth=3)
plot(MA2, color=color.new(color.red, 0), title='Plot MA2', linewidth=3)

plotshape(buy, title='LONG SIGNAL', style=shape.circle, location=location.belowbar, color=color.new(color.green, 0), size=size.normal)
plotshape(sell, title='SHORT SIGNAL', style=shape.circle, location=location.abovebar, color=color.new(color.red, 0), size=size.normal)

/////////////////////////// SIGNAL FOR STRATEGY /////////////////////////

Signal = buy ? 1 : sell ? -1 : 0
plot(Signal, title='🔌Connector🔌', display = display.data_window)

Basically, I identified my buy, sell conditions in the code and added this at the bottom of my indicator code

Signal = buy ? 1 : sell ? -1 : 0
plot(Signal, title="🔌Connector🔌", transp=100)

Important Notes

🔥 The Strategy Template expects the value to be exactly 1 for the bullish signal, and -1 for the bearish signal

Now you can connect your indicator to the Strategy Template using the method below or that one

Step 2: Connect the connector

1) Add your updated indicator to a TradingView chart
2) Add the Strategy Template as well to the SAME chart
3) Open the Strategy Template settings and in the Data Source field select your 🔌Connector🔌 (which comes from your indicator)

From then, you should start seeing the signals and plenty of other stuff on your chart

🔥 Note that whenever you'll update your indicator values, the strategy statistics and visual on your chart will update in real-time


- Color Candles: Color the candles based on the trade state ( bullish , bearish , neutral)
- Close positions at market at the end of each session: useful for everything but cryptocurrencies
- Session time ranges: Take the signals from a starting time to an ending time
- Close Direction: Choose to close only the longs, shorts, or both
- Date Filter: Take the signals from a starting date to an ending date
- Set the maximum losing streak length with an input
- Set the maximum winning streak length with an input
- Set the maximum consecutive days with a loss
- Set the maximum drawdown (in % of strategy equity)
- Set the maximum intraday loss in percentage
- Limit the number of trades per day
- Limit the number of trades per week
- Stop-loss: None or Percentage or Trailing Stop Percentage or ATR - I'll add shortly multiple options for the trailing stop loss
- Take-Profit: None or Percentage or ATR - I'll add also a trailing take profit
- Risk-Reward based on ATR multiple for the Stop-Loss and Take-Profit

Special Thanks

Special thanks to @JosKodify as I borrowed a few risk management snippets from his website: https://kodify.net/tradingview-programmi...


📧 dave@best-trading-indicator

⏩ Trading Course: https://best-trading-indicator.com/pages/concepts

🐦 Twitter: https://twitter.com/bti_trading

💰 Discord: https://discord.gg/8Q4MBus

TradingViewの精神に則り、このスクリプトの作者は、トレーダーが理解し検証できるようにオープンソースで公開しています。作者に敬意を表します!無料で使用することができますが、このコードを投稿で再利用するには、ハウスルールに準拠する必要があります。 お気に入りに登録してチャート上でご利用頂けます。