Force exit in a trend following system

Hello all,

I wanted to reach out to ask for help with a situation I've encountered a few times within a trend following system. I'm seeking a way to have the system exit a specific position at a specific date, and I haven't been able to find a way to do so. Basically I feel like I'm asking how to be able to add discretion into a system.

For example the system will generate an entry signal rarely on a security like a preferred stock or acquisition vehicle which then never has enough movement to generate an exit signal and the system gets stuck in the position. Or a an existing holding has been acquired and instead of recognizing the acquisition and generating new entry, it is going back in time and suggesting that it entered a different position.

I'd like to not change the base code of the system which would then change the backtest. For example I'm trying to not enter a hold period or similar to remove the positions that it gets stuck in. Some have been very good long term holdings.

I feel like the acquisitions might be a separate issue as I note that my data source (Norgate Premium Data) doesn't have the symbol anymore, so it's generating a survivor bias.

What I have been trying to do is code something like "if Symbol=X and date=Y sell the position". I don't think that is something that I can code into my system after a few attempts. Is this something I would accomplish by editing the data source? For example would I just somehow overwrite a data value that could force an exit? Or is this something I would need to do in a custom backtest. I'm not at all familiar with the custom backtests. Programming is not my strongest suite so if that is the case any resources to help with learning how to do that would be appreciated.

Please let me know if I can address any questions to help with the answer. I appreciate you taking a look!

@paulbruns, I think that actually, you can include in your code some specific SELL clause.

Take a look at his backtest section (created from snippets).
The sample target in my example is AEP (the original trade lasted 22 bars):


If I add this code:

// your normal sell rules above here...
dn = dateNum(); // I use DateNum() because the numerical date format is unambiguous
// force a SELL signal on AEP on March, 1 2024 - ignored if you do not have an open AEP position
Sell = iif(Name() == "AEP" and dn >= 1240301, True, Sell); 
Buy  = iif(Name() == "AEP" and dn >= 1240301, False, Buy); // avoid also futures buy

The backtest results are this (see the shorter trade - in my test, I buy/sell on open with a delay of 1 so the operation is closed on the next trading day - March 1, 2024, is a Friday).

Then the backtest later no longer buys this asset (and obviously from the above date results may vary due to new signals/positions).

This is a contrived example. In real trading, you should add code (similar to the one posted) to operate on future dates.


Hello Beppe,

That works. Thank you! I didn't realize the solution could be so simple. I guess in my digging I didn't come across or know how to use Name. Thank you for a quick answer with great examples, I really appreciate it.