Automated Trading Buttons Amibroker AFL

This is the most simple and most powerful AFL template. You can send signals of type LE (Long Entry), LX (Long Exit), SE (Short Entry) and SX (Short Exit). You can send any order type, product type etc. 

How to Use:

This is not a AFL strategy but simply template for testing automatic trading. Simply apply this AFL on chart to start auto trading with buttons.

Download Link:

Direct Code for Copy-Paste

_SECTION_BEGIN( "Button Trading" );

instr= ParamList("Instrument Name","EQ|FUTIDX|FUTSTK|OPTIDX|OPTSTK|FUTCAR|FUTCOM"); 
expiry= ParamStr("Expiry","");
strike= ParamStr("Strike","");
type= ParamStr("Option Type", "");
otype= ParamList("Order Type", "M,L,SLL,SLM");
qty= ParamStr("Quantity", "1");
ptype= ParamToggle("Use LTP as Order Price?", "YES|NO");
price= ParamStr("Limit Price","");
tprice= ParamStr("TriggerPrice", "");
slprice= ParamStr("SLPrice", "");
sqprice= ParamStr("SquareOff Price", "");
stg= ParamStr("Strategy Tag", "STG1");
port= ParamStr("Port","");

sym = Name()+ "|"+expiry+ "|" +strike+ "|" +type;
trigprice= tprice + "|" + slprice + "|" + sqprice+ "|";// +;
stag= stg+ "|" + port;


function GetSecondNum()
{
    Time = Now( 4 );
    Seconds = int( Time % 100 );
    Minutes = int( Time / 100 % 100 );
    Hours = int( Time / 10000 % 100 );
    SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
    return SecondNum;
}
 
function PopupWindowEx( popupID, bodytext, captiontext, timeout, left, top )
{
    displayText = bodytext + captiontext;
    if ( ( StaticVarGetText( "prevPopup" + popupID ) != displayText) OR ( StaticVarGet( "prevPopupTime" + popupID ) < GetSecondNum() ) )
    {
        StaticVarSetText( "prevPopup" + popupID, displayText);
        StaticVarSet( "prevPopupTime" + popupID, GetSecondNum() + timeout );
        PopupWindow( bodytext, Captiontext + popupID, timeout, Left, top );
    }
}


Button = ParamToggle( "Enable Button Trading", "YES|NO" );
x1= Status( "pxchartleft" )+10;
y1= Status( "pxcharttop" )+20;

if ( Button == 0 )
{
    click = GetCursorMouseButtons() == 9;
    Px = GetCursorXPosition( 1 );
    Py = GetCursorYPosition( 1 );

    x2 = x1 + 60;
    y2 = y1 + 60;
    GfxSelectSolidBrush(  ColorRGB( 0, 102, 0 ) ); //buy
    GfxSelectFont( "Tahoma", 13, 100 );
    GfxSetBkMode( 1 );
    GfxSetTextColor( colorWhite );
    GfxRoundRect( x1, y1, x2, y2 , 7, 7 ) ;
    GfxTextOut( "LE", x1 + 20, y1 + 20 );

    sx1 = x2;
    sy1 = y1;
    sx2 = sx1 + 60;
    sy2 = sy1 + 60;
    GfxSelectSolidBrush(  ColorRGB( 255, 204, 204 ) );//sell
    GfxRoundRect( sx1, sy1, sx2, sy2 , 7, 7 ) ;
    GfxSetTextColor( ColorRGB( 153, 0, 0 ) );
    GfxTextOut( "SE", sx1 + 20, sy1 + 20 );

    ex1 = x1;
    ey1 = y1+60;
    ex2 = ex1 + 60;
    ey2 = ey1 + 60;
    GfxSelectSolidBrush(  ColorRGB( 255, 204, 204 ) );//sell
    GfxRoundRect( ex1, ey1, ex2, ey2 , 7, 7 ) ;
    GfxSetTextColor( ColorRGB( 153, 0, 0 ) );
    GfxTextOut( "LX", ex1 + 20, ey1 + 20 );

    GfxSelectSolidBrush(  ColorRGB( 0, 102, 0 ) );//sell
    GfxRoundRect( ex2, ey1, ex2+60, ey2 , 7, 7 ) ;
    GfxSetTextColor( colorWhite );
    GfxTextOut( "SX", ex2 + 20, ey1 + 20 );

    if ( px > x1 AND px<x2 AND py>y1 AND py < y2 AND Click )
    {
        _TRACE( "# ," + NumToStr(Nz(StaticVarGet("counter")),0,False) + ", BUY triggered from button, "  );
		AlertIf( 1, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 1, 2, 1 );    
    StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
    	PopupWindowEx( "ID:1", "BUY", "Buy Triggered from Button "+Name(), 1, -1, -1 );
	if (ptype==0)
	{
	bp= NumToStr(Close[BarCount-1],1.2, False);
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",sym,otype,trigprice,bp,qty,instr,stag);
	}
	else
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",sym,otype,trigprice,price,qty,instr,stag);
	}

    if ( px > sx1 AND px<sx2 AND py>sy1 AND py < sy2 AND Click )
    {
        _TRACE( "# ," + NumToStr( DateTime(), formatDateTime ) + ", SELL triggered from button, " );
		AlertIf( 2, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 2, 2, 1 );
    StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
    bp= NumToStr(Close[BarCount-1],1.2, False);
	PopupWindowEx( "ID:3", "SHORT", "Short Triggered from Button "+Name(), 1, -1, -1 );
	if (ptype==0)
	{
	bp= NumToStr(Close[BarCount-1],1.2, False);
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SE",sym,otype,"",bp,qty,instr,stag);
	}
	else
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SE",sym,otype,"",price,qty,instr,stag);
    }

    if ( px > ex1 AND px<ex2 AND py>ey1 AND py<ey2 AND Click )
    {
        _TRACE( "# ," + NumToStr( DateTime(), formatDateTime ) + ", SELL triggered from button, " );
		AlertIf( 3, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 2, 2, 1 );
    StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
    bp= NumToStr(Close[BarCount-1],1.2, False);
	PopupWindowEx( "ID:3", "SELL", "Sell Triggered from Button "+Name(), 1, -1, -1 );
	if (ptype==0)
	{
	bp= NumToStr(Close[BarCount-1],1.2, False);
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LX",sym,otype,"",bp,qty,instr,stag);
	}
	else
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LX",sym,otype,"",price,qty,instr,stag);
    }

    if ( px > ex2 AND px<(ex2+60) AND py>ey1 AND py < ey2 AND Click )
    {
        _TRACE( "# ," + NumToStr( DateTime(), formatDateTime ) + ", SELL triggered from button, " );
		AlertIf( 4, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 2, 2, 1 );
    StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
    bp= NumToStr(Close[BarCount-1],1.2, False);
	PopupWindowEx( "ID:3", "Cover", "Cover  Triggered from Button "+Name(), 1, -1, -1 );
	if (ptype==0)
	{
	bp= NumToStr(Close[BarCount-1],1.2, False);
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SX",sym,otype,"",bp,qty,instr,stag);
	}
	else
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SX",sym,otype,"",price,qty,instr,stag);
    }

}
_SECTION_END();

/*
//Algoji_Signal(ID,Type,Symbol,OrderType,TrigPrice,Price,Qty,InstrumentName,StrategyTag)
            //Symbol = Symbol | Expiry | Strike | OptionType 
            //TrigPrice = TrigPrice | StopLoss | SquareOff | TrailingTicks
            //StrategyTag = StrategyTag | Port
            
            AlgojiBroadcastSignal("7", "LE","EURINR","M","","","300","NCD","","","","","STRAT1","");