forked from acelan/cryptsy_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_bot.php
More file actions
178 lines (157 loc) · 5.14 KB
/
my_bot.php
File metadata and controls
178 lines (157 loc) · 5.14 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
178
<?php
require_once "cryptsy_lib.php";
require_once "cryptsy_bot.php";
require_once "algo/acelan.php";
include "config.php";
class my_bot extends cryptsy_bot
{
public function my_bot($config)
{
if(!isset($config['base_coin']) || !isset($config['target_coin']))
{
print("Should have base_coin and target_coin set up in config file.\n");
exit(0);
}
$this->config['base_coin'] = $config['base_coin'];
$this->config['target_coin'] = $config['target_coin'];
$this->config['profit_percent'] = isset($config['profit_percent']) ? $config['profit_percent'] : 1.02;
$this->config['stop_lost_percent'] = isset($config['stop_lost_percent']) ? $config['stop_lost_percent'] : 0.98;
$this->config['order_proportion'] = isset($config['order_proportion']) ? $config['order_proportion'] : 0.7;
$this->config['sell_proportion'] = isset($config['sell_proportion']) ? $config['sell_proportion'] : 0.7;
$this->config['round'] = isset($config['round']) ? $config['round'] : 0;
$this->config['min_target_coin'] = isset($config['min_target_coin']) ? $config['min_target_coin'] : 0;
$this->config['buy_count'] = 0;
$this->config['sell_count'] = 0;
$this->config['init_buy_sell_count'] = 0;
if (isset($config['init_buy_count'])) {
$this->config['buy_count'] = $config['init_buy_count'];
$this->config['init_buy_sell_count'] = 1;
}
if (isset($config['init_sell_count'])) {
$this->config['sell_count'] = $config['init_sell_count'];
$this->config['init_buy_sell_count'] = 1;
}
$this->config['log_filename'] = '';
if (isset($config['log_filename']))
$this->config['log_filename'] = $config['log_filename'];
$this->set_key($config['public_key'] , $config['private_key'], strtoupper($config['target_coin']."/".$config['base_coin']));
}
/*
* [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
* [datetime] => 2014-02-09 04:46:27
* [tradetype] => Buy
* [tradeprice] => 0.00000163
* [quantity] => 43123.00000000
* [total] => 0.07029049
* )
* )
*/
protected function init($data)
{
// initialize
if(function_exists("my_init"))
my_init($data,$this->config);
// read history data
// train your parmaters
$this->show_status($data);
}
/*
* main algorithm
*/
protected function tick($data)
{
$this->data = $data;
$action = my_algo($data,$this->config);
return $action;
}
protected function done($data)
{
if(sizeof($data["my_orders"]) != 0)
$this->show_status($data);
if(function_exists("my_done"))
my_done($data,$this->config);
}
protected function show_status($data)
{
$this->data = $data;
$my_base = $this->data["my_wallet"][strtoupper($this->config["base_coin"])];
$my_target = $this->data["my_wallet"][strtoupper($this->config["target_coin"])];
$cur_buy_price = $this->data["cur_buy_price"];
$cur_sell_price = $this->data["cur_sell_price"];
$my_orders = $this->data["my_orders"];
$myorder_base = 0;
$myorder_target = 0;
foreach($my_orders as $order)
{
if($order["ordertype"] == "Buy")
$myorder_base = $order["total"];
if($order["ordertype"] == "Sell")
$myorder_target = $order["quantity"];
}
if(sizeof($my_orders) == 0)
{
$dt = new DateTime();
$dt->setTimezone(new DateTimeZone('EST'));
$datetime = $dt->format('Y\-m\-d\ h:i:s');
}
else
$datetime = $my_orders[0]["created"];
$total_base = $my_base + $myorder_base + ($my_target+$myorder_target)*$cur_buy_price;
$output_str = "";
$output_str .= "=================================================================================\n";
$output_str .= $datetime." - total ".strtolower($this->config["base_coin"])."=".$total_base." , cur_price=".$cur_buy_price.", my_".strtolower($this->config["base_coin"])."=".$my_base." , my_".strtolower($this->config["target_coin"])."=".$my_target." , b_c=".$this->config['buy_count']." , s_c=".$this->config['sell_count']."\n";
foreach($my_orders as $order)
$output_str .= " my orders - ".$order["ordertype"]." ".$order["quantity"]." at price ".$order["price"]." , total ".strtolower($this->config["base_coin"])." ".$order["total"]."\n";
print($output_str);
if($this->config["log_filename"] != "")
{
$file = $this->config["log_filename"];
file_put_contents($file, $output_str, FILE_APPEND);
}
}
private $config;
private $data;
private $log_filename;
}
$my_bot = new my_bot($config);
$my_bot->run();
?>