-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.py
86 lines (65 loc) · 2.85 KB
/
proc.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
import numpy as np
from eth.gdax_priv import *
from eth.gdax_client import *
from eth.gdax_msg import *
import eth.num_utils as nu
import time
from datetime import datetime, timedelta
def Print(txt):
if(do_print):
now = datetime.now()
nowstr = now.strftime("%m/%d %H:%M:%S")
print("%s -- %s" % (nowstr, txt))
def DoBuy(auth, price, size):
params = LimitOrderParams(price = price, size = size, side = 'buy')
return auth.buy(params)
def DoSell(auth, price, size):
params = LimitOrderParams(price = price, size = size, side = 'sell')
return auth.sell(params)
def PerformOrder(auth, price, size, poll_interval = 5.0, order_side=None, completion_callback = None , do_print = True):
if(order_action != 'buy' and order_action != 'sell'):
raise Exception("Order side must be specified.")
if(do_print):
Print("Placing %s order: %.4f ETH @ $%.2f" % (order_side, params.size, params.price))
if(order_side == 'buy'):
order = auth.DoBuy(price, size)
else:
order = auth.DoSell(price, size)
if(order.isError):
raise Exception("Error placing order: %s" % order)
oid = order.id
err_attempts = 3
while(True):
time.sleep(poll_interval)
order = auth.getOrder(oid)
if(order.isError):
err_attempts -= 1
if(err_attempts == 0):
raise Exception("Error retrieving order info: %s" % order)
continue
else:
err_attempts = 3
if(order.status == 'open'):
continue
elif(order.status == 'done'):
if(do_print):
Print("The %s order has completed." % order_side)
if(completion_callback != None):
return completion_callback(auth, price, size)
else:
raise Exception("Received order status other than 'open' or 'done': %s" % order.status)
def PerformBuy(auth, price, size, poll_interval = 5.0, completion_callback = None ,do_print = True):
PerformOrder(auth, price, size,
poll_interval = poll_interval,
completion_callback = completion_callback,
do_print = do_print,
order_side = 'buy')
def PerformSell(auth, price, size, poll_interval = 5.0, completion_callback = None ,do_print = True):
PerformOrder(auth, price, size,
poll_interval = poll_interval,
completion_callback = completion_callback,
do_print = do_print,
order_side = 'sell')
def PerformBuySell(auth, buy_price, sell_price, size, poll_interval = 5.0, completion_callback = None, do_print = True):
PerformBuy(auth, buy_price, size, poll_interval = poll_interval, do_print = do_print)
PerformSell(auth, sell_price, size, poll_interval = poll_interval, do_print = do_print, completion_callback = completion_callback)