编辑精选OPEN-SOURCE SCRIPT

Ultimate Strategy Template

已更新
Hello Traders

As most of you know, I'm a member of the PineCoders community and I sometimes take freelance pine coding jobs for TradingView users.

Off the top of my head, users often want to:

- convert an indicator into a strategy, so as to get the backtesting statistics from TradingView
- add alerts to their indicator/strategy
- develop a generic strategy template which can be plugged into (almost) any indicator

My gift for the community today is my Ultimate Strategy Template

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



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



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

Settings

- 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
- Take-Profit: None or Percentage or ATR
- Risk-Reward based on ATR multiple for the Stop-Loss and Take-Profit

This script is open-source so feel free to use it, and optimize it as you want

Alerts

Maybe you didn't know it but alerts are available on strategy scripts.

I added them in this template - that's cool because:

- if you don't know how to code, now you can connect your indicator and get alerts
- you have now a cool template showing you how to create alerts for strategy scripts

Source: tradingview.com/blog/en/strategy-alerts-are-live-now-18770/

I hope you'll like it, use it, optimize it and most importantly....make some optimizations to your indicators thanks to this Strategy template

Special Thanks

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

Additional features

I thought of plenty of extra filters that I'll add later on this week on this strategy template

Best
Dave
版本注释
V2.0
Added the trade direction feature

You can now:

- open only the longs or shorts or both
- close only the longs or shorts or both
版本注释
- Added the close on opposite mode
When only longs or shorts are selected, close on opposite forces the closing of the trades based on the opposite signal
It's recommended to select it if your backtest is unidirectional without exits (TP, SL, etc...)
版本注释
- Added two fields for strategy.exit and strategy.entry functions

1) comment (string) An optional parameter. Additional notes on the order. Replaces the {{strategy.order.comment}} placeholder when it is used in the "Create Alert" dialog box's "Message" field.

2) alert_message (string) An optional parameter which replaces the {{strategy.order.alert_message}} placeholder when it is used in the "Create Alert" dialog box's "Message" field.

Both placeholders in alerts to make the strategy compatible with automated trading bots (3commas, ProfitView, Gunbot, etc.)
版本注释
- Added the custom close signal (lines 117 to 119)
Now you can also send a custom close based on your indicator.

Use Case

Your connected indicator already contains signals to exit based on hard exit/indicator and you want to use those signals in the strategy template to exit the opened trades.

How to use it?

Option enabled only if you use the Use Custom Close from the Strategy Template

Use the values:

  • 1 for a Long Entry
  • -1 for a Short Entry
  • 2 for a Close Long Exit
  • -2 for a Close Short Exit


In your connected indicator, update your external connector like this

版本注释
- Added a TP2 option where you can close X% of your position at TP1 and the strategy will close the rest at TP2 (.... or at Stop-Loss)
- Refactored a bit the user inputs to group them by theme
版本注释
I published an updated more advanced version (click on the link or image below to access it)

Ultimate Strategy Template (Advanced Edition)
backtestbacktesterbacktestingMoving AveragesstrategystrategytestingtemplateTrend Analysis

开源脚本

本着真正的TradingView精神,此脚本的作者已将其开源,以便交易者可以理解和验证它。向作者致敬!您可以免费使用它,但在出版物中重复使用此代码受网站规则约束。 您可以收藏它以在图表上使用。

想在图表上使用此脚本?


⭐️ Listed as an Official TradingView Trusted TOP Pine Programmer

📧 Coding/Consulting Inquiries: dave@best-trading-indicator

Telegram: Daveatt

⏩ Course: best-trading-indicator.com

Twitter: twitter.com/bti_trading
更多:

免责声明