-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_private.py
128 lines (97 loc) · 3.17 KB
/
func_private.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
from datetime import datetime, timedelta
from func_utils import format_number
import time
from pprint import pprint
# Get existing open positions
def is_open_positions(client, market):
# Protect API
time.sleep(0.2)
# Get positions
all_positions = client.private.get_positions(
market=market,
status="OPEN"
)
# Determine if open
if len(all_positions.data["positions"]) > 0:
return True
else:
return False
# Check order status
def check_order_status(client, order_id):
order = client.private.get_order_by_id(order_id)
if order.data:
if "order" in order.data.keys():
return order.data["order"]["status"]
return "FAILED"
# Place market order
def place_market_order(client, market, side, size, price, reduce_only):
# Get Position Id
account_response = client.private.get_account()
position_id = account_response.data["account"]["positionId"]
# Get expiration time
server_time = client.public.get_time().data
expiration = datetime.fromisoformat(server_time["iso"].replace("Z", "")) + timedelta(seconds=70)
# Place an order
placed_order = client.private.create_order(
position_id=position_id,
market=market,
side=side,
order_type="MARKET",
post_only=False,
size=size,
price=price,
limit_fee='0.015',
expiration_epoch_seconds=expiration.timestamp(),
time_in_force="FOK",
reduce_only=reduce_only
)
# Return result
return placed_order.data
# Abort all open positions
def abort_all_positions(client):
# Cancel all orders
client.private.cancel_all_orders()
# Protect API
time.sleep(0.5)
# Get markets for reference of tick size
markets = client.public.get_markets().data
#pprint(markets)
# Protect API
time.sleep(0.5)
# Get all open positions
positions = client.private.get_positions(status="OPEN")
all_positions = positions.data["positions"]
#pprint(all_positions)
# Handle open positions
close_orders = []
if len(all_positions) > 0:
# Loop through each position
for position in all_positions:
# Determine Market
market = position["market"]
# Determine Size
side = "BUY"
if position["side"] == "LONG":
side = "SELL"
# Get Price
price = float(position["entryPrice"])
#print("price=", price)
accept_price = price * 1.7 if side == "BUY" else price * 0.3
#print(markets["markets"][market])
tick_size = markets["markets"][market]["tickSize"]
accept_price = format_number(accept_price, tick_size)
#print("accept_price=", accept_price)
# Place order to close
order = place_market_order(
client,
market,
side,
position["sumOpen"],
accept_price,
True
)
# Append the result
close_orders.append(order)
# Protect API
time.sleep(0.2)
return close_orders