-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
orderManagement.py
94 lines (74 loc) · 3.63 KB
/
orderManagement.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
from order import Order
import time
from decimal import Decimal
class OrderManagement():
def __init__(self):
self._clientDERIBIT = None
def run(self, clientMap):
self._clientDERIBIT = clientMap["deribit"]
def cancelAll(self):
try:
orders = self._clientDERIBIT.cancelall()
print(str(orders) + " any outstanding orders are cancelled")
except Exception as e:
self.exceptionHandler(e, " error on cancelling all")
def cancelOrder(self, orderObject):
if not isinstance(orderObject, Order):
return
orderObjectCancelled = None
try:
orderObjectCancelled = self._clientDERIBIT.cancel(orderObject.getId())
print("cancel order " + str(orderObjectCancelled))
except Exception as e:
self.exceptionHandler(e, " Cancelling order Error, trying again")
return orderObjectCancelled
def buyOrder(self, indiceName,size, price, reduce_only=False):
try:
lastOrderObject = self._clientDERIBIT.buy(instrument=indiceName, quantity=str(size),price=str(price),reduce_only=reduce_only, postOnly=True, hidden=True, label=None)["order"]
print("Buy order sent - " + str(lastOrderObject))
return lastOrderObject
except Exception as e:
self.exceptionHandler(e, " Error creating an order buy, exiting")
return None
def sellOrder(self, indiceName,size, price, reduce_only=False):
# try using bulk orders to create these orders
try:
lastOrderObject = self._clientDERIBIT.sell(instrument=indiceName, quantity=str(size),price=str(price), reduce_only=reduce_only, postOnly=True, hidden=True, label=None)["order"]
print("Sell order sent - " + str(lastOrderObject))
return lastOrderObject
except Exception as e:
self.exceptionHandler(e, " Error creating an order sell, exiting")
return None
def orderStatus(self, listOfOrders, instrument):
orderItem = None
mapOfOutcomes = {"open":False ,"cancelled": False, "fullyFilled": False, "partFilled": False, "orderStatus": orderItem}
while(True):
try:
orderMapList = self._clientDERIBIT.getopenorders(instrument=instrument)
# orders from Deribit
for order in orderMapList:
# order we have save from when we sent them
for localOrder in listOfOrders:
# some order may come back as a None due to the connection timing out
if localOrder == None or localOrder.isOrderNone():
continue
if order["order_id"] == localOrder.getId():
# print("order cancelled")
mapOfOutcomes["open"] = True
mapOfOutcomes["fullyFilled"] = False
return mapOfOutcomes
else:
mapOfOutcomes["fullyFilled"] = True
return mapOfOutcomes
except Exception as e:
self.exceptionHandler(e, " order status function throw an error")
def getOpenOrders(self, indiceName):
while(True):
try:
orders = self._clientDERIBIT.getopenorders(instrument=indiceName)
return orders
except Exception as e:
self.exceptionHandler(e, " getOpenOrders function throw an error")
def exceptionHandler(self, e, task):
print(str(e), " error occured when doing " + task)
time.sleep(5)