TwapAlgo
Time-Weighted Average Price (TWAP). Splits the total volume into SliceCount equal slices and sends one child order per slice at the configured interval - slice 1 at start, slice 2 at start + interval, ... - until the time window StartAt .. EndAt elapses. Each slice is a Market order by default (immediate execution). Setting LimitPrice sends every slice as a Limit order at that price, wherever the market is at the time - the algo does not move the price to follow it. Outstanding child orders are tracked by the caller; this algo only emits Register actions when the current slice is due and reports finished once the total volume is exhausted or EndAt has passed. What a slice failed to execute is folded into the slices that are still to come; once every one of them has been dispatched, what is left goes out on its own while the window is still open.
Implements: IPositionModifyAlgo, IDisposable
Constructors
public TwapAlgo(Sides side, decimal totalVolume, int sliceCount, DateTime startAt, DateTime endAt, decimal? limitPrice)
twapAlgo = TwapAlgo(side, totalVolume, sliceCount, startAt, endAt, limitPrice)
Initializes a new instance of the TwapAlgo.
- side
- Order side.
- totalVolume
- Total volume to execute.
- sliceCount
- How many slices the volume is cut into.
- startAt
- When the algo may start.
- endAt
- When the algo must be done.
- limitPrice
- Price limit, or to send market orders.
Properties
public bool IsFinished { get; }
value = twapAlgo.IsFinished
Whether the algorithm has finished.
public decimal? LimitPrice { get; }
value = twapAlgo.LimitPrice
Price limit, or when slices go out as market orders.
public decimal RemainingVolume { get; }
value = twapAlgo.RemainingVolume
Remaining volume to execute.
public int SliceCount { get; }
value = twapAlgo.SliceCount
How many slices the volume is cut into.
Methods
public void Dispose()
twapAlgo.Dispose()
Disposes of items in the pool that implement IDisposable.
public PositionModifyAction GetNextAction()
result = twapAlgo.GetNextAction()
Get the next action to take. The call advances the algorithm: a returned Register counts as dispatched, so the caller places it and reports back through Decimal), OnOrderFailed or Decimal). An action that is discarded leaves the algorithm waiting for a report that never comes.
public void OnOrderCanceled(decimal matchedVolume)
twapAlgo.OnOrderCanceled(matchedVolume)
Notify that an order was canceled.
- matchedVolume
- Volume that traded since the previous notification and before cancellation.
public void OnOrderFailed()
twapAlgo.OnOrderFailed()
Notify that an order failed. Nothing traded.
public void OnOrderMatched(decimal matchedVolume)
twapAlgo.OnOrderMatched(matchedVolume)
Notify that an order was matched.
- matchedVolume
- Volume that traded since the previous notification for this order: a caller reporting every partial fill passes the increment, one reporting only the end passes the whole traded volume. The same volume is never reported twice.
public void UpdateMarketData(DateTime time, decimal? price, decimal? volume)
twapAlgo.UpdateMarketData(time, price, volume)
Update market data.
- time
- Current time.
- price
- Last trade price.
- volume
- Last trade volume.
public void UpdateOrderBook(IOrderBookMessage depth)
twapAlgo.UpdateOrderBook(depth)
Update order book data.
- depth
- Order book.