forked from DTIV/viteXtrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders.py
215 lines (199 loc) · 6.2 KB
/
orders.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
import json
import requests
from time import time
import hmac
import hashlib
from functions import get_time, get_all_pairs
from requests import get, post
f = open("config.json")
config = json.load(f)
key = config['vite_key']
def order_status(order):
'''
Get order status from object returned from limit order
'''
if order != None and 'data' in order:
return order['data']['status']
else:
if order != None and 'msg' in order:
return order['msg']
return None
def get_balance(net=config['mainnet']):
'''
Get balance from ViteX
'''
url = f"{net}/api/v2/balance"
try:
data = get(url,{"address": f"{config['viteconnect_address']}"}).json()
except:
data = get(url,{"address": f"{config['delegation_address']}"}).json()
if data['data']:
return data['data']
else:
return data
def get_open_orders(net=config['mainnet']):
'''
Get open orders from ViteX
'''
url = f"{net}/api/v2/balance"
try:
data = get(url,{"address": f"{config['viteconnect_address']}"}).json()
except:
data = get(url,{"address": f"{config['delegation_address']}"}).json()
if data['data']:
return data['data']
else:
return "No Open Orders For Address", data
def get_active_positions(cache):
'''
Get active postions from cache
'''
active_list = []
for symbol in cache:
if cache[symbol]['data']['active'] == True:
active_list.append(symbol)
if len(active_list) > 0:
return active_list
else:
active_list.append("No Active Positions")
return active_list
def get_pnl(cache):
'''
Get Profit and Loss of active pairs
'''
pnl = {}
active = get_active_positions(cache)
if "No Active Positions" not in active:
for symbol in active:
size = cache[symbol]['data']['size']
pnl_calc = (cache[symbol]['data']['entry'] * size) - (cache[symbol]['ohlc']['close'][0] * size)
pnl[symbol] = pnl_calc
return pnl
else:
pnl = ['No Active Positions']
return pnl
def get_tpsl(entry_price, side: int):
'''
Get takeprofit and stoploss values base on entry price
'''
if side:
entry_price = 0.001064
sl_prc, tp_prc = 10, 10
sl_prc = config['stoploss']/100 * entry_price
tp_prc = config['takeprofit']/100 * entry_price
stoploss = entry_price + sl_prc
takeprofit = entry_price - tp_prc
return stoploss, takeprofit
else:
sl_prc = config['stoploss']/100 * entry_price
tp_prc = config['takeprofit']/100 * entry_price
stoploss = entry_price - sl_prc
takeprofit = entry_price + tp_prc
return stoploss, takeprofit
def position_size(close):
'''
Calculate position size base on available funds in quote currency
'''
size = config['size']
if config['dynamic_size']:
data = get_balance()
quote = config['quote_currency']
if type(data['data']) == dict:
quote_bal = float(data[quote]['available'])
size = round(quote_bal / float(close),3)
return str(size)
size=1
return size
def create_sig(tx):
'''
Create HMAC SHA256 Signature
'''
print("l76 - creating signature")
hash256= bytes(tx, 'utf8')
secret = bytes(config['vite_secret'], encoding='utf8')
return hmac.new(secret,hash256, hashlib.sha256).hexdigest()
def limit_order(size, price, side, symbol, live):
'''
LIMIT ORDER: SIGNATURE NOT VERIFYING!
'''
global key
#time
endpoint = "/api/v2/order/test"
mainnet = config['mainnet']
stimestamp= int(str(time()*1000).split(".")[0])
serverTime= get_time()
if (stimestamp < (serverTime + 1000) and (serverTime - stimestamp) <= 5000):
#TX STRING
tx = fr"amount={size}&key={key}&price={price}&side={side}&symbol={symbol}×tamp={stimestamp}"
signature = create_sig(tx)
#API CALL
data = {
'amount': size,
'key': config['vite_key'],
'price': '0.09',
'side': '0',
'symbol': symbol,
'timestamp': str(stimestamp),
'signature': signature
}
response = requests.post(mainnet+endpoint, data=data)
r = json.loads(response.text)
return r
else:
print("Server Time Error!")
# reject request
def buy_set(live, order, sl, tp, data, message, entry, size, active=True):
'''
Set cache on buy orders
'''
if order != None and 'data' in order.keys() or not live:
if live:
data['orderId'] = order['data']['orderId']
data['buy_status'] = order_status(order)
else:
data['buy_status'] = 'Filled'
data['flagged'] = True
data['size'] = size
data['entry'] = entry
data['active'] = active
data['stoploss'] = sl
data['takeprofit'] = tp
data['message'] = message
data['side'] = 'buy'
data['timestamp'] = time()
else:
if live:
data['message'] = f"{order['code']} - {order['msg']}"
data['size'] = None
data['entry'] = None
data['flagged'] = False
data['active'] = False
data['buy_status'] = None
data['stoploss'] = None
data['takeprofit'] = None
data['side'] = None
return data
def sell_set(live, order, data, message, entry, active=False):
'''
Set cache on short or sell orders
'''
if (order != None and 'data' in order) or not live:
if live:
data['orderId'] = order['data']['orderId']
data['sell_status'] = order_status(order)
else:
data['sell_status'] = 'Filled'
data['entry'] = entry
data['flagged'] = True
data['active'] = active
data['message'] = message
data['side'] = 'sell'
else:
if live:
data['message'] = f"{order['code']} - {order['msg']}"
data['entry'] = None
data['flagged'] = False
data['active'] = False
data['sell_status'] = None
data['orderId'] = None
return data