Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and 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. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in and Co-Creator of Algomojo (Algorithmic Trading Platform for DIY Traders)

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 Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and 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. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in and Co-Creator of Algomojo (Algorithmic Trading Platform for DIY Traders)

Sending Futures/Spot – Signals to Two-Legged Options Execution Module…

Thought of making an open-source Tradingview Pinescript code demanded by most of the option traders to control the cost, control the risk, and configure...
Rajandran R
16 min read

Sending Smart Futures/Spot – Signals to Two-Legged Options Execution…

Thought of making an open source AFL code demanded by most of the option traders to control the cost, to control the risk, and...
Rajandran R
12 min read

How to Send Orders from Google SpreadSheets using Algomojo…

You might think "Is it really possible to do send automated orders from Google Sheets? Are you crazy?" but hey thanks to Google Appscript...
Rajandran R
2 min read

Leave a Reply

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