Adding Actions

Actions are constrained asynchronous events produced by a venue/system that affect model state.

Defining actions

Fill action

action fill {
  fill_price : Price
  fill_qty : Qty
  validate { this.fill_qty > 0.0 }
  validate { this.fill_qty <= state.LeavesQty }
  validate { this.fill_price > 0.0 }
}

Book-state action

action bookState {
  bestBid : Price
  bestAsk : Price
  validate{
    this.bestAsk > this.bestBid &&
    this.bestBid > 0.0 &&
    this.bestAsk > 0.0
  }
}

Receiving actions

receive (f:fill) {
  state.LeavesQty = state.LeavesQty - f.fill_qty
  state.AvgPx = ( state.AvgPx * state.CumQty + f.fill_qty * f.fill_price ) / ( f.fill_qty + state.CumQty )
  state.CumQty = state.CumQty + f.fill_qty
  if state.LeavesQty == 0.0 then
    {
      state.OrdStatus = OrdStatus.Filled
      state.ExecType = ExecType.Trade
    }
  else
    {
      state.OrdStatus = OrdStatus.PartiallyFilled
      state.ExecType = ExecType.Trade
    }
  send ExecutionReport {
    state with
    ExecID = fresh();
  }
}
receive (ba:bookState){
  state.bestBid = ba.bestBid
  state.bestAsk = ba.bestAsk
}

Related: Messages and Actions and Custom Message Fields and Cases.