Market Depth / Order Flow Analysis

The following functions can be used for retrieving market depth or BBO data from the broker or from historical .t2 data files, and for analyzing the order flow from the distribution of quotes and their volumes in the order book (Zorro S required).


BTC/USD June 2018, cumulative order volume distribution

The above histogram is generated from a order flow distribution analysis. The height of a bar is the cumulative volume of a price in the order book, blue for bid and red for ask prices. The volume is expressed as a 0..1 percentage of the total ask or bid volume, whichever is higher. You can see that the situation in the diagram is dominated by offers (100% vs. 65%), which might indicate a further falling bitcoin price.
 

orderUpdate (string Name, int Handle): int

Puts the list of order book quotes corresponding to the current time in a dataset with the given Handle for further evaluation. In [Test] or [Train] mode the quotes are read from a historical data file Name.t2 or Name_YYYY.t2 that is supposed to contain historical order book data. In [Trade] mode the quotes are directly downloaded from the broker API. Every T2 record of the resulting dataset contains 3 fields: the timestamp, the quoted price (negative for bid), and the quoted volume. The T2 struct is defined in trading.h. The function sets the OrderRow variable to the row of the first quote in the dataset, and returns the number of quotes with equal time stamps.
 

orderCVD (T2* Quotes, int N, var Distance): int

Generates a cumulative volume distribution of the price/volume pairs from the given Quotes list of T2 records. The list is normally generated by a previous orderUpdate call. The Quotes pointer can be obtained from a dataset with dataStr(Handle,OrderRow,0). Distance is the maximum deviation of the highest ask or lowest bid from the center price; quotes further away are not considered. The function returns the number of quotes within Distance.

cpd(var Price): var

Returns the cumulative relative volume of the given Price, in percent (0..100). 100 is equivalent to the total ask or bid volume, whichever is higher. Negative prices return the bid volume, positive prices the ask volume.

Parameters:

Name Name of the order book file, without the trailing ".t2" and without a year number, or 0 for using the current asset name.
Handle Handle of the dataset to receive the order book data.
Quotes List of quotes in T2 format.
N Number of quotes.
Distance Maximum order book range.
Price Quote price.
   

Remarks:

Example:

// plot an order flow profile
void main() 
{
  StartDate = 20180110;
  LookBack = 0;
  BarPeriod = 10;
  PlotScale = 10;
  set(PLOTNOW);

  assetList("AssetsCrypto");
  asset("BTC/USD");

// load today's order book
  int N = orderUpdate("BTCUSD",1);
  T2* Quotes = dataStr(1,OrderRow,0);
  printf("\nOrderbook: %i quotes",N);

  var Distance = 0.05*price(); // +/- 5%
  int N2 = orderCVD(Quotes,N,Distance);
  printf(", %i in range",N2);
  Distance *= 1.5;
  var Price = priceClose() - Distance;
  int i;
  for(i=0; i<100; i++) {
    Price += Distance/50;
    plotBar("Ask",i,Price,cpd(Price),BARS|LBL2,RED);
  }
  Price = priceClose() - Distance;
  for(i=0; i<100; i++) {
    Price += Distance/50;
    plotBar("Bid",i,Price,cpd(-Price),BARS|LBL2,BLUE);
  }
}

See also:

contractCPD, cdf

 

► latest version online