-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgoldstar.php
353 lines (284 loc) · 12.8 KB
/
goldstar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* @author Ebo Eppenga
* @copyright 2022
*
* GoldStar Buy and Sell bot based on signals from for example TradeView
* or any other platform using PHP Binance API from JaggedSoft.
*
* goldstar.php?id=a1&action=SELL&pair=MATICUSDT&key=12345
*
* For more information please see:
* https://github.com/eppenga/goldstar-crypto-trading-bot
*
*/
// Set error reporting
error_reporting(E_ALL & ~E_NOTICE);
// Log query string
$log_qstring = "data/log_string.txt";
if (!file_exists($log_qstring)) {file_put_contents($log_qstring, "");}
file_put_contents("data/log_string.txt", date("Y-m-d H:i:s") . $_SERVER['REQUEST_URI'] . "\n", FILE_APPEND | LOCK_EX);
// Debug for Trailing test
$test_Trail = false;
// Configuration
if (!file_exists("config.php")) {echo "Error: Configuration file does not exist!"; exit();}
include "config.php";
// Dispay header
include "header.php";
// Check logfiles based on Bot ID
include "check_logfiles.php";
// Check variables
include "check_variables.php";
// Functions
include "functions.php";
// Query string parameters
include "querystring.php";
// Connect to Binance
require 'vendor/autoload.php';
$api = new Binance\API($binance_key, $binance_secret);
/*** START PROGRAM ***/
/* Get price of pair */
$price = (float)$api->price($pair);
/* Check if we have enough to pay fees and get important variables */
include "check_base.php";
/* Get all important variables */
$set_coin = minimumQuote();
/* Check if we enough BNB for discount */
include "check_bnb.php";
/** Report **/
echo "<b>GoldStar</b><br />";
echo "Date : " . date("Y-m-d H:i:s") . "<br />";
echo "Bot ID : " . $id . "<br />";
echo "Pair : " . $pair . "<br />";
echo "Spread : " . $spread . "%<br />";
echo "Markup : " . $markup . "%<br />";
echo "Multiplier : " . $multiplier . "x<br />";
echo "Compounding: " . $set_coin['compFactor'] . "x<br />";
if ($tv_advice) {echo "TradingView: (" . $tv_recomMin . "-" . $tv_recomMax . "), (" . implode(",", $tv_periods) . ")<br />";}
if ($tb_enabled) {echo "Trailing : " . $tb_distance . "%<br />";}
echo "Order value: " . $set_coin['minBUY'] * $price . " " . $set_coin['quoteAsset'] . "<br />";
echo "Available : " . $set_coin['balanceQuote'] . " " . $set_coin['quoteAsset'] . "<br />";
if (!empty($compounding)) {echo "Total value: " . ($compounding * $set_coin['compFactor']) . " " . $set_coin['quoteAsset'] . "<br />";}
echo "Command : " . $action; if ($limit) {echo " / LIMIT";} echo "<br /><br /><hr /><br />";
/*** BUY action ***/
if ($action == "BUY") {
// Buy cycle
if ($orderID == 0) {
echo "<i>Trying to buy " . $set_coin['minBUY'] . " " . $set_coin['baseAsset'] . " at " . $price ." " . $set_coin['quoteAsset'] . "...</i><br /><br /><hr /><br />";
// Check if there are sold LIMIT orders
if ($limit) {include("limit_filled.php");}
// Check if price is outside spread
$nobuy = false;
$price_min = $price * (1 - $spread / 100);
$price_max = $price * (1 + $spread / 100);
if (!$test_Trail) {
$handle = fopen($log_trades, "r");
while (($line = fgetcsv($handle)) !== false) {
$buy_price = $line[6] / $line[5];
if (($buy_price >= $price_min) && ($buy_price <= $price_max)) {
$nobuy = true;
echo "<i>Skipping buy because existing trade within " . round((($price / $buy_price) - 1) * 100, 2) . "%...</i><br /><br /><hr /><br />";
break;
}
}
fclose($handle);
}
// Check for TradingView advice
if (!$test_Trail) {
if (($tv_advice) && (!$nobuy)) {
echo "<i>TradingView says";
$tv_eval = evalTradingView($pair, $tv_periods, $tv_recomMin, $tv_recomMax);
if (!$tv_eval) {
$nobuy = true;
echo " skipping...</i><br /><br /><hr /><br />";
} else {
echo " buying...</i><br /><br /><hr /><br />";
}
}
}
// Check for trailing buy active
if (file_exists($log_trailing) && (!$nobuy)) {
$nobuy = true;
echo "<i>Skipping buy because trailing buy is active...</i><br /><br /><hr /><br />";
}
}
// Buy if spread = 0 or no adjacent order or to register to log files after trailing
if ((!$nobuy) || ($spread == 0 || $orderID <> 0)) {
// Minimum order
$quantity = $set_coin['minBUY'];
// Caclulate buy
$buy = $quantity * $price;
$commission = $buy * ($fee / 100);
// Set for reporting
$total_buy = $buy;
$total_quantity = $quantity;
// Buy Order
echo "<b>BUY Order</b><br /><br />";
// Check if we have enough quote balance to buy
$quantityQuote = $set_coin['balanceQuote'];
if ($orderID == 0) {
if ($quantityQuote < (2 * $buy)) {
$message = date("Y-m-d H:i:s") . "," . $id . ",Error: Insufficient " . $set_coin['quoteAsset'] . " to buy " . $set_coin['baseAsset'];
echo "<font color=\"red\"><b>" . $message . "</b></font><br /><br />";
logCommand($message, "error");
exit();
}
}
// BUY BUY BUY!
$tb_active = false;
if (!$tb_enabled) {
// Normal BUY
$order = $api->marketBuy($pair, $quantity);
logCommand($order, "binance");
} else {
// Trailing BUY, if an Order ID is supplied trailing buy ended and we just register
if (($orderID == 0) || ($test_Trail)) {
// Start trailing
$tb_active = true;
echo "<i>Initiating trailing buy...</i><br /><br /><hr /><br />";
// Create Trailing ID file
file_put_contents("data/log_trailing.csv", $id);
// Create Trailing settings file
$link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$link = parse_url($link, PHP_URL_SCHEME) . "://" . parse_url($link, PHP_URL_HOST) . dirname(parse_url($link, PHP_URL_PATH)) . "/";
$message = $id . "," . $pair . "," . $quantity . "," . $set_coin['tickSize'] . "," . $link;
file_put_contents($log_trailing, $message);
pclose(popen("start php trailing.php", "r"));
} else {
// Get order status after trailing BUY
$order = $api->orderStatus($pair, $orderID);
logCommand($order, "binance");
}
}
// Skip if going to trailing buy
if (!$tb_active) {
// Adjust and report Binance information
$orderstatus = extractBinance($order);
$unique_id = $orderstatus['order'];
$price = $orderstatus['price'];
$quantity = $orderstatus['base'];
$buy = $orderstatus['quote'];
$commission = $orderstatus['commission'];
$total_quantity = $quantity;
// Report
echo "<b>LIVE BUY Trade</b><br />";
echo "Quantity : " . $quantity . "<br />";
echo "BUY Price : " . $price . "<br />";
echo "BUY Value : " . $buy . "<br />";
echo "Commission : " . $commission . " (" . $fee . "%)<br /><br />";
echo "Symbol : " . $order['symbol'] . "<br />";
echo "Order ID : " . $order['orderId'] . "<br />";
echo "Time : " . $order['time'] . "<br />";
echo "Status : " . $order['status'] . "<br /><br />";
// Add a limit order
if ($limit) {include("limit_order.php");}
echo "<hr /><br />";
// Update log files for BUY order
if (!isset($unique_id)) {$unique_id = uniqid();}
$message = date("Y-m-d H:i:s") . "," . $id . "," . $unique_id . "," . $pair . ",BUY," . $quantity . "," . $buy;
logCommand($message, "buy");
$message = date("Y-m-d H:i:s") . "," . $id . "," . $unique_id . "," . $pair . ",BUY," . $quantity . "," . $buy . ",0," . (-1 * $commission) . "," . $tradetype;
logCommand($message, "history");
}
}
}
/*** SELL action ***/
if ($action == "SELL") {
echo "<i>Trying to SELL...</i><br /><br /><hr /><br />";
// Loop through all the BUY orders
$handle = fopen($log_trades, "r");
while (($line = fgetcsv($handle)) !== false) {
// Loop through the lines
// Found a BUY order
if ($line[4] == "BUY") {
// Count and calculate BUY and SELL
$counter = $counter + 1; // General counter
$quantity = $line[5]; // Total BUY quantity
$buy = $line[6]; // Total BUY value (quantity * price)
$buy_price = $buy / $quantity; // Buy price
$buy_fee = $buy * ($fee / 100); // Buy fee
$sell_fee = ($quantity * $price) * ($fee / 100); // Sell fee
$fees = $buy_fee + $sell_fee; // Total fees(BUY plus SELL fees)
$markups = ($quantity * $price) * ($markup / 100); // Total markup based on total BUY funds
$sell = ($quantity * $price); // Total SELL value
$sell_price = $sell / $quantity; // Sell price
$profit = $sell - $buy - $fees; // Profit
echo "<b>SELL Order ";
echo $counter . "</b><br /><br />";
echo "<i>BUY</i>" . "<br />";
echo "Quantity : " . $quantity . "<br />";
echo "BUY Price : " . $buy_price . "<br />";
echo "BUY Value : " . $buy . "<br />";
echo "Commission : " . $buy_fee . "<br /><br />";
// We can SELL with profit!!
if (($sell - $fees - $markups) >= $buy) {
// Do some calculations for now and later
$total_orders = $total_orders + 1;
$total_profit = $total_profit + $profit;
$total_quantity = $total_quantity + $quantity;
$total_sell = $total_sell + $sell;
$total_fees = $total_fees + $fees;
// SELL SELL SELL!
$order = $api->marketSell($pair, $quantity);
logCommand($order, "binance");
// Adjust
$orderstatus = extractBinance($order);
$price = $orderstatus['price'];
$quantity = $orderstatus['base'];
$sell = $orderstatus['quote'];
$commission = $orderstatus['commission'];
$markups = ($quantity * $price) * ($markup / 100);
$fees = $buy_fee + $commission;
$profit = $sell - $buy - $fees;
// Report
echo "<b>LIVE SELL Trade</b><br />";
echo "Quantity : " . $quantity . "<br />";
echo "SELL Price : " . $price . "<br />";
echo "SELL Value : " . $sell . "<br />";
echo "Markup : " . $markups . " (" . $markup . "%)<br />";
echo "Commission : " . $commission . " (" . $fee . "%)<br />";
echo "Profit : " . $profit . "<br /><br />";
echo "Symbol : " . $order['symbol'] . "<br />";
echo "Order ID : " . $order['orderId'] . "<br />";
echo "Time : " . $order['time'] . "<br />";
echo "Status : " . $order['status'] . "<br /><br />";
// Log to history
echo "<i>Profit, we can sell!</i><br /><br />";
$history .= date("Y-m-d H:i:s") . "," . $line[1] . "," . $line[2] . "," . $pair . ",SELL," . $quantity . "," . $sell . "," . $profit . "," . $commission . "\n";
} else {
// Log to trades
echo "<i>Insufficient profit, we can not sell!</i><br /><br />";
$trades .= $line[0] . "," . $line[1] . "," . $line[2] . "," . $line[3] . "," . $line[4] . "," . $line[5] . "," . $line[6] . "\n";
}
echo "<hr /><br />";
}
}
fclose($handle);
}
/** Report results and end program **/
echo "<b>Results</b><br />";
if ($total_orders > 0) {
// Report
echo "Total orders : " . $total_orders . "<br />";
echo "Total quantity: " . $total_quantity . "<br />";
echo "Total sell : " . $total_sell . "<br />";
echo "Total fees : " . $total_fees . "<br />";
echo "Average price : " . (($total_sell + $total_fees) / $total_quantity) . "<br />";
echo "Total profit : " . $total_profit . "<br /><br />";
// Create new trades log file
echo "<i>Creating " . $log_trades . " file...</i><br />";
file_put_contents($log_trades, $trades);
// Log to history file
echo "<i>Updating " . $log_history . " file...</i><br />";
logCommand($history, "history");
}
// Log to runtime file
echo "<i>Updating " . $log_runs . " file...</i><br />";
$message = date("Y-m-d H:i:s") . "," . $id . "," . $pair . "," . $action . "," . $total_quantity . "," . max($total_buy, $total_sell);
logCommand($message, "run");
// End program
echo "<i>Ending program...</i><br />";
echo "</pre>
</body>
</html>";
?>