· 4 years ago · Jun 21, 2021, 07:04 PM
1<?php
2
3 //this is the liquid_trader.php file
4
5
6 require 'liquid_includes.php';
7
8 function sendNotification($messageText) {
9 mail($emailAddress, "Liquidbot Trade", $messageText, 'From: liquidbot@liquidtrades.com');
10 }
11
12
13 $msg = '';
14
15 //Make sure that it is a POST request.
16 if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
17
18 $msg .= 'Request method must be POST!';
19 sendNotification($msg);
20 //throw new Exception('Request method must be POST!');
21 }
22
23 //Make sure that the content type of the POST request has been set to application/json
24 $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
25
26 $msg .= $contentType;
27
28 if(strcasecmp($contentType, 'application/json; charset=utf-8') != 0){
29
30 $msg .= 'Content type must be: application/json; charset=utf-8';
31 sendNotification($msg);
32 //throw new Exception('Content type must be: application/json');
33 }
34
35 //Receive the RAW post data.
36 $content = trim(file_get_contents("php://input"));
37
38 $msg .= $content;
39
40 //Attempt to decode the incoming RAW post data from JSON.
41 $tradingViewAlertArray = json_decode($content, true);
42
43 //If json_decode failed, the JSON is invalid.
44 if(!is_array($tradingViewAlertArray)){
45
46 $msg .= 'Received content contained invalid JSON!';
47 sendNotification($msg);
48 //throw new Exception('Received content contained invalid JSON!');
49 }
50
51 //receive the alert from tradingview
52
53 $liquidBotAlert = $tradingViewAlertArray['LiquidBotAlert'];
54 $orderAction = $liquidBotAlert['orderAction'];
55
56 $buySellBool = false;
57
58 if($orderAction == 'buy'){
59
60 $buySellBool = true;
61
62 } else {
63
64 $buySellBool = false;
65
66 }
67
68 $msg .= $liquidBotAlert;
69
70 $urlPrice = 'https://api.binance.us/api/v3/avgPrice';
71 $urlAccount = 'https://api.binance.us/api/v3/account';
72 $urlOrder = 'https://api.binance.us/api/v3/order';
73
74 $headers = array(
75 "X-MBX-APIKEY: " . $apiKeY,
76 "Content-Type: application/json;charset=UTF-8"
77 );
78
79 //get current price
80
81 $ch = curl_init();
82 $data = array('symbol' => $symbol);
83 curl_setopt($ch, CURLOPT_URL,$urlPrice . '?' . http_build_query($data));
84 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
85 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
86 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
87 $server_output = curl_exec($ch);
88 curl_close ($ch);
89
90 $server_response = json_decode($server_output, true);
91 $currentPrice = $server_response['price'];
92
93 var_dump('currentPrice: ' .$currentPrice);
94
95 //get account balance
96
97 $ch = curl_init();
98 $data = array( 'recvWindow' => 30000,
99 'timestamp' => time()*1000);
100 $hash = hash_hmac('sha256', http_build_query($data), $secretKey); //create the hash so that the server can check for integrity
101 $data[signature] = $hash; //add the signature
102 curl_setopt($ch, CURLOPT_URL,$urlAccount . '?' . http_build_query($data));
103 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
104 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
105 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
106 $server_output = curl_exec($ch);
107 curl_close ($ch);
108
109 $server_response = json_decode($server_output, true);
110
111 $balances = $server_response['balances'];
112 $currencyBaseFreeBalance = '';
113 $currencyQuoteFreeBalance = '';
114
115 foreach ($balances as &$arrayAsset) {
116
117 if($arrayAsset['asset'] == $currencyBase){
118 $currencyBaseFreeBalance = $arrayAsset['free'];
119 }
120
121 if($arrayAsset['asset'] == $currencyQuote){
122 $currencyQuoteFreeBalance = $arrayAsset['free'];
123 }
124 }
125
126 var_dump('currencyBaseFreeBalance: ' . $currencyBaseFreeBalance);
127 var_dump('currencyQuoteFreeBalance: ' . $currencyQuoteFreeBalance);
128
129 //execute the trade
130
131 $tradeQuantity = '';
132 $buySellString = '';
133
134 if($buySellBool){
135
136 $buySellString = 'BUY';
137 $tradeQuantity = $currencyQuoteFreeBalance;
138
139 } else {
140
141 $buySellString = 'SELL';
142 $tradeQuantity = $currencyBaseFreeBalance;
143 }
144
145 //process whether it is buy or sell
146
147
148 $data = array( 'symbol' => $symbol,
149 'side' => $buySellString,
150 'type' => 'LIMIT',
151 'timeInForce' => 'GTC',
152 'quantity' => strval(round(floatval($tradeQuantity),3)),
153 'price' => strval(round(floatval($currentPrice),6)),
154 'recvWindow' => 30000,
155 'timestamp' => time()*1000); //'1499827319559');
156
157 // use key 'http' even if you send the request to https://...
158
159 $hash = hash_hmac('sha256', http_build_query($data), $secretKey); //create the hash so that the server can check for integrity
160 $data[signature] = $hash; //add the signature to the POST request body
161
162 var_dump($data);
163
164 $ch = curl_init();
165
166 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
167 curl_setopt($ch, CURLOPT_URL,$urlOrder);
168 curl_setopt($ch, CURLOPT_POST, 1);
169 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
170 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
171
172 $server_output = curl_exec($ch);
173
174 curl_close ($ch);
175
176 var_dump($server_output);
177
178 if ($server_output == "OK") {
179
180
181 } else {
182
183 }
184
185 //notify over sms
186 $msg .= $server_output;
187
188 sendNotification($msg);
189
190?>