
When placing a buy or sell order, you can try to figure out what price to place it at if you're not a one-hit wonder, or if you're expecting a spike/sharp drop with your brain. Even if you place an order at a good price, the market can make you look like shit at the end of the day. The starting point is the assumption that the average of the bid and ask prices is the ideal price for market participants, but it's unfair to just average prices. Higher-volume quotes should contribute more to the average, while lower-volume quotes should contribute less. So the average is averaged by weighting the average by quantity, which is VWAP (VWAP is the most popular method, but not the only one).
Especially for futures trading in algorithmic trading, the data from the order book (quotes and open interest) is an important factor. One of the best explanations of VWAP can be found on the Amateur Quant's blog,
VWAP (Volume Weighted Average Price) is a price expressed as a volume weighted average and is mainly used in algorithmic trading (systematic trading). Algorithmic trading aims to buy as low or sell as high as possible without shocking the market when executing large orders. However, since it is unclear how much is too low or too high, orders are executed at an average unit price to ensure that neither the client nor the execution agent is unhappy. A popular average unit price used in this case is VWAP. VWAP is a volume-weighted average price, which means that even if you bought at a high price, if there was a lot of volume, you shouldn't be too unhappy because other buyers also bought at a high price.
Here's a VWAP made in Python. You can input data from the order book.
def calculate_vwap(order_book):
bids = order_book['bids']
asks = order_book['asks']
bid_idx = 0
ask_idx = 0
AskVWAP = 0
BidVWAP = 0
sumAsks = 0
sumBids = 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]
AskVWAP = AskVWAP + ask_price * ask_qty
BidVWAP = BidVWAP + bid_price * bid_qty
sumAsks = sumAsks + ask_qty
sumBids = sumBids + bid_qty
bid_idx = bid_idx + 1
ask_idx = ask_idx + 1
except IndexError:
break
TotalVWAP = (AskVWAP + BidVWAP) / (sumAsks + sumBids)
return TotalVWAP
if __name__ == "__main__":
# 1호가 [ 321.7 , 165.0 ] [ 321.75 , 5.0 ]
# 2호가 [ 321.65 , 128.0] [ 321.8 , 100.0 ]
# 3호가 [ 321.6 , 134.0 ] [ 321.85 , 109.0]
# 4호가 [ 321.55 , 109.0] [ 321.9 , 110.0 ]
# 5호가 [ 321.5 , 134.0 ] [ 321.95 , 119.0]
order_book = {
'bids': [
[ 321.7 , 165.0 ],
[ 321.65 , 128.0],
[ 321.6 , 134.0 ],
[ 321.55 , 109.0],
[ 321.5 , 134.0 ]
],
'asks': [
[ 321.75 , 5.0 ],
[ 321.8 , 100.0 ],
[ 321.85 , 109.0],
[ 321.9 , 110.0 ],
[ 321.95 , 119.0]
]
}
vwap = calculate_vwap(order_book)
print('VWAP :', vwap)