Coding question for SPY shorting SIM

Good morning, I’m looking for overbought situations when the SPY is in a downtrend (SPY SMA < 200d) to get long SH. I create my own variable which is the sum of the last 4 days close of ETF “SH”. Then I want to buy SH when that variable is at the lowest level in 3 days. I tried this code below, but get an error. Any idea where I’m going wrong in the second line of code?


In line #2, it could be the (0). Line #1 setvar is @closesum, (no (0)). Unless you can use (x) on variables, but I wasn’t aware that you could.

Hmmm… taking out the (0) itself from the second line does not solve the issue. I wonder if I use a counter if P123 would save the variable output for comparison?

There are a couple of problem with the code.

If you want to find if the current SP500 bar close is lower that the three prior closes (inclusive), try;

Close(0,$sp500)=LowVal(3,0,$SP500)

If you want to exclude the current bar from the history, set the offset parameter to one;

Close(0,$sp500)=LowVal(3,1,$SP500)

Or try using an eval statement:

Is this what you’re after?

Reading your question again, you’re looking at the sum of the last 4 days of SH, and then go long at the lowest day. I think for this you’d need to create a custom aggregate series for SH (import close prices), then track the 4 day sum in the series. The “lowval” screen rule should work then. Haven’t tried this, but after trying different ideas, this seems to be the way (according to the documentation).

Thank you, yes that’s what I’m looking for (a 3 day low in the sum of the last 4 close days of SH). It might be easier to just use the 3 day high point in the #SP500 series. Then buy SH when the custom variable hits a 3 day high.