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 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 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

Line Trading – OpenAlgo Automation Module for Amibroker Users

The line Trading Automation tool is designed for Manual traders who want to perform level-based trade execution faster and also bring some advanced trade...
Rajandran R
55 sec read

Mini Certification Course on Algorithmic Trading

Welcome to the Mini Certification on Algorithmic Trading using Amibroker and OpenAlgo! This comprehensive 4-part series is designed to equip you with the knowledge...
Rajandran R
1 min read

Pivot Reversal Strategy – Tradingview Pinescript to Amibroker AFL…

Pivot Reversal Strategy is a popular in-built strategy in Tradingview Platform. Here I had attempted to convert the pinescript v5 to Amibroker AFL Code....
Rajandran R
3 min read

Leave a Reply

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