Skip to content

Instantly share code, notes, and snippets.

@gelinger777
Forked from iUmarov/gasp.py
Created December 3, 2020 21:36
Show Gist options
  • Save gelinger777/68e5d89adfd45a87398c8a4e6614b763 to your computer and use it in GitHub Desktop.
Save gelinger777/68e5d89adfd45a87398c8a4e6614b763 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