Buy Sell button trading in Amibroker

Buy Sell button trading in Amibroker

What is Amibroker Button trading?

One of the feature available in Amibroker AFL Formula editor is low-level graphic functions. By using set of these functions we can create button overlays on existing charts and capture the mouse click events.

When Buy or Sell button clicked, we can send the order details to Broker using their API or Trading bridge. This eliminates the manual order placing process.

AFL Formula with Buy Sell buttons

Following is the AFL code to display Buy Sell button in Amibroker chart display.

// Intraday trading AFL with buttons for Buy and Sell
// Created by Nagaraj H R
// https://EasyAlgo.in

//#include <easytrader.afl>

_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();

_SECTION_BEGIN(“EMA”);
P = ParamField(“Price field”,-1);
Periods = Param(“Periods”, 20, 2, 300, 1, 10 );
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( “Color”, colorCycle ), ParamStyle(“Style”) );
_SECTION_END();

_SECTION_BEGIN(“Buy Sell Buttons”);

procedure Button1 (Text, x1, y1, x2, y2, buttonColor)
{
GfxSetOverlayMode(0);
GfxSelectFont(“Tahoma”, 11, 700);
GfxSetBkMode(1);
GfxGradientRect(x1, y1, x2, y2, buttonColor, buttonColor);
GfxSetTextColor(colorWhite);
GfxDrawText(Text, x1, y1, x2, y2, 32|1|4|16);
}
enableButtons = ParamToggle (“Show Buy Sell Buttons”, “No|Yes”, 1);
if (enableButtons)
{
//Get mouse left button click event
buttonClickVal = GetCursorMouseButtons() ;
MouseX = Nz(GetCursorXPosition(1)); //
MouseY = Nz(GetCursorYPosition(1)); //

Button1 (“Buy”, 10, 30, 60, 60, colorGreen);
BuyButtonArea = MouseX >= 10 AND MouseX <= 60 AND MouseY >= 30 AND MouseY <= 60;
BuyButtonClick = BuyButtonArea AND buttonClickVal == 9;

Button1 (“Sell”, 70, 30, 120, 60, colorRed);
SellButtonArea = MouseX >= 70 AND MouseX <= 120 AND MouseY >= 30 AND MouseY <= 60;
SellButtonClick = SellButtonArea AND buttonClickVal == 9;

// specify the trade details
ezSymbolName = “NIFTY20JULFUT”;
ezExchangeName = “NFO”;
ezQuantity = 75; //For nifty future 1 lot
//

if (BuyButtonClick)
{
_TRACE(“Buy Button clicked”);
tradeType = “BUY”;
//for EasyTrader integration
//Place_MIS_Order (ezExchangeName, ezSymbolName, tradeType, ezQuantity, “LIMIT”, LastValue(C), 0 );

}

if (SellButtonClick)
{
_TRACE(“Sell Button clicked”);
tradeType = “SELL”;
//for EasyTrader integration
//Place_MIS_Order (ezExchangeName, ezSymbolName, tradeType, ezQuantity, “LIMIT”, LastValue(C), 0 );
}
}
_SECTION_END();

Leave a Reply