Skip to content

Commit f2e5d56

Browse files
authored
Added Visualizing Market Data with Entry Orders
1 parent d3b3dcf commit f2e5d56

1 file changed

Lines changed: 36 additions & 11 deletions

File tree

examples/RoibalBot.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def convert_time_binance(gt):
110110
return tt
111111

112112

113-
def market_depth(sym, num_entries=15):
113+
def market_depth(sym, num_entries=20):
114114
#Get market depth
115115
#Retrieve and format market depth (order book) including time-stamp
116116
i=0 #Used as a counter for number of entries
@@ -124,10 +124,18 @@ def market_depth(sym, num_entries=15):
124124
bid_price = []
125125
bid_quantity = []
126126
bid_tot = 0.0
127+
place_order_ask_price = 0
128+
place_order_bid_price = 0
129+
max_order_ask = 0
130+
max_order_bid = 0
127131
print("\n", sym, "\nDepth ASKS:\n")
128132
print("Price Amount")
129133
for ask in depth['asks']:
130134
if i<num_entries:
135+
if float(ask[1])>float(max_order_ask):
136+
#Determine Price to place ask order based on highest volume
137+
max_order_ask=ask[1]
138+
place_order_ask_price=round(float(ask[0]),5)-0.0001
131139
#ask_list.append([ask[0], ask[1]])
132140
ask_price.append(float(ask[0]))
133141
ask_tot+=float(ask[1])
@@ -139,36 +147,53 @@ def market_depth(sym, num_entries=15):
139147
print("Price Amount")
140148
for bid in depth['bids']:
141149
if j<num_entries:
150+
if float(bid[1])>float(max_order_bid):
151+
#Determine Price to place ask order based on highest volume
152+
max_order_bid=bid[1]
153+
place_order_bid_price=round(float(bid[0]),5)+0.0001
142154
bid_price.append(float(bid[0]))
143155
bid_tot += float(bid[1])
144156
bid_quantity.append(bid_tot)
145157
#print(bid)
146158
j+=1
147-
return ask_price, ask_quantity, bid_price, bid_quantity
159+
return ask_price, ask_quantity, bid_price, bid_quantity, place_order_ask_price, place_order_bid_price
148160
#Plot Data
149161

150-
def visualize_market_depth(wait_time_sec='60', tot_time='480', sym='ETHUSDT'):
162+
def visualize_market_depth(wait_time_sec='1', tot_time='1', sym='ICXBNB'):
151163
cycles = int(tot_time)/int(wait_time_sec)
152164
start_time = time.asctime()
153165
fig, ax = plt.subplots()
154-
for i in range(0,int(cycles)):
155-
ask_pri, ask_quan, bid_pri, bid_quan = market_depth(sym)
166+
for i in range(1,int(cycles)+1):
167+
ask_pri, ask_quan, bid_pri, bid_quan, ask_order, bid_order = market_depth(sym)
156168

157169
#print(ask_price)
158170
plt.plot(ask_pri, ask_quan, color = 'red', label='asks-cycle: {}'.format(i))
159171
plt.plot(bid_pri, bid_quan, color = 'blue', label = 'bids-cycle: {}'.format(i))
172+
160173
#ax.plot(depth['bids'][0], depth['bids'][1])
161-
time.sleep(int(wait_time_sec))
162-
end_time = time.asctime()
174+
max_bid = max(bid_pri)
175+
min_ask = min(ask_pri)
176+
max_quant = max(ask_quan[-1], bid_quan[-1])
177+
spread = round(((min_ask-max_bid)/min_ask)*100,5) #Spread based on market
178+
proj_order_spread = round(((ask_order-bid_order)/ask_order)*100, 5)
179+
price=round(((max_bid+min_ask)/2),5)
180+
plt.plot([price, price],[0, max_quant], color = 'green', label = 'Price - Cycle: {}'.format(i)) #Vertical Line for Price
181+
plt.plot([ask_order, ask_order],[0, max_quant], color = 'black', label = 'Ask - Cycle: {}'.format(i))
182+
plt.plot([bid_order, bid_order],[0, max_quant], color = 'black', label = 'Buy - Cycle: {}'.format(i))
183+
ax.annotate("Max Bid: {} \nMin Ask: {}\nSpread: {} %\nCycle: {}\nPrice: {}"
184+
"\nPlace Bid: {} \nPlace Ask: {}\n Projected Spread: {} %".format(max_bid, min_ask, spread, i, price, bid_order, ask_order, proj_order_spread),
185+
xy=(max_bid, ask_quan[-1]), xytext=(max_bid, ask_quan[0]))
186+
if i==(cycles+1):
187+
break
188+
else:
189+
time.sleep(int(wait_time_sec))
190+
#end_time = time.asctime()
163191
ax.set(xlabel='Price', ylabel='Quantity',
164-
title='Binance Order Book: {} \n {} - {}'.format(sym, start_time, end_time))
192+
title='Binance Order Book: {} \n {}\n Cycle Time: {} seconds - Num Cycles: {}'.format(sym, start_time, wait_time_sec, cycles))
165193
plt.legend()
166194
plt.show()
167195

168196

169-
170-
171-
172197
def coin_prices(watch_list):
173198
#Will print to screen, prices of coins on 'watch list'
174199
#returns all prices

0 commit comments

Comments
 (0)