VWAP Trading Bot Development
VWAP (Volume Weighted Average Price) is the volume-weighted average price. Shows the fair price of an instrument considering trading volume. Traders use VWAP as dynamic support/resistance level and to assess execution quality.
What is VWAP
VWAP is calculated cumulatively from the start of trading session (or period):
VWAP = Σ(Price × Volume) / Σ(Volume)
Price above VWAP → instrument trades above fair value (sell potential).
Price below VWAP → below fair value (buy potential).
Bot Implementation
import pandas_ta as ta
from decimal import Decimal
class VWAPBot:
def __init__(self, symbol: str, session_start_hour: int = 0):
self.exchange = ccxt.binance({'apiKey': API_KEY, 'secret': SECRET})
self.symbol = symbol
self.session_start_hour = session_start_hour
# VWAP bounds — standard deviations from VWAP
self.upper_band_std = 2.0 # sell when price above VWAP + 2σ
self.lower_band_std = 2.0 # buy when price below VWAP - 2σ
async def calculate_vwap(self) -> tuple[Decimal, Decimal, Decimal]:
"""Returns (vwap, upper_band, lower_band)"""
# Get data from session start
ohlcv = await self.exchange.fetch_ohlcv(self.symbol, '1m', limit=480) # 8 hours
df = pd.DataFrame(ohlcv, columns=['ts','open','high','low','close','volume'])
# Typical price
df['typical_price'] = (df['high'] + df['low'] + df['close']) / 3
# VWAP: cumulative weighted average
df['tp_vol'] = df['typical_price'] * df['volume']
df['cum_tp_vol'] = df['tp_vol'].cumsum()
df['cum_vol'] = df['volume'].cumsum()
df['vwap'] = df['cum_tp_vol'] / df['cum_vol']
current_vwap = Decimal(str(df['vwap'].iloc[-1]))
current_price = Decimal(str(df['close'].iloc[-1]))
# Standard deviation from VWAP
deviation = df['close'] - df['vwap']
std = Decimal(str(deviation.std()))
upper_band = current_vwap + std * Decimal(str(self.upper_band_std))
lower_band = current_vwap - std * Decimal(str(self.lower_band_std))
return current_vwap, upper_band, lower_band
async def get_signal(self) -> str:
vwap, upper, lower = await self.calculate_vwap()
current_price = await self.get_current_price()
if current_price < lower:
return 'BUY' # price well below VWAP
elif current_price > upper:
return 'SELL' # price well above VWAP
elif abs(current_price - vwap) / vwap < Decimal('0.001'):
return 'CLOSE_POSITION' # price at VWAP — exit
return 'HOLD'
VWAP as Execution Tool (VWAP Execution)
In institutional trading, VWAP is not only an indicator but also an execution algorithm for large orders. Task: execute 1000 BTC so average execution price is close to market VWAP.
class VWAPExecution:
"""Split large order into parts proportional to predicted volume"""
def __init__(self, total_qty: Decimal, start_time: datetime, end_time: datetime):
self.total_qty = total_qty
self.start_time = start_time
self.end_time = end_time
self.executed_qty = Decimal('0')
def get_slice_quantity(self, current_time: datetime,
predicted_volume_pct: Decimal) -> Decimal:
"""How much to execute right now"""
remaining_qty = self.total_qty - self.executed_qty
# Execute proportional to expected volume for next period
slice_qty = self.total_qty * predicted_volume_pct
# Don't exceed remaining
return min(slice_qty, remaining_qty)
Development of VWAP bot with bands strategy and VWAP deviation monitoring: 2–3 weeks.







