Setting up a screen/backtest for BlackStar's Trend Following system

A study Blackstar did many years ago was to test if trends work. Their test chose an entry point with an indicator of the ultimate trend – an all-time new high of the stock price.

Then they created an exit signal as well, based on a condition of technical indicators.

I’m trying to do something like that with P123. I’ve got a very simple model set up right now:

https://www.portfolio123.com/app/screen/summary/231671

Universe(SP500)
ShowVar(@NHV, HighVal(2500, 0))
ShowVar(@NHB, HighValBar(2500, 0))
ShowVar(@LV, LowVal(@NHB, 0))
@LV > 0.85 * @NHV

I just chose the S&P 500 universe so I could easily limit the number of stocks in my testing of the rules.

Basically, it finds when the highest price was within the last 2500 days (a P123 limit). Then it checks the lowest price since it hit that high price, omitting any stock that has pulled back more than 15% from that high (arbitrary).

This gives about double the return of the S&P 500, with about half the drawdown.

However, what I would like to do is check the lowest value of a technical indicator instead of the lowest price. As a simple example, how would I omit any stock that has had a daily RSI(14) of less than 50 since it hit a new high?

I thought I could use LOOPMIN(), but it appears a variable value like @NHB cannot be passed for the “iteration” parameter.

Any suggestions? Am I overlooking something simple?

Can you just do it directly?

LoopMin("RSI(14,CTR)",300)

This may work for your particular example;

ShowVar(@RSIB, isNA(LoopSum("Eval(RSI(14,CTR)>=50,1,NA)",502,0,1,1,1),0))
@RSIB>=@NHB

In addition to being a hack, LoopSum limits the look-back to 502 bars. You may be able to concatenate additional LoopSums to look back as far as you want. I didn’t try that.

Walter

EDIT: It appears that LoopSum needs to be limited to 300 bars since that’s the offset limit for RSI.

Except I needed it to be:

LoopMin(“RSI(14,CTR)”,@NHB)

…and I would have to make sure @NHB is no larger than 300, because of the RSI() limitation. But it balks at @NHB being there instead of a fixed integer.

The only way that I can think of to do this on P123 would be the Min function.

SetVar(@min1,Min(RSI(14,0),RSI(14,1),RSI(14,2)…)
SetVar(@min2,Min(RSI(14,21),RSI(14,22),RSI(14,23)…)
ShowVar(@min,Min(@min1,@min2…))

Each min would be able to handle up to 20 calculations. Personally, I view this as too much work to use practically out to 300, but the structure should work. I’d probably use custom formulas instead of SetVar, so that I would NEVER have to create it again.

Thanks. I’ll give that a try. I did run into the limitations on other testing. I’m hoping they aren’t going to be an issue, although I’m sure at least one exception would show up. :frowning:

The EVAL() function was the key that I needed. The variation I ended up with:

ShowVar(@RSIB, LoopMin(“Eval(CTR<@NHB,RSI(14,CTR),NA)”,300,0,1,1,1))
@RSIB > 50

But that was just a simple technical analysis formula I used to frame the question. Not sure what I’ll end up with.

ShowVar(@RSIB, LoopMin("Eval(CTR<@NHB,RSI(14,CTR),NA)",300,0,1,1,1))

Cool. That expression didn’t even occur to me. I’ll kick it around, too. My attempt just stuck to comparing bar counts.