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

Multi Broker – Multi Client Algomojo Trading – Amibroker AFL Code

7 min read

Here are simple Amibroker AFL – Button-based Trading modules which help traders to route their orders to different brokers simultaneously. Here is the prototype execution module to route orders to multiple brokers for multiple clients.

Who can make use of this module?

Any traders who have access to Amibroker and access to Algomojo Trading Platform can trigger their buy & sell signal generation from their trading system to multiple brokers and multiple accounts (friends & family). i.e one Amibroker to fire orders in multiple accounts + multiple brokers.

Which brokers are supported?

Current module supports Aliceblue, Angel Broking Tradejini, Zebu, Upstox, Enrich and Mastertrust

How many accounts one can connect?

Currently there is no restriction on the number of clients one can connect.

Multiple Broker – Multi Client Account Controls – AFL Coding Block

Multi-client account control block contains the configuration of multiple brokers, multiple clients, trading symbols, and control to configure a number of trading accounts. If more clients need to be added then here is the sample format. Client details need to be configured in order to send multiple client accounts.

_SECTION_BEGIN("Multi Client Account Controls - Algomojo");

TotalAccounts = Param("Total Accounts",4,1,25,1);

ClientId1 = ParamStr("ClientId1","TS24xx");
Broker1 = ParamStr("Broker1","tj");
ApiKey1 =  ParamStr("ApiKey1","86cbef19e7e61ccee91e49763434234290d5814e");
ApiSecret1 =  ParamStr("ApiSecret1","2c674f6696f5527e40af1b34234");
TradingSymbol1 = ParamStr("TradingSymbol1","SBIN-EQ");
Quantity1 = ParamStr("Quantity1","5");
TokenNo1 = ParamStr("TokenNo1","");  //Requires only in case of Angel Broking

ClientId2 = ParamStr("ClientId2","AB043477");
Broker2 = ParamStr("Broker2","ab");
ApiKey2 =  ParamStr("ApiKey2","e53434343ed1103e5b0c4a8");
ApiSecret2 =  ParamStr("ApiSecret2","e5397d13434348537f50c16ca");
TradingSymbol2 = ParamStr("TradingSymbol2","SBIN-EQ");
Quantity2 = ParamStr("Quantity2","15");
TokenNo2 = ParamStr("TokenNo2","");  //Requires only in case of Angel Broking

ClientId3 = ParamStr("ClientId3","331555");
Broker3 = ParamStr("Broker3","up");
ApiKey3 =  ParamStr("ApiKey3","781981bf45343364e8ef30");
ApiSecret3 =  ParamStr("ApiSecret3","02aa902dbe344f7a88af59e0aa7e5d01");
TradingSymbol3 = ParamStr("TradingSymbol3","SBIN");
Quantity3 = ParamStr("Quantity3","25");
TokenNo3 = ParamStr("TokenNo3","");  //Requires only in case of Angel Broking

ClientId4 = ParamStr("ClientId4","K1903453");
Broker4 = ParamStr("Broker4","an");
ApiKey4 =  ParamStr("ApiKey4","b48e4db30e445334538c03eb4ee26");
ApiSecret4 =  ParamStr("ApiSecret4","fc3ed44d3453453546f9ff2e3ae003a");
TradingSymbol4 = ParamStr("TradingSymbol4","SBIN-EQ");
Quantity4 = ParamStr("Quantity4","35");
TokenNo4 = ParamStr("TokenNo4","");  //Requires only in case of Angel Broking

_SECTION_END();

Parameter Based Button Trading Module – Test Module to Check Multi Broker – Multi Client Trade Execution

_SECTION_BEGIN("Multi Client Account Controls - Algomojo");

TotalAccounts = Param("Total Accounts",4,1,25,1);

ClientId1 = ParamStr("ClientId1","TS24xx");
Broker1 = ParamStr("Broker1","tj");
ApiKey1 =  ParamStr("ApiKey1","86cbef19e7e61ccee91e49763434234290d5814e");
ApiSecret1 =  ParamStr("ApiSecret1","2c674f6696f5527e40af1b34234");
TradingSymbol1 = ParamStr("TradingSymbol1","SBIN-EQ");
Quantity1 = ParamStr("Quantity1","5");
TokenNo1 = ParamStr("TokenNo1","");  //Requires only in case of Angel Broking

ClientId2 = ParamStr("ClientId2","AB043477");
Broker2 = ParamStr("Broker2","ab");
ApiKey2 =  ParamStr("ApiKey2","e53434343ed1103e5b0c4a8");
ApiSecret2 =  ParamStr("ApiSecret2","e5397d13434348537f50c16ca");
TradingSymbol2 = ParamStr("TradingSymbol2","SBIN-EQ");
Quantity2 = ParamStr("Quantity2","15");
TokenNo2 = ParamStr("TokenNo2","");  //Requires only in case of Angel Broking

ClientId3 = ParamStr("ClientId3","331555");
Broker3 = ParamStr("Broker3","up");
ApiKey3 =  ParamStr("ApiKey3","781981bf45343364e8ef30");
ApiSecret3 =  ParamStr("ApiSecret3","02aa902dbe344f7a88af59e0aa7e5d01");
TradingSymbol3 = ParamStr("TradingSymbol3","SBIN");
Quantity3 = ParamStr("Quantity3","25");
TokenNo3 = ParamStr("TokenNo3","");  //Requires only in case of Angel Broking

ClientId4 = ParamStr("ClientId4","K1903453");
Broker4 = ParamStr("Broker4","an");
ApiKey4 =  ParamStr("ApiKey4","b48e4db30e445334538c03eb4ee26");
ApiSecret4 =  ParamStr("ApiSecret4","fc3ed44d3453453546f9ff2e3ae003a");
TradingSymbol4 = ParamStr("TradingSymbol4","SBIN-EQ");
Quantity4 = ParamStr("Quantity4","35");
TokenNo4 = ParamStr("TokenNo4","");  //Requires only in case of Angel Broking

_SECTION_END();



_SECTION_BEGIN("Multi Broker Common Module");


//Common Mapping
Exchange = ParamList("Exchange","NFO|NSE|BSE|CDS|MCX",1);  //Common Mapping
Retention = ParamStr("Retention","DAY");
Product = ParamList("Product Type","MARKET|LIMIT",0); // Pricetype
OrderType = ParamList("Product code","CNC|NRML|MIS",0); // CNC - Cash  and Carry, NRML - Normal, MIS - Margin Intraday Squareoff
AMO = "NO";
ver = ParamStr("API Version","1.0");
tradedelay = Param("Execution Delay",0,0,1); // 0 - Execution with No Delay after a signal, 1- Execution at the end of the candle
stgy_name = ParamStr("Strategy Name","Multi Broker Execution"); // Strategy Name
EnableAlgo = ParamList("Algo Mode","Disable|Enable|LongOnly|ShortOnly",0); // Algo Mode


//Trigger Controls
PlaceBuyOrder = ParamTrigger("PlaceBuyOrder","PRESS");
PlaceSellOrder = ParamTrigger("PlaceSellOrder","PRESS");



//Initialization
resp = "";




//Static Varibales for Order Protection

static_name_ = Name()+GetChartID()+interval(2)+stgy_name;
static_name_algo = Name()+GetChartID()+interval(2)+stgy_name+"algostatus";
//StaticVarSet(static_name_algo, -1); 


//Algo Dashboard

GfxSelectFont( "BOOK ANTIQUA", 14, 100 );
GfxSetBkMode( 1 );
if(EnableAlgo == "Enable")
{
AlgoStatus = "Algo Enabled";
GfxSetTextColor( colorGreen ); 
GfxTextOut( "Algostatus : "+AlgoStatus +" ChartID = " +GetChartID() , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=1)
{
_TRACE("Algo Status : Enabled"+" ChartID = "+GetChartID());
StaticVarSet(static_name_algo, 1);
}
}
if(EnableAlgo == "Disable")
{
AlgoStatus = "Algo Disabled";
GfxSetTextColor( colorRed ); 
GfxTextOut( "Algostatus : "+AlgoStatus+" ChartID = "+GetChartID() , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=0)
{
_TRACE("Algo Status : Disabled"+" ChartID = "+GetChartID());
StaticVarSet(static_name_algo, 0);
}
}
if(EnableAlgo == "LongOnly")
{
AlgoStatus = "Long Only";
GfxSetTextColor( colorYellow ); 
GfxTextOut( "Algostatus : "+AlgoStatus+" ChartID = "+GetChartID() , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=2)
{
_TRACE("Algo Status : Long Only");
StaticVarSet(static_name_algo, 2);
}
}
if(EnableAlgo == "ShortOnly")
{
AlgoStatus = "Short Only";
GfxSetTextColor( colorYellow ); 
GfxTextOut( "Algostatus : "+AlgoStatus+" ChartID = "+GetChartID() , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=3)
{
_TRACE("Algo Status : Short Only");
StaticVarSet(static_name_algo, 3);
}
}


function apidata(i,Action){


	
	if(VarGetText("Broker"+i)=="an")
	{
	price = "0";
	triggerprice = "0";
	
	if(OrderType == "CNC")
	{
	producttype = "DELIVERY";
	} 
	if(OrderType == "NRML")
	{
	producttype = "CARRYFORWARD";
	} 
	if(OrderType == "MIS")
	{
	producttype = "INTRADAY";
	} 
	variety = "NORMAL"; 
	
	if( Action =="B")
	{
	
	order = "BUY";
	
	}
	
	if( Action =="S")
	{
	
	order = "SELL";
	
	}
	
	
	
	api_data = "{\"stgy_name\":\""+stgy_name+"\",\"disclosedquantity\":\""+"0"+"\",\"duration\":\""+Retention+"\",\"exchange\":\""+Exchange+"\",\"ordertype\":\""+Product+"\",\"price\":\""+price+"\",\"producttype\":\""+producttype+"\",\"quantity\":\""+VarGetText("Quantity"+i)+"\",\"squareoff\":\""+"0"+"\",\"stoploss\":\""+"0"+"\",\"symboltoken\":\""+VarGetText("TokenNo"+i)+"\",\"tradingsymbol\":\""+VarGetText("TradingSymbol"+i)+"\",\"trailingStopLoss\":\""+"0"+"\",\"transactiontype\":\""+order+"\",\"triggerprice\":\""+triggerprice+"\",\"variety\":\""+variety+"\"}";
	VarSetText("ApiData"+i,api_data);
	
	}
	if(VarGetText("Broker"+i)=="up")
	{
	
	
	
	if( exchange == "NSE")
	{
	exch = "NSE_EQ";
	}
	
	if( exchange == "NFO")
	{
	exch = "NSE_FO";
	}
	
	if( exchange == "CDS")
	{
	exch = "NCD_FO";
	}
	
	if( exchange == "MCX")
	{
	exch = "MCX_FO";
	}
	
	Price = "0";
	TrigPrice = "0";
	
	if(Product == "MARKET")
	{
	prctype = "MKT";
	}
	if(Product == "LIMIT")
	{
	prctype = "LMT";
	}
	
	api_data = "{\"strg_name\":\""+stgy_name+"\",\"symbol\":\""+VarGetText("TradingSymbol"+i)+"\",\"exchange\":\""+exch+"\",\"transaction_type\":\""+Action+"\",\"duration\":\""+Retention+"\",\"order_type\":\""+prctype+"\",\"quantity\":\""+VarGetText("Quantity"+i)+"\",\"disclosed_quantity\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"price\":\""+Price+"\",\"trigger_price\":\""+TrigPrice+"\",\"product\":\""+OrderType+"\",\"is_amo\":\""+AMO+"\"}";
	VarSetText("ApiData"+i,api_data);
	}
    
    if(VarGetText("Broker"+i)=="ab" || VarGetText("Broker"+i)=="mt" || VarGetText("Broker"+i)=="tj" 
			|| VarGetText("Broker"+i)=="en" || VarGetText("Broker"+i)=="tj")
	{
	s_prdt_ali = "BO:BO|CNC:CNC|CO:CO|MIS:MIS|NRML:NRML"; //Product Alias
	if(Product == "MARKET")
	{
	prctype = "MKT";
	}
	if(Product == "LIMIT")
	{
	prctype = "LMT";
	}
	
	api_data = "{\"stgy_name\":\""+stgy_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+VarGetText("TradingSymbol"+i)+"\",\"exch\":\""+Exchange+"\",\"Ttranstype\":\""+Action+"\",\"Ret\":\""+Retention+"\",\"prctyp\":\""+prctype+"\",\"qty\":\""+VarGetText("Quantity"+i)+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+OrderType+"\",\"AMO\":\""+AMO+"\"}";
	VarSetText("ApiData"+i,api_data);
	}
    
    
   }





if (PlaceBuyOrder) {
	algomojo=CreateObject("AMAMIBRIDGE.Main");
	
	for(i=1;i<=TotalAccounts;i++)
	{
    apidata(i,"B");     
    _TRACE("Client ID : "+VarGetText("ClientId"+i)+ " API Request : "+VarGetText("ApiData"+i));  
    resp=algomojo.AMDispatcher(VarGetText("ApiKey"+i), VarGetText("ApiSecret"+i),"PlaceOrder",VarGetText("ApiData"+i),VarGetText("Broker"+i),ver);
    _TRACE("Client ID : "+VarGetText("ClientId"+i)+ " Broker Server Response : "+resp);
    }
}


if (PlaceSellOrder) {
	algomojo=CreateObject("AMAMIBRIDGE.Main");
	
	for(i=1;i<=TotalAccounts;i++)
	{
    apidata(i,"S");   
    _TRACE("Client ID : "+VarGetText("ClientId"+i)+ " API Request : "+VarGetText("ApiData"+i));  
    resp=algomojo.AMDispatcher(VarGetText("ApiKey"+i), VarGetText("ApiSecret"+i),"PlaceOrder",VarGetText("ApiData"+i),VarGetText("Broker"+i),ver);
    _TRACE("Client ID : "+VarGetText("ClientId"+i)+ " Broker Server Response : "+resp);
    }
}



_SECTION_END();

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();
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