max hold days script

Hello, I am trying to create a sell script that does what the max hold days function in the backtester does. I would like to use it as part of an “or” function as a sell criteria. What’s the best way to do this? Do I need to put the buy criteria in the code in order to get a TimeSinceSignal?

For instance I would like to simply have a “sell 30 days from entry” option.



Hi Chad and Others using this Forum

When seeking assistance on a script, please help yourself and those of us that are inclined to respond by ALWAYS posting your attempt at the script. Include in your script comments describing what the follow line(s) of code is a attempting to achieve. The more comments the better. That way we have a chance of understanding exactly what it is you are trying to achieve, and you, and others, will learn by the advice subsequently provided. Also, your attempt may only need minor changes and so engenders a more speedy response (like one I addressed the other day that only need commas in numbers changed to periods). The more detail provided as to what you are trying to do makes is much easier to provide assistance, so also include, when possible, a chart annotated with appropriate comments that shows the undesired outcome of your unsuccessful attempt at scripting.

Note, I am not an employee of Optuma, but one who is willing to assist those that demonstrate that they have tried something and are not just asking for some-one else to do all the work in creating the script desired.



Hi Chad,

As an Enterprise Services client you have access to the quantitative Signal Tester which will allow you to set the Periods After to 30. This will calculate the average performance and statistics 30 days after each signal without the need for an exit, as per this example (tip: drag the vertical yellow line in the results to the left to see the results before the 30 days):




Hey Darren, thanks, but I think my question was misunderstood. I am looking to write my own script to find the number of days in a trade so that I can then use it as an “or” function in the backtester sell script top exit rather than the “N max hold days” option provided (see screen shot).

If you use the max hold day function in the backtester it will always sell after 30 days. I do not want this, but I want it as an option. I want to run a script that will sell based on my 1. “other sell criteria” “or” 2. a “30/60/90/120/n # of holding days” but do not know how to best go about the n # of holding days script.

Is the correct way to do this to use TimeSinceSignal or is there a better command?

I looked over the entire list of commands and couldn’t find one for DaysInTrade or something similar. Thanks



Hi Chad,

There’s two ways to do this, which is best will depend on your preference. The TimeSinceSignal() function is one way, however if your entry script occurs again within the max time count you’ve set, the count will reset. If your entry criteria triggers often enough it’s possible you may never exit out.

The other option is to use the Offset() function. You can wrap it around your entry script and adjust the offset to the max days you want. This will trigger 30/60/90 days after an entry criteria has triggered, even if another entry occurs within that time.

Here’s a quick example using Close CrossingAbove 150SMA as the entry script so you can see how the two work:


Green = Entry Criteria, Red = Max Day Exit

The two scripts in the above example:

Time Since Signal Example

V1 = CLOSE() CrossesAbove MA(BARS=150) ;
Plot1 = V1 ;
Plot2 = V2 ;
plot2.Colour = Red;

Offset Example

V1 = CLOSE() CrossesAbove MA(BARS=150) ;
Plot1 = V1 ;
Plot2 = V2 ;
plot2.Colour = Red;