Stops on Percentages for Amibroker (AFL)
kaiji over 16 years ago Amibroker (AFL)
Stops on percentages can be set to low,high,close and open on the bars for long and short stops.
By Chris
Indicator / Formula
Copy & Paste Friendly
// code from http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
// I added the short side and some options. Just drag and drop onto a price chart
_SECTION_BEGIN("Long Short % Stop");
//---long var
Stoplong = 1-Param("stop % Long", 7, 0.1, 20, 0.1) / 100;
Plong = ParamField("Price Field Long");
Longcol = ParamColor("Color Long Stop", colorCycle);
Buy = 1; //Change for your long signal eg Short=Cross(ma(c,10),ma(c,20));
Sell = 0;
traillongARRAY = Null;
trailstoplong = 0;
//----short var
StopLevel = 1-Param("Stop Short %", 7, 0.1, 20, 0.1) / 100;
P = ParamField("Price Field Short");
Shortcol = ParamColor("Color Short Stop", colorCycle);
Short = 1; // Change for your short signal eg Short=Cross(ma(c,20),ma(c,10));
Cover = 0;
trailARRAY = Null;
trailstop = 0;
//---calculate
for (i = 2; i < BarCount; i++)
{
//---long
if (trailstoplong == 0 AND Buy[i])
{
trailstoplong = Plong[i] *Stoplong;
}
else
Buy[i] = 0;
// remove excess buy signals
if (trailstoplong > 0 AND Plong[i] < trailstoplong)
{
Sell[i] = 1;
SellPrice[i] = trailstoplong;
trailstoplong = 0;
}
if (trailstoplong > 0)
{
trailstoplong = Max(Plong[i] *Stoplong, trailstoplong);
traillongARRAY[i] = trailstoplong;
}
//---short
if (trailstop == 0 AND Short[i])
{
trailstop = P[i] / StopLevel; // set intinal % stop then trailing stop takes over
}
else
Short[i] = 0;
// remove excess short signals
if (trailstop > 0 AND P[i] > trailstop)
{
Cover[i] = 1;
CoverPrice[i] = trailstop;
trailstop = 0;
}
if (trailstop > 0)
{
trailstop = Min(P[i] / stoplevel, trailstop);
trailARRAY[i] = trailstop;
}
}
// ----display results
Plot(traillongARRAY, "Stop Long", Longcol);
Plot(trailARRAY, "Stop Short", shortcol);
//PlotShapes(Short*shapeUpArrow,ParamColor( "Arrow Short Color", colorCycle),0,Low);
//PlotShapes(Cover*shapeDownArrow,ParamColor( "Arrow Cover Color", colorCycle),0,High);
//PlotShapes(Buy*shapeUpArrow,ParamColor( "Arrow Buy Color", colorCycle),0,Low);
//PlotShapes(Sell*shapeDownArrow,ParamColor( "Arrow Sell Color", colorCycle),0,High);
//Plot( Close,"Price",colorBlack,styleBar);
_SECTION_END();0 comments
Leave Comment
Please login here to leave a comment.