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 use Amibroker to Parse the Position Book in Algomojo Trading Platform

4 min read

This is a simple tutorial that explains how to access the position book using Amibroker and get various details about the position book details to use further in your trading system.

Supported Brokers : Aliceblue, Tradejini, Zebu

What are the parameters one can get from Position Book?

From the Position Book one can get various details about the open positions. However here are some of the most important and most common extracted information from the position book by automated traders.

1.unrealisedprofitloss
2.realisedprofitloss
3.Exchange
4.Netqty
5.Buyavgprc
6.Sellavgprc
7.MtoM
8.Company Name

Sample PositionBook Response

[
    {
        "Fillsellqty": "0",
        "realisedprofitloss": "0.00",
        "NetSellavgprc": "0.00",
        "Opttype": "XX",
        "Exchangeseg": "nse_cm",
        "Netqty": "3",
        "CFBuyavgprc": "0.00",
        "sSqrflg": "Y",
        "netbuyamt": "824.55",
        "Tsym": "EIHAHOTELS-EQ",
        "Netamt": "-824.55",
        "netsellqty": "0",
        "Sqty": "0",
        "FillsellamtCF": "0.00",
        "NetBuyavgprc": "274.85",
        "MtoM": "16.20",
        "Symbol": "EIHAHOTELS",
        "posflag": "true",
        "Series": "EQ",
        "netSellamt": "0.00",
        "Fillbuyamt": "824.55",
        "unrealisedprofitloss": "16.20",
        "PriceNumerator": "1",
        "Type": "DAY1",
        "netbuyqty": "3",
        "BLQty": 1,
        "Fillbuyqty": "3",
        "CFsellqty": "0",
        "s_NetQtyPosConv": "N",
        "CFbuyqty": "0",
        "Sellavgprc": "0.00",
        "Bqty": "3",
        "Exchange": "NSE",
        "CFSellavgprc": "0.00",
        "Fillsellamt": "0.00",
        "actid": "TS2499",
        "GeneralDenomenator": "1",
        "Instname": "NA",
        "discQty": "10",
        "Expdate": "NA",
        "LTP": "280.25",
        "Buyavgprc": "274.85",
        "Token": "15399",
        "GeneralNumerator": "1",
        "companyname": "EIH ASSOCIATED HOTELS LTD",
        "stat": "Ok",
        "PriceDenomenator": "1",
        "FillbuyamtCF": "0.00",
        "Stikeprc": "0",
        "BEP": "274.85",
        "Pcode": "MIS"
    },
    {
        "Fillsellqty": "1",
        "realisedprofitloss": "0.00",
        "NetSellavgprc": "106.60",
        "Opttype": "XX",
        "Exchangeseg": "nse_cm",
        "Netqty": "-1",
        "CFBuyavgprc": "0.00",
        "sSqrflg": "Y",
        "netbuyamt": "0.00",
        "Tsym": "L&TFH-EQ",
        "Netamt": "106.60",
        "netsellqty": "1",
        "Sqty": "1",
        "FillsellamtCF": "0.00",
        "NetBuyavgprc": "0.00",
        "MtoM": "-0.30",
        "Symbol": "L&TFH",
        "posflag": "true",
        "Series": "EQ",
        "netSellamt": "106.60",
        "Fillbuyamt": "0.00",
        "unrealisedprofitloss": "-0.30",
        "PriceNumerator": "1",
        "Type": "DAY1",
        "netbuyqty": "0",
        "BLQty": 1,
        "Fillbuyqty": "0",
        "CFsellqty": "0",
        "s_NetQtyPosConv": "N",
        "CFbuyqty": "0",
        "Sellavgprc": "106.60",
        "Bqty": "0",
        "Exchange": "NSE",
        "CFSellavgprc": "0.00",
        "Fillsellamt": "106.60",
        "actid": "TS2499",
        "GeneralDenomenator": "1",
        "Instname": "NA",
        "discQty": "10",
        "Expdate": "NA",
        "LTP": "106.90",
        "Buyavgprc": "0.00",
        "Token": "24948",
        "GeneralNumerator": "1",
        "companyname": "L&T FINANCE HOLDINGS LTD",
        "stat": "Ok",
        "PriceDenomenator": "1",
        "FillbuyamtCF": "0.00",
        "Stikeprc": "0",
        "BEP": "106.60",
        "Pcode": "MIS"
    }]

One can get the detailed positionbook API response from Algomojo Documentation

To Build this module we need 2 components

1)Header PositionBook Module that needs to be placed inside the Amibroker\Formulas\Include folder
2)Main Module which send the data to PositionBook Module for Parsing

Header Module

Copy the Header Execution Module to Amibroker\Formulas\include folder. Save the AFL under the name positionbook.afl

//store the file under the name - PositionBook.afl under the Amibroker\Formulas\Include folder

uid = ParamStr("Client ID","TS2499");
user_apikey = ParamStr("user_apikey","86cbef19e7e61ccee91e497690d5814e"); //Enter your API key here
api_secret = ParamStr("api_secret","8e8b207de43446c65f379bf2145b62fc"); //Enter your API secret key here
s_prdt_ali = "BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML";
broker = ParamStr("Broker","tj"); //Broker Short Code - ab - aliceblue, tj - tradejini, zb - zebu, en - enrich
ver = ParamStr("API Version","1.0");

RequestTimedRefresh(1,False);

function GetPositionsBook()
{

algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data ="{\"uid\":\""+uid+"\",\"actid\":\""+uid+"\",\"type\":\""+"NET"+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\"}";
resp=algomojo.AMDispatcher(user_apikey, api_secret,"PositionBook",api_data,broker,ver);
_TRACE(" Position Book  : " +resp);
return resp;
}

function JsonParse(resp,Tsym,Pcode,stringinfo)
{

//Initialization
flag = 0;
possym = "";
posNetqty =0;


for( item = 0; ( sym = StrExtract( resp, item,'{' )) != ""; item++ )
{

sym = StrTrim(sym," ");
Tsym = StrTrim(Tsym," ");

if(StrFind(sym,Tsym) AND StrFind(sym,Pcode)) //Matches the symbol and //Matches the Order Type
{

flag = 1; //turn on the flag

data = sym;



for( jitem = 0; ( posdetails = StrExtract( data, jitem,',' )) != ""; jitem++ )
{
  
  if(StrFind(posdetails,"\""+stringinfo+"\""))
  {
   posdetails = StrExtract(posdetails,1,':');
   _TRACE("\n"+stringinfo+" : "+StrTrim(posdetails,"\""));
  }
  

} //end of for loop
}

}//end of for loop

}

Main Module

Copy the Main Execution Module to Amibroker\Formulas\Algomojo Platform. Save the AFL under the name PositonBook Json Parsing.afl

#include <PositionBook.afl>


Symbol = ParamStr("Symbol","EIHAHOTELS-EQ");
Pcode = ParamList("Pcode","NRML|CO|MIS",2);
trigger = ParamTrigger("Trigger Button","Get PositionBook");

if(trigger)
{
resp = GetPositionsBook();
JsonParse(resp,Symbol,Pcode,"unrealisedprofitloss"); //output in string format
JsonParse(resp,Symbol,Pcode,"realisedprofitloss"); //output in string format
JsonParse(resp,Symbol,Pcode,"Exchange"); //output in string format
JsonParse(resp,Symbol,Pcode,"Netqty"); //output in string format
JsonParse(resp,Symbol,Pcode,"Buyavgprc"); //output in string format
JsonParse(resp,Symbol,Pcode,"Sellavgprc"); //output in string format
JsonParse(resp,Symbol,Pcode,"companyname"); //output in string format
JsonParse(resp,Symbol,Pcode,"MtoM"); //output in string format

}

Enable the Logging Feature in Amibroker to check the output in the Amibroker Log window

Sample Output

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