Open position on weekly drop in a given % range
Author: lanatmwan
Creation Date: 5/27/2020 3:18 AM
profile picture

lanatmwan

#1
I'm trying to setup a pretty basic strategy using rule-based before I make customizations via code. I'm hoping someone can point out how to set the condition to open the position as I'm seeing rules similar to what I need but not quite right.

SPY Strategy:

-If previous week closed down between -0.5% and -3.0% (vs. the previous week)
- Buy at the open Monday
- Sell Friday at close OR Sell if 3% trail stop triggered

profile picture

Eugene

#2
With Rules available you can hardly accomplish this using Daily data. If you switch to Weekly bars you can get it done several ways:

A. From "Price (or Volume) Action" group of conditions:



Buy At Market (i.e. Monday)
Price is higher by X% than it was a number of bars ago (Lookback = 1, Percentage = -3)
Price is lower by X% than it was a number of bars ago (Lookback = 1, Percentage = -0.5 0.5)
Sell At Close (i.e. Friday)
OR
Sell at percent-based Trailing Stop (Percent: 3.0)


B. From "ROC" group of conditions:



Buy At Market (i.e. Monday)
Price is more than X% below previous day's Price (Lookback = 1, Percent = -3)
Price is more than X% above previous day's Price (Lookback = 1, Percent = -0.5 0.5)
Sell At Close (i.e. Friday)
OR
Sell at percent-based Trailing Stop (Percent: 3.0)

profile picture

lanatmwan

#3
I really appreciate the detailed response.

After getting the community rules I was able to setup option A but I wanted to visually verify the results on the chart but they don't look right to me. For example, if I am reading the chart right, the new position in the attached pic gets placed after a week where the market closed 0.18% up from the previous week. I verified both price conditions were set the way you indicated and they are definitely set to negative values. Am I misreading the chart or is there an actual problem?


profile picture

Eugene

#4
I stand corrected. Despite it may seem counterintuitive, the correct way to use the "Price is lower by X%..." condition is to set the percentage in positive numbers:

Price is lower by X% than it was a number of bars ago (Lookback = 1, Percentage = 0.5)

Otherwise the generated code takes the 1.005% value instead of 0.995%
CODE:
Please log in to see this code.


Same fix to Option B. Post #2 edited.

profile picture

lanatmwan

#5
Counter intuitive indeed but that worked great, thanks!
profile picture

Eugene

#6
You're welcome!
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).