Time In Force¶
Source: adopted from here
Introduction¶
Time in force (TIF
) is a special instruction to indicate how long an order will remain live before it is executed, cancelled or expires. As a result, it gives the trader or the electronic algorithm a mechanism of controlling time for an order. A few selected TIF
values include:
Day
- Good Till Cancel (
GTC
) - At the Opening (
OPG
) - Immediate Or Cancel (
IOC
) - At the Close (
CLS
)
The FIX tag for time in force is 59
and additional details are available here.
Question¶
In algorithmic trading, a Smart Order Router (SOR) creates child orders and sends them to different exchanges for execution and each child order has an attribute called tif
. The function simOrdersTIF
simulates a list of child orders with two attributes: time
for order creation time and tif
for the order's time in force.
simOrdersTIF:{
nOrders:10000;
seed:-314159;
openTime:`time$09:30;
closeTime:`time$16:00;
listTifs:`Day,20#`IOC;
system "S ",string seed;
times:asc closeTime&openTime+nOrders?390*60*1000;
system "S ",string seed;
tifs:nOrders?listTifs;
([] time:times;tif:tifs)
};
orders:simOrdersTIF[];
Find all clusters of IOC
orders and the cluster with the longest duration. A cluster of IOC
orders is defined as a group of IOC
orders created in a row. A cluster's duration is the time difference between the first order and the last order within a cluster.
Answer¶
The following code snippet is suggested. I intentionally break it into multiple lines to make the explanations easier.
iocClusters:select time,tif,isIOC:`IOC=tif from orders;
iocClusters:update iocGroup:sums 1_(>)prior (0,isIOC) from iocClusters;
iocClusters:update duration:last time-first time by iocGroup from iocClusters where isIOC;
select from iocClusters where duration=max duration
Some explanations:
- Line 1 updates the table with a boolean flag to indicate whether an order is an
IOC
order. - Line 2 creates an IOC group for each IOC cluster. The snippet
1_(>)prior (0,isIOC)
flags the first IOC order in an IOC cluster. Thesums
simply creates an increasing group index for each IOC cluster. - Line 3 finds the time duration of each IOC clusters.
- Line 4 gives the IOC clusters with maximal duration.