Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, 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. Building Algo Platforms, 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 Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, 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. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Interactive Brokers – Smart Order Chart Trading Module using…

The IB Controller is an interface designed to facilitate automatic trading with AmiBroker and Interactive Brokers (IB) TWS (Trader Workstation). It serves as a...
Rajandran R
8 min read

Introducing OpenAlgo V1.0: The Ultimate Open-Source Algorithmic Trading Framework…

OpenAlgo V1.0 is an open-source algorithmic trading platform to automate your trading strategies using Amibroker, Tradingview, Metatrader, Python etc. Designed with the modern trader...
Rajandran R
2 min read

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

Leave a Reply

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