Skip to content

Instantly share code, notes, and snippets.

@iUmarov
Created December 2, 2018 00:22
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save iUmarov/ff3689dcbd93ac081e3c4f526caa8a4a to your computer and use it in GitHub Desktop.
Save iUmarov/ff3689dcbd93ac081e3c4f526caa8a4a to your computer and use it in GitHub Desktop.
# This is my implmentation of calculating Global Average Symmetric Price (GASP) as it's mentioned in this video: https://www.youtube.com/watch?v=wpmTuNvGQjQ
# Presenter in the video doesn't explain briefly how to calculate the GASP, but, I kinda figured it out myself
order_book = {
'bids': [
[97, 0.1],
[96, 0.2],
[95, 6]
],
'asks': [
[98, 4],
[99, 4.6],
[100, 4.8],
]
}
order_book_sec = {
'bids': [
[99, 20],
[98, 10]
],
'asks': [
[100, 17],
[101, 15]
]
}
def calculate_gasp(order_book):
bids = order_book['bids']
asks = order_book['asks']
numerator = 0
denominator = 0
bid_idx = 0
ask_idx = 0
while True:
try:
bid_price = bids[bid_idx][0]
bid_qty = bids[bid_idx][1]
ask_price = asks[ask_idx][0]
ask_qty = asks[ask_idx][1]
if bid_qty < ask_qty:
numerator = numerator + (bid_qty * bid_price + bid_qty * ask_price)
print("{0} * {1} + {2} * {3}".format(bid_qty, bid_price, bid_qty, ask_price))
bid_idx = bid_idx + 1
denominator = denominator + 2 * bid_qty
asks[ask_idx][1] = ask_qty - bid_qty
elif ask_qty < bid_qty:
numerator = numerator + (ask_qty * bid_price + ask_qty * ask_price)
print("{0} * {1} + {2} * {3}".format(ask_qty, ask_price, ask_qty, ask_price))
denominator = denominator + 2 * ask_qty
ask_idx = ask_idx + 1
bids[bid_idx][1] = bid_qty - ask_qty
except IndexError:
break
print("Numerator", numerator)
print("Denominator", denominator)
return numerator / denominator
gasp = calculate_gasp(order_book_sec)
print(gasp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment