Standard Deviation on Moving Average
Author: maninjapan
Creation Date: 12/21/2009 4:14 AM
profile picture

maninjapan

#1
Im trying to create a simple indicator that just has an SMA and a 2 Standard deviation of the SMA above and below, not sure how to write the standard deviation part yet. Any examples would be much appreciated.
Below is my half hack attempt.

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

Eugene

#2
CODE:
Please log in to see this code.

See StdDev indicator description.
CODE:
Please log in to see this code.
profile picture

Cone

#3
By the way, SMA +/- StdDev are called "Bollinger Bands". See BBandUpper and BBandLower.
profile picture

Eugene

#4
You hit the nail on the head, but I was confused by this: "a 2 Standard deviation of the SMA above and below". For Bollinger Bands, one would pass the Close as the parameter.
profile picture

maninjapan

#5
Thanks Guys, I figured out what I was trying to do. I want to use longer term STD Deviations on the intra day MA. Next Question how would one apply STDDev.Series of Daily to an intra day chart?
profile picture

Eugene

#6
Take a look at Multi-Time Frame Analysis in the WealthScript Guide.
profile picture

maninjapan

#7
Thanks Eugene, I think I got most of it, it compiles ok, but comes back with a runtime error.

Is the problem in the DataSeriesSTDEV.. calculations??

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

Eugene

#8
QUOTE:
but comes back with a runtime error

Because there's a bug :(

I'll get back to you shortly.
profile picture

maninjapan

#9
Ok, thanks Eugene
profile picture

Eugene

#10
And the bug is: any series of a series (be it SMA, StdDev... etc.) in any combination produces a runtime error when external series are involved.
profile picture

maninjapan

#11
Ok, I think I get it. In this case the Daily data is an external series, even though its the same symbol?

profile picture

Eugene

#12
No, the bug affects trying to create a 2nd order derivative of an external series - namely closeDaily1 and closeDaily2.
CODE:
Please log in to see this code.
profile picture

Eugene

#13
And the workaround, as usual with those synch bugs, is to create the series manually by filling in the values in the loop:
CODE:
Please log in to see this code.
profile picture

maninjapan

#14
Eugene, Finally got this up and running, There still seems to be a problem with the way its calculation the Standard Deviation Bands. They just appear to be tracking the moving average. I am running it on 5 minute ACII data. Do I need the daily files in there as well? I would post an image, but not sure how to....
CODE:
Please log in to see this code.
profile picture

Eugene

#15
QUOTE:
They just appear to be tracking the moving average.

They are tracking the moving averages, that's how they are coded:
CODE:
Please log in to see this code.

So, what's wrong with this?
profile picture

maninjapan

#16
Sorry, maybe tracking isnt the right word. Right now both the upper and lower band return the same value as the moving average. I only see the close price and one line on my chart. The one line is actually the MA, upper and lower standard deviations as they split into 3 lines on the last bar.
profile picture

maninjapan

#17
Still not able to get this up and running correctly...

The problem seems to lie with the
CODE:
Please log in to see this code.
part of this calculation
If I change it to a fixed value such as 1, the bands appear above and below.
If I plot the smaSTD seperately it shows up with the expected value, however when part of the following calculation, it returns the same value as just ActualSpreadSMA, leaving me without any bands.

Hope that makes sense
CODE:
Please log in to see this code.
profile picture

maninjapan

#18
I also just tried PrintDebug to see what values I was getting,
CODE:
Please log in to see this code.

but it returned TestWealthLab.Indicators.SMA, not the actual value... whats the correct way to get this to return the actual value in the debug window?
I tried
CODE:
Please log in to see this code.
but that returned an error on compile
profile picture

Eugene

#19
I don't understand the problem. Got a set of 2 bands plotted nicely:
CODE:
Please log in to see this code.
profile picture

Eugene

#20
QUOTE:
whats the correct way to get this to return the actual value in the debug window?

It doesn't have to do with Debug window. Please read the WealthScript Programming Guide > DataSeries, the part that deals with accessing individual values of DataSeries.
QUOTE:
but that returned an error on compile

Right syntax, wrong place. Outside the loop? "An error" that you didn't specify tells you that "bar" is missing in this context?
profile picture

maninjapan

#21
Ok, Ill go back and follow through the instructions again.. Sure its something Ive messed up here....
profile picture

maninjapan

#22
Eugene, I believe I have found the error... If I use a straight number such as 1.5, (as you did), the bands come up ok. However, if I replace it with a parameter ( I was trying to optimize it), such as
CODE:
Please log in to see this code.


I get the problem as described above.
profile picture

maninjapan

#23
ok, Ive tried adding another multiplier a ratio of a short and lond Daily based STD, Ive tried charting it to see if its working, but its not coming back as expected. It returnes a value, around the close/open but returns zero for the rest of the intraday. I would expect it to look like a daily MA on an intraday chart. Changing values slightly once a day....
The new Dataseries are LongSTD and Short STD
CODE:
Please log in to see this code.


profile picture

Eugene

#24
QUOTE:
The error I receive is error CS0103, here is how I have positioned it in the code

Please carefully re-read my reply above:
QUOTE:

Right syntax, wrong place. Outside the loop? "An error" that you didn't specify tells you that "bar" is missing in this context?

Right syntax but wrong place. Move the statement inside a loop by Bars.
QUOTE:
Eugene, I believe I have found the error... If I use a straight number such as 1.5, (as you did), the bands come up ok. However, if I replace it with a parameter ( I was trying to optimize it), such as
CODE:
Please log in to see this code.

I get the problem as described above.

Sometimes I wish I have some telepathic abilities, but no - I have not. Taking a look at your posting @ 1/11/2010 6:59 AM shows that the code you run and the code you post are two different animals. :)

Anyway, please see Programming Trading Strategies > Strategy Parameters in the WealthScript Programming Guide. Sounds like you're defining a variable/parameter incorrectly somewhere. How are the Band1 variable and the associated StrategyParameter defined?
profile picture

maninjapan

#25
Eugene, thanks for the replies. I get your point too. Apologies for the sloppy work and erratic questions.
The PrintDebug now works fine but Im not sure about the the Paramater. I replaced it with another paramter that works ok, but still getting the same problem.
I tested it as follows and recieved the value I expected
CODE:
Please log in to see this code.


However, when I used the following
CODE:
Please log in to see this code.

or
CODE:
Please log in to see this code.


it returns the same value as ActualSpreadSMA, so that means Band1/10 is returning zero?
profile picture

maninjapan

#26
Eugene, I was looking through some other posts and came across this example. Made the adjustments, and the code above works fine now. Took me a while.......

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

Eugene

#27
A couple of corrections:
CODE:
Please log in to see this code.
profile picture

maninjapan

#28
Thanks for confirming that Eugene. I have one more thing I would like to have a go at here. I've made a couple of changes and would like to add a short Std Dev and a Long Std Dev, to create a volatility ratio of sorts.
Ive tried to add it as follows, I've created a new pane for the 2 Std Dev's, and they return a value at the open of the day and then drops to zero. The idea is for it to act as a multiplier, so I want it to keep its value throughout the day.....
the DataSeries in Question are ShortSTD and LongSTD
Probably easiest if I posted a screenshot, but not sure how to.

CODE:
Please log in to see this code.

profile picture

maninjapan

#29
I had a fresh look at this today, ran the 2 StdDev.series, LongSTD and ShortSTD in PrintDebug on 15minute data and they only returned their actual values 1 bar a day. The rest of the day they returned zero. ActualSpreadDaily returned its correct value for every bar. I know Ive changed the name of the DataSeries, but Im pretty sure Ive followed it as you outlined it earlier. Any advice?

CODE:
Please log in to see this code.


profile picture

maninjapan

#30
Sorry guys, Im still not getting anywhere with this.

Any ideas?
profile picture

Cone

#31
You're assigning SD upper and lower indicators to "Close*0". Is there reason to expect these series to return values other than zero?
profile picture

maninjapan

#32
Cone, please refer to the post above which includes a bit more of the code. This is actually the fix Eugene gave me earlier. The bands work fine if I replace the following code with anything else.
CODE:
Please log in to see this code.


I have made the above code its own indicator and the problem definately lies in this part. Applied to an intraday chart, it returns a value on the open of the first bar of the day, but then goes to '0' for the rest of the day. the dataseries 'ActualSpreadDaily' holds its value for the whole day. Im sure a screenshot would make it very clear, Im jsut not sure how to post it.

profile picture

maninjapan

#33
here we go, first attempt at a screenshot here.



Ive changed the bands just to be +/- a fixed number. They work fine.
The ActualSpread Daily returns fine as seen in the top Pane, changing once a day and holding its value throughout the day. The problem lies in the 2nd pane, this is the STD Dev data series.

Ive probably incorrectly typed something somewhere, but havent been able to figure it out....
profile picture

Eugene

#34
When, like me, you have a dozen of projects in development and a recurring deadline, it's hard to keep track with those script variations which materialize again after apparently helping with the last variation for the final time.

However, your once little project has grown to pretty involving - from a pair trading script, you're building a solution with deviation bands, intraday time frame, an index on-the-fly with custom weights... where it stops?

So, when you have over 50 support tickets open, helping with another "couple of changes" is not nearly as easy as you think it sounds. Help me to help you - explain what you want in great detail, stressing the use of intraday time frame (that appeared only lately) and present the complete code as is - not the incomplete one posted above, to save time on filling in the missing variables. Thank you.
profile picture

maninjapan

#35
Eugene, yes, fully understood. I have apparently underestimated the complexity of this. I was trying to keep things short and easy, which was making things worse not easier.

Let me start again with the problem I am having. As the strategy stands now, I am trying to create an intraday Pairs trading system that uses a set of bands based on Daily Standard Deviations. (with your help so far) I have the intraday Spread with a set of bands. the problem I am having now, is the ShortSTD and LongSTD DataSeries.
CODE:
Please log in to see this code.

It returns a value for the first bar of the intraday, but returns nothing for the rest of the day. This is visible in the screenshot posted above.
To check that the problem lies with this DataSeries, I also plotted the ActualSpreadDaily DataSeries and this returns the value exactly as it should. The Bars also plot fine if I replace the ShortSTD/LongSTD DataSeries with something else.
So, I would like to figure out how to display these LongSTD and ShortSTD DataSeries correctly. The above screenshot should help make my problem clearer.

Thanks, and apologies for not making it easier to understand from the beginning.

Thanks.

Here is the code in full (excluding the buy/Sell signals)
CODE:
Please log in to see this code.
profile picture

Eugene

#36
Thank you for the detailed explanation, much appreciated. I see the "problem" with the ShortSTD/LongSTD, but it does not look like a problem to me. The period (10/30) is too tight for intraday bars, and the (synched to daily data) ActualSpreadDaily series are static (i.e., have the same value throughout the day), explaining what you see.

Switch the scale to, say, 60-minute. Drop a 10..30 period moving average on to the ActualSpreadDaily. You'll see the same behavior.
profile picture

maninjapan

#37
Eugene, thank you for the reply, you have helped me see the error. An error in my understanding rather than an error in the coding.
I would like to Synch the ShortSTD/LongSTD Dataseries to Daily data as well so they too would show as static throughout the day. so the 10 and 30 actually represent 10 days and 30 days even on an intraday chart.

Is it as simple as moving the data series up to the SetScaleDaily section? (Which I will try while I wait for a reply)

Thank you again
profile picture

maninjapan

#38
tried moving the DataSeries up to Set Scale Daily Section but no luck...
CODE:
Please log in to see this code.
profile picture

Eugene

#39
QUOTE:
I would like to Synch the ShortSTD/LongSTD Dataseries to Daily data as well so they too would show as static throughout the day.

Wrap them in a SetScaleDaily/RestoreScale block.
profile picture

maninjapan

#40
Sorry to keep bugging you on this Eugene, I thought I understood what you meant, and tried the following. HOwever it came back with a runtime error. DataSeries do not have equal number of values for mathematical operation.

Here is what I thought you meant

CODE:
Please log in to see this code.



And here is the full code just in case


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

Eugene

#41
You got it right. Your code runs on my test symbols fine.
QUOTE:
DataSeries do not have equal number of values for mathematical operation.

Could you post the full error message text (i.e. what's there below this line)?
profile picture

maninjapan

#42
Apoliges, there was a mistake in the code, it was referencing the wrong spread. I'm trying to get the 30 day Standard Deviation of the ActualSpreadDaily Using the code as below, still doesnt get the ShortSTD and LongSTD to hold its value throughout the intraday.

CODE:
Please log in to see this code.



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

maninjapan

#43
Still havent figured out this problem yet. The Daily STD Dev values, still dont hold their values on an intraday chart apart outside of the first open of theday. Would really like to figure this out
profile picture

Eugene

#44
This in unrelated, but still, there's a mistake here:
CODE:
Please log in to see this code.

Obviously, both closeDaily1 and closeDaily2 point to the same symbol. SetContext should precede, but I'd suggest rewriting the snippet w/o using SetContext this way:
CODE:
Please log in to see this code.

(btw, is the shifting the series purposeful?)

Re: LongSTD, ShortSTD -- I'm puzzled too as I couldn't find a clear explanation for this phenomenon, but am noticing that it occurs a) on high-granularity data (e.g. 5-minute, not on 60-minute bars), and b) just for StdDev/StdError series (e.g. not with SMA).

Robert? Any idea is welcome.
profile picture

Cone

#45
I'm not following the thread (too long to start with now), but if you boil it down to a simple example, then sure I'll take a look.
profile picture

Eugene

#46
Sure. What we want here is to persist the daily StdDev series through the whole intraday session like the SMA (uncomment to feel the difference):
CODE:
Please log in to see this code.
profile picture

Cone

#47
We'll have to call a bug a "bug" until proven otherwise. The problem seems to be re-scaling ActualSpreadDaily in the Daily time frame for the second time after having Synchronized it in the base time frame. Re-ordering the operations, these [equivalent] solutions appear to work properly -

Solution #1
CODE:
Please log in to see this code.


Solution #2 - just do it in the same SetScaleDaily operation.
CODE:
Please log in to see this code.
profile picture

Eugene

#48
QUOTE:
Solution #2 - just do it in the same SetScaleDaily operation.

*facepalm*

Thank you :)
profile picture

maninjapan

#49
Guys, Just applied Solution 2 that Cone supplied, seems to be working exactly as expected now. Charts showing fine and entries look right. Thanks a lot guys for sticking this one out.

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).