Have started using the Zerodha Pi bridge for Amibroker. Needed help with coding this strategy: Buy when MACD crosses MACD signal line from below and exit is with trailing stop loss. If someone can also help in giving me the code on firing the order from AFL to Pi.
I don't know AFL, someone from our tech team just did this
StopLevel = 1 - Param("trailing stop %", 3, 0.1, 10, 0.1)/100; Buy = Cross( MACD(), Signal() ); Sell = 0; trailARRAY = Null; trailstop = 0; for( i = 1; i < BarCount; i++ ) { if( trailstop == 0 AND Buy[ i ] ) { trailstop = High[ i ] * stoplevel; } else Buy[ i ] = 0; // remove excess buy signals if( trailstop > 0 AND Low[ i ] < trailstop ) { Sell[ i ] = 1; SellPrice[ i ] = trailstop; trailstop = 0; } if( trailstop > 0 ) { trailstop = Max( High[ i ] * stoplevel, trailstop ); trailARRAY[ i ] = trailstop; } } PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low); PlotShapes(Sell*shapeDownArrow,colorRed,0,High); Plot( Close,"Price",colorBlack,styleBar); Plot( trailARRAY,"trailing stop level", colorRed ); //============================================================================================================================PlugIn============================================================================================// brd=Null; if(IsNull(brd)) { brd = CreateStaticObject("pibridge.Bridge"); } ClientID_String = ParamStr("ClientID", "DH0149"); Trading_Symbol = ParamStr("Trading Symbol", "NIFTY15JANFUT"); Quantity = Param("Lots/Quantity",1, 1, 10, 1 ,1); Start_Trading = ParamToggle("Start Trading", "No|Yes",1); if(Start_Trading) { GfxSetBkColor(colorBlue); GfxSetTextColor( colorWhite ); GfxSelectFont("Times New Roman", 25, 900, True ); GfxTextOut("You are Trading", 200 , 20 ); } currTimestamp = LastValue( TimeNum() ); LastTimestamp = Nz( StaticVarGet("LastBarTimestamp_"+Name()+"_" +GetChartID()) ); if ( currTimestamp != LastTimestamp ) { StaticVarSet( "LastBarTimestamp_"+Name()+"_" +GetChartID(), currTimestamp ); if( LastValue( Ref(Buy,-1) ) AND Start_Trading==1 ) { brd.PlaceOrder("NFO", Trading_Symbol, Trading_Symbol,"SYSTEM1A", 1, Quantity, Quantity, LastValue(Close), 0, "LIMIT", "NRML", ClientID_String,"DAY"); PopupWindow("Time: " + Now() + "\n\n\n\n Price: " + LastValue(Close),"Buy " + Trading_Symbol, 7200, 640*mtRandom(), 480*mtRandom()); PlaySound("C:\\Windows\\Media\\tada.wav"); } if( LastValue( Ref(Sell,-1) ) AND Start_Trading==1 ) { brd.PlaceOrder("NFO", Trading_Symbol, Trading_Symbol,"SYSTEM1A", 2, Quantity, Quantity, LastValue(Close), 0, "LIMIT", "NRML", ClientID_String,"DAY"); PopupWindow("Time: " + Now() + "\n\n\n\n Price: " + LastValue(Close),"Sell " + Trading_Symbol, 7200, 640*mtRandom(), 480*mtRandom()); PlaySound("C:\\Windows\\Media\\tada.wav"); } }