Back in August, Tradingview added a major new feature to their alert system. This new feature, known as “webhooks” means that Tradingview can now send an alert directly to any server. This was big news for all the third-party applications that attempt to bridge the automation gap between Pine script and brokers. Instead of monitoring emails or popups via a browser extension, alerts can now be pushed directly to applications in a very efficient and reliable way. To give you an idea of some of the possibilities that have now opened up, I will hand you over to Tradingview to explain in their own words:
By using webhooks and Zapier.com you can send triggered alert details to Slack, Twitter and thousands of other platforms. By connecting to apps such as 3commas, Instabot Trader, or Alertatron the world of algotrading is also open, as the alerts can be delivered in a matter of seconds.
Source: https://www.tradingview.com/blog/en/webhooks-for-alerts-now-available-14054/
You don’t need to connect with only those application either. Tradingivew allows you fire off alerts to any server in the world and with that, it means we can now bridge the two big frameworks covered on this site! So without further ado, here is an introduction to…
tv2bt
A data feed for Backtrader which will allow you to receive trade signals and/orOHLCV
data from Tradingview. It aims to open access to the plethora of open source strategies and indicators on the Tradingview and allow you to use them to influence your own live strategies. In addition, it can also be used to find some good, reliable intra-day data.
IMPORTANT NOTE: If you plan to use tv2bt
with bt-ccxt-store
, please download the latest source code from the repository here: https://github.com/Dave-Vallance/bt-ccxt-store
A minor change has been made so that bt-ccxt-store
sends orders using self.p.dataname
instead of self.symbol
. This might not make much sense, but don’t worry. Just get the latest code.
The DataFeed
The datafeed should act like any other Backtrader data feed with one small addition, a signal line. Therefore, you can expect to find the following:
- Date
- Open
- High
- Low
- Close
- Volume
- Signal
These lines are populated according to data you include in your alerts. As such, sending OHLCV
data is entirely optional. You can choose to send only signals from Tradingview if you wish. If you do this, the OHLCV
data values will appear asNaN
in Backtrader and can be safely ignored.
Similarly, if an alert containsdatetime
information, then the date & time will be used. If not, the current time of when the signal was received by Backtrader shall be used instead.
The feed is designed to be as flexible and simple as possible. Therefore, you should consider how you want to parse the data and react to it. Thesignal
line can accept any number given to it. It will be up to you to decide what these signals mean in Backtrader. (For example, 1 = Go long, -1 = go_short etc).
You can also create one or many signals for the same instrument. Just give them unique datanames/tickers
and then add some code to handle them appropriately in the strategy when next()
is called.
The repository comes with an example strategy that contains alert syntax that you can build from. Start from there and build up.
Parameters
kickstart
: Will send an initial signal alert with the value0
during initialization. This should be used if you add a Tradingview feed that only contains signals. The reason is that it appears Backtrader will not callnext()
until it has at least one bar of data in all data feeds added. Kickstarting will ensure the system can callnext()
with other DataFeeds before your first signal arrives.debug
: Turns on some extra debug prints.
Setup
If you have not noticed already, this article assumes the reader has some experience in programming and Backtrader. This means you should be at least familiar with some Backtrader platform concepts (such as what a data feed is!) and how to checkout a GitHub repository. Since there are a few files in the tv2bt
module, the code of each will not be copy and pasted here.
Head over to GitHub and get a copy of the repository here: https://github.com/Dave-Vallance/tv2bt
Once you have a copy, you can install it by running setup.py
. If you have checked out the repository using git
, then it could be a good idea to install it in development mode. This means that Python will not install the source code into your site-packages. Instead, a link will be added pointing to the folder with the source code you checked out. This, in turn, means that if you there are changes in the future (from updates or your own tweaks) they will work immediately without requiring you to re-install the code.
python setup.py install develop
Remember to replace python
with python3
if you also have python 2.x installed on your system (OSX and Linux users).
Port Forwarding
A simple Flask server is used to receive the webhooks from Tradingview. This means, that you will need to have the flask
package installed and configure some port forwarding on your router to the machine running the datafeed. Redirect port 80
(HTTP) to port 8123
. If you don’t do this, no alerts will be received.
Setting up Tradingview
To setup Tradingview forOHLCV
data, you can use the following simple script to fire off alerts:
//@version=4 study("DataFeed") plot(close) alert = true msg = "{'symbol':'XBT/USD', 'DT':'{{time}}', 'O':{{open}}, 'H':{{high}}, 'L':{{low}}, 'C':{{close}}, 'V':{{volume}}}" alertcondition(alert, title='data', message=msg)
The alert uses some special syntax that will replace the parts between the double curly brackets{{}}
with real data. However, notice that the ticker (XBT/USD
) does not use{{ticker}}
. This because the ticker you use in Backtrader might be different to the ticker used by Tradingview.
If you wantSignals
only you can you the following line into any alert that you configure:
{'symbol':'[INSERT TICKER]', 'action':1}
Of course, remember to replace [INSERT TICKER]
with the samedataname
you give to the data feed.
It is also worth noting that you can combine everything together! There is no need to send OHLVC
and Signals
separately. If you have a study()
that can pass a signal every bar, then send everything in one datafeed.
Testing / Troubleshooting
If you are having trouble receiving alerts, a special test script has been provided to push alerts to the system in the same manner that Tradingview would. First, fire up the example_strategy.py
and then run alert_tests.py
. You should see some test data pushed to the system.
In addition to this, there is a second tool for firing signal alerts only. This is fire_alerts.py
. The difference here, is that you can send as many alerts as you like and also specify theaction
. This can be useful when testing with live brokers.
If you are able to see these alerts but you are not seeing Tradingview alerts, check your alert settings at Tradingview side or your port forward settings. If you don’t see the alerts when running this script, ensure you have not made any changes to example_strategy.py
Find This Post Useful?
If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.
Backtest Rookies is a registered with Brave publisher!
Brave users can drop us a tip.
Alternatively, support us by switching to Brave using this referral link and we will receive some BAT!
Referral Link
Enjoying the content and thinking of subscribing to Tradingview? Support this site by clicking the referral link before you sign up!
3HxNVyh5729ieTfPnTybrtK7J7QxJD9gjD
0x9a2f88198224d59e5749bacfc23d79507da3d431
M8iUBnb8KamHR18gpgi2sSG7jopddFJi8S
Thanks Boss
This is brilliant !