Hi @Tomasz - It turns out the code and trades are correct but the chart is showing incorrect sell arrows. Not sure why though. I am not explicitly plotting the signals and instead rely on the option in chart parameters to show trade arrows.
Below is the chart (with incorrectly plotted signals) and corresponding trades in the back test results.
Chart:
Trades:
My guess is the chart is using RSI signal exit in the code as the signal to plot instead of actual sell signal that is triggered by stop loss. Don't know why.
@durga_public - you need to read more docs. There are THREE different kind of arrows that show different things.
You displayed “RAW” Arrows that are just raw, unprocessed signals that you put into Buy and Sell variables. They are RAW, do NOT include any processing (so they also don’t include any ANY stops)
You should have used Show current trade arrows instead.
Hi @Tomasz - Thanks. I searched docs to figure issue with my code but has not realized there are different types of Arrow signals and should look for docs on that.
Hi @fxshrat - Thank you for the links. Couple comments -
The first link says the equity function is available only for backward compatibility.
I noticed that even if I put plots in each of my strategies, when I click the rows in the backtester window, the plots show up in bottom panels of the chart. It means, I have to recreate my default chart layout in each strategy and resize panels to see the trade markers in the context of my chart settings.
@Tomasz solution is perfect for me as I have to just right click and say “show current trades”. They will show up in the context of my default chart layout and I do not have to plot in each strategy.
Yes, for majority of things Equity function is obsolete since existence of Portfolio equity.
But that does not mean it is not required anymore.
If you have single security systems and you want to plot Applystops via PlotShapes then it is required to use Equity() function to make stops appear via PlotShapes.
If you want to plot Applystop() via PlotShapes() function you have to evaluate stops via Equity() function.
Also check again in comments section of ApplyStop function reference of users guide:
That way you can give different colors and different shapes for each exit via plotshapes contrary to clicking plotting trade arrows via backtester where all exits have same color and same shape.
So here is full example using your code from first post.
/// @link http://forum.amibroker.com/t/problem-using-applystop-to-trigger-max-stop-loss/1966/7
rsiLength = 2;
buySignal = Cross(RSI(rsiLength), 10);
buyLimitPrice = ValueWhen(buySignal, Close) - (0.1 * ATR(10));
Buy = Ref(buySignal, -1);
Buy = Hold(Buy,2) AND (Low < buyLimitPrice); //Buy = Buy AND (Low < buyLimitPrice);
BuyPrice = Min(Open, buyLimitPrice);
sellSignal = Cross(90, RSI(rsiLength));
Sell = sellSignal;
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
Short = Cover = 0;
// Stops
buySignalBarLow = Ref( ValueWhen(buySignal, Low), -1 ); // use entry bar low
maxLossStopPoints = BuyPrice - buySignalBarLow - 0.1*ATR(10);
ApplyStop(stopTypeLoss, stopModePoint, maxLossStopPoints, ExitAtStop=2);
ApplyStop(stopTypeProfit,stopModePoint, 2*ATR(10), ExitAtStop=1);
// Sample code for chart pane to plot shapes
if( Status( "action" ) == actionIndicator ) {
SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );
//_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
Plot( C, "Price", colorDefault, styleCandle );// comment this line if you have price plot already (i.e. if overlaying on a price chart)
Equity( 1, 0 ); // evaluate stops, all quotes
PlotShapes( Buy*shapeSmallUpTriangle, colorGreen, 0, L, -15);
/*
1 - regular exit
2 - max. loss
3 - profit target
4 - trailing
5 - n-bar stop
6 - ruin stop
*/
regSell = Sell == 1;
PlotShapes( regSell*shapeSmallDownTriangle, colorRed, 0, H, -15);
SLstop = Sell == 2;// max loss
TGTstop = Sell == 3;// profit target
PlotShapes( SLstop*shapeDownArrow, colorOrange, 0, H, -15 );
PlotShapes( TGTstop*shapeDownArrow, colorAqua, 0, H, -15 );
}
Here is sample picture using upper code applied on SPY.
So read shape is standard stop. Orange colored shape is SL. And aqua colored shape is profit target stop.
End of first part
This is your picture for others to understand
Solution is very simple.
Close the bottom pane
Save my upper plotshapes sample code to AFL file
Instead of double clicking that system AFL from "Charts" window first left click the top price pane (the one of your picture)
Then right click my upper system AFL sample code (the one being saved to Charts window before) and choose "Overlay" in context menu.
The system code will be overlayed on top pane’s price. It is a copy of your originally saved one.
If you want to remove that system code from top pane again do following:
Right click the Chart title of the top pane (the one with price)
in the appearing context menu you will see the name of your system code (and before that one you will see Delete…) So click that specific deletion line and the copy of your system code will be removed again from your top chart pane only but not from Charts window (since it is just a copy). So don't panic.