Exit "indicator-trade" after Nbars, plus extending that Nbars after another condition happens during the trade

Capture

_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();

mydatetime = DateTimeToStr(GetCursorXPosition());
myBarIndex = BarIndex();

MA_lookback = 22; 
MA_Price = ParamField("MA_Price",3);
MA_Color = ParamColor("MA_Color", colorYellow );
MA_Style = ParamStyle("SlowStyle", styleLine | styleNoLabel);
EntryExit_Style = ParamStyle("EntryExit_Style", styleDashed | styleNoLabel);
ExitNbars = 5;

myMA = MA(MA_Price, MA_lookback);
myATR = ATR(20);

myEntryPrice = myMA - 1 * myATR;
myExitPrice = myMA + 1 * myATR;

Plot(myMA,"myMA",MA_Color,MA_Style);

Plot(myEntryPrice,"myEntryPrice",colorWhite,EntryExit_Style);
Plot(myExitPrice,"myExitPrice",colorWhite,EntryExit_Style);


EntryPotentialPrc = Min(Open, ref( myEntryPrice, -1 ));
ExitPotentialPrc = Max(Open, ref( myExitPrice, -1 ));

EntryCondition = Low <= Ref(myEntryPrice, -1);
ExitCondition = High >= ref( myExitPrice, -1 ); // OR EntryBarsSince >= ExitNbars ;


Flip1 = Flip( EntryCondition, ExitCondition );
EntryPrc = ValueWhen( Flip1 == 1 AND Ref(Flip1, -1) == 0, Ref(EntryPotentialPrc,0)); 
PlotShapes(IIf(ref( Flip1, 0 ) == 1, shapeSmallCircle, shapeNone),colorAqua,  0, EntryPrc, Offset=0); 

ExitPrc = ValueWhen(  High >= ref( myExitPrice, -1 ) AND ref( Flip1, -1 ) == 1 , Ref(ExitPotentialPrc,0)); 
PlotShapes(IIf(ref( Flip1, 0 ) == 0 AND ref( Flip1, -1 ) == 1, shapeSmallCircle, shapeNone),colorWhite,  0, myExitPrice, Offset=0); 

EntryBarsSince = BarsSince(Flip1 == 1 AND Ref(Flip1, -1) == 0);

printf( "EntryBarsSince: " + NumToStr( EntryBarsSince ) + "\n" );

I have an "indicator trade" here, IE: No Amibroker Buy or Sell. Because of this, I cannot use ApplyStop of NbarExit. I am trying to disqualify (or exit) the indicator-trade after "ExitNbars", which is an input at the top. I acquire the number of bars since the trade has been on with "EntryBarsSince" at the bottom of the code. And I would like to exit this indicator-trade after X bars. You can see my attempt (remmed) in the "ExitCondition", but that is acquired too late, because it comes after ExitCondition thus I cannot use it in ExitCondition.

How do I solve this? Can I use a loop to look ahead somehow? Also, it gets a tad more difficult because I actually have something else that will extend the ExitNbars from 5 to 10 if another condition happens but I figured we would get this first part solved first. But I wanted to mention this so that you can think about it and maybe solve both in one step.

@vjsworld perhaps changing your signal from a "State" signal to an "Impulse" signal will help (though I do not know the nature of your planned trading strategy).

This is an example of what I am referring to (untested) and perhaps you can see if it helps achieve your goal. Good luck!

// compare to original EntryCondition etc.
// an impulse signal instead of a state signal
EntryCondition2 = Cross( Ref( myEntryPrice, -1 ), Low );
SinceEntryCross = BarsSince(EntryCondition2);
ExitCondition2 = Cross( High, ref( myExitPrice, -1 ) ) OR SinceEntryCross > ExitNbars ;
Flip2 = Flip( EntryCondition2, ExitCondition2 );

2 Likes