Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

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 Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Angel Broking Multi-Client Amibroker Trading Module

Got frequent requirements from Angel Broking Customers to automate Amibroker based trading system where one single generation from charts could punch orders in multiple...
Rajandran R
6 min read

Option Button Trading to Trade ATM, ITM, OTM Options…

Here is a simple button trading module which can be used to send option trading order with buttons. Users can configure the button to...
Rajandran R
7 min read

Multi Broker – Multi Client Algomojo Trading – Amibroker…

Here are simple Amibroker AFL - Button-based Trading modules which help traders to route their orders to different brokers simultaneously. Here is the prototype...
Rajandran R
4 min read

Leave a Reply

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