Order filling and HFT trading

The Fill variable supports several modes for simulating naive or realistic order filling, as well as a HFT simulation mode. HFT (High Frequency Trading) is a trading method that exploits inefficiencies on the millisecond or microsecond time scale. It is very different to normal trading. The usual trading accessories, such as bars, candles, or indicators, are not used. The deciding factor is speed. HFT systems do normally not run on a trading platform - even Zorro would be too slow - but are directly programmed in a high speed language, such as C, Pentium assembler, shader language (HLSL) or IC design language (VHDL).

Zorro can be used for testing HFT systems in Fill mode 8. This mode uses the callback function for analyzing price quotes and sending trade orders. Bars are not available in this mode, so the run function needs not be used; a main function for initializing the system is sufficient. Trades are only opened and closed with enter and exit commands; TMFs, stops, or profit targets are not supported, nor are indicators and series. Hedge mode must be 2, TradesPerBar set to the maximum number of trades in the simulation. Price quotes are entered to the system with the priceQuote function. For considering latency, any price quote must include the exchange time stamp. The two-way latency between exchange and PC location is set up with EntryDelay and determines the price quote at which the enter or exit order will be filled.

Fill

Determines how order fills are simulated in [Test] and [Train] mode.

Range:

0 Naive order filling. Trades open or close at the current market price or at their Entry, Stop, or TakeProfit limits, regardless of latency, slippage, or intrabar price movement. 'Cheating' is possible by setting unrealistic stop or profit limits in a TMF. This mode can be used for special purposes, f.i. for comparing with results of other platforms.
1 Realistic order filling (default). Trades open or close at the current price plus extra Slippage. When Slippage is set to 0, this mode simulates a latency-free connection to the market that immediately reacts on any price quote. Cheating is not possible. With daily bars, this mode simulates order entry immediately before the market closes.
3 Delayed order filling. Trades open or close at the next price quote plus extra Slippage. With daily bars, this mode simulates order entry at the open price of the next day.
8 HFT fill mode. Trades open or close at the last priceQuote with a time stamp determined by EntryDelay.

Type:

int

Example:

function run()
{
  BarPeriod = 1440;
  Fill = 3; // enter trades at next day's open 
  ...
}

HFT simulation framework:

// Simulate a stock trading HFT system

#define LATENCY  2.5   // simulate 2.5 milliseconds latency
#define MAXTRADES  20000  // max number of trades

typedef struct QUOTE {
  char  Name[24];
  var  Time,Price;
} QUOTE; // Quote struct by the NxCore plugin

int callback(QUOTE *Quote) // called by the plugin
{
  static int Counter = 0; // quote counter
  if(0 == (++Counter % 1000)) { // every 1000 quotes...
    Bar++; // update bar count in info window
    print(TO_INFO,0);  // update info window
    if(!wait(0)) return 0; // check if [Stop] clicked
  }
    
  asset(Quote->Name+1); // NxCore adds an "e" to the asset name
  priceQuote(Quote->Time,Quote->Price);
  ...
// trade algorithm here
  ...
  if(!(NumOpenLong+NumPendingLong) && SignalLong) {
    exitShort();
    enterLong();
  } else if(!(NumOpenShort+NumPendingShort) && SignalShort) {
    exitLong();
    enterShort();
  }
  return 1;
}

function main()
{
  if(Broker != "NxCore") {
    quit("Please select NxCore plugin!");
    return;
  }

  StartDate = 20170103;
  EndDate = 20170131;
  LookBack = 0;
  TradesPerBar = MAXTRADES;
  set(LOGFILE);
  assetList("AssetsHFT");
  EntryDelay = LATENCY/1000.;
  Hedge = 2;
  Fill = 8;
  Lots = 1;
  Verbose = 3;

// process the NxCore history tape files, one for each day
  login(1);	// for initilizing the plugin
  int NxDate;
  for(NxDate = StartDate; NxDate = EndDate; NxDate++) {
    string NxHistory = strf("History\\NxTape%8d.nx2",NxDate);
    printf("\nProcessing tape %s..",NxHistory);
    brokerCommand(SET_HISTORY,NxHistory);
    assetHistory("WHATEVER",0);
    if(!wait(0)) break; // check if [Stop] clicked
  }
}

See also:

enter, exit, Hedge, Slippage, mode, priceQuote

 

► latest version online