Buy Sell Double

This is a simple and powerful auto trading template for strategies where you want to trade DOUBLE quantity on reverse signal. It should be used only for strategies where buy= cover and sell= short.

Parameters:

  1. Instrument Name: Select the instrument name for the market segment you want to trade
  2. 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
  3. Strategy Tag: You can use any strategy tag to manage signals coming from mutiple strategies in Amibroker. Eg. STG1, STG2, STG3 etc.
  4. Disable Long: If you want to disable all Long signals (use only Short side for trading), then turn this Yes
  5. Disable Short: If you want to disable all Short signals (use only Long side for trading), then turn this Yes
  6. Initial qty-  quantity you want to trade . it will get double after the 1st trade signal
  7. Previous Session Position- If yes it will double the quantity from 1st  trade signal else it will double from second trade signal
  8. Reset – reset the trading quantity to initial quantity

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. Apply on chart. If shows error, uncomment the line below remove // from the first line i.e. // Short= Sell; Cover= Buy;
  5. 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

/*
1. Use this for strategies where you want to trade DOUBLE quantity on reverse signal
2. use only for strategies where buy= cover and sell= short
3. If shows error in integration, uncomment the line below */

// Short= Sell; Cover= Buy;

instr= ParamList("Instrument Name","EQ|FUTIDX|FUTSTK|OPTIDX|OPTSTK|FUTCOM"); 
delay= ParamToggle("Trade Intrabar?", "YES|Candle Completion");
stag= ParamStr("Strategy Tag", "STG1");
dlong= ParamToggle("Disable Long?", "NO|YES");
dshort= ParamToggle("Disable Short?", "NO|YES");
qt= Param("Initial Quantity", 1, 1, 10000);
prevp= ParamToggle("Previous Session Position", "NO|YES");


if(dlong){Buy=Sell=0;}
if(dshort){Short=Cover=0;}


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

if(ParamTrigger("Reset Qty", "RESET")) aStaticVarSet("InTrade",0 );
sym = Name();
    bp= sp= NumToStr(Close[BarCount-1],1.2, False);
    qty= NumToStr(qt, 1.0, False);

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;
//https://algoji.com/ 
if ( RTBuy )
{
	 if( Nz(aStaticVarGet("InTrade")) >0 ) qt= qt*2;
	 else if(prevp) qt= qt*2;
	 aStaticVarSet("InTrade",1 );
	 aStaticVarSet("lastdt",dt );
    qty= NumToStr(qt, 1.0, False);
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
            _TRACE( "#"+Nz(StaticVarGet("counter"))+",LE,"+sym+",,," +bp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",sym,"M","",bp,qty,instr,stag);
}
//https://algoji.com/
if ( RTShort )
{
	 if( Nz(aStaticVarGet("InTrade")) >0 ) qt= qt*2;
	 else if(prevp) qt= qt*2;
	 aStaticVarSet("InTrade",1 );
	 aStaticVarSet("lastdts",dts );
    qty= NumToStr(qt, 1.0, False);
	 StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
            _TRACE( "#"+Nz(StaticVarGet("counter"))+",SE,"+sym+",,," +sp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SE",sym,"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,"+sym+",,," +bp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",sym,"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"))+",SE,"+sym+",,," +sp +","+qty+","+instr+",,");
	Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SE",sym,"M","",sp,qty,instr,stag);
    }

}