· 8 years ago · Jan 27, 2018, 04:16 PM
1<?php
2//if (isset($_SERVER["REMOTE_ADDR"])) { die; };
3
4header('Content-Type: text/html; charset=utf-8');
5date_default_timezone_set('Asia/Jakarta');
6
7include_once(dirname(__FILE__).'/classes/db.class.php');
8include_once(dirname(__FILE__).'/classes/dbo.class.php');
9include_once(dirname(__FILE__).'/classes/Konfigurasi.php');
10include_once (dirname(__FILE__).'/classes/vip.class.php');
11
12/* Settings */
13$market = 'str_idr';
14
15 $api_key = ''; // your API-key
16 $secret_key = ''; // your Secret-key
17//$candle_pair = array('btc_idr','bch_idr','btg_idr','eth_idr','etc_idr','ltc_idr','str_idr','nxt_idr','waves_idr','xrp_idr','xzc_idr','ignis_idr');
18$candle_pair = array('str_idr');
19
20// Engine : MACD
21$macd_setting = array(12, 26, 9);
22$time_frame = 15; // in minutes, option: 1, 15, 30, 60, 120, 240, 1440
23
24$settings = array(
25 // PERHATIAN: Karena VIP belum support daftar aktif market, maka input manual di sini
26
27/*
28 'btc_idr' => array(
29 'trade' => array('step' => 1000, 'thickness' => 0, 'asset' => 1, 'min_sell' => 0.0001, 'min_buy' => 50000, 'limit' => 50000, 'taker_fee' => 0.3),
30 'ext_data' => array('market' => 'poloniex', 'pair' => 'USDT_BTC', 'qty_candles' => 120, 'timeframe' => 15),
31 'interval' => 600,
32 ),
33 'bch_idr' => array(
34 'trade' => array('step' => 1000, 'thickness' => 0, 'asset' => 1000000, 'min_sell' => 0.001, 'min_buy' => 50000, 'limit' => 500000, 'taker_fee' => 0.3),
35 'ext_data' => array('market' => 'poloniex', 'pair' => 'USDT_BCH', 'qty_candles' => 120, 'timeframe' => 15),
36 'interval' => 900,
37 ),
38 'btg_idr' => array(
39 'trade' => array('step' => 1000, 'thickness' => 0, 'asset' => 12, 'min_sell' => 0.01, 'min_buy' => 50000, 'limit' => 5000, 'taker_fee' => 0.3),
40 // 'ext_data' => array('market' => 'hitbtc', 'pair' => 'BTGUSD', 'qty_candles' => 120, 'timeframe' => 15),
41 'interval' => 900,
42 ),
43 'eth_idr' => array(
44 'trade' => array('step' => 1000, 'thickness' => 0, 'asset' => 1000000, 'min_sell' => 0.01, 'min_buy' => 50000, 'limit' => 500000, 'taker_fee' => 0.3),
45 'ext_data' => array('market' => 'poloniex', 'pair' => 'USDT_ETH', 'qty_candles' => 120, 'timeframe' => 15),
46 'interval' => 900,
47 ),
48 'etc_idr' => array(
49 'trade' => array('step' => 100, 'thickness' => 0, 'asset' => 1000000, 'min_sell' => 0.1, 'min_buy' => 50000, 'limit' => 500000, 'taker_fee' => 0.3),
50 'ext_data' => array('market' => 'poloniex', 'pair' => 'USDT_ETC', 'qty_candles' => 120, 'timeframe' => 15),
51 'interval' => 900,
52 ),
53 'ltc_idr' => array(
54 'trade' => array('step' => 1000, 'thickness' => 0, 'asset' => 1000000, 'min_sell' => 0.01, 'min_buy' => 50000, 'limit' => 500000, 'taker_fee' => 0.3),
55 'ext_data' => array('market' => 'poloniex', 'pair' => 'USDT_LTC', 'qty_candles' => 120, 'timeframe' => 15),
56 'interval' => 900,
57 ),
58 'ignis_idr' => array(
59 'trade' => array('step' => 1, 'thickness' => 0, 'asset' => 2000, 'min_sell' => 5, 'min_buy' => 50000, 'limit' => 50, 'taker_fee' => 0.3),
60 // 'ext_data' => array('market' => 'poloniex', 'pair' => 'USDT_NXT', 'qty_candles' => 120, 'timeframe' => 15),
61 'interval' => 900,
62 ),
63 'nxt_idr' => array(
64 'trade' => array('step' => 1, 'thickness' => 0, 'asset' => 2000, 'min_sell' => 5, 'min_buy' => 50000, 'limit' => 50, 'taker_fee' => 0.3),
65 'ext_data' => array('market' => 'poloniex', 'pair' => 'USDT_NXT', 'qty_candles' => 120, 'timeframe' => 15),
66 'interval' => 600,
67 ),
68 'waves_idr' => array(
69 'trade' => array('step' => 10, 'thickness' => 0, 'asset' => 12, 'min_sell' => 0.1, 'min_buy' => 50000, 'limit' => 5000, 'taker_fee' => 0.3),
70 // 'ext_data' => array('market' => 'hitbtc', 'pair' => 'WAVESBTC', 'qty_candles' => 120, 'timeframe' => 15),
71 'interval' => 900,
72 ),
73*/
74 'str_idr' => array(
75 'trade' => array('step' => 1, 'thickness' => 0, 'asset' => 2000, 'min_sell' => 20, 'min_buy' => 50000, 'limit' => 50, 'taker_fee' => 0.3),
76 'ext_data' => array('market' => 'poloniex', 'pair' => 'USDT_STR', 'qty_candles' => 120, 'timeframe' => 15),
77 'interval' => 600,
78 ),
79/*
80 'xrp_idr' => array(
81 'trade' => array('step' => 1, 'thickness' => 0, 'asset' => 2000, 'min_sell' => 10, 'min_buy' => 50000, 'limit' => 50, 'taker_fee' => 0.3),
82 'ext_data' => array('market' => 'poloniex', 'pair' => 'USDT_XRP', 'qty_candles' => 120, 'timeframe' => 15),
83 'interval' => 900,
84 ),
85 'xzc_idr' => array(
86 'trade' => array('step' => 100, 'thickness' => 0, 'asset' => 12, 'min_sell' => 0.1, 'min_buy' => 50000, 'limit' => 5000, 'taker_fee' => 0.3),
87 // 'ext_data' => array('market' => 'binance', 'pair' => 'XZCBTC', 'qty_candles' => 120, 'timeframe' => 15),
88 'interval' => 900,
89 ),
90 'xrp_btc' => array(
91 'trade' => array('step' => 0.00000001, 'thickness' => 0, 'asset' => 0.02, 'min_sell' => 0.0001, 'min_buy' => 0.0001, 'limit' => 0.01, 'taker_fee' => 0),
92 'ext_data' => array('market' => 'poloniex', 'pair' => 'btc_xrp', 'qty_candles' => 120, 'timeframe' => 15),
93 ),
94 'drk_btc' => array(
95 'trade' => array('step' => 0.000001, 'thickness' => 0, 'asset' => 0.02, 'min_sell' => 0.0001, 'min_buy' => 0.0001, 'limit' => 0.01, 'taker_fee' => 0),
96 'ext_data' => array('market' => 'poloniex', 'pair' => 'btc_dash', 'qty_candles' => 120, 'timeframe' => 15),
97 ),
98 'eth_btc' => array(
99 'trade' => array('step' => 0.00001, 'thickness' => 0, 'asset' => 0.02, 'min_sell' => 0.0001, 'min_buy' => 0.0001, 'limit' => 0.01, 'taker_fee' => 0),
100 'ext_data' => array('market' => 'poloniex', 'pair' => 'btc_eth', 'qty_candles' => 120, 'timeframe' => 15),
101 ),
102 'ltc_btc' => array(
103 'trade' => array('step' => 0.000001, 'thickness' => 0, 'asset' => 0.02, 'min_sell' => 0.0001, 'min_buy' => 0.0001, 'limit' => 0.01, 'taker_fee' => 0),
104 'ext_data' => array('market' => 'poloniex', 'pair' => 'btc_ltc', 'qty_candles' => 120, 'timeframe' => 15),
105 ),
106 'str_btc' => array(
107 'trade' => array('step' => 0.00000001, 'thickness' => 0, 'asset' => 0.02, 'min_sell' => 0.0001, 'min_buy' => 0.0001, 'limit' => 0.01, 'taker_fee' => 0),
108 'ext_data' => array('market' => 'poloniex', 'pair' => 'btc_str', 'qty_candles' => 120, 'timeframe' => 15),
109 ),
110 'nxt_btc' => array(
111 'trade' => array('step' => 0.00000001, 'thickness' => 0, 'asset' => 0.02, 'min_sell' => 0.0001, 'min_buy' => 0.0001, 'limit' => 0.01, 'taker_fee' => 0),
112 'ext_data' => array('market' => 'poloniex', 'pair' => 'btc_nxt', 'qty_candles' => 120, 'timeframe' => 15),
113 ),
114 'nem_btc' => array(
115 'trade' => array('step' => 0.00000001, 'thickness' => 0, 'asset' => 0.02, 'min_sell' => 0.0001, 'min_buy' => 0.0001, 'limit' => 0.01, 'taker_fee' => 0),
116 'ext_data' => array('market' => 'poloniex', 'pair' => 'btc_xem', 'qty_candles' => 120, 'timeframe' => 15),
117 ),
118 'bts_btc' => array(
119 'trade' => array('step' => 0.00000001, 'thickness' => 0, 'asset' => 0.02, 'min_sell' => 0.0001, 'min_buy' => 0.0001, 'limit' => 0.01, 'taker_fee' => 0),
120 'ext_data' => array('market' => 'poloniex', 'pair' => 'btc_bts', 'qty_candles' => 120, 'timeframe' => 15),
121 ),
122 'doge_btc' => array(
123 'trade' => array('step' => 0.00000001, 'thickness' => 0, 'asset' => 0.02, 'min_sell' => 0.0001, 'min_buy' => 0.0001, 'limit' => 0.01, 'taker_fee' => 0),
124 // 'ext_data' => array('market' => 'poloniex', 'pair' => 'btc_doge', 'qty_candles' => 120, 'timeframe' => 15),
125 ),
126*/
127 );
128
129$vip = new Vip($market, $api_key, $secret_key);
130sort($candle_pair);
131$num_format = array('idr' => array('decimal' => 0, 'dec_point' => '.', 'thousand' => ','),'btc' => array('decimal' => 8, 'dec_point' => '.', 'thousand' => ','));
132
133
134// Candle Manager
135$candle_timeframe = array(1 => array(5,15),15 => array(30, 60, 120), 60 => array(240, 1440));
136echo "\n Candle Manager : ";
137
138
139foreach($candle_pair as $pair)
140{
141 $vip->pair = $pair;
142 $candles = $vip->getVipCandle(5, 1, 120);
143 array_pop($candles);
144 $last_candle = end($candles);
145 $time = time(); $interval = isset($settings[$vip->pair]['interval']) ? $settings[$vip->pair]['interval'] : 900 ; // interval in seconds
146// $time = $time - ($time % $interval);
147
148 echo "\n ".$vip->getMarketName($vip->pair)." ";
149 $int = abs($time - $last_candle['time']);
150// echo $int."\n".$last_candle['time']."\n";
151 if($int > $interval)
152 {
153 $trades = $vip->getTrade();
154 if(is_array($trades))
155 {
156 if($newCandle = $vip->vipCandleManager($trades))
157 {
158 $first = reset($trades);
159 $last = end($trades);
160 $sec = $first['date'] - $last['date'];
161 $sec_remain = $sec % 60;
162 $min = ($sec - $sec_remain)/60;
163 echo (count($newCandle) - 1) ." candlestick ditambahkan (TF 1 minute). ";
164 echo " - ".$min.":".$sec_remain;
165 // Candle dibuat dengan TF 1 menit, lalu TF lain diturunkan dari TF ini
166 foreach($candle_timeframe as $basictime => $time)
167 {
168 foreach($time as $timeframe)
169 {
170 $candles = $vip->getVipCandle($timeframe, $basictime);
171
172 }
173 }
174 }
175 }
176 else
177 {
178 echo "Error Occured";
179 }
180 }
181 else echo "last candle is taken on ".$last_candle['date'];
182}
183
184
185
186
187// Technical Analyse: We use MA cross
188//echo "\n\n Technical Analyse : Use MA Cross\n Short MA : ".$ma_short."\n Long MA : ".$ma_long."\n Time frame : ".$time_frame."\n\n Market Condition : ";
189
190$ta_setting = array('macd' => $macd_setting);
191$signal_buy = $signal_sell = array();
192foreach($candle_pair as $pair)
193{
194 $vip->pair = $pair;
195 echo "\n ".$vip->getMarketName($vip->pair)." ";
196 $candles = $vip->readVipCandle($time_frame);
197 $analyse = $vip->technicalAnalyse($candles, $time_frame, $ta_setting);
198 $last = end($analyse);
199 $prev = prev($analyse);
200 if(isset($ta_setting['macd']) && isset($last['macd']))
201 {
202 /*
203 MACD
204 macd
205 signal
206 histogram
207
208
209 */
210 if($last['macd']['line'] > 0)
211 {
212 $signal_buy[$vip->pair] = 1;
213 echo " UP ";
214
215 }
216 else
217 {
218 $signal_sell[$vip->pair] = 1;
219 echo "DOWN ";
220 }
221
222 }
223 else echo "ERROR";
224}
225
226// Trading Bot
227$vip->pair = $market;
228$pairs = explode('_', $vip->pair);
229$allpairs = array();
230$error = $balance = $reduce_delay = false;
231echo "\n\n Bot Trading Pair : ".$vip->getMarketName($vip->pair);
232echo "\n Get Open Order (ALL) : ";
233if($open_orders = $vip->openOrder($allpairs))
234{
235 echo "Done\n";
236 if(isset($open_orders['orders'][$vip->pair]))
237 {
238 $k = $open_orders['orders'][$vip->pair];
239 if(isset($k['sell']))
240 foreach($k['sell'] as $order)
241 {
242 $ticker = $vip->public_query('ticker');
243 if($ticker['sell'] == $order['price'])
244 {
245 echo " ".$vip->pair." Order ".$order['type']." #".$order['order_id']." diteruskan \n";
246 }
247 else
248 {
249 if($balance = $vip->cancelOrder($order))
250 echo " ".$vip->pair." Order ".$order['type']." #".$order['order_id']." dibatalkan \n";
251 }
252 }
253 if(isset($k['buy']))
254 foreach($k['buy'] as $order)
255 {
256 $ticker = $vip->public_query('ticker');
257 if($ticker['buy'] == $order['price'])
258 {
259 echo " ".$vip->pair." Order ".$order['type']." #".$order['order_id']." diteruskan \n";
260 }
261 else
262 {
263 if($balance = $vip->cancelOrder($order))
264 echo " ".$vip->pair." Order ".$order['type']." #".$order['order_id']." dibatalkan \n";
265 }
266 }
267 }
268// print_r($open_orders);
269
270}
271else
272{ echo "Error\n";$error = true; }
273
274if(!$balance)
275{
276 echo "\n Get Balance : ";
277 if($balance = $vip->getBalance())
278 echo "Done";
279 else
280 { echo "Error";$error = true; }
281}
282else echo "\n Balance : ";
283
284if(!$error)
285{
286 foreach($balance as $currency => $value)
287 {
288 $curr = $currency == 'idr' ? 'idr' : 'btc';
289 if($value > 0)
290 echo "\n ".strtoupper($currency)." : ".number_format($value, $num_format[$curr]['decimal'], '.', ',');
291 }
292 echo "\n";
293}
294else
295{ echo "Error\n";$error = true; }
296
297
298$rec_price = $vip->recomendedPrice(false);
299if(isset($signal_buy[$vip->pair]) && $signal_buy[$vip->pair] && $balance[$pairs[1]] > $settings[$vip->pair]['trade']['min_buy'])
300{
301 $params = array('type' => 'buy', $pairs[1] => $balance[$pairs[1]], 'price' => $rec_price['buy']);
302 if($neworder = $vip->addOrder($params))
303 {
304 echo "\n Order #".$neworder['order_id']." ".$params['price']." ditambahkan";
305 $reduce_delay = true;
306 }
307}
308else if(isset($signal_sell[$vip->pair]) && $signal_sell[$vip->pair] && $balance[$pairs[0]] > $settings[$vip->pair]['trade']['min_sell'])
309{
310 $params = array('type' => 'sell', $pairs[0] => $balance[$pairs[0]], 'price' => $rec_price['sell']);
311 if($neworder = $vip->addOrder($params))
312 {
313 echo "\n Order #".$neworder['order_id']." ".$params['price']." ditambahkan";
314 $reduce_delay = true;
315 }
316}
317
318echo "\n";
319if(!$reduce_delay)
320{
321 echo "\n";
322 echo "\n +----------------------------------------------------------------+";
323 echo "\n | LAST UPDATE : ".date("Y-m-d H:i:s")." |";
324 echo "\n +----------------------------------------------------------------+\n";
325
326sleep(300);
327}
328sleep(3);
329/*
330
331*/