Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

How to Calculate the Order Execution Response time using Amibroker

2 min read

To send automated orders using Amibroker usually one uses the API-based trading solutions. API works on the concept of request and response. Every Place order request that we send from amibroker to the broker server is responded with a corresponding response message from the broker server (Order Successful, Order No, Error Messages, Session Expired, etc).

In this tutorial we are going to use Amibroker to measure the order latency using Algomojo Trading Platform.

Trade Round Trip Time & Trade Latency

The round trip time is how long it takes for an order request sent from an Amibroker to the exchange via broker server, and for the response to get back to the original source(Amibroker). Basically, the latency in each direction, plus the processing time.

Latency measures how long it takes for order request from Amibroker (Source) to reach Exchange(destination host). Latency is often undesirable, and especially so in Automated Trading where speed and responsiveness are essential elements of the trading business.

The one with the lowest latency will be the first to get their order filled at the market price; those with higher latency are likely to pay a higher price for the same trading instrument by executing at market price.

How to Measure Order Execution Time using Amibroker GetPerformanceCounter Function

GetPerformanceCounter function can be a in trading system automation to measure time in milliseconds between various events. In our example we are going to use to measure the difference between the order request sent from Amibroker to the order confirmation response from the exchange via broker.

      _TRACE("Broker Server Request : "+ api_data);
      //enabling the counter before placing order request
      GetPerformanceCounter(True); // reset counter to zero  
      response = algomojo.AMDispatcher(apikey,apisecret,"PlaceOrder",api_data,broker,ver);
      //calculating the value of the counter after the order is placed
      elapsedtime = GetPerformanceCounter()/1000;
      _TRACEf("Broker Server Response : %s", response);
      _TRACE("Broker API Reponse Time : "+elapsedtime+" secs");

Here is the simple Amibroker AFL Code to measure the Order response time.

use the parameter button controls to send the trading orders and order execution response time is calculated and displayed in Amibroker log window

_SECTION_BEGIN("Calculate the Order Execution Time");

SetChartOptions(0, chartShowArrows | chartShowDates); //x-Axis will be plottted
//plot the candles
Plot(Close,"Close",colorDefault,GetPriceStyle() | styleNoTitle);

apikey = ParamStr("user_apikey","86cbef19e7e61ccee91e497690d5814e"); //Enter your API key here
apisecret = ParamStr("api_secret","2c674f6696f5527e40af1b052c262178"); //Enter your API secret key here

broker = Paramlist("Broker","ab|tj|zb|en|mt"); //Broker Short Code - ab - aliceblue, tj - tradejini, zb - zebu, en - enrich, an - angel broking, up - upstox, mt - master trust
ver = ParamStr("API Version","1.0");

trigger = ParamTrigger("PlaceOrder","Send Order");
clearlogs = ParamTrigger("Logs","CLEAR");

if(clearlogs)
{
_TRACE("!CLEAR!"); 
}


if(trigger)

{
//send order to the broker

api_data = "{
        \"strg_name\":\"Test Strategy\",
        \"s_prdt_ali\":\"BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML\",
        \"Tsym\":\"CRUDEOIL21MAYFUT\",
        \"exch\":\"MCX\",
        \"Ttranstype\":\"B\",
        \"Ret\":\"DAY\",
        \"prctyp\":\"MKT\",
        \"qty\":\"1\",
        \"discqty\":\"0\",
        \"MktPro\":\"NA\",
        \"Price\":\"0\",
        \"TrigPrice\":\"0\",
        \"Pcode\":\"MIS\",
        \"AMO\":\"NO\"
      }";
      
      algomojo=CreateObject("AMAMIBRIDGE.Main");
      
      _TRACE("Broker Server Request : "+ api_data);
      //enabling the counter before placing order request
      GetPerformanceCounter(True); // reset counter to zero  
      response = algomojo.AMDispatcher(apikey,apisecret,"PlaceOrder",api_data,broker,ver);
      //calculating the value of the counter after the order is placed
      elapsedtime = GetPerformanceCounter()/1000;
      _TRACEf("Broker Server Response : %s", response);
      _TRACE("Broker API Reponse Time : "+elapsedtime+" secs");
      //request to the Broker
      //_TRACE("Time is"+Now());
           

}

_SECTION_END();
Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

P-Signal Strategy Long Only Strategy – Amibroker AFL Code

This tutorial provides an overview of the P-Signal reversal strategy, a quantitative trading strategy that utilizes statistical parameters and error functions to generate probabilistic...
Rajandran R
2 min read

Reversal Finder – Tradingview to Amibroker AFL Code Conversion

Reversal Finder is designed to identify potential reversal signals in a stock's price action. It highlights specific bars that meet the criteria for a...
Rajandran R
2 min read

CM Williams VIX FIX Pinescript to Amibroker AFL Conversion

Thought of converting some of the top-rated pinescript codes to Amibroker AFL code. So started with CM Williams VIX FIX with 51,000+ ratings for...
Rajandran R
1 min read

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More