Order filling and HFT simulation

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 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. In this mode, only direct trading, receiving price quotes, and plotting data are supported; all functions such as indicators, series, or the run function are not used. Use the main function for initializing the system and for looping through bars. The maximum number of bars must be set up with MaxBars. Trades are opened and closed only with enter and exit commands; TMFs, stops, or profit targets are not used in HFT mode. Hedge mode must be 2. Price quotes are entered to the system with the priceQuote function. Any price quote must include the exchange time stamp, since the system is synchronized to it. 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.

For determining the latency of your system, make sure that your PC clock is exactly in sync with the clock used at the exchange, then compare the exchange time stamp of incoming live price quotes with the arrival time stamp. Double this time for getting the roundtrip latency and add the reaction time of your hardware and software. As a rule of thumb, the time stamp difference is about 1 ms per any 300 km distance to the exchange (light travels at 300.000 km/sec), plus about 1..2 ms additional delay due to signal routing. EntryDelay can be set up with a precision of 1 microsecond. Price quote data by data vendors has usually a precision of 1 ms.

Fill

Determines how order fills are simulated in [Test] and [Train] mode and how orders entries affect other open trades.

Range:

NAIVE 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.
ATCLOSE Realistic order filling (default). Trades open or close at the current price quote 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.
DELAYED 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.
HFT HFT fill mode. Trades open or close at the last priceQuote with a time stamp determined by EntryDelay.

Type:

int

Remarks:

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

typedef struct QUOTE {
  char  Name[24];
  var  Time,Price,Size;
} 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...
    if(!wait(0)) return 0; // check if [Stop] was clicked
  }
    
  asset(Quote->Name+1); // NxCore adds "e" to the asset name
  priceQuote(Quote->Time,Quote->Price);
  ...
// trade algorithm here, generate TradeSignalLong/Short
  ...
  if(!(NumOpenLong+NumPendingLong) && TradeSignalLong) {
    exitShort();
    enterLong();
  } else if(!(NumOpenShort+NumPendingShort) && TradeSignalShort) {
    exitLong();
    enterShort();
  }
  return 1;
}

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

  StartDate = 20170103;
  EndDate = 20170131;
  LookBack = 0;
  set(LOGFILE);
  assetList("AssetsHFT");
  EntryDelay = LATENCY/1000.;
  Hedge = 2;
  Fill = HFT;
  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);
    if(!wait(0)) break; // check if [Stop] was clicked
  }
}

See also:

enter, exit, Hedge, Slippage, mode, priceQuote

 

► latest version online