forked from acelan/cryptsy_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptsy_bot.php
More file actions
177 lines (167 loc) · 4.44 KB
/
cryptsy_bot.php
File metadata and controls
177 lines (167 loc) · 4.44 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
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
<?php
require_once "cryptsy_lib.php";
abstract class cryptsy_bot
{
// public section
public function run()
{
if(!isset($this->key))
{
print("No public key and/or secret key are provided!\n");
exit(0);
}
if(!isset($this->tick_timeout))
$this->tick_timeout = 3;
$this->cryptsy = new Cryptsy( $this->key, $this->secret);
// get data
$data = $this->update_data();
$this->init($data);
$this->order_count = sizeof($data["my_orders"]);
while(1)
{
sleep($this->tick_timeout);
// prepare $data
$this->data = $this->update_data();
$action = $this->tick($this->data);
if( isset($action['cancel_order']))
$this->cancel_order($action['cancel_order']);
if( isset($action['cancel_market_orders']))
$this->cancel_market_orders();
if( isset($action['create_buy_order']))
{
foreach($action['create_buy_order'] as $order)
$this->create_buy_order($order['price'],$order['quantity']);
}
if( isset($action['create_sell_order']))
{
foreach($action['create_sell_order'] as $order)
$this->create_sell_order($order['price'],$order['quantity']);
}
if(sizeof($action) != 0)
{
$this->data = $this->update_data();
$this->done($this->data);
}
}
}
// protected section
protected abstract function init($data);
protected abstract function tick($data);
protected abstract function done($data);
protected function set_key($key,$secret,$label) {$this->key=$key; $this->secret=$secret; $this->label=$label;}
protected function set_label($label) { $this->label = $label; }
private function create_buy_order($price, $quantity)
{
$this->cryptsy->create_buy_order($this->label,$price,$quantity);
$this->order_count = $this->wait_order_succeed($this->order_count+1);
}
private function create_sell_order($price, $quantity)
{
$this->cryptsy->create_sell_order($this->label,$price,$quantity);
$this->order_count = $this->wait_order_succeed($this->order_count+1);
}
private function cancel_market_orders()
{
$this->cryptsy->cancel_market_orders($this->label);
$this->order_count = $this->wait_order_succeed(0);
}
private function cancel_order($order_id)
{
$this->cryptsy->cancel_order($order_id);
$this->order_count = $this->wait_order_succeed(sizeof($this->data['my_orders'])-1);
}
/*
* [cur_buy_price] => 0.00000209
* [cur_sell_price] => 0.00000210
* [my_wallet] => Array
* (
* [LTC] => 0.00000000
* [BTC] => 0.00000000
* [FTC] => 0.00000000
* [LEAF] => 0.00000000
* [MEOW] => 0.00000000
* [CASH] => 0.00000000
* [VTC] => 0.00000000
* )
* [my_orders] => Array
* (
* [0] => Array
* (
* [orderid] => 39960050
* [created] => 2014-02-09 04:46:27
* [ordertype] => Buy
* [price] => 0.00000163
* [quantity] => 43123.00000000
* [orig_quantity] => 43123.00000000
* [total] => 0.07029049
* )
* [1] => Array
* (
* [orderid] => 39960053
* [created] => 2014-02-09 04:46:28
* [ordertype] => Sell
* [price] => 0.00000169
* [quantity] => 12571.00000000
* [orig_quantity] => 12571.00000000
* [total] => 0.02124499
* )
* )
* [my_trade] => Array
* (
* [0] => Array
* (
* [orderid] => 39960050
* [created] => 2014-02-09 04:46:27
* [ordertype] => Buy
* [price] => 0.00000163
* [quantity] => 43123.00000000
* [orig_quantity] => 43123.00000000
* [total] => 0.07029049
* )
* )
*/
protected function update_data()
{
$cur_buy_price = $this->cryptsy->get_buy_price($this->label);
$cur_sell_price = $this->cryptsy->get_sell_price($this->label);
$my_orders = $this->cryptsy->get_orders($this->label);
$my_wallet = $this->cryptsy->get_wallet();
$my_trades = $this->cryptsy->get_mytrades($this->label);
$data = array(
'cur_buy_price' => $cur_buy_price,
'cur_sell_price' => $cur_sell_price,
'my_wallet' => $my_wallet,
'my_orders' => $my_orders,
'my_trade' => $my_trades,
);
return $data;
}
protected $tick_timeout;
// private section
private function wait_order_succeed($num_orders)
{
// won't leave the loop if we can't get correct number of orders
$counter = 0;
while( 1 )
{
$counter++;
$my_orders = $this->cryptsy->get_orders($this->label);
if( (sizeof($my_orders) == $num_orders))
break;
if($counter > 10) // re-try 10 times
{
// leave anyway
break;
}
sleep(3);
}
return sizeof($my_orders);
}
private $cryptsy;
private $key;
private $secret;
private $label;
private $data;
private $order_count;
}
?>