After Intraday V2.0 Amibroker Module – a readymade drag and drop module to convert any trading strategy into the intraday trading system with alerts, stop loss, target feature with intraday controls many users demanded a plug-n-play aka another drag and drop module to convert any trading strategy coded in Amibroker with Buy,Sell, Short,Cover into Multi Targets (Target1, Target2), Stoploss and Trailing Stoploss Module

Features of the Module
- Module with Stoploss and Two Target Management (T1-Partial Exit and T2- Complete Exit)
- Drag and Drop on Any of your simple trading strategy rest amibroker module will take care.
- Dashboard which shows the current target1, target2, stoploss and trailingstoploss levels.
- Easy to Integrate Automated Trading Modules and Algotrading platforms like Algomojo.
Steps to use the module
1)Download the AFL Code and Store it as trademanagement.afl under any of the Amibroker Formulas Folder.
2)Ensure your strategy has Buy,sell,short,cover parameters. Apply your trading system on a blank chart and drag and drop the trademanagement.afl on top of your trading system
3)if your strategy doesn’t has short and cover variables use short =0; and cover = 0; in your code before dragging and dropping
4)Use the code only on Top of the trading strategy with plain vanilla buy,sell,short,cover variables defined.
5)Avoid giving any trade delay in your strategy as it is already handled inside the module
or sigscaleout is not implemented Module. AFL module is built for Algotraders who want to automate their trading system
Stoploss, Multi Target, and Trailing Stoploss Module – Amibroker AFL Code
//Stoploss and Target Module - Readymade Module (Drag and Drop on top of your trading strategy)
//Built in Target1, Target2, Stoploss and Trailing Stoploss Based Exit (Touch based Exit)
//Regular Exit(Close of Candle/Touch of the level)
//Warning : Use the Module only on top of a trading strategy with proper buy, sell,short,cover variables defined
//Avoid giving any delay in your strategy as it is already handles inside the module
//Warning Code is not backtestable for Multi Targets as sigscalein/sigscaleout is not implemented
//Module is built for Algotraders who want to automate their trading system
//Coded by Rajandran R - Founder - Marketcalls / Co-Founder - Algomojo
//Coded on 25th Nov 2022
//Website - www.marketcalls.in / www.algomojo.com
//Version - 1.0
_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("Stoploss, Multi Target and Trailing Stoploss Module");
//Remove Excessive Signals
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);
TickSz = Param("Tick Size",0.05);
mode = ParamList("Stop and Target Mode","Points|Percentage");
stops = Param("Stoploss",50,0.05,1000,0.05);
target1 = Param("Target 1",100,0.05,1000,0.05);
target2 = Param("Target 2",100,0.05,1000,0.05);
tsl = Param("Trailing Stop",100,0.05,1000,0.05);
Quantity = Param("Entry Quantity",1000,1,10000,1);
target1exit = Param("Target 1 Exit(qty)",500,1,10000,1);
target2exit = Paramstr("Target 2 Exit(qty)","FULL"); //Dummy control mostly information purpose only
//Initializing the Open Position
openposition = 0;
//Signal Executes on Close of the Candle
SignalDelay = Param("Signal Delay",1,0,10,1);
Buy = Ref(Buy,-SignalDelay);
iSell = Ref(Sell,-SignalDelay);
SetTradeDelays(0,0,0,0); //No Trade Delays provided
BuyPrice = ValueWhen(Buy,Open);
Buytsllevel = Null;
if(mode=="Points")
{
BuyStop = BuyPrice - stops;
BuyTarget1 = BuyPrice + target1;
BuyTarget2 = BuyPrice + target2;
Buytsllevel = BuyPrice - tsl;
}
if(mode=="Percentage")
{
BuyStop1 = (100-stops)/100*BuyPrice;
BuyTarget1 = (100+target1)/100*BuyPrice;
BuyTarget2 = (100+target2)/100*BuyPrice;
Buytsllevel1 = (100-tsl)/100*high;
//Round it of to nearest Tick Size
BuyStop = TickSz * round(Buystop1/TickSz);
BuyTarget1 = TickSz * round(BuyTarget1/TickSz);
BuyTarget2 = TickSz * round(BuyTarget2/TickSz);
Buytsllevel = TickSz * round(Buytsllevel1/TickSz);
}
buytslarray = Null;
//if buytrailstop >0 -> long trades are running
buytrailstop = 0;
bt1flag = 0;
bt1hit = False;
bt2hit = False;
for(i=0;i<BarCount;i++)
{
//Identifying Very First Buy
if(Buy[i] AND buytrailstop == 0)
{
//Calculate the trailstop
buytrailstop = Buytsllevel[i];
openposition[i] = Quantity; //fullposition
}
//else
//{
//Buy[i] = false; //Removing Excessive Buy
//}
//if buy signal continues at the same time if the trailstop hits
if(buytrailstop>0 AND Low[i] < buytrailstop)
{
Sell[i] = True;
SellPrice[i] = buytrailstop;
buytrailstop = 0; // reset the flag to zero -> so that it can take fresh buy signal
openposition[i] =0;
bt1flag = 0;
}
//if buy signal continues at the same time if the Target1 hits
if(buytrailstop>0 AND high[i] > BuyTarget1[i] AND bt1flag==0 )
{
bt1hit[i] = True;
openposition[i] = Quantity - target1exit;
bt1flag = 1; //enabling target1 hit flag - to avoid multiple target 1 hits
}
//if buy signal continues at the same time if the Target2 hits
if(buytrailstop>0 AND high[i] > BuyTarget2[i])
{
bt2hit[i] = True;
openposition[i] = 0;
bt1flag = 0; //enabling target1 hit flag
Sell[i] = True;
SellPrice[i] = BuyTarget2[i];
buytrailstop = 0;
}
if(buytrailstop>0 AND low[i] < Buystop[i])
{
openposition[i] = 0;
bt1flag = 0; //reset target1 hit flag
Sell[i] = True;
SellPrice[i] = Buystop[i];
buytrailstop = 0;
}
// if buy signal continues -> upgrade my stoploss
if(buytrailstop>0)
{
if(mode=="Percentage")
{
buytrailstop = Max( Buytsllevel[i],buytrailstop); //Trailing to the newer stoploss
}
if(mode=="Points")
{
buytrailstop = Max( High[i]-tsl,buytrailstop); //Trailing to the newer stoploss
}
openposition[i] = openposition[i-1]; //previous open position continues
buytslarray[i] = buytrailstop;
}
} //end of for loop
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
buycontinue = Flip(Buy,Sell);
//Plot Stops and Targets, TrailingStops - only if the trade is continuing
Plot(IIf(buycontinue OR Sell, BuyStop,Null),"BuyStops",colorRed,styleDashed | styleThick);
Plot(IIf(buycontinue OR Sell, Buytarget1,Null),"BuyTarget1",colorGreen,styleDashed | styleThick);
Plot(IIf(buycontinue OR Sell, Buytarget2,Null),"BuyTarget2",colordarkGreen,styleDashed | styleThick);
Plot(IIf(buycontinue OR Sell, buytslarray,Null),"BuyTSL",colorYellow,styleDashed | styleThick);
/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(Sell * shapestar, colorBrightGreen, 0, High, 12);
PlotShapes(IIf(bt1hit, shapeCircle, shapeNone),colorGreen, 0,H, Offset=25);
Short = Ref(Short,-SignalDelay);
iCover = Ref(Cover,-SignalDelay);
ShortPrice = ValueWhen(Short,Open);
Shorttsllevel = Null;
if(mode=="Points")
{
ShortStop = ShortPrice + stops;
ShortTarget1 = ShortPrice - target1;
ShortTarget2 = ShortPrice - target2;
Shorttsllevel = ShortPrice + tsl;
}
if(mode=="Percentage")
{
ShortStop1 = (100+stops)/100*ShortPrice;
ShortTarget1 = (100-target1)/100*ShortPrice;
ShortTarget2 = (100-target2)/100*ShortPrice;
Shorttsllevel1 = (100+tsl)/100*Low;
//Round it of to nearest Tick Size
ShortStop = TickSz * round(Shortstop1/TickSz);
ShortTarget1 = TickSz * round(ShortTarget1/TickSz);
ShortTarget2 = TickSz * round(ShortTarget2/TickSz);
Shorttsllevel = TickSz * round(Shorttsllevel1/TickSz);
}
Shorttslarray = Null;
//if Shorttrailstop >0 -> long trades are running
Shorttrailstop = 0;
st1flag = 0;
st1hit = False;
st2hit = False;
for(i=0;i<BarCount;i++)
{
//Identifying Very First Short
if(Short[i] AND Shorttrailstop == 0)
{
//Calculate the trailstop
Shorttrailstop = Shorttsllevel[i];
openposition[i] = -1*Quantity; //fullposition
}
//else
//{
//Short[i] = false; //Removing Excessive Short
//}
//if Short signal continues at the same time if the trailstop hits
if(Shorttrailstop>0 AND high[i] > Shorttrailstop)
{
Cover[i] = True;
CoverPrice[i] = Shorttrailstop;
Shorttrailstop = 0; // reset the flag to zero -> so that it can take fresh Short signal
openposition[i] =0;
st1flag = 0;
}
//if Short signal continues at the same time if the Target1 hits
if(Shorttrailstop>0 AND low[i] < ShortTarget1[i] AND st1flag==0 )
{
st1hit[i] = True;
openposition[i] = Quantity + target1exit;
st1flag = 1; //enabling target1 hit flag - to avoid multiple target 1 hits
}
//if Short signal continues at the same time if the Target2 hits
if(Shorttrailstop>0 AND low[i] < ShortTarget2[i])
{
st2hit[i] = True;
openposition[i] = 0;
st1flag = 0; //enabling target1 hit flag
Cover[i] = True;
CoverPrice[i] = ShortTarget2[i];
Shorttrailstop = 0;
}
if(Shorttrailstop>0 AND high[i] > Shortstop[i])
{
openposition[i] = 0;
st1flag = 0; //reset target1 hit flag
Cover[i] = True;
CoverPrice[i] = Shortstop[i];
Shorttrailstop = 0;
}
// if Short signal continues -> upgrade my stoploss
if(Shorttrailstop>0)
{
if(mode=="Percentage")
{
Shorttrailstop = Min( Shorttsllevel[i],Shorttrailstop); //Trailing to the newer stoploss
}
if(mode=="Points")
{
Shorttrailstop = Min( Low[i]+tsl,Shorttrailstop); //Trailing to the newer stoploss
}
openposition[i] = openposition[i-1]; //previous open position continues
Shorttslarray[i] = Shorttrailstop;
}
} //end of for loop
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);
Shortcontinue = Flip(Short,Cover);
//Plot Stops and Targets, TrailingStops - only if the trade is continuing
Plot(IIf(Shortcontinue OR Cover, ShortStop,Null),"ShortStops",colorViolet,styleDashed | styleThick);
Plot(IIf(Shortcontinue OR Cover, Shorttarget1,Null),"ShortTarget1",colorRed,styleDashed | styleThick);
Plot(IIf(Shortcontinue OR Cover, Shorttarget2,Null),"ShortTarget2",colorDarkRed,styleDashed | styleThick);
Plot(IIf(Shortcontinue OR Cover, Shorttslarray,Null),"ShortTSL",colorAqua,styleDashed | styleThick);
/* Plot Short and Cover Signal Arrows */
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(st1hit, shapeCircle, shapeNone),colorGreen, 0,H, Offset=25);
PlotShapes(Cover * shapestar, colorRed, 0, Low, -12);
_SECTION_END();
//Dashboard Controls.
_SECTION_BEGIN("Trading Dashboard");
strategy_name = ParamStr("Strategy Name","Simple Trading System");
fontsize = Param("Font Size",14,12,36,1);
GfxSelectFont("BOOK ANTIQUA", fontsize, 400);
GfxSetBkMode(1); //Transparent Mode
GfxSetTextColor(colorWhite);
//build dyanmic dashboard colors based on the ongoing trades
color = IIf(buycontinue,colorGreen,IIf(shortcontinue,colorRed,colorGrey40));
GfxSelectPen(colorWhite);
GfxSelectSolidBrush(SelectedValue(color));
width = Status("pxchartwidth"); //output will be in terms of number of pixels
height = Status("pxchartheight");
//GfxRoundRect(20,height-150,320,height-30,15,15);
GfxGradientRect(20,height-150,320,height-30,SelectedValue(color),colorGrey40);
sigstatus = WriteIf(buycontinue,"Buy Signal",WriteIf(shortcontinue,"Short Signal","No Trade - Relax"));
PNL = IIf(buycontinue,Close-buyprice,IIf(shortcontinue,ShortPrice-close,Null));
GfxTextOut(strategy_name,30,height-150);
GfxTextOut("Target1"+" : "+IIf(buycontinue,Buytarget1,IIf(shortcontinue,Shorttarget1,Null)),30,height-120);
GfxTextOut("Target2"+" : "+IIf(buycontinue,Buytarget2,IIf(shortcontinue,Shorttarget2,Null)),30,height-100);
GfxTextOut("Stop"+" : "+IIf(buycontinue,Buystop,IIf(shortcontinue,Shortstop,Null)),30,height-80);
GfxTextOut("TSL"+" : "+IIf(buycontinue,Buytslarray,IIf(shortcontinue,Shorttslarray,Null)),30,height-60);
_SECTION_END();
The post Stoploss, Multi Target, and Trailing Stoploss Module – Amibroker AFL Code appeared first on Marketcalls.