Skip to content

2020.01.20

The most aggressive price Source: adopted from here

Introduction

A set of child orders may be created in each evaluation cycle of a Smart Order Router (SOR) and these orders might be distributed among multiple price levels and even across different trading venues. The code below simulates this order creation process. In each evaluation cycle, five orders are generated, each of which has different price but has the save evaluation id. In this example, there are two parent orders: one for BUY and one for SELL.

genCO:{[parentId;waveId;side]
    n:-5;

    system "S -314159";
    ids:`long$.z.N+n?1000;

    system "S -314159";
    prices:100+0.01*n?100;

    system "S -314159";
    sizes:100*1+n?10;

    ([] poid:parentId;wid:waveId;coid:ids;side:side;price:prices;size:sizes)
  };

genOrders:{
    buyOrders:raze {poid:`long$22:32:12.163;genCO[poid;x;`BUY]} each 101+til 20;
    sellOrders:raze {poid:`long$23:32:12.163;genCO[poid;x;`SELL]} each 101+til 20;
    buyOrders,sellOrders
  };

orders:genOrders[];

Question

Find the most aggressive order, i.e. buy order with the highest price and sell order with the lowest price, within each evaluation cycle.

Answer

The following two approaches are proposed:

Method 1

raze {[s]
  f:$[s=`BUY;max;min];
  select from orders where side=s,price=(f;price) fby ([] poid;wid)
} each `BUY`SELL

In above implementation:

  • Use raze to merge multiple conforming tables into a single table
  • Conditional selection operator ($) is used to select which function to use based on the order side
  • To group on multiple columns in a fby, tabulate them in group

Method 2

{
  data:`poid`wid`p xasc update p:?[side=`BUY;price;-1*price] from orders;
  data:delete p from data;
  0!select by poid,wid from data
}[]

A few notes about the second implementation:

  • select by group from table gives you the last row in a group
  • Use 0! on to remove key(s) of a keyed table