Custom Webhook
Introduction
This feature is where on the official Cybotrade (opens in a new tab) platform, there are only three ways to write a trading strategies:
- Python Code
- Webhook
- Graph
However, for normal users only strategies written with Python Code is allowed to be published to arena and allow users to copy trade at the moment. We have a private and dedicated pipeline for publishers who wish to publish their webhook strategies to arena instead. By doing so, we separate the server for normal users and publishers and this would reduce traffic congestion and ensure that the signals by publishers will always have the fastest delivery speed.
Architecture
In the diagram above, Publisher's strategy refers to your own running strategy, it can be anything such as a running Python process, etc.
Each publisher will have their own dedicated webhook server for each strategy e.g.
https://strategy-1.publisher.cybotrade.rs
,
you can acquire this from the team.
If your strategy is using TradingView alerts only, you'll need to spin up a separate Python server that you can process your own TradingView alerts and forward them to us. More details on this is mentioned here.
Example scenario
Take for example you have a strategy that generate buy or sell signals every ten minute and at the moment there are 50 followers.
1:21PM
- your strategy sends a 'buy' signal to our server, we forward the 'buy' call to all 50 followers.1:31PM
- your strategy sends a 'sell' signal to our server, we forward the 'sell' call to all 50 followers.1:35PM
- a new follower joins the copy trade.1:41PM
- your strategy sends a 'buy' signal to our server, we forward the 'buy' call to all 51 followers.1:45PM
- a follower stops following.1:51PM
- your strategy sends a 'sell' signal to our server, we forward the 'sell' call to all 50 followers.
Signals documentation
Sending signal to the webhook server is straightforward since there is only one endpoint. You can refer to the API Reference to see the schema we have for our API.
Here is an example in Python
from os import environ
import hmac
import hashlib
import time
import json
import requests
def generate_signature(api_key: str, api_secret: str, timestamp: int, payload: dict) -> str:
params_str = f"{api_key}{timestamp}{json.dumps(payload)}"
hash = hmac.new(
bytes(api_secret, "utf-8"),
params_str.encode("utf-8"),
hashlib.sha256,
).hexdigest()
return hash
API_KEY = environ["API_KEY"]
API_SECRET = environ["API_SECRET"]
WEBHOOK_URL = 'http://localhost:8000'
payload = {"id": "Market Buy", "side": "sell"}
timestamp = int(time.time() * 10 ** 3)
headers = {
"X-CYBOTRADE-TIMESTAMP": str(timestamp),
"X-CYBOTRADE-API-KEY": API_KEY,
"X-CYBOTRADE-SIGN": generate_signature(API_KEY, API_SECRET, timestamp, payload),
"Content-Type": "application/json"
}
resp = requests.post(url=f"{WEBHOOK_URL}/signal", headers=headers, data=json.dumps(payload))
print('-----------')
print('| Request |')
print('-----------')
print('Headers: ', resp.request.headers)
print('Body: ', resp.request.body)
print()
print('------------')
print('| Response |')
print('------------')
print('Headers: ', resp.headers)
print('Body: ', resp.content)
print('Status Code: ', resp.status_code)
print('Reason: ', resp.reason)