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.

Button Trading in Amibroker using AlgoMojo Platform (Supports Newer and Older Amibroker Versions)

5 min read

This tutorial demonstrates how to send orders from Amibroker to Algomojo using Simple GUI buttons using modern amibroker tools and also using the older versions of Amibroker less than Amibroker 6.22.

Button Trading using Amibroker 6.22 Version or Higher

GUI buttons are newer functions in Amibroker 6.22 version onwards. This AFL code requires Amibroker 6.22 version or higher and access to Algomojo API + Trading Account with Algomojo Partner Broker

Amibroker AFL Code for Amibroker 6.22 Version or Higher

/*
Created By : Rajandran R (Founder - Marketcalls / Co-Founder Algomojo )
Created on : 01 Oct 2020.
Website : www.marketcalls.in / www.algomojo.com
*/

_SECTION_BEGIN("Button Trading - Algomojo");

//Notes
//This afl code works only on Amibroker 6.22 and higher version
//Requires Algomojo API to place orders from button
//Replace the API Key and API Secrety key with yours

Version(6.22);

RequestTimedRefresh(1, False); // Send orders even if Amibroker is minimized or Chart is not active

user_apikey = ParamStr("user_apikey","xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //Enter your API key here
api_secretkey = ParamStr("api_secretkey","xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //Enter your API secret key here
s_prdt_ali = ParamList("product alias","BO:BO|CNC:CNC|CO:CO|MIS:MIS|NRML:NRML",3); //Product Alias
Tsym = ParamStr("Trading Symbol","RELIANCE-EQ"); //Symbol Name
exch = ParamList("Exchange","NFO|NSE|BSE|CDS|MCX|NCDEX|BFO|MCXSXFO|MCXSX",1); //Exchange
Ret = ParamList("Ret","DAY|IOC",0); //Retention
prctyp = ParamList("prctyp","MKT|L|SL|SL-M",0); // Pricetype
Pcode = ParamList("Product code","NRML|BO|CNC|CO|MIS",4); // Product Code
qty = Param("Quantity",40,0,100000,1); // Quantity
AMO = ParamList("AMO Order","NO|YES",0); //AMO Order
placeordertype = ParamList("Place Order On","Realtime|CandleCompletion",0); // Place Order Type
EnableAlgo = ParamList("Algo Mode","Disable|Enable|LongOnly|ShortOnly",0); // Algo Mode
stgy_name = ParamStr("Strategy Name","Test Strategy Chart"); // Strategy Name

static_name_ = Name()+GetChartID()+interval(2)+stgy_name;
static_name_algo = Name()+GetChartID()+interval(2)+stgy_name+"algostatus";
//StaticVarSet(static_name_algo, -1); 
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);
}
}



resp = "";




function Buyorder()
{
	
    algomojo=CreateObject("XLAMIBRIDGE.Main");
    api_data ="{\"stgy_name\":\""+stgy_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"B"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+qty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
    resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data);
    StaticVarSet(static_name_+"buyCoverAlgo",1); //Algo Order was triggered, no more order on this bar
    _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ qty*2 +"  Signal : Buy and Cover Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
	Say( "Order Placed" ); 
}

function Sellorder()
{
    algomojo=CreateObject("XLAMIBRIDGE.Main");
    api_data ="{\"stgy_name\":\""+stgy_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"S"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+qty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
    resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data);
    StaticVarSet(static_name_+"sellAlgo",1); //Algo Order was triggered, no more order on this bar
    _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ qty +"  Signal : Sell Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            
    Say( "Order Placed" );
}

function GuiButtonTrigger( ButtonName, x, y, width, Height ) {
	/// @link http://forum.amibroker.com/t/guibuttons-for-everyone/1716/4
	/// by beaver & fxshrat
	/// version 1.1
	global IDset;
	local id, event, clickeven;
	
	if( typeof( IDset ) == "undefined" ) IDset = 0; 

	//_TRACEF( "IDset before: %g", IDset );	
	GuiButton( ButtonName, ++IDset, x, y, width, height, 7 ); 
	//_TRACEF( "IDset after: %g", IDset );
	result = 0;
	id = GuiGetEvent( 0, 0 );// receiving button id
	event = GuiGetEvent( 0, 1 );// receiving notifyflag
	clickevent = event == 1;
	BuyClicked = id == 1 && clickevent;
	SellClicked = id == 2 && clickevent;
	if( BuyClicked AND StaticVarGet(Name()+GetChartID()+"buyAlgo")==0 ) 
	{
		BuyOrder();
		result = 1;
		StaticVarSet(Name()+GetChartID()+"buyAlgo",1); 
	}
	else
	{
		StaticVarSet(Name()+GetChartID()+"buyAlgo",0);
	}
	if( SellClicked AND StaticVarGet(Name()+GetChartID()+"sellAlgo")==0 ) 
	{
		SellOrder();
		result = -1;
		StaticVarSet(Name()+GetChartID()+"sellAlgo",1); 
	}
	else
	{
		StaticVarSet(Name()+GetChartID()+"sellAlgo",0); 
	}
	return result;
	
}
	
	BuyTrigger = GuiButtonTrigger( "Buy "+qty+" Shares", 0, 100, 200, 30 );
	SellTrigger = GuiButtonTrigger( "Sell "+qty+" Shares", 200, 100, 200, 30 );
	GuiSetColors( 1, 3, 2, colorRed, colorBlack, colorRed, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); 

Title = "Trigger: " + WriteIf(BuyTrigger==1,"Buy Triggered",WriteIf(BuyTrigger==-1,"Sell Triggered","0"));

SetChartOptions(0 , chartShowArrows | chartShowDates);
Plot(Close,"Candle", colorDefault, styleCandle);

_SECTION_END();

Button Trading AFL Module for Older Versions less than Amibroker v6.22

/*
Created By : Rajandran R(Founder - Marketcalls / Co-Founder Algomojo )
Created on : 01 Oct 2020.
Website : www.marketcalls.in / www.algomojo.com
*/

_SECTION_BEGIN("Button Trading - Algomojo For Old Amibroker Versions");

//Notes
//This afl code works only on Amibroker 6.22 and higher version
//Requires Algomojo API to place orders from button
//Replace the API Key and API Secret key with yours

Title = " ";


RequestTimedRefresh(1, False); // Send orders even if Amibroker is minimized or Chart is not active

user_apikey = ParamStr("user_apikey","xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //Enter your API key here
api_secretkey = ParamStr("api_secretkey","xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //Enter your API secret key here
s_prdt_ali = ParamList("product alias","BO:BO|CNC:CNC|CO:CO|MIS:MIS|NRML:NRML",3); //Product Alias
Tsym = ParamStr("Trading Symbol","RELIANCE-EQ"); //Symbol Name
exch = ParamList("Exchange","NFO|NSE|BSE|CDS|MCX|NCDEX|BFO|MCXSXFO|MCXSX",1); //Exchange
Ret = ParamList("Ret","DAY|IOC",0); //Retention
prctyp = ParamList("prctyp","MKT|L|SL|SL-M",0); // Pricetype
Pcode = ParamList("Product code","NRML|BO|CNC|CO|MIS",4); // Product Code
qty = Param("Quantity",40,0,100000,1); // Quantity
AMO = ParamList("AMO Order","NO|YES",0); //AMO Order
placeordertype = ParamList("Place Order On","Realtime|CandleCompletion",0); // Place Order Type
EnableAlgo = ParamList("Algo Mode","Disable|Enable|LongOnly|ShortOnly",0); // Algo Mode
stgy_name = ParamStr("Strategy Name","Test Strategy Chart"); // Strategy Name

static_name_ = Name()+GetChartID()+interval(2)+stgy_name;
static_name_algo = Name()+GetChartID()+interval(2)+stgy_name+"algostatus";
//StaticVarSet(static_name_algo, -1); 
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);
}
}



resp = "";




function Buyorder()
{
	
    algomojo=CreateObject("XLAMIBRIDGE.Main");
    api_data ="{\"stgy_name\":\""+stgy_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"B"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+qty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
    resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data);
    StaticVarSet(static_name_+"buyCoverAlgo",1); //Algo Order was triggered, no more order on this bar
    _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ qty*2 +"  Signal : Buy and Cover Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
	Say( "Order Placed" ); 
}

function Sellorder()
{
    algomojo=CreateObject("XLAMIBRIDGE.Main");
    api_data ="{\"stgy_name\":\""+stgy_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"S"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+qty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
    resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data);
    StaticVarSet(static_name_+"sellAlgo",1); //Algo Order was triggered, no more order on this bar
    _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ qty +"  Signal : Sell Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            
    Say( "Order Placed" );
}


X0 = 20;
Y0 = 100;
X1 = 100;

LBClick = GetCursorMouseButtons() == 9;	// Click
MouseX  = Nz(GetCursorXPosition(1));		// 
MouseY  = Nz(GetCursorYPosition(1));		//

procedure DrawButton (Text, x1, y1, x2, y2, colorFrom, colorTo)
{
	GfxSetOverlayMode(0);
	GfxSelectFont("Verdana", 9, 700);
	GfxSetBkMode(1);
	GfxGradientRect(x1, y1, x2, y2, colorFrom, colorTo);
	GfxDrawText(Text, x1, y1, x2, y2, 32|1|4|16);
}
GfxSetTextColor(colorWhite);

if(EnableAlgo == "Enable")
{

	DrawButton("Buy", X0, Y0, X0+X1, Y0+30, colorGreen, colorGreen);
	CursorInBuyButton = MouseX >= X0 AND MouseX <= X0+X1 AND MouseY >= Y0 AND MouseY <= Y0+30;
	BuyButtonClick = CursorInBuyButton AND LBClick;
	
	DrawButton("Sell", X0, Y0+40, X0+X1, Y0+70, colorRed, colorRed);
	CursorInSellButton = MouseX >= X0 AND MouseX <= X0+X1 AND MouseY >= Y0+40 AND MouseY <= Y0+70;
	SellButtonClick = CursorInSellButton AND LBClick;
	
	if( BuyButtonClick AND StaticVarGet(Name()+GetChartID()+"buyAlgo")==0 ) 
	{
		BuyOrder();
		StaticVarSet(Name()+GetChartID()+"buyAlgo",1); 
	}
	else
	{
		StaticVarSet(Name()+GetChartID()+"buyAlgo",0);
	}
	if( SellButtonClick AND StaticVarGet(Name()+GetChartID()+"sellAlgo")==0 ) 
	{
		SellOrder();
		StaticVarSet(Name()+GetChartID()+"sellAlgo",1); 
	}
	else
	{
		StaticVarSet(Name()+GetChartID()+"sellAlgo",0); 
	}
	
	
	
	

}

_SECTION_END();

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = "");
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();


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.

VB Script for Placing Orders From Excelsheet to Algomojo…

This tutorial provides a sample VB Script code to place orders from the Algomojo Platform. Algomojo offers Free API + Free Trading Platform to...
Rajandran R
2 min read

How to use Postman to Send Orders to Your…

Postman is an API testing tool that started in 2012 as a side project by Abhinav Asthana to simplify API workflow in testing and...
Rajandran R
1 min read

What is Algomojo API?

Algomojo offers Free API + Free Trading Platform to algomojo users to Place, Modify, Cancel Orders. Currently, Algomojo API is free for the users...
Rajandran R
2 min read

One Reply to “Button Trading in Amibroker using AlgoMojo Platform (Supports Newer…”

Leave a Reply

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