-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
147 lines (128 loc) · 5.37 KB
/
main.py
File metadata and controls
147 lines (128 loc) · 5.37 KB
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
import json
import urllib3
import time
urllib3.disable_warnings()
http = urllib3.PoolManager()
APIKEY = 0
def assetPairs() :
r = http.request('GET', 'https://hft-service-dev.lykkex.net/api/AssetPairs')
liste = json.loads(r.data)
for k in liste :
print(k["Name"], end = " -- ")
return liste
def assetPairsId(id) :
r = http.request('GET', 'https://hft-service-dev.lykkex.net/api/AssetPairs/'+str(id))
liste = json.loads(r.data)
print(liste["Name"])
return liste
def isAlive() :
r = http.request('GET', 'https://hft-service-dev.lykkex.net/api/IsAlive')
liste = json.loads(r.data)
try :
print("Server is alive, version :", liste["Version"])
if liste["IssueIndicators"] != [] :
print("Issues indicators :")
for k in liste["IssueIndicators"] :
print(k)
else :
print("Without issue indicators x)")
return True
except :
print("Server is dead, due to :", liste["ErrorMessage"])
return False
def getBalance() : #untested coz no api key given
r = http.request('GET', 'https://hft-service-dev.lykkex.net/api/Wallets', fields = {'api-key' : APIKEY})
liste = json.loads(r.data)
for k in liste :
print(k["Balance"], "of", k["AssetId"], "including", k["Reserved"], "reserved")
return liste
def marketOrder(assetId, buyorsell, volume) : #untested coz no api key given
order = {"AssetPairId": assetId, "OrderAction": buyorsell, "Volume": volume}
order = json.dumps(order)
r = http.request('POST', 'https://hft-service-dev.lykkex.net/api/Orders/market', fields = {'order' : order, 'api-key' : APIKEY})
try :
liste = json.loads(r.data)
try :
print(liste["Error"])
return False
except :
print("Executed market order", str(buyorsell), "volume :", volume, "on asset", str(assetId))
return True
except :
return #print("Something went wrong :/")
def limitOrder(assetId, buyorsell, volume) : #untested coz no api key given
order = {"AssetPairId": assetId, "OrderAction": buyorsell, "Volume": volume}
order = json.dumps(order)
r = http.request('POST', 'https://hft-service-dev.lykkex.net/api/Orders/limit', fields = {'order' : order, 'api-key' : APIKEY})
try :
liste = json.loads(r.data)
try :
print(liste["Error"])
return False
except :
print("Executed limit order", str(buyorsell), "volume :", volume, "on asset", str(assetId))
return True
except :
return #print("Something went wrong :/")
def cancelOrder(id) : #untested coz no api key given
r = http.request('POST', 'https://hft-service-dev.lykkex.net/api/Orders/'+str(id)+'/Cancel', fields = {'id' : id, 'api-key' : APIKEY})
if r.status == 20 :
print("Order", id, "canceled")
return True
else :
print("Order not canceled")
return False
def infoOrder(id) : #untested coz no api key given
r = http.request('GET', 'https://hft-service-dev.lykkex.net/api/Orders/'+str(id), fields = {'id' : id, 'api-key' : APIKEY})
liste = json.loads(r.data)
print("Status", liste["Status"])
print("Remaining volume", liste["RemainingVolume"])
print("Price", liste["Price"], "on assets id", liste["AssetPairId"])
return liste
def allOrder() : #untested coz no api key given
status = ["Pending", "InOrderBook", "Processing", "Matched", "NotEnoughFunds", "NoLiquidity", "UnknownAsset", "Cancelled", "LeadToNegativeSpread"]
for s in status :
r = http.request('GET', 'https://hft-service-dev.lykkex.net/api/Orders?status='+s, fields = {'api-key' : APIKEY})
liste = json.loads(r.data)
print("Order", s)
for k in liste :
print("Remaining volume", k["RemainingVolume"])
print("Price", k["Price"], "on assets id", k["AssetPairId"])
print("")
def orderBookAsset(id) :
r = http.request('GET', 'https://hft-service-dev.lykkex.net/api/OrderBooks/'+str(id))
#isBuy looks like useless coz false => negative value of volume
liste = json.loads(r.data)
buy = []
sell = []
for k in liste :
if k["IsBuy"] :
buy.extend(k["Prices"])
else :
sell.extend(k["Prices"])
return (buy, sell)
#End of API no need of full order book for all assets
#Buy low sell high, low level => we dont look at volume
volumeTraded = 50
assetTraded = "AUDUSD"
frequency = 0.5
#End of parameters
miniBuy = 99999999999
maxiSell = 0
notBuyYet = True
notSellYet = True
while True :
time.sleep(1/frequency)
#First we uptade miniBuy and maxiSell
buy, sell = orderBookAsset(assetTraded)
buyAvalable = min([i["Price"] for i in buy])
sellAvalable = max([i["Price"] for i in sell])
miniBuy = min(miniBuy, buyAvalable)
maxiSell = max(maxiSell, sellAvalable)
print("Order book analysed // min buy :", miniBuy, " // top sell :", maxiSell, "// spread :", maxiSell-miniBuy)
#We then look if we can buy lower than maxiSell or sell higher than maxiBuy
if buyAvalable < maxiSell and notBuyYet :
notBuyYet = not marketOrder(assetTraded, "Buy", volumeTraded)
if sellAvalable > miniBuy and notSellYet :
notSellYet = not marketOrder(assetTraded, "Sell", volumeTraded)
#End of the strategy, we may loop on assets using multithreading, was asking simple strategy : it cant lose