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 Fetch Trading Account Balance using Amibroker

2 min read

Amibroker recently launched their Amibroker 6.4 version which comes with new Javascript engines with JSON native object support which is very much required to parse any JSON Strings. Most of the Trading API response is in JSON format this 6.4 upgrade will definitely bring more innovative features and code simplification in building intelligent execution strategies in the future.

Amibroker JSON Parsing – Tutorial using Algomojo API

Requirements

Amibroker 6.4 or higher version, Access to Algomojo API

Supported Brokers

Aliceblue, Tradejini, Zebu

Sample JSON Response for Limits API to fetch trading fund details

{
    "OpeningBalance": "30.52",
    "Netcashavailable": "31.52",
    "grossexposurevalue": "0.00",
    "segment": "ALL",
    "cncrealizedmtomprsnt": "-0.0",
    "valueindelivery": "0.00",
    "cncsellcreditpresent": "0.00",
    "cncunrealizedmtomprsnt": "-0.0",
    "deliverymarginprsnt": "0",
    "grossCollateral": "0",
    "BuyPower": "31.52",
    "PayoutAmt": "-0.00",
    "cncbrokerageprsnt": "0",
    "turnover": "0",
    "unrealisedmtom": "-0.00",
    "adhocscripmargin": "0.00",
    "specialmarginprsnt": "0",
    "BOmarginRequired": "0.00",
    "losslimit": "0",
    "IPOAmount": "-0.00",
    "elm": "0.00",
    "cdsspreadbenefit": "0.00",
    "realisedmtom": "-0.00",
    "category": "ONLINE",
    "nfospreadbenefit": "0.00",
    "Utilizedamount": "0.00",
    "exposuremargin": "0.00",
    "Adhoc": "1.00",
    "t1grossCollateral": "0",
    "directcollateralvalue": "0.000000",
    "StockValuation": "0.00",
    "credits": "31.52",
    "branchadhoc": "0.000000",
    "adhocmargin": "1.000000",
    "additionalmarginprsnt": "0",
    "spanmargin": "0.00",
    "premiumpresent": "0.00",
    "BookedPNL": "-0.00",
    "varmargin": "0.00",
    "marginScripBasketCustomPresent": "0",
    "tendermarginprsnt": "0",
    "cncmarginused": "0",
    "brokerageprsnt": "0",
    "UnbookedPNL": "-0.00",
    "debits": "0.00",
    "COMarginRequired": "0.00",
    "multiplier": "1.00",
    "stat": "Ok",
    "mfamount": "0.00",
    "scripbasketmargin": "0.00",
    "buyExposurePrsnt": "0",
    "notionalcash": "0",
    "additionalpreexpirymarginprsnt": "0",
    "cncMarginElmPrsnt": "0",
    "PayinAmt": "0.00",
    "cncMarginVarPrsnt": "0",
    "mfssamountused": "0.00",
    "sellExposurePrsnt": "0"
}

Amibroker AFL Codes for JSON Parsing Example to fetch Trading Account Balance.

//Parsing the Fund Balance using Algomojo API
_SECTION_BEGIN("JSON Parsing Example");

uid = ParamStr("Client ID","TS2499");
user_apikey = ParamStr("user_apikey","86cbef19e7e61ccee91e497690d5814e"); //Enter your API key here
api_secret = ParamStr("api_secret","eb4a0e79ff9e1bc8a05940cad8d2e94e"); //Enter your API secret key here
broker = ParamList("Broker","ab|tj|zb"); //Broker Short Code - ab - aliceblue, tj - tradejini, zb - zebu, en - enrich
ver = ParamStr("API Version","1.0");
trigger = ParamTrigger("Funds","Fetch Funds");

RequestTimedRefresh(1,False);

EnableScript("jscript");
<%
  function _Parse( json_string )
  {
	return JSON.parse( json_string );
  }
%>

function ParseJSON( json_string )
{
   script = GetScriptObject();
   return script._Parse( json_string );
}


function GetFunds()
{

algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data ="{\"uid\":\""+uid+"\",\"actid\":\""+uid+"\",\"segment\":\""+"ALL"+"\",\"Exchange\":\""+""+"\",\"product\":\""+""+"\"}";
resp=algomojo.AMDispatcher(user_apikey, api_secret,"Limits",api_data,broker,ver);
_TRACE(" Account Balance Response  : " +resp);
return resp;
}



if(Now(4)%30==0)  //access funds every 30 seconds
{
obj = ParseJson(GetFunds());
StaticVarSetText("Balance",obj.OpeningBalance);
StaticVarSetText("Cash",obj.Netcashavailable);
StaticVarSetText("Turnover",obj.turnover);
_TRACE("Account Balance :"+StaticVarGetText("Balance"));
_TRACE("Cash Available :"+StaticVarGetText("Cash"));
_TRACE("Turnover :"+StaticVarGetText("Turnover"));
}

_SECTION_END();

Amibroker Log Output

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.

Introduction to QuantZilla 2.0

QuantZilla is a 75+ hours of immersive coding mentor-ship program on designing trading systems, converting trading ideas into indicators and trading strategies, automating the...
Rajandran R
4 min read

How to Install Amibroker and Ninjatrader on MacBook Air

This is a routine question we get from Mac users and in this tutorial, we will be learning how to install Windows 11 inside...
Rajandran R
49 sec read

Automated Trading Landscape of Indian Markets

Automated trading or rule-based trading is something not new for Indian retail users. And all thanks to those modern technology-driven brokers who brought API-based...
Rajandran R
5 min read

Leave a Reply

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