Skip to content

Commit 4611ed0

Browse files
add webhook
1 parent e35213b commit 4611ed0

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
11
# php-tradingview-binance-webhook-skeleton
2+
3+
A minimum viable webhook for tradingview alerts to trade on binance written in php.
4+
5+
## Requirements
6+
7+
1. Webhosting for php webhook (e.g. uberspace.de if you are located in germany)
8+
2. Binace API and secret key (get webhosting first because you need to add IP address of webserver to unlock trading for the API key)
9+
3. TradingView alerts (you can create a strategy, backtest it and create an alert which calls the php webhook :))
10+
11+
## TradingView Alert
12+
13+
Add the following message to your alert and set webhook url in the notifcations.
14+
15+
```
16+
{{ticker}},{{strategy.order.action}},{{strategy.order.price}}
17+
```
18+
19+
The {{strategy.order.price}} is optional but you can use it later to compare price of strategy with price of fulfilled order.

webhook.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
$allowedIps = [
4+
'52.89.214.238',
5+
'34.212.75.30',
6+
'54.218.53.128',
7+
'52.32.178.7'
8+
];
9+
10+
// only allow requests from tradingview servers
11+
if (!in_array($_SERVER['REMOTE_ADDR'], $allowedIps)) {
12+
die('not allowed');
13+
}
14+
15+
$input = file_get_contents('php://input');
16+
17+
$inputArray = explode(',', $input);
18+
$symbol = $inputArray[0];
19+
$side = $inputArray[1];
20+
$quantity = 0.001; // increase if you want but first test your strategy (risk management)
21+
22+
$data = [
23+
'symbol' => $symbol,
24+
'side' => $side,
25+
'type' => 'MARKET',
26+
'quantity' => $quantity,
27+
'recvWindow' => '5000',
28+
'timestamp' => floor(microtime(true) * 1000)
29+
];
30+
31+
$totalParams = \http_build_query($data);
32+
33+
// add your binance api and secret key with permissions to trade
34+
$apiKey = '';
35+
$secretKey = '';
36+
37+
$signature = hash_hmac('sha256', $totalParams, $secretKey);
38+
39+
$data['signature'] = $signature;
40+
41+
$ch = curl_init();
42+
curl_setopt($ch, CURLOPT_URL, 'https://api.binance.com/api/v3/order');
43+
curl_setopt($ch, CURLOPT_HTTPHEADER, [sprintf('X-MBX-APIKEY: %s', $apiKey)]);
44+
curl_setopt($ch, CURLOPT_POST, 1);
45+
curl_setopt($ch, CURLOPT_POSTFIELDS, \http_build_query($data));
46+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
47+
$result = curl_exec($ch);
48+
curl_close($ch);
49+
50+
$httpResponseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
51+
52+
// do some more stuff if you want

0 commit comments

Comments
 (0)