-
Notifications
You must be signed in to change notification settings - Fork 3
/
MovingAverage.py
293 lines (272 loc) · 8.32 KB
/
MovingAverage.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'''market maker program'''
from transact_cb import *
from stored import *
import numpy as np
import time, json, requests, csv, datetime
import pandas as pd
'''
Get ma quantity to replace 0 as base quantity
Get orig_price of asset to calculate profit and loss based on total value and totalvalue vs market value (if mkt loses)
'''
class MarketMaker:
def make_mark(self,product,trade_quant,spread_thresh,order_thresh):
profit = 0
open_bids = 0
open_asks = 0
#spread_thresh is min spread, order thresh is about 2 times spread if tight
num_orders = 0
p1 = product.split('/')[0]
p2 = product.split('/')[1]
prod_string = p1+'-'+p2
ma_quantity = 0
ma_level = 30 #enter seconds for malevel
x = cb_balance()
for i in x:
if i[0]=='USD':
starting_usd_balance =float(i[1])
if i[0] =='BTC':
starting_btc_balance = float(i[1])
quant_change = 0
breaker = 0
while True:
if breaker==1:
break
print (cancel_all())
zzz=0
heartbeat=1
USD, bid, ask,b_depth,a_depth,adj_imb = gdax(prod_string)
if USD ==0:
break
start_price = float(USD)
titles = ('time','low','high','open','close','volume')
init_time = 0
hist_data = []
print ('wait' ,ma_level,'...')
while init_time <= ma_level:
USD = float(USD)
hist_data.append(USD)
time.sleep(1)
init_time = init_time + 1
USD, bid, ask,b_depth,a_depth,adj_imb = gdax(prod_string)
if USD == 0:
break
last_values = hist_data[-ma_level:]
ma_price = np.sum(last_values)/ma_level
while True and zzz==0:
heartbeat=heartbeat+1
if heartbeat ==10:
print ('.',spread,ma_quantity)
heartbeat=1
time.sleep(1)
USD, bid, ask,b_depth,a_depth,adj_imb = gdax(prod_string)
if USD == 0:
break
bid = float(bid)
ask = float(ask)
USD = float(USD)
spread = max(spread_thresh,(ask-bid-spread_thresh*5))
hist_data.append(USD)
last_values = hist_data[-ma_level:]
ma_price = np.sum(last_values)/ma_level
'''Currently ma_quantity is just one of the trade_quant, if volume and trades are high enough, this can increase'''
if USD - ma_price > 0:
ma_quantity = -trade_quant*2
elif USD- ma_price < 0:
ma_quantity = trade_quant*2
else:
ma_quantity = 0
change = 0
x = cb_balance()
if x == 'ERROR':
break
for i in cb_balance():
if i[0]=='BTC':
quant_change = float(i[1]) - starting_btc_balance
if ask - bid > 0:
open_bids = 0
open_asks = 0
x = cb_open()
if x =='ERROR':
break
for i in x:
if i['side'] == 'buy':
open_bids += 1
bid_price = float(i['price'])
bid_id = i['id']
else:
open_asks += 1
ask_price = float(i['price'])
ask_id = i['id']
open_orders = open_asks + open_bids
if quant_change <=ma_quantity:
if open_bids == 0:
open_bid_price = round(float(ask),2)
print ('BID PLACED',cb_trade_agg('buy',trade_quant,open_bid_price,prod_string))
num_orders += 1
if num_orders > 1:
num_trades += 1
change = 1
elif abs(bid-bid_price) > order_thresh:
print ('Modification Cancel:',cancel_order(bid_id))
open_bid_price = ask - round(float(ask),2)
print ('BID PLACED',cb_trade_agg('buy',trade_quant,open_bid_price,prod_string))
num_orders += 1
change = 1
else:
pass
elif quant_change > ma_quantity:
if open_bids > 0:
print ('MA Cancel:',cancel_order(bid_id))
if quant_change >=ma_quantity:
if open_asks == 0:
open_ask_price = round(float(bid),2)
print ('ASK PLACED',cb_trade_agg('sell',trade_quant,open_ask_price,prod_string))
num_orders +=1
change = 1
elif abs(ask-ask_price) > order_thresh:
print ('Modification Cancel (Ask):',cancel_order(ask_id))
open_ask_price = round(float(bid),2)
print ('ASK PLACED',cb_trade_agg('sell',trade_quant,open_ask_price,prod_string))
num_orders +=1
change = 1
else:
pass
elif quant_change < ma_quantity:
if open_asks > 0:
print ('MA Cancel:',cancel_order(ask_id))
if change > 0:
x = cb_balance()
if x == 'ERROR':
break
for i in x:
if i[0]=='USD':
USD_balance = float(i[1])
if i[0] =='BTC':
bitcoin = float(i[1])
btc_change = bitcoin-starting_btc_balance
usd_change = USD_balance-starting_usd_balance
total_change = usd_change + (btc_change * (bid + ask)/2)
num_trades=0
for i in cb_history():
num_trades = num_trades + 1
static_p_balance = USD_balance + bitcoin * start_price
market_rate = starting_usd_balance + starting_btc_balance*(bid+ask)/2
versus_mkt = USD_balance + bitcoin*USD - market_rate
open_orders = 0
x = cb_open()
if x =='ERROR':
break
for i in x:
open_orders += 1
print ('Open Orders:',open_orders,
'Base Profit - USD',usd_change ,
'Subset Profit - BTC',btc_change ,
'Adj Profit',total_change,
'Versus Market',versus_mkt,
'Number of Trades:',num_trades,
'Number of Orders:',num_orders,
'Quantity Change:',quant_change)
if abs(btc_change) > .1 or total_change < -.1 or abs(usd_change)>30:
breaker=1
break
def arb_test(self):
while True:
time.sleep(1)
change = 0
for i in cb_balance():
if i[0]==p1:
quant_change_p1 = float(i[1]) - starting_p1_balance
elif i[0]==p2:
quant_change_p2 = float(i[1]) - starting_p2_balance
elif i[0]==p3:
quant_change_p3 = float(i[1]) - starting_p3_balance
USD, bid1,ask1,bid2,ask2,bid3,ask3 = datagrab()
#this tells us if theres an arb op. if mbuy1, buy btc, sell for eth, sell for USD
if ask1 < (bid2/bid3)-.0025*bid2 -.0025*bid3:
print ('Arb number UBEU:',ask1 - (bid2/bid3))
qee = trade_quant1/bid3
trade_quant3 = round(float(qee),2)
print (trade_quant3, qee)
change = 1
y = 0
while y < 3:
x = cb_trade('buy',trade_quant1,ask1,prod_string1)
try:
if x['created_at']:
y = 3
print ('ARB UBEU TRADE (btc usd):',x)
except:
print ('ERROR in ARB UBEU Trade (btc usd)',x)
y =+ 1
y = 0
while y < 3:
x = cb_trade('sell',trade_quant3,bid3,prod_string3)
try:
if x['created_at']:
y = 3
print ('ARB UBEU TRADE (eth btc):',x)
except:
print ('ERROR in ARB UBEU Trade (eth btc)',x)
y =+ 1
y=0
while y < 3:
x = cb_trade('sell',trade_quant3,bid2,prod_string2)
try:
if x['created_at']:
y = 3
print ('ARB UBEU TRADE (eth usd):',x)
except:
print ('ERROR in ARB UBEU Trade (eth usd)',X)
y =+ 1
USD, bid1,ask1,bid2,ask2,bid3,ask3 = datagrab()
if bid1 > (ask2/bid3)+.0025*ask2 +.0025*bid3:
print ('Arb number BUEB',bid1 - (ask2/bid3))
qee = trade_quant1/bid3
trade_quant3 = round(float(qee),2)
print (trade_quant3, qee)
change = 1
y = 0
while y < 3:
x = cb_trade('sell',trade_quant1,ask1,prod_string1)
try:
if x['created_at']:
y = 3
print ('ARB BUEB TRADE (btc usd):')
except:
print ('ERROR in ARB BUEB Trade (btc usd',x)
y = y + 1
y = 0
while y < 3:
x = cb_trade('buy',trade_quant3,ask2,prod_string2)
try:
if x['created_at']:
y = 3
print ('ARB BUEB TRADE (eth usd):')
except:
print ('ERROR in ARB BUEB Trade (eth usd',x)
y = y + 1
y=0
while y < 3:
x = cb_trade('sell',trade_quant3,ask3,prod_string3)
try:
if x['created_at']:
y = 3
print ('ARB BUEB TRADE (btc eth):')
except:
print ('ERROR in ARB BUEB Trade (btc eth',x)
y = y + 1
USD, bid1,ask1,bid2,ask2,bid3,ask3 = datagrab()
def datagrab(self):
USD = float(USD_gdax())
bid1, ask1 = mmgdax(prod_string1)
bid1 = float(bid1)
ask1 = float(ask1)
bid2, ask2 = mmgdax(prod_string2)
bid2 = float(bid2)
ask2 = float(ask2)
bid3, ask3 = mmgdax(prod_string3)
bid3 = float(bid3)
ask3 = float(ask3)
return USD, bid1,ask1,bid2,ask2,bid3,ask3
mm= MarketMaker()
mm.make_mark('BTC/USD',.02,.02,.2)