Repository navigation
Expand file tree
/
Copy pathBasicTransactionStrategy.class.php
More file actions
120 lines (107 loc) · 4.55 KB
/
Copy pathBasicTransactionStrategy.class.php
File metadata and controls
120 lines (107 loc) · 4.55 KB
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
<?php
namespace BinanceBot;
class BasicTransactionStrategy extends TransactionStrategy
{
/**
* @return mixed
*/
#[\Override]
public function placeBuyOrder()
{
$pennyStocks = array();
$sorted = $this->BinanceBotPrices->getAllPrices();
asort($sorted);
foreach ($sorted as $symbol => $price) {
if (
substr($symbol, strlen(BinanceBotSettings::getInstance()->base_currency) * -1)
!= BinanceBotSettings::getInstance()->base_currency
) {
continue;
}
if (count($this->BinanceBotOrders->getAllOpenBuyOrdersBySymbol($symbol)) > 0) {
continue;
}
if ($price < BinanceBotSettings::getInstance()->max_unit_price) {
$change = $this->api->prevDay($symbol)['priceChangePercent'];
$trigger = BinanceBotSettings::getInstance()->downward_trigger_percent;
if ($change < $trigger) {
$openOrders = $this->BinanceBotOrders->getAllOpenSellOrdersBySymbol($symbol
. BinanceBotSettings::getInstance()->base_currency);
if (count($openOrders) > 0) {
continue;
}
$retracements = $this->BinanceBotCandles->getBestStocks();
$retracementskeys = array_keys($this->BinanceBotCandles->getBestStocks());
$delim = count($retracements) > BinanceBotSettings::getInstance()->max_open_buy_orders * 2
? BinanceBotSettings::getInstance()->max_open_buy_orders * 2 : count($retracements);
$found = false;
for ($t = 0; $t < $delim; $t++) {
if ($retracementskeys[$t] == $symbol) {
$found = true;
break;
}
}
if ($found == true) {
$pennyStocks[$symbol] = $price;
}
}
}
}
// no stocks meet criteria
if (count($pennyStocks) == 0) {
$this->BinanceBotPrices->forceupdate();
return 0;
}
$buysymbol = array_rand($pennyStocks);
$asset = substr($buysymbol, 0, strlen(BinanceBotSettings::getInstance()->base_currency) * -1);
// make sure we dont own this stock already
if ($this->BinanceBotHoldings->getHoldings($asset) != false) {
$held = $this->BinanceBotHoldings->getHoldings($asset);
$ignoreBelow = BinanceBotSettings::getInstance()->ignore_less_than_amount_dollars;
if ($held > $ignoreBelow) {
// already own some of this stock, no need to buy more
print "Already own " . $pennyStocks[$buysymbol] . "\n";
return;
}
}
$buyAmount = BinanceBotSettings::getInstance()->buy_total_amount;
$buyAt = BinanceBotSettings::getInstance()->buy_at_percent;
$quantity = round($buyAmount / ($pennyStocks[$buysymbol] * $buyAt));
$this->BinanceBotOrders->placeBuyOrder($buysymbol, sprintf(
"%f",
$pennyStocks[$buysymbol] * BinanceBotSettings::getInstance()->buy_at_percent
), $quantity);
printf(
"Buy %s of %s @ %f\n",
$quantity,
$buysymbol,
($pennyStocks[$buysymbol] * BinanceBotSettings::getInstance()->buy_at_percent)
);
}
/**
* @return int
*/
#[\Override]
public function placeSellOrder()
{
return $this->sellAvailableHoldings(
function ($symbol, $data, $orderdetails) {
$sellPrice = $orderdetails['price'] * BinanceBotSettings::getInstance()->sell_at_percent;
print $symbol . BinanceBotSettings::getInstance()->base_currency . " "
. strval(floor($data['available']))
. " * " . sprintf("%f", $sellPrice) . "\n";
printf(
$symbol . BinanceBotSettings::getInstance()->base_currency . " %f \n",
(floor($data['available']) * $sellPrice)
);
$this->BinanceBotOrders->placeSellOrder(
$symbol . BinanceBotSettings::getInstance()->base_currency,
sprintf("%f", $sellPrice),
floor($data['available'])
);
printf("Sell %s @ %f\n", $symbol, $sellPrice);
return true;
}
);
}
}