forked from NicoleQiujuan/high-frequency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
high_frequency.py
executable file
·76 lines (64 loc) · 3.78 KB
/
high_frequency.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import time
from kumex.client import Trade, Market
class Hf(object):
def __init__(self):
# read configuration from json file
with open('config.json', 'r') as file:
config = json.load(file)
self.api_key = config['api_key']
self.api_secret = config['api_secret']
self.api_passphrase = config['api_passphrase']
self.sandbox = config['is_sandbox']
self.symbol_a = config['symbol_a']
self.symbol_b = config['symbol_b']
self.spread_mean = float(config['spread_mean'])
self.leverage = float(config['leverage'])
self.size = int(config['size'])
self.num_param = float(config['num_param'])
self.trade = Trade(self.api_key, self.api_secret, self.api_passphrase, is_sandbox=self.sandbox)
self.market = Market(self.api_key, self.api_secret, self.api_passphrase, is_sandbox=self.sandbox)
def get_symbol_price(self, symbol):
ticker = self.market.get_ticker(symbol)
return float(ticker['price'])
if __name__ == '__main__':
hf = Hf()
while 1:
# ticker of symbols
price_af = hf.get_symbol_price(hf.symbol_a)
price_bf = hf.get_symbol_price(hf.symbol_b)
# position of symbols
position_a = hf.trade.get_position_details(hf.symbol_a)
position_a_qty = int(position_a['currentQty'])
position_b = hf.trade.get_position_details(hf.symbol_b)
position_b_qty = int(position_b['currentQty'])
# interval of price
new_spread = price_af - price_bf
print('new_spread =', new_spread)
if position_a_qty == position_b_qty == 0 and new_spread < (hf.spread_mean - hf.num_param):
buy_order = hf.trade.create_limit_order(hf.symbol_a, 'buy', hf.leverage, hf.size, price_af + 1)
print('buy %s,order id =%s' % (hf.symbol_a, buy_order['orderId']))
sell_order = hf.trade.create_limit_order(hf.symbol_b, 'sell', hf.leverage, hf.size, price_bf - 1)
print('sell %s,order id =%s' % (hf.symbol_b, sell_order['orderId']))
elif position_a_qty == position_b_qty == 0 and new_spread > (hf.spread_mean + hf.num_param):
buy_order = hf.trade.create_limit_order(hf.symbol_a, 'sell', hf.leverage, hf.size, price_af - 1)
print('sell %s,order id =%s' % (hf.symbol_a, buy_order['orderId']))
sell_order = hf.trade.create_limit_order(hf.symbol_b, 'buy', hf.leverage, hf.size, price_bf + 1)
print('buy %s,order id =%s' % (hf.symbol_b, sell_order['orderId']))
elif position_a_qty > 0 and position_b_qty < 0 and new_spread > hf.spread_mean:
buy_order = hf.trade.create_limit_order(hf.symbol_a, 'sell', position_a['realLeverage'],
position_a_qty, price_af + 1)
print('sell %s,order id =%s' % (hf.symbol_a, buy_order['orderId']))
sell_order = hf.trade.create_limit_order(hf.symbol_b, 'buy', position_a['realLeverage'],
position_a_qty, price_bf - 1)
print('buy %s,order id =%s' % (hf.symbol_b, sell_order['orderId']))
elif position_a_qty < 0 and position_b_qty > 0 and new_spread < hf.spread_mean:
buy_order = hf.trade.create_limit_order(hf.symbol_a, 'buy', position_a['realLeverage'],
position_a_qty, price_af - 1)
print('buy %s,order id =%s' % (hf.symbol_a, buy_order['orderId']))
sell_order = hf.trade.create_limit_order(hf.symbol_b, 'sell', position_a['realLeverage'],
position_a_qty, price_bf + 1)
print('sell %s,order id =%s' % (hf.symbol_b, sell_order['orderId']))
time.sleep(60)