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