-
Notifications
You must be signed in to change notification settings - Fork 3
/
Bot.class.php
103 lines (85 loc) · 3.96 KB
/
Bot.class.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
<?php
require 'bi-api.php';
require 'BinanceBotOrders.class.php';
require 'BinanceBotHoldings.class.php';
require 'BinanceBotPrinter.class.php';
require 'BinanceBotPrices.class.php';
require 'ITransactionStrategy.php';
require 'TransactionStrategy.class.php';
require 'BasicTransactionStrategy.class.php';
require 'PeriodBasedTransactionStrategy.class.php';
require 'BinanceBotSettings.class.php';
require 'BinanceBotCandles.class.php';
require 'ISMSGateway.php';
require 'sms/EirSMS.class.php';
class Bot
{
private $apiKey = "";
private $priKey = "";
protected $api = null;
protected $BinanceBotPrices = null;
protected $BinanceBotOrders = null;
protected $BinanceBotHoldings = null;
protected $BinanceBotPrinter = null;
protected $BinanceBotCandles = null;
protected $BinanceBotSMSGateway = null;
protected $BinanceBotTransactionStrategy = null;
private $lastrunFile = "cache/lastrun.txt";
public function __construct()
{
@mkdir( BinanceBot\BinanceBotSettings::getInstance()->cachedir );
$this->apiKey = BinanceBot\BinanceBotSettings::getInstance()->apikey;
$this->priKey = BinanceBot\BinanceBotSettings::getInstance()->prikey;
$deleteCache = $this->doCachedRun();
$this->api = new Binance\API( $this->apiKey, $this->priKey );
$smsgateway = BinanceBot\BinanceBotSettings::getInstance()->smsGateway;
$class = 'BinanceBot\\'. $smsgateway;
$this->BinanceBotSMSGateway = new $class();
$this->BinanceBotPrices = new BinanceBot\BinanceBotPrices( array( $deleteCache, $this->api ) );
$this->BinanceBotOrders = new BinanceBot\BinanceBotOrders( array( $deleteCache, $this->api, $this->BinanceBotPrices, $this->BinanceBotSMSGateway ) );
$this->BinanceBotHoldings = new BinanceBot\BinanceBotHoldings( array( $deleteCache, $this->api, $this->BinanceBotPrices, $this->BinanceBotSMSGateway ) );
$this->BinanceBotCandles = new BinanceBot\BinanceBotCandles( array( false, $this->api, $this->BinanceBotPrices, $this->BinanceBotHoldings, $this->BinanceBotOrders ) );
$this->BinanceBotPrinter = new BinanceBot\BinanceBotPrinter( array( $this->api, $this->BinanceBotHoldings, $this->BinanceBotOrders, $this->BinanceBotPrices, $this->BinanceBotCandles ) );
$strategy = BinanceBot\BinanceBotSettings::getInstance()->strategy;
$class = 'BinanceBot\\'. $strategy;
$this->BinanceBotTransactionStrategy = new $class( array( $this->api, $this->BinanceBotHoldings, $this->BinanceBotOrders, $this->BinanceBotPrices, $this->BinanceBotCandles ) );
}
public function doCachedRun()
{
$lastrun = time();
if( file_exists( BinanceBot\BinanceBotSettings::getInstance()->lastRunFile ) )
{
$lastrun = file_get_contents( BinanceBot\BinanceBotSettings::getInstance()->lastRunFile );
}
file_put_contents( BinanceBot\BinanceBotSettings::getInstance()->lastRunFile, $lastrun );
if( $lastrun > time() - BinanceBot\BinanceBotSettings::getInstance()->cache_tmeout_seconds )
{
unlink( BinanceBot\BinanceBotSettings::getInstance()->lastRunFile );
return false;
}
return true;
}
public function run()
{
$i = 1;
$updating = false;
while( true )
{
BinanceBot\BinanceBotSettings::getInstance()->update();
$this->BinanceBotPrices->update();
$this->BinanceBotHoldings->update();
$this->BinanceBotOrders->update( $this->BinanceBotHoldings->getHoldingsChanged() );
$this->BinanceBotPrinter->update();
$this->BinanceBotTransactionStrategy->limitOrders( "BUY" );
$this->BinanceBotTransactionStrategy->limitOrders( "SELL" );
if( ( $i % 1800 == 0 || $updating == true ) && $this->BinanceBotCandles->getDueForUpdate( false ) == true )
{
$updating = true;
$this->BinanceBotCandles->update();
$updating = $this->BinanceBotCandles->getDueForUpdate( false );
}
$i++;
sleep( 1 );
}
}
}