Single Broker Multi-Client Amibroker Chart based Trade Execution Module

Got frequent requirements from many traders to automate Amibroker based trading system where one single generation from charts could punch orders in multiple accounts (friends & family). Here is the readymade execution module one can plugin with their trading system to send multi orders and easily configure multi-client under the same broker.

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 accounts (friends & family). i.e one Amibroker to fire orders in multiple accounts.

Which brokers are supported?

Current module supports Aliceblue, Tradejini, Zebu, Enrich and Mastertrust

How many accounts one can connect?

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

Multi Client Account Controls – AFL Coding Block

Multi-client account control block contains the configuration of 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);
TradingSymbol = ParamStr("TradingSymbol","SBIN-EQ");
Broker = Paramlist("Broker","tj|ab|zb|mt|en",0);  //Broker Short Code ab - aliceblue, tj - tradejini, zb - zebu, mt-mastertrust, en- enrich

//Client 1 Details
ClientId1 = ParamStr("ClientId1","TS2499");
ApiKey1 =  ParamStr("ApiKey1","86cbef19e7e61ccee91e497690d54455");
ApiSecret1 =  ParamStr("ApiSecret1","2c674f6696f5527e40af1b052454455");
Quantity1 = ParamStr("Quantity1","5");

//Client 2 Details
ClientId2 = ParamStr("ClientId2","TZ2322");
ApiKey2=  ParamStr("ApiKey2","76cbef19e7e61ccee91e497690d54456");
ApiSecret2 =  ParamStr("ApiSecret2","2c674f6696f5527e40af1b052454455");
Quantity2 = ParamStr("Quantity2","10");

//Client 3 Details
ClientId3 = ParamStr("ClientId3","TM4433");
ApiKey3 =  ParamStr("ApiKey3","16cbef19e7e61ccee91e497690d54457");
ApiSecret3 =  ParamStr("ApiSecret3","2c674f6696f5527e40af1b052454455");
Quantity3 = ParamStr("Quantity3","15");

//Client 4 Details
ClientId4 = ParamStr("ClientId4","TM4435");
ApiKey4 =  ParamStr("ApiKey4","26cbef19e7e61ccee91e497690d54458");
ApiSecret4 =  ParamStr("ApiSecret4","2c674f6696f5527e40af1b052454455");
Quantity4 = ParamStr("Quantity4","25");


_SECTION_END();

Chased Based Multi Client Trading Module Amibroker AFL

This module is a ready-made module that can be connected along with Amibroker based trading system already configured for Buy, Sell, Short, Cover Conditions.


//Single Broker - Multi Client Algomojo Trading AFL
//Coded by Algomojo
//Version 1.0
//Date : 02nd May 2021
//Website : www.algomojo.com

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

TotalAccounts = Param("Total Accounts",4,1,25,1);
TradingSymbol = ParamStr("TradingSymbol","SBIN-EQ");
Broker = Paramlist("Broker","tj|ab|zb|mt|en",0);  //Broker Short Code ab - aliceblue, tj - tradejini, zb - zebu, mt-mastertrust, en- enrich

//Client 1 Details
ClientId1 = ParamStr("ClientId1","TS2499");
ApiKey1 =  ParamStr("ApiKey1","86cbef19e7e61ccee91e497690d54455");
ApiSecret1 =  ParamStr("ApiSecret1","2c674f6696f5527e40af1b052454455");
Quantity1 = ParamStr("Quantity1","5");

//Client 2 Details
ClientId2 = ParamStr("ClientId2","TZ2322");
ApiKey2=  ParamStr("ApiKey2","76cbef19e7e61ccee91e497690d54456");
ApiSecret2 =  ParamStr("ApiSecret2","2c674f6696f5527e40af1b052454455");
Quantity2 = ParamStr("Quantity2","10");

//Client 3 Details
ClientId3 = ParamStr("ClientId3","TM4433");
ApiKey3 =  ParamStr("ApiKey3","16cbef19e7e61ccee91e497690d54457");
ApiSecret3 =  ParamStr("ApiSecret3","2c674f6696f5527e40af1b052454455");
Quantity3 = ParamStr("Quantity3","15");

//Client 4 Details
ClientId4 = ParamStr("ClientId4","TM4435");
ApiKey4 =  ParamStr("ApiKey4","26cbef19e7e61ccee91e497690d54458");
ApiSecret4 =  ParamStr("ApiSecret4","2c674f6696f5527e40af1b052454455");
Quantity4 = ParamStr("Quantity4","25");


_SECTION_END();

_SECTION_BEGIN("Single Broker Multi Client Charting Module");


//Common Mapping
Exchange = ParamList("Exchange","NFO|NSE|BSE|CDS|MCX",1);  //Common Mapping
Retention = ParamStr("Retention","DAY");
Product = ParamList("Product Type","MKT|LMT",0); // Pricetype
OrderType = ParamList("Product code","CNC|NRML|MIS",2); // 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



//Initialization
resp = ""; //api response


//Configure Trade Execution Delay


AlgoBuy = lastvalue(Ref(Buy,-tradedelay));
AlgoSell = lastvalue(Ref(Sell,-tradedelay));
AlgoShort = lastvalue(Ref(Short,-tradedelay));
AlgoCover = lastvalue(Ref(Cover,-tradedelay));






//Static Variables 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 , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=1)
{
_TRACE("Algo Status : Enabled");
StaticVarSet(static_name_algo, 1);
}
}
if(EnableAlgo == "Disable")
{
AlgoStatus = "Algo Disabled";
GfxSetTextColor( colorRed ); 
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=0)
{
_TRACE("Algo Status : Disabled");
StaticVarSet(static_name_algo, 0);
}
}
if(EnableAlgo == "LongOnly")
{
AlgoStatus = "Long Only";
GfxSetTextColor( colorYellow ); 
GfxTextOut( "Algostatus : "+AlgoStatus , 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 , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=3)
{
_TRACE("Algo Status : Short Only");
StaticVarSet(static_name_algo, 3);
}
}

function apidata(Accounts,Action,multiplier){


	head = "{\"orders\" : [";
	tail = "  ]}";
	body = "";
	
	
	for(i=1;i<=Accounts;i++)
	{
	qty = int(StrToNum(VarGetText("Quantity"+i))*multiplier);
	msg = "{   \"order_refno\":\""+i+"\",   \"user_apikey\":\""+VarGetText("ApiKey"+i)+"\", \"api_secret\":\""+VarGetText("ApiSecret"+i)+"\", \"strg_name\": \"Test Strategy\", \"s_prdt_ali\":\"BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML\", \"Tsym\":\""+TradingSymbol+"\", \"exch\":\""+Exchange+"\", \"Ttranstype\":\""+Action+"\", \"Ret\":\"DAY\", \"prctyp\":\"MKT\", \"qty\":\""+qty+"\", \"discqty\":\"0\", \"MktPro\":\"NA\", \"Price\":\"0\", \"TrigPrice\":\"0\", \"Pcode\":\""+OrderType+"\", \"AMO\":\"NO\" }";
                
		if(i==1)
		{
		body = msg; 
		}    
		else
		{
		body = body + "," + msg;
		}   
		}
    api_data = head + body + tail;
	return api_data;
	
}

//Buy and Sell Order Functions

function Buyorder(multiplier)
{
	
    algomojo=CreateObject("AMAMIBRIDGE.Main");
	api = apidata(TotalAccounts,"B",multiplier);     
    _TRACE("Broker API Data  "+api);
    GetPerformanceCounter(True);
    resp=algomojo.AMDispatcher(ApiKey1, ApiSecret1,"PlaceMultiOrder",api,Broker,ver);
    elapsed = GetPerformanceCounter()/1000;
    _TRACE("Execution Time : "+elapsed);
    _TRACE("Broker API Response  "+resp);
    _TRACE("Execution Period : "+elapsed);
}



function sellorder(multiplier)
{
    algomojo=CreateObject("AMAMIBRIDGE.Main");
	api = apidata(TotalAccounts,"S",multiplier);  
    _TRACE("Broker API Data  "+api);
    GetPerformanceCounter(True);
    resp=algomojo.AMDispatcher(ApiKey1, ApiSecret1,"PlaceMultiOrder",api,Broker,ver);
    elapsed = GetPerformanceCounter()/1000;
    _TRACE("Execution Time : "+elapsed);
    _TRACE("Broker API Response  "+resp);
    _TRACE("Execution Period : "+elapsed);
}


//Execution Module

if(EnableAlgo != "Disable")
    {
        lasttime = StrFormat("%0.f",LastValue(BarIndex()));
        
        SetChartBkColor(colorDarkGrey);
        if(EnableAlgo == "Enable")
        {   
            if (AlgoBuy==True AND AlgoCover == True AND StaticVarGet(static_name_+"buyCoverAlgo")==0 AND StaticVarGetText(static_name_+"buyCoverAlgo_barvalue") != lasttime )
            {
            // reverse Long Entry 
                buyorder(2);
                StaticVarSetText(static_name_+"buyCoverAlgo_barvalue",lasttime);  
                StaticVarSet(static_name_+"buyCoverAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name()+ "Signal = Buy AND Cover");
        
            }
            else if ((AlgoBuy != True OR AlgoCover != True))
            {   
                StaticVarSet(static_name_+"buyCoverAlgo",0);
            }
            
            if (AlgoBuy==True AND AlgoCover != True AND StaticVarGet(static_name_+"buyAlgo")==0 AND StaticVarGetText(static_name_+"buyAlgo_barvalue") != lasttime)
            {
            // Long Entry 
                buyorder(1);
                StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"buyAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name()+ "Signal = Buy");
            }
            else if (AlgoBuy != True)
            {   
                StaticVarSet(static_name_+"buyAlgo",0);
                
            }
            if (AlgoSell==true AND AlgoShort != True AND StaticVarGet(static_name_+"sellAlgo")==0 AND StaticVarGetText(static_name_+"sellAlgo_barvalue") != lasttime)
            {     
            // Long Exit 
                sellorder(1);
                StaticVarSetText(static_name_+"sellAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"sellAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name()+ "Signal = Sell");
            }
            else if (AlgoSell != True )
            {   
                StaticVarSet(static_name_+"sellAlgo",0);
            }
            if (AlgoShort==True AND AlgoSell==True AND  StaticVarGet(static_name_+"ShortSellAlgo")==0 AND StaticVarGetText(static_name_+"ShortSellAlgo_barvalue") != lasttime)
            {
            // reverse Short Entry 
                sellorder(2);
                StaticVarSetText(static_name_+"ShortsellAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"ShortSellAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name()+ "Signal = Short and Sell");
            }
            else if ((AlgoShort != True OR AlgoSell != True))
            {   
                StaticVarSet(static_name_+"ShortSellAlgo",0);
            }
                
            if (AlgoShort==True  AND  AlgoSell != True AND StaticVarGet(static_name_+"ShortAlgo")==0 AND  StaticVarGetText(static_name_+"ShortAlgo_barvalue") != lasttime)
            {
            // Short Entry
                sellorder(1);
                StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"ShortAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name()+ "Signal = Short");
            }
            else if (AlgoShort != True )
            {   
                StaticVarSet(static_name_+"ShortAlgo",0);
            }
            if (AlgoCover==true AND AlgoBuy != True AND StaticVarGet(static_name_+"CoverAlgo")==0 AND StaticVarGetText(static_name_+"CoverAlgo_barvalue") != lasttime)
            {
            // Short Exit
                buyorder(1);
                StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"CoverAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name()+ "Signal = Cover");
            }
            else if (AlgoCover != True )
            {   
                StaticVarSet(static_name_+"CoverAlgo",0);
            }
        }
        
        
        
        else if(EnableAlgo == "LongOnly")
        {
            
            if (AlgoBuy==True AND StaticVarGet(static_name_+"buyAlgo")==0 AND StaticVarGetText(static_name_+"buyAlgo_barvalue") != lasttime)
            {  
            //  Long Entry
                buyorder(1);
                StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"buyAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name()+ "Signal = Buy");
            }
            else if (AlgoBuy != True)
            {   
                StaticVarSet(static_name_+"buyAlgo",0);
            }
            if (AlgoSell==true AND StaticVarGet(static_name_+"sellAlgo")==0 AND StaticVarGetText(static_name_+"sellAlgo_barvalue") != lasttime)
            {  
            // Long Exit
                sellorder(1);
                StaticVarSetText(static_name_+"sellAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"sellAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name()+ "Signal = Sell");
            }
            else if (AlgoSell != True )
            {   
                StaticVarSet(static_name_+"sellAlgo",0);
            }
        }
        else if(EnableAlgo == "ShortOnly")
        {
            if (AlgoShort==True AND StaticVarGet(static_name_+"ShortAlgo")==0 AND StaticVarGetText(static_name_+"ShortAlgo_barvalue") != lasttime)
            {
            // Short Entry
                sellorder(1);
                StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"ShortAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name()+ "Signal = Short");
            }
            else if (AlgoShort != True )
            {   
                StaticVarSet(static_name_+"ShortAlgo",0);
            }
            if (AlgoCover==true AND StaticVarGet(static_name_+"CoverAlgo")==0 AND StaticVarGetText(static_name_+"CoverAlgo_barvalue") != lasttime)
            {
            // Short Exit
                buyorder(1);
                StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"CoverAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name()+ "Signal = Cover");
            }
            else if (AlgoCover != True)
            {   
                StaticVarSet(static_name_+"CoverAlgo",0);
            }
        }
        
    }//end main if

    
_SECTION_END();

Feel Free to use modify the code according to your requirements. Use this module responsibly!

Test your system properly with the module before using the module in a live automation trading environment.

Leave a Comment

Your email address will not be published. Required fields are marked *