-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathocbtcarb.py
176 lines (148 loc) · 8.12 KB
/
ocbtcarb.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
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
import configparser
from allcoin.market import AllCoinMarket
from allcoin.position import AllCoinPosition
from allcoin.settlement import AllCoinSettlement
from allcoin.trade import AllCoinTrade
from bitz.market import BitZMarket
from bitz.position import BitZPosition
from bitz.settlement import BitZSettlement
from bitz.trade import BitZTrade
from common.settlementfactory import SettlementFactory
from constants import Constants
def calculate_pnl_withdraw(buy_price, sell_price, amount, buy_fee, sell_fee, buy_withdraw_fee, sell_withdraw_fee):
return sell_price * amount - buy_price * amount - buy_fee - sell_fee - sell_withdraw_fee - buy_withdraw_fee
def calculate_pnl(buy_price, sell_price, amount, buy_fee, sell_fee):
return sell_price * amount - buy_price * amount - buy_fee - sell_fee
if __name__ == '__main__':
config = configparser.ConfigParser()
config.read(['config.ini', 'secret_config.ini'])
all_coin_market_url = config['allcoin']['market_url']
all_coin_market = AllCoinMarket(all_coin_market_url)
all_coin_trade_url = config['allcoin']['trade_url']
all_coin_api_key = config['allcoin']['api_key']
all_coin_secret_key = config['allcoin']['secret_key']
all_coin_trade = AllCoinTrade(all_coin_trade_url, all_coin_api_key, all_coin_secret_key)
all_coin_settlement = AllCoinSettlement()
bit_z_market_url = config['bitz']['market_url']
bit_z_market = BitZMarket(bit_z_market_url)
bit_z_trade_url = config['bitz']['trade_url']
bit_z_api_key = config['bitz']['api_key']
bit_z_secret_key = config['bitz']['secret_key']
bit_z_trade_pwd = config['bitz']['trade_pwd']
bit_z_trade = BitZTrade(bit_z_trade_url, bit_z_api_key, bit_z_secret_key, bit_z_trade_pwd)
bit_z_settlement = BitZSettlement()
# all coin position manager
all_coin_position = AllCoinPosition(
init_positions={
'oc': 0.0,
'btc': 0.08604735
},
position_redis_key=Constants.REDIS_KEY_ALL_COIN_POSITIONS,
open_order_redis_key_prefix=Constants.REDIS_KEY_ALL_COIN_OPEN_ORDER_IDS_PREFIX,
cancelled_order_redis_key_prefix=Constants.REDIS_KEY_ALL_COIN_CANCELLED_ORDER_IDS_PREFIX,
closed_order_redis_key_prefix=Constants.REDIS_KEY_ALL_COIN_CLOSED_ORDER_IDS_PREFIX,
order_redis_key_prefix=Constants.REDIS_KEY_ALL_COIN_ORDER_PREFIX,
trade_pair_redis_key=Constants.REDIS_KEY_ALL_COIN_TRADE_PAIRS,
url=all_coin_trade_url,
api_key=all_coin_api_key,
secret_key=all_coin_secret_key
)
# bit z position manager
bit_z_position = BitZPosition(
init_positions={
'oc': 45454.49000000,
'btc': 0.0
},
position_redis_key=Constants.REDIS_KEY_BIT_Z_POSITIONS,
open_order_redis_key_prefix=Constants.REDIS_KEY_BIT_Z_OPEN_ORDER_IDS_PREFIX,
cancelled_order_redis_key_prefix=Constants.REDIS_KEY_BIT_Z_CANCELLED_ORDER_IDS_PREFIX,
closed_order_redis_key_prefix=Constants.REDIS_KEY_BIT_Z_CLOSED_ORDER_IDS_PREFIX,
order_redis_key_prefix=Constants.REDIS_KEY_BIT_Z_ORDER_PREFIX,
trade_pair_redis_key=Constants.REDIS_KEY_BIT_Z_TRADE_PAIRS,
url=bit_z_trade_url,
api_key=bit_z_api_key,
secret_key=bit_z_secret_key
)
# settlement set up
SettlementFactory.settlements = {
Constants.EXCHANGE_NAME_ALL_COIN: all_coin_settlement,
Constants.EXCHANGE_NAME_BIT_Z: bit_z_settlement
}
SettlementFactory.default_settlement = bit_z_settlement
symbol = 'oc_btc'
min_amount = all_coin_settlement.get_min_buy_quantity(symbol)
if min_amount <= bit_z_settlement.get_min_sell_quantity(symbol):
min_amount = bit_z_settlement.get_min_sell_quantity(symbol)
while True:
try:
bit_z_oc_position = bit_z_position.get_position('oc')
if bit_z_oc_position < min_amount:
break
all_coin_asks = all_coin_market.oc_btc_asks()
all_coin_ask_1_price_float = all_coin_asks[0][0]
all_coin_ask_1_volume_float = all_coin_asks[0][1]
bit_z_bids = bit_z_market.oc_btc_bids()
bit_z_bid_1_price_str = bit_z_bids[0][0]
bit_z_bid_1_volume_str = bit_z_bids[0][1]
bit_z_bid_1_price_float = float(bit_z_bid_1_price_str)
bit_z_bid_1_volume_float = float(bit_z_bid_1_volume_str)
sell_price_str = bit_z_bid_1_price_str
sell_price_float = float(sell_price_str)
buy_price_float = all_coin_ask_1_price_float
buy_price_str = '%.8f' % buy_price_float
all_coin_corresponding_oc_position = all_coin_position.get_position('btc') / buy_price_float
if all_coin_corresponding_oc_position < min_amount:
break
amount_float = bit_z_bid_1_volume_float
if amount_float > all_coin_ask_1_volume_float:
amount_float = all_coin_ask_1_volume_float
if amount_float > 10000.00:
amount_float = 10000.00
if amount_float > bit_z_oc_position:
amount_float = bit_z_oc_position
if amount_float > all_coin_corresponding_oc_position:
amount_float = all_coin_corresponding_oc_position
if amount_float < min_amount:
amount_float = min_amount
amount_float = float(int(amount_float))
amount_str = '%.2f' % amount_float
if all_coin_settlement.is_valid_buy_order(symbol, buy_price_float, amount_float) and \
bit_z_settlement.is_valid_sell_order(symbol, sell_price_float, amount_float):
buy_fee = all_coin_settlement.calculate_buy_fee(symbol, buy_price_float, amount_float)
sell_fee = bit_z_settlement.calculate_sell_fee(symbol, sell_price_float, amount_float)
# buy_withdraw_fee = all_coin_settlement.calculate_withdraw_fee('oc', amount_float) * buy_price_float
# sell_withdraw_fee = bit_z_settlement.calculate_withdraw_fee('btc', amount_float * sell_price_float)
# pnl = calculate_pnl(buy_price_float, sell_price_float, amount_float, buy_fee, sell_fee, buy_withdraw_fee, sell_withdraw_fee)
pnl = calculate_pnl(buy_price_float, sell_price_float, amount_float, buy_fee, sell_fee)
# if pnl > 0 and sell_price_float / buy_price_float > 1.10:
if pnl > -100 and sell_price_float / buy_price_float > 0.8:
all_coin_trade.buy(symbol, '0.00000030', amount_str)
bit_z_trade.sell(symbol, '0.008', amount_str)
print('buy price %s buy amount %s' % (buy_price_str, amount_str))
print('sell price %s sell amount %s' % (sell_price_str, amount_str))
# print('sell price, buy price, amount, sell fee, buy fee, pnl')
# print(
# '%(sell_price)s, %(buy_price)s, %(amount)s, %(sell_fee).8f, %(buy_fee).8f, '
# '%(pnl).8f' % {
# 'sell_price': sell_price_str,
# 'buy_price': buy_price_str,
# 'amount': amount_str,
# 'sell_fee': sell_fee,
# 'buy_fee': buy_fee,
# 'pnl': pnl
# })
# print('sell price, buy price, amount, sell fee, buy fee, sell withdraw fee, buy withdraw fee, pnl')
# print(
# '%(sell_price)s, %(buy_price).8f, %(amount)s, %(sell_fee).8f, %(buy_fee).8f, '
# '%(sell_withdraw_fee).8f, %(buy_withdraw_fee).8f, %(pnl).8f' % {
# 'sell_price': sell_price_str,
# 'buy_price': buy_price_float,
# 'amount': amount_str,
# 'sell_fee': sell_fee,
# 'buy_fee': buy_fee,
# 'sell_withdraw_fee': sell_withdraw_fee,
# 'buy_withdraw_fee': buy_withdraw_fee,
# 'pnl': pnl
# })
except Exception as e:
print(e)