Options Amibroker Algo Trading AFL

This is a basic template used for trading in options.

  1. You can use it on any chart of cash/future to trade in options based on signals generated in the chart
  2. If Buy signal generated, it will Buy Call and exit Long position in Put
  3. If Sell signal generated, it will Buy Put and exit Long position in Call

Parameters:

  1. Base symbol- symbol of derivative you want to trade in.
  2. Expiry- Expiry of that Put/Call
  3. Put Strike – strike for selecting Put for trading
  4. Call strike- strike for selecting Cut for trading
  5. Instrument Name: should be OPTIDX or OPTSTK
  6. Trade Intrabar: If you want to trade immediately when signal appears, select Yes. If you want to wait for signal confirmation until completion of candle, select Candle Completion
  7. Quantity- Total quantity in which you want to trade. For example, in Nifty 1 lot use 75, Nifty 2 lot use 150…

How to Use:

This is not a AFL strategy but simply template for automatic trading. Take the following steps to start automatic trading with your own strategy:

  1. In Amibroker, go to Analysis, then Formula Editor
  2. In Formula Editor window, go to File -> Open and open your strategy with buy/sell code.
  3. Scroll down to bottom of code. Copy-paste here code from this strategy template.
  4. Save this file in your computer at location which you easily remember. Now this file is ready for auto trading from both Amibroker charts and Scanner.

Download Link:

Direct Code for Copy-Paste

///section begines for options
bsym= ParamStr("Base Symbol", "BANKNIFTY");
expiry= ParamStr("Expiry","25Apr2019");
pstrike= ParamStr("Put Strike","30000");
cstrike= ParamStr("Call Strike","30000");

instr= ParamList("Instrument Name","OPTIDX|OPTSTK"); 
delay= ParamToggle("Trade Intrabar?", "YES|Candle Completion");
qt= Param("Quantity", 20, 1, 10000);
stag= ParamStr("Strategy Tag", "STG1");

csym= bsym + "|" + expiry + "|" + cstrike + "|" + "CE";
psym= bsym + "|" + expiry + "|" + pstrike + "|" + "PE";

if(delay)
{Buy=Ref(Buy,-1); Sell=Ref(Sell,-1); Short= Ref(Short,-1); Cover= Ref(Cover,-1);}

global algoji;
algoji = Name() + NumToStr( Interval() / 60, 1.0, False ) ;

procedure aStaticVarSet( SName, Svalue )
{
    global algoji;

        StaticVarSet( Sname + algoji, Svalue );
}

function aStaticVarGet( SName )
{
    global algoji;
    Var = StaticVarGet( Sname + algoji );

    if ( IsNull( Var = StaticVarGet( Sname + algoji ) ) )
        Var = 0;

    return Var;
}



Checkdt=Nz(aStaticVarGet("lastdt"));
dt = LastValue( DateTime() );
Checkdts=Nz(aStaticVarGet("lastdts"));
dts = LastValue( DateTime() );
RTBuy = LastValue( Buy) AND Checkdt != dt;
RTShort = LastValue( Short) AND Checkdts != dts;
    bp= sp= "0";
    qty= NumToStr(qt, 1.0, False);

//https://algoji.com/ 
if ( RTBuy )
{
	 aStaticVarSet("lastdt",dt );
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
	_TRACE( "#"+Nz(StaticVarGet("counter"))+",LE,"+csym+",,," +bp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",csym,"M","",bp,qty,instr,stag);
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
	_TRACE( "#"+Nz(StaticVarGet("counter"))+",LX,"+psym+",,," +bp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LX",psym,"M","",bp,qty,instr,stag);
}
//https://algoji.com/
if ( RTShort )
{
	 aStaticVarSet("lastdts",dts );
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
    _TRACE( "#"+Nz(StaticVarGet("counter"))+",LE,"+psym+",,," +sp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",psym,"M","",sp,qty,instr,stag);
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
    _TRACE( "#"+Nz(StaticVarGet("counter"))+",LX,"+csym+",,," +sp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LX",csym,"M","",sp,qty,instr,stag);
}

Button = ParamToggle( "Enable Button Trading", "YES|NO" );


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 );
    }
}



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( "Buy", x1 + 14, 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( "Sell", sx1 + 14, sy1 + 20 );

    if ( px > x1 AND px<x2 AND py>y1 AND py < y2 AND Click )
    {
		AlertIf( 1, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 1, 2, 1 );    
    	PopupWindowEx( "ID:1", "BUY", "Buy Triggered from Button "+Name(), 1, -1, -1 );
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
	_TRACE( "#"+Nz(StaticVarGet("counter"))+",LE,"+csym+",,," +bp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",csym,"M","",bp,qty,instr,stag);
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
	_TRACE( "#"+Nz(StaticVarGet("counter"))+",LX,"+psym+",,," +bp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LX",psym,"M","",bp,qty,instr,stag);
	}
//https://algoji.com/
    if ( px > sx1 AND px<sx2 AND py>sy1 AND py < sy2 AND Click )
    {
		AlertIf( 2, "SOUND C:\\Windows\\Media\\tada.wav", "Audio alert", 2, 2, 1 );
	PopupWindowEx( "ID:3", "SHORT", "Short Triggered from Button "+Name(), 1, -1, -1 );
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
    _TRACE( "#"+Nz(StaticVarGet("counter"))+",LE,"+psym+",,," +sp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",psym,"M","",sp,qty,instr,stag);
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
    _TRACE( "#"+Nz(StaticVarGet("counter"))+",LX,"+csym+",,," +sp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LX",csym,"M","",sp,qty,instr,stag);
    }

}