Hi all,
My short system is supposed to cover its trades in 2 ways. However, on different cover prices and I hope to get your help to code the respective loop. Here the details
1 - Profit case: If the price crosses / opens below the dark "cover line" I want the backtest to cover the trade immediately/on the same bar on the level of the line (or OPEN if that is lower) - this would be at point 1 on the picture
2 - Loss case: If the price touches the red stop loss line (point 3) I want the backtest to cover the trade on the next bar OPEN (point 2).
HOWEVER, if on the same bar the price touches the stop loss AND the cover line only rule 1 shall apply and the trade is covered immediately on the level of the line (or OPEN if that is lower)
I tried to do it without a loop but if I change the applystop to "2" the backtest takes the LOW of the next bar open, which is why I think a loop would solve the problem. Beneath the picture is the code I used. Many thanks for your inputs in advance!!!
Nico
CoverLine = Ref(EMA(C,6),-1)
CoverTrigger = L <= CoverLine;
Cover = CoverTrigger;
CoverPrice = min(open,Coverline);
ApplyStop(stopTypeLoss,stopModePercent,60,1);
// How do I code a loop that ...
// 1. ... in case only the CoverLine is touched (or price just is below that line) the backtest exits on the coverline price/OPEN of the SAME bar
// 2. ... in case the stop level AND the CoverLine are both touched on the same bar the backtest exits on the coverline price/OPEN of the SAME bar
// 3. ... in case the stop level is touched (but not the CoverLine) the backtest exits on the OPEN price of the NEXT bar
I tried my luck in coding the loop but so far no luck - at the moment I get Error 29, Variable ExitPrice used without having been initialized.
Below you see the code, that is supposed to determine both the cover price as well as the trade bar for the actual exit of the trade (current bar or next bar, depending on exit trigger). I'd be super grateful if anyone could help me.
// Logic for covering an open trade with CoverLine = EMA(6) and InitialStopLossLine = Initial Open * 1,6
// 1. Exit trade if price opens below CoverLine at the OPEN price of the current bar
// 2. Exit trade if price touches CoverLine at the price of the CoverLine of the current bar
// 3. If price touches InitialStopLossLine and the CoverLine on same bar, exit on the same/current bar at CoverLine
// 4. If price touches InitialStopLossLine but not the CoverLine exit at the OPEN of the NEXT bar
// 5. If price does not touch/exceed either InitialStopLossLine or CoverLine stay in trade
CoverLine = Ref(EMA(C,6),-1); // Cover price = Open or Short Term EMA from previous bar
for (i = 0; i < BarCount; i++) // Loop through all bars
{
if (Short [i]) // If in trade
{
if ( Open [i] <= Coverline [i] ) // If exit is indicated by Open < CoverLine
{
Cover [i] = True; // Trade covered
ExitPrice [i] = Open [i]; // Return Bar's OPEN as Exit price
}
if ( Low [i] <= Coverline [i] ) // If exit is indicated by Low <= CoverLine
{
Cover [i] = True; // Trade covered
ExitPrice [i] = Coverline [i]; // Return Bar's OPEN as Exit price
}
if ( High [i] >= Open [0] * ( 1*StopLossPercent/100 ) ) // If exit is indicated because High >= Initial Stop Loss level (based on Open of the trade's FIRST bar)
{
Cover [i+1] = True; // Trade covered NEXT Bar
ExitPrice [i] = Open [i+1]; // Return NEXT Bar's OPEN as Exit price
}
}
else
{
Short [i] = True; // Still in trade
}
}
CoverPrice = ExitPrice; // Cover price is the exit price determined by above cases
/// @link https://forum.amibroker.com/t/looping-to-generate-cover-prices-dependent-on-stop-loss-or-profit-exit/25497/3
/// 1. ... in case only the CoverLine is touched (or price just is below that line)
/// the backtest exits on the coverline price/OPEN of the SAME bar
/// 2. ... in case the stop level AND the CoverLine are both touched on the same bar
/// the backtest exits on the coverline price/OPEN of the SAME bar
/// 3. ... in case the stop level is touched (but not the CoverLine)
/// the backtest exits on the OPEN price of the NEXT bar
/// by AmiBroker.com and fxshrat@gmail.com
SetPositionSize( 1, spsShares );
percent = 2;// set percent stop loss value
Short = Cross(MA(C, 50), C);
ShortPrice = Open;
profit_stop = Ref(EMA(C, 60), -1);
is_profit = L <= profit_stop;
Cover = 0;
short_stop = Null;
pcnt_level = 1 + percent/100;
shortentryprice = shortentrybar = 0;
for ( i = 1; i < BarCount; i++ ) {
if ( Short[ i ] && shortentryprice == 0 ) {
shortentryprice = ShortPrice[ i ] * pcnt_level;
shortentrybar = i;
} else Short[ i ] = 0;
is_loss = H[ i - 1 ] > shortentryprice AND i >= shortentrybar + 1;
CoverSig = (is_loss OR is_profit[ i ]);
if ( CoverSig && shortentryprice > 0 ) {
Cover[ i ] = 1;
if ( is_loss )
CoverPrice[ i ] = Open[ i ];
else
CoverPrice[ i ] = Min(O[ i ], profit_stop[ i ]);
shortentryprice = 0;
} else Cover[ i ] = 0;
if ( shortentryprice > 0 )
short_stop[ i ] = shortentryprice;
}
Plot( C, "Price", colorDefault, styleBar );
Plot( profit_stop, "profit stop", colorGreen );
Plot( short_stop, "loss stop", colorRed );
PlotShapes( Short * shapeDownArrow, colorViolet, 0, H );
PlotShapes( Cover * shapeUpArrow,colorAqua, 0, L);
PlotShapes( (Short OR Cover) * shapeSmallCircle,
IIf( Cover, colorAqua, colorViolet ), 0,
IIf( Cover, CoverPrice, ShortPrice ), 0 );
_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 ) );
Thanks to your code I was able to deep dive and find out that my short system actually works better with an in-market stop loss that covers a trade immediately once touched. Otherwise there are too strong gap ups against the system. I now use the cover to profit on the same bar as previously described and the "normal" applystop that covers the trade immediately when touched. As last test I would like to add a nbar stop to this system that covers the trade on the close of the 6th bar. My problem is that I cannot just use applystop(stopTypeNBar) because the system would use the coverline as exit price. In the following I summarized what I would like to accomplish. The difference to before is 1. the in-market stop loss and 2. the nbar stop on the close of the 6th bar (want to optimize later to find the most suitable "n").
I hope its ok if I could ask you once more for your expertise - how would you code that?
Thanks so much for any feedback on this!!!
Nico
// Logic for covering an open trade with CoverLine = EMA(6), InitialStopLossLine = Initial Open * 1,6 as well as nbar stop at 6.
// 1. Exit trade if price opens below CoverLine at the OPEN price of the current bar
// 2. Exit trade if price touches CoverLine at the price of the CoverLine of the current bar
// 3. If price touches InitialStopLossLine but not the CoverLine exit on current bar at initial stop loss level
// 4. If price touches InitialStopLossLine and the CoverLine on same bar, exit on the same/current bar at initial stop loss level (NEW)
// 5. If price does not touch/exceed either InitialStopLossLine or CoverLine stay in trade until nbar stop and exit on the 6th bar
CoverLine = Ref(EMA(C,6),-1)
CoverTrigger = L <= CoverLine;
Cover = CoverTrigger;
CoverPrice = min(open,Coverline);
ApplyStop(stopTypeLoss,stopModePercent,60,1);