/tradingview-webhooks-bot

a framework 🏗 for trading with tradingview webhooks!

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

demopic

tvwb_demo.webm

📀 Live Demo 🖥

There's now a live demo available at http://tvwb.robswc.me.
Feel free to check it out or send it some webhooks!

DigitalOcean Referral Badge

The What 🔬

Tradingview-webhooks-bot (TVWB) is a small, Python-based framework that allows you to extend or implement your own logic using data from Tradingview's webhooks. TVWB is not a trading library, it's a framework for building your own trading logic.

The How 🏗

TVWB is fundamentally a set of components with a webapp serving as the GUI. TVWB was built with event-driven architecture in mind that provides you with the building blocks to extend or implement your own custom logic. TVWB uses Flask to handle the webhooks and provides you with a simple API to interact with the data.

Quickstart 📘

If you an experienced Python developer, you can skip the Installation and Serving the app Sections. They cover installing with pip and serving a flask app.

Ensure you're in the src directory.

Creating an action

python3 tvwb.py action:create NewAction --register

This creates an action and automatically registers it with the app. Learn more on registering here.

Note, action and event names should always be in PascalCase.

Linking an action to an event

python3 tvwb.py action:link NewAction WebhookReceived

This links an action to the WebhookReceived event. The WebhookReceived event is fired when a webhook is received by the app and is currently the only default event.

Editing an action

Navigate to src/components/actions/NewAction.py and edit the run method. You will see something similar to the following code. Feel free to delete the "Custom run method" comment and replace it with your own logic. Below is an example of how you can access the webhook data.

class NewAction(Action):
    def __init__(self):
        super().__init__()

    def run(self, *args, **kwargs):
        super().run(*args, **kwargs)  # this is required
        """
        Custom run method. Add your custom logic here.
        """
        data = self.validate_data()  # always get data from webhook by calling this method!
        print('Data from webhook:', data)

Running the app

python3 tvwb.py start

Sending a webhook

Navigate to http://localhost:5000. Ensure you see the WebhookReceived Event. Click "details" to expand the event box. Find the "Key" field and note the value. This is the key you will use to send a webhook to the app. Copy the JSON data below, replacing "YOUR_KEY_HERE" with the key you copied.

{
    "key": "YOUR_KEY_HERE",
    "message": "I'm a webhook!"
}

The key field is required, as it both authenticates the webhook and tells the app which event to fire. Besides that, you can send any data you want. The data will be available to your action via the validate_data() method. (see above, editing action)

On tradingview, create a new webhook with the above JSON data and send it to http://ipaddr:5000/webhook. You should see the data from the webhook printed to the console.

FAQs

So how do I actually trade?

To actually submit trades, you will have to use a library like ccxt for crypto currency. For other brokers, usually there are SDKs or APIs available. The general workflow would look something like: webhook signal -> tvwb (use ccxt here) -> broker. Your trade submission would take place within the run method of a custom action.

The tvwb.py shell

You can use the tvwb.py shell command to open a python shell with the app context. This allows you to interact with the app without having to enter python3 tvwb.py every time.

How do I get more help?

At the moment, the wiki is under construction. However, you may still find some good info on there. For additional assistance you can DM me on Twitter or join the Discord. I will try my best to get back to you!