How to create dataseries that calculates only on negative or only on positive values in the series
Author: mikesblack
Creation Date: 3/29/2010 8:22 PM
profile picture

mikesblack

#1
For example: I want to do a study of Zscore of ROC.Series given negative values only for ROC .

For all values of ROC.Series <0 ,

ROC.Series(Close,3)-SMA.Series(ROC.Series(Close,3,N))/StdDev.Series( ROC.Series(Close,3), N, WealthLab.Indicators.StdDevCalculation.Population );

Now for a look back at 100 bars perhaps 50 are negative. In that case I would like to calculate on 50 bars only. Those 50 bars would be used for N.

How do you do this?
profile picture

Eugene

#2
Create a data series, count all occurrences of (ROC.Series < 0) in a loop. Now, use the derived N value on the current bar to calculate your 2nd formula in the loop (i.e. bar by bar).
profile picture

Cone

#3

This snippet should help -

CODE:
Please log in to see this code.


So in the loop, N = negSum[bar];
profile picture

mikesblack

#4
Thank you Eugene and thank you Cone.

I understand and have implemented the counter. Now given N is the count of all negative values over 100 day look back, can you help me further by showing me how to allow for the following Z formula to calculate on only those negative ROC.Series(Close,3) values?

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

Eugene

#5
See DataSeries > Filling a Custom DataSeries > Example 2 in the WealthScript Programming Guide.
profile picture

mikesblack

#6

Sorry Eugene. Can you help me out with this? How do I get from the example provided in the programming guide to my objective? I'm not quite getting it. Thanks

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

Eugene

#7
This is a very basic concept, so let me explain step by step.

The example provided shows how to create a blank data series:
CODE:
Please log in to see this code.

In your case, it's "DataSeries ZROC3" instead of "random".

Then it establishes a loop by Bars:
CODE:
Please log in to see this code.
The key here is to assign a value to the DataSeries at every bar:
CODE:
Please log in to see this code.

In your case, it transforms to:
CODE:
Please log in to see this code.

Hope it's clear now.
profile picture

mikesblack

#8
Wish it were Eugene, but unfortunately I am still confused.

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

Eugene

#9
Please read my message carefully. The last line already contains 99% of what you're looking for.

Still, I suggest you take a look at the various sources explaining this basic concept:

1. WealthScript Programming Guide > DataSeries
2. QuickRef > DataSeries > [] indexer Property
3. Introductory | Bars, Loops, and Bar + 1
profile picture

mikesblack

#10
Funny how something that seems so basic or intuitive for someone can be so confounding and elusive for the other. 99% of nothing is still nothing, even when care is put into understanding it. I appreciate your leads, and will look further at the links you gave me, but I would really appreciate it much more if you could provide the code to this specific problem. Perhaps given the interest I have for this specific situation will allow this basic concept to transfer to general insight. Thanks! :)
profile picture

Eugene

#11
Looping bar by bar can be found in absolutely every Wealth-Lab Strategy. This is why it is one of the essential concepts. When you open a new blank Strategy window, you will face that main (trading) loop.

If you really want to be able to code a Strategy yourself (and to program this indicator as well), please follow the link and open the chapters of QuickRef/Programming Guide. Right when you grasp the concept of looping by bars, you will quickly see the solution is already given in my reply 3/31/2010 1:12 AM, last code line.
profile picture

mikesblack

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


What do you think is the obstacle to the source of my confusion? A basic concept should be easily registered, no? For me it is not so easy. From the line of code you suggest the solution is given, so long as I grasp the concept of looping by bars. What am I missing? Is it an if statement? If ROC.Series(Close,3)<0 then...
I really don't want to frustrate you with this, I really want to understand this and so thank you for your patients with my thick scull.

I have the first part nailed down. A count of negative values for establishing N.
CODE:
Please log in to see this code.
profile picture

Cone

#13
CODE:
Please log in to see this code.
I can't complete it for you because there are errors in every formula that you've posted (except mine in your last post).

For example -
QUOTE:
// Should this ...
SMA.Series(ROC.Series(Close,3,N))

// be this? (where N is the SMA Period)
SMA.Series(ROC.Series(Close,3),N)


If that's the case, you should not be using the .Series method because that would result in a new series created for every different value of N. Use the .Value method instead. Here's what I can interpolate, but you must check the formula -

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

Eugene

#14
Mike, have you ever created a trading system using code? If yes, have you noticed the trading loop there? What you're missing is the same thing: the loop by bars.

Just give one more shot to my walkthrough from 3/31/2010 1:12 AM. What you present is a one-line definition of some DataSeries. It works by itself, undoubtedly, but to apply the negative closes counter by Robert, the formula needs a little "massage":

1. Setting up a loop (I showed you how)
2. Assigning some double value to the series, i.e. "filling custom data series with values" (also shown)

The negSum data series contains the required N value for each bar, i.e. the number of negative bars in given lookback.

So, if you create a variable of type integercalled "N" before that loop, and will keep assigning the value of the negSum data series on the current bar while in the loop, you can pass the N as the new, variable period parameter for your custom data series ZROC3.

This can't be explained any more simple and clear.
profile picture

mikesblack

#15
Cone,

Thank you.

Please follow my example below, so we understand each other:
ROC
BAR
0: -1
-1: -1
-2: 2
-3: 3.5
-4: -5
-5: -2.1
-6: 5
-7 -.2
-8 .3
-9 -1.2



lookback =10
N = 6
calculations based on(-1, -1, -5, -2.1, -.2, -1.2)
So then:
CODE:
Please log in to see this code.


ROC.Series(Close,3)[bar] = -1
SMA.Series(ROC.Series(Close,3),N)= -1.75
StdDev.Series( ROC.Series(Close,3), N, WealthLab.Indicators.StdDevCalculation.Population ) = 1.70

-1-(-1.75)/1.70= .44
So what if ROC(Close,3)[bar] should happen to be a positive value? No calculation would happen and so some default such as 0 I imagine?



profile picture

Cone

#16
If you want the average of the just the negative numbers, then you can't use the SMA indicator (create a conditional sum and divide at the end), and, if you want a default value for ROC value positive bars, then you have to insert that too.... just do it inside the same loop and assign the values to ZROC3neg[bar] just as I did.

Well, you could use SMA if you simply zeroed all the positive values in the the ROC.Series.

This should do it -

CODE:
Please log in to see this code.
Note that the final DataSeries, h, is the ROC.Series(Close,3) with only the negative values. However, I'm not sure if you want h[bar] or ROC.Series(Close,3)[bar] as the first argument in the equation.
profile picture

mikesblack

#17
Thank you very much. Not clear about your final question.
QUOTE:
However, I'm not sure if you want h[bar] or ROC.Series(Close,3)[bar] as the first argument in the equation.
profile picture

Cone

#18
"argument" wasn't the right word; I meant "operand".

Instead of h and h1, let me make the series references more meaningful, and you fix the equation (the one that assigns values to ZROC3neg) if required -

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

mikesblack

#19
Thanks very much for clearing that up for me. The only thing that I did to conform to my interest was to switch roc[bar] in the numerator with rocNegOnly[bar].

Thanks for your efforts.

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

Cone

#20
roc[bar] (or rocNegOnly[bar]) isn't in the numerator, so if it should be, you need to add parentheses where required.
profile picture

mikesblack

#21
As such?

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

Cone

#22
I don't know, it's your formula. :)

Besides, I thought you were going to replace roc[bar] with rocNegOnly[bar] in the numerator.
profile picture

mikesblack

#23
Yes, that is what I was after and thanks again for helping me out.
profile picture

mikesblack

#24
Robert-
All this has lead to some ambiguity related to time series calculations. If you can help with this
Two questions that are related:
1. Does calculation of Std Deviation in WL use the rolling SMA in it's calculation for the numerator( Summation of deviation squared or or is it calculated on the average for the bar calculated?
2. I would like to create an Auto correlation formula, and to do so would require me to Sum over a series length using the Close[Bar]that varies in the time sequence; however calculate on the present average( e.g. using 100 bars Sum ( (Close[bar-100]- Average[bar])+ (Close[bar-99]-Average[bar])+.....

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