TradingView Signals
TradingView signals are received through webhooks (opens in a new tab) provided on the platform. However, it is not safe to expose your API Key and API Secret publicly hence it is required to set up a separate Python HTTP server to redirect your signals from TradingView to Cybotrade (opens in a new tab).
You can follow the steps below to build your setup.
Set up a simple Python HTTP server
First, you'll need to install the following packages:
pip install fastapi "uvicorn[standard]" requests
Then, create a file main.py
with:
import hmac
import hashlib
import time
import json
import requests
from starlette.requests import Request
from starlette.responses import Response
from fastapi import FastAPI
API_KEY = "XXX"
API_SECRET = "XXX"
WEBHOOK_URL = 'https://strategy-1.publisher.cybotrade.rs'
app = FastAPI()
@app.post("/webhook")
async def webhook(request: Request):
message = (await request.body()).decode('utf-8')
if "buy" in message:
payload = {"side": "buy"}
elif "sell" in message:
payload = {"side": "sell"}
else:
return Response(status_code=400)
requests.post(url=f"{WEBHOOK_URL}/signal", headers={
"X-CYBOTRADE-API-KEY": API_KEY,
"X-CYBOTRADE-SIGN": generate_signature(API_KEY, API_SECRET, payload)
}, data=payload)
return Response(status_code=200)
def generate_signature(api_key: str, api_secret: str, payload: dict) -> str:
timestamp = int(time.time() * 10**3)
params_str = f"{api_key}{timestamp}{json.dumps(payload)}"
hash = hmac.new(
bytes(api_secret, "utf-8"),
params_str.encode("utf-8"),
hashlib.sha256,
)
return hash.hexdigest()
Run the server with:
python -m uvicorn main:app --reload
and that should spin up the HTTP server locally on your machine. To test it you can use any tools you'd like such as Postman,
Insomnia, etc. If you're on a UNIX system, you can do so using curl
.
curl -v -X POST http://localhost:8000/webhook -d 'Greedy Strategy (10, 10, 5): order buy @ 0.01 filled on BTCUSDT. New strategy position is 0.01'
The example we have above is using fastapi (opens in a new tab), but you can use any libraries you like to build the server if you're familiar with Python.
Note that the message we used here is the default provided by TradingView, if you would like to do some custom processing before forwarding the signal to us, you're welcomed to do so. To see what you can customise in TradingView's alert messages see here (opens in a new tab).
Hosting your server
As for deploying it, there are numerous ways you can do it but a cheap and simple way is to do it on AWS EC2. As long as you get a HTTP URL to your server, it is fine whatever way you deploy it.
We won't go into details in this part, do seek for help from our team if you require any.
Setup TradingView
Alerts can be created on data series, indicator plots, strategy orders and drawing objects. Follow TradingView's docs (opens in a new tab) to set it up. After setting up your alerts, you can select ‘Webhook URL’ under ‘Alert Actions’ and put in your deployed server's URL.
After doing that, whenever your strategy triggers an alert TradingView will forward that signal to your web server.