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
144 lines (137 loc) · 3.45 KB
/
cryptsy_bot.php
File metadata and controls
144 lines (137 loc) · 3.45 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
<?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);
}
$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->tick($this->update_data());
}
}
// protected section
protected abstract function init($data);
protected abstract function tick($data);
protected function set_key($key,$secret,$label) {$this->key=$key; $this->secret=$secret; $this->label=$label;}
protected 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);
}
protected 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);
}
protected function cancel_market_orders()
{
$this->cryptsy->cancel_market_orders($this->label);
$this->order_count = $this->wait_order_succeed(0);
}
/*
* [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 $order_count;
}
?>