Last updated 8 days ago
The Time-Weighted Average Price (TWAP) algo attempts to spread out an order evenly throughout a period of time. Using a TWAP execution is one way to achieve an average price that matches the market and can help minimize slippage and reduce costs.
# twap_algo_id = client.create_twap_algo( # name="My TWAP", # market="BTC-USDT BINANCE Perpetual/USDT Crypto*BINANCE/DIRECT", # dir=OrderDirection.BUY, # quantity=1, # interval_ms=1000, # reject_lockout_ms=10000, # end_time=datetime.now() + timedelta(hours=1), # account=None, # optional # take_through_frac=None, # optional # )
import { create } from '@afintech/sdk'; const c = create({/* config */}); const twapAlgoId = await c.createTwapAlgo({ name: 'My TWAP', market: 'BTC-USDT BINANCE Perpetual/USDT Crypto*BINANCE/DIRECT', dir: 'buy', quantity: '1', intervalMs: 1_000, rejectLockoutMs: 10_000, endTime: inAnHour(), account: null, takeThroughFrac: null, }); console.log('twapAlgoId', twapAlgoId); function inAnHour() { const now = Date.now(); const hour = 1_000 * 60 * 60; return new Date(now + hour).toISOString(); }
mutation SendTwapAlgoRequest($algo: CreateTwapAlgo!) { createTwapAlgo(twapAlgo: $algo) } input CreateTwapAlgo { name: Str! market: MarketId! dir: Dir! quantity: Decimal! intervalMs: Int! rejectLockoutMs: Int! endTime: DateTime! account: AccountId takeThroughFrac: Decimal }
# status = client.get_twap_status(twap_algo_id) # status.order_id # status.order # status.creation_time # status.status # status.fraction_complete # status.realized_twap # status.quantity_filled
import { create } from '@afintech/sdk'; const c = create({/* config */}); const status = await c.twapStatus([], twapAlgoId) console.log('status', status[0]);
query GetTwapStatus($orderId: OrderId!) { twapStatus(orderId: $orderId) { orderId order { name orderId marketId dir quantity endTime accountId intervalMs rejectLockoutMs takeThroughFrac } creationTime status fractionComplete realizedTwap quantityFilled } }