Multiple timeframe script: 5-min & Daily RSI and SMA
Author: sfronz
Creation Date: 1/3/2020 9:01 PM
profile picture

sfronz

#1
Hello All,

I was wondering if someone could take a quick look at my script and see what I am doing wrong. I have not scripted in a few years and my attempt to adjust a rules based script based on a few existing scripts from Eugene is unfortunately not quite hitting the mark.

The intent of the script is to purchase based on a 5-min bar above the 8 period moving average, if the price is > 200 day SMA and if the 2-day RSI < 25. Exit based on an RSI > 70 or if held 8 days or longer.

Any help or pointing in the right direction would be appreciated.

I am coding on Fidelity Wealth-Lab Pro 6.9.

CODE:
Please log in to see this code.
profile picture

Eugene

#2
Hello,

I don't know what you're doing wrong but if you clarify on how the end result is different from your expectations, that might help troubleshooting.

QUOTE:
if (bar - p.EntryBar >= 1040)

Hardcoding number of intraday bars that way is (almost) always suboptimal. Here's a visual and easy design pattern that should do the job:

Intraday | Building blocks of Intraday trading strategies > Exiting after N days in an intraday strategy
profile picture

sfronz

#3
Thank you Eugene. To clarify, the system is not buying or selling when it should be based on separate programs for things such as the RSI(2)<25 logic.

However, in poking around at the link provided above, I cannot get "Exiting after N days in an intraday strategy" to operate correctly and sell consistently on the 3rd day when using the unaltered posted code against Dow 30 5-min Fidelity data.

Would that point towards a data issue?


profile picture

Eugene

#4
QUOTE:
To clarify, the system is not buying or selling when it should be based on separate programs for things such as the RSI(2)<25 logic.

Still, I don't know when it "should" be taking the trades you expect. To make sure your expectations are reasonable, could you point to an example of a deviation that is easy to reproduce? Don't forget to include the symbols, single symbol or multi-symbol mode, your position sizing, and data loading settings.

QUOTE:
Would that point towards a data issue?

Again, that would just point to a lack of details. How did you determine it, precisely?
profile picture

sfronz

#5
The simplest example of a deviation would be that the system is buying and selling on the same day. The 2-day RSI cannot be both greater than 70 and less than 25 on the same day.

I've disabled the 8 bar intraday trigger to help isolate the issue. Code below.

I downloaded the Dow 30, 5-min bars from Fidelity through WL.

I ran the backtest on the same 5 min bars with $5000 per trade for 5000 bars.
profile picture

Eugene

#6
QUOTE:
The simplest example of a deviation would be that the system is buying and selling on the same day.

It's perfectly OK since your code runs intraday and is not programmed to not take subsequent trades on the same day. Why would one expect the opposite?

In the simplest case you could use the "Max Entries Per Day" PosSizer but its per-bar logic does not extend to a "day" on intraday bars. So for intraday, these solutions should work:

1. To incorporate in your Strategy code, depending on what you're after:

* Intraday | Building blocks of Intraday trading strategies > One entry per day per symbol
* Intraday / Multi-Time Frame | Keep from holding positions overnight

2. No coding required:

* Position Options PosSizer from MS123 PosSizers library. Option "Max pos. per day" supports intraday bars as well as Daily.
profile picture

sfronz

#7
Sorry, to be clear it is buying and selling the same positions in one day.

For example, JNJ is bought on 10/1/19 and is sold on 10/1/19. Each individual positon is bought and sold on the same calendar day.

The multiple positions are desired.
profile picture

Eugene

#8
QUOTE:
Each individual positon is bought and sold on the same calendar day.

What's wrong with this, if it follows the rules you programmed?

QUOTE:
The multiple positions are desired.

I couldn't find this mentioned as a requirement before, and your strategy code is single-position. In addition, "The simplest example of a deviation would be that the system is buying and selling on the same day" does not line up with "Multiple positions are desired".

If you wish to get anywhere with this, please come up with an unambiguous and clear list of trading rules. It may be easier to work with.
profile picture

sfronz

#9
I apologize if I am not conveying the information well. Let's try this:

Buy if

2-day RSI < 25 on day prior closing

PriceClose of prior 5-min bar > 200-day SMA
PriceClose of prior 5-min bar > 8-period SMA on 5-minute bars
Multiple positions are allowed.
Max of only 1 purchase per day per symbol (FUTURE ADD)

Sell at open

2-day RSI > 70 on day prior closing
if trade open for 8 calendar days
2-day RSI > 65 on day prior closing for two consecutive days (FUTURE ADD)

profile picture

Eugene

#10
That makes it clear to me. One note; if you approach scripting a multi-position system, make sure to check out the WealthScript Programming Guide > Programming Trading Strategies > Multi-Position Strategies chapter.

QUOTE:
Max of only 1 purchase per day per symbol(FUTURE ADD)

See post #6 above for the PosSizer solution.

QUOTE:
I ran the backtest on the same 5 min bars with $5000 per trade for 5000 bars.

5000 bars are not enough. To build the 200-day SMA (in an intraday 5-min script? hmm), it needs at least 6.5 * 12 * 5 (i.e. 390) * 200 = 78,000 bars loaded. A simple alternative that does not require loading so many intraday data would be to employ GetAllDataForSymbol from C.Components in your script. It would pick the EOD data straight from a Daily DataSet. Let me know if interested.

Here you go:
CODE:
Please log in to see this code.
profile picture

sfronz

#11
Wow! Thank you Eugene. I see that I still have a lot to learn.
profile picture

Eugene

#12
My pleasure to help. We live while we learn.
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).