Exit Condition Based on the 5th Wednesday After Trade Entry

Hello AmiBroker community,

Merry Christmas and Happy New Year. I hope you are all doing well. I am currently working on refining my trading strategy, and I am facing a challenge in implementing an exit condition that requires exiting on the 5th Wednesday after entering a trade.

I have attempted to exit after a certain number of bars

bars = Optimize("exit_nbar", 23, 23, 27, 1); // exit after 5 weeks
ApplyStop( stopTypeNBar, stopModeBars, bars, True );

While this partially achieves the goal of exiting after 5 weeks, it doesn't ensure that the exit falls on a Wednesday.

I've also considered using DayofWeek()==3,BarsSince() or Sum() but my strategy can open multiple positions at the same time with SetBacktestMode( backtestRegularRawMulti ). Managing individual open positions to exit exactly on the 5th Wednesday is proving to be challenging.

Could someone please provide guidance or suggest an approach to implement an exit condition that ensures the exit occurs on the 5th Wednesday after entering each individual trade?

Any assistance or insight would be greatly appreciated. Thank you in advance!

Best regards

To elaborate on the question a bit. Let's say the system open a trade on the week 1 regardless of which day, then it has to exit on the Wednesday of week 6.

You might try something like this. SumSince(Buy, DayOfWeek() == 3);

@datascience, maybe that's overkill, but I think using low level CBT allows you to do it also when using backtestRegularRawMulti.
I suggest, in the first phase, to create a weekIDs array like:

dow = DayOfWeek();
newWeek = dow < ref(dow, -1);
weekIDs = NZ(cum(newWeek), 0) + 1;

and then in the inner CBT loop over OpenPositions, for every open trade, lookup the weekID corresponding to its entry date. Subtract it from the current bar weekID and check for your exit conditions (to exit also when Wednesday and/or following days are holidays) - Something like:

	if ( 
		((weekIDs[i] - entryWeek) == 5) AND (dow[i] >= 3) OR
		 ((weekIDs[i] - entryWeek) > 5)
		)  // bo.ExitTrade...

Maybe there are simpler methods, but if you don't get better suggestions you could try this route.

Thanks beppe. I will take a look and try it out. Maybe a simpler way is to use Python to backtest in this specific case. I thought of a solution but might not be totally accurate:

Sell = DayOfWeek()==3 AND sum(Buy,23) == 0
1 Like

Hi Ray,

My backtesting nearly always ends up using a CBT (and would therefore will tend to use a solution similar to @beppe), if you would be happy taking the Python route then the performance hit of a phase one idea like this might a possibility. Note: not extensively tested.

DoW = DayOfWeek();

IsWed = DoW == 3;
WeekCount = Cum( Dow < Ref( Dow, -1 ) );

Sell = False;
Intrade = False;
for( i = 0; i < BarCount; i++ )
{
    if( Buy[i] AND NOT InTrade )
    {
        InTrade = True;
        BuyWeek = WeekCount[i];
    }
    if( InTrade AND IsWed[i] AND WeekCount[i] == BuyWeek + 5 )
    {
        InTrade = False;
        Sell[i] = True;
    }
}

John.

Thanks John! This looks like the solution I need. Will test out the code

You could try

Sell = SumSince( Buy, DayOfWeek() ==3 ) == 5; // 5th wednesday since buy

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.