Skip to content

Price Return

image Source: adopted from here

Introduction

One frequently conducted analysis is to evaluate the relationship between a predicting variable (e.g. trade imbalance or quote imbalance) and the price return over a given look-ahead horizon \Delta t. For example, it is assumed that the price is p_t at time t and the price at t+\Delta t is p_{t+\Delta t}. The price return over the time period \Delta t is defined as $$ r_{\Delta t} = \frac{p_{t+\Delta t} - p_t}{p_t} $$

where the \Delta t can vary from a few milliseconds to a few minutes, depending on the trading horizon your trading strategy is targeting.

Question

The function simPrices simulates the price movement in the continuous trading session from 09:30 to 16:00.

simPrices:{
  n:100000;
  system "S -314159";
  :([]time:asc 09:30+n?"n"$06:30;price:20+0.01*sums?[n?1.<0.5;-1;1]);
  };
prices:simPrices[];

For each row in the table, calculate the price return for horizon \Delta t, where \Delta t is from 1 second to 5 seconds. So the resulting table has 7 columns:

time price r1 r2 r3 r4 r5
0D09:30:00.018905382 20.01

Your implementation should be generic enough to calculate the return for any number of horizons.

Answer

The suggested answer is as follows:

futRet:(,'/) {[d;i]
  t:prices`time;
  w:(t;t+"v"$i);
  r:wj[w;`time;d;(d;({p0:first x;p1:last x;-1+p1%p0};`price))];
  ?[r;();0b;enlist[`$"r",string i]!enlist `price]
  }[prices;] each 1+til 5;
prices,'futRet

Some explanations on the implementation:

  • {[d;i] ...}[prices;] defines an anonymous unary function using function projection over a binary function. The original binary function takes two arguments: the first argument is a table, which has at least two columns like time and price and the second argument is the prediction horizon in the unit of seconds.
  • (,'/) joins all tables side by side.
  • w:(t;t+"v"$i); defines a time window for wj.
  • (d;({p0:first x;p1:last x;-1+p1%p0};`price)) calculates the price return for a given time window. Note that the column name is still price.
  • Line 5 renames price to ri, where i is an integer from 1 to 5.
  • The last line joins the price return with original table side by side.