Intraday ratio standard deviation on a Daily chart
Author: trader rog
Creation Date: 6/9/2011 9:09 PM
profile picture

trader rog

#1
Hello everyone,
I am looking to incorporate an intraday ratio standard deviation of two stocks into the code below. The idea is to enter a pair intraday after the standard deviation first widens x% from the previous day's close, then enter when it retraces y%.

For example, if the ko/pep ratio closed yesterday at 2 s.d., then today I would look for a further widening to 2.2 (10% wider from prev close), and enter when it retraces to 2.09 (5% retrace from 2.2).

Entries should be AtMarket bar+1 using 5 minute bars. Exits should be ExitAtClose of the last bar of day.

The main issue here is how to update the ratio s.d. intraday so entries can be based on it. The ratio s.d. number is called "DeltaNorm" in the code below.

I understand that I will need to use the SetScaleDaily() method, Synchronize, etc., but calculating the ratio s.d. intraday is tripping me up and my code looks like a mess. Therefore, I included the Daily code below from which to work from. I appreciate any help someone can offer.

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

Eugene

#2
No the SetScaleDaily etc. isn't needed I think: you just need to get the value of the DataSeries at the previous day's close. So here goes (for intraday data, run on the 1st symbol of the pair):
CODE:
Please log in to see this code.
profile picture

Cone

#3
I don't think that's what he's after Eugene. He want the Daily StdDev calculated on an intraday basis. I've got some experience creating those indicators (RSI, SMA, StochK, StockD), so I'll give it a try. It could be a neat idea for a TSL article :)

If I can get this one done, I'll work on formalizing a new Indicator Library - WealthLab.Indicators.IntraDaily
profile picture

Eugene

#4
I think I read his words literally:
QUOTE:
I am looking to incorporate an intraday ratio standard deviation
profile picture

Cone

#5
Well, maybe you're right, but trader_roq said, I understand that I will need to use the SetScaleDaily() method, Synchronize, etc., but calculating the ratio s.d. intraday is tripping me up and my code looks like a mess. So that make me think he's working with a Daily SD, but wants to know the current day's value every 5 minutes.
profile picture

trader rog

#6
Cone you are exactly right. I am working with a Daily SD and want to know the current day's value every 5 minutes. Sorry I wasn't more clear Eugene.
profile picture

Cone

#7
Here's the indicator part of the solution. Notice how in the demo, the "hybrid" indicator synchs up with the Daily indicator on the last bar, but the first sample of the next day usually changes value significantly. That's due to the DataSeries sample from period - 1 bars ago falling out of the window and being replaced with the latest intraday data. Consequently, you should probably start your comparison from that point forward, not from the yesterday's closing sample.

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

trader rog

#8
Thank you Cone! This looks like it will do the trick.

How can I incorporate this into the code that Eugene posted above so that the entries and exits are also accounted for?

Thanks again. I appreciate it.
profile picture

Eugene

#9
Since there's no magic way of merging code (have to be done manually) and you already have the two thorough coding solutions by us, have you come up with something while trying to replace one SD series (from my code) with another (from Cone's)?
profile picture

trader rog

#10
I have tried to merge the two but the thing I'm not sure of is how to apply Cone's code to 2 stocks because it was written for just one stock.
profile picture

Cone

#11
CODE:
Please log in to see this code.
This is pretty much how all indicators that don't use a Bars object parameter work. Come to think of it, another way to do it without SetContext is this:

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

trader rog

#12
Ok so now I know how to call the sd function for each stock, but to compute DeltaNorm, wouldn't I need the sd of the Delta?
profile picture

Cone

#13
Well, looking at your code, it doesn't look like you even want the SD of each of the stocks, instead, you only want the SD of the ratio. Just pass the ratio series to the sd function.
profile picture

trader rog

#14
This is what I came up with. It verifies but it's not working properly as no trades show up in the backtest.

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

trader rog

#15
Cone / Eugene - can one of you guys please take a glance at the code above and let me know where I'm going wrong? That would be really helpful.
profile picture

Eugene

#16
Sorry, I currently have no time -- I've been busy upgrading an extension library for upcoming changes in 6.2. Experience suggests me that this week, as it usually happens after a new WL release, the place is going to be hot with lots of questions and requests.
profile picture

Cone

#17
QUOTE:
The idea is to enter a pair intraday after the standard deviation first widens x% from the previous day's close, then enter when it retraces y%.
I don't get it. You enter when it widens and enter again when it retraces?

Also, are you really interested in intraday movements while using 200-day averages and deviations? On a 5-minute basis?
profile picture

trader rog

#18
The entry is on the retrace only. This is mainly for one reason: I rarely sit in front of the computer, and if big news comes out on one of the stocks in the pair intraday and the spread blows out, the retrace rule will do a good job at protecting me from being a sitting duck.

I am interested in intraday movements in the spread because paying more careful attention to entries and exits allows for better risk management and a greater understanding of each pairs' behavioral tendencies. Also, in times of heightened market volatility, there is good potential to add value to an already profitable strategy using intraday entries.
profile picture

Cone

#19
Understood, thanks.

The main problem here is that StdDevDailyIntraday uses a SetScale function, which is only valid for the standard (OHLC/V) series. In other words, it won't work for a ratio (or even a reference to a standard series). The Intraday/Daily SD function needs to be reformulated to apply here. Sorry about that.
profile picture

trader rog

#20
No problem. It's a complicated issue for sure. Do you think it's still doable?
profile picture

Cone

#21
Sure, anything is possible!
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).