Count the historical crossovers of Moving Average Possible?

Questions about MultiCharts and user contributed studies.
kajkanjan
Posts: 33
Joined: 12 Feb 2012
Has thanked: 4 times
Been thanked: 1 time

Count the historical crossovers of Moving Average Possible?

Postby kajkanjan » 21 Apr 2013

Hi All,

Is there a way to count (look back) or calculate the amount of times that a moving average has crossed over itself?
So if I apply a 5 period moving average to a chart and a 20 period moving average, can I count the amount of times the 5 period moving average would-have/should-have crossed above or below the 20 period moving average?

I also was hoping to be able to have a variable that will allow me to set how far back i look. So as an example, i want to look back only for crossovers in the past 60 bars (approx 12 weeks).

Not sure if this is possible to do, but feedback appreciated.

The reason why i am trying to do this is to very roughly determine if a stock is trending is a sideways/messy range as opposed to trending. Theoretically, a high count of crossovers within a set period should crudely filter out the sideways ranging stock. In theory...

Thank you for your time and help!
Kindest Regards,
Kaj

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Count the historical crossovers of Moving Average Possib

Postby TJ » 22 Apr 2013

Instead of "look back",
try to think in terms of "look forward".

When the moving averages cross over,
immediately capture the date/time value and save it to a variable.

When you arrived at the point that you need to know the amount of times a moving average has crossed over itself,
simply recall the stored date/time value,
do a subtraction of the current date/time, and you will have the time difference.


see post #12 on
Manipulating Dates and Times
viewtopic.php?f=16&t=6929

kajkanjan
Posts: 33
Joined: 12 Feb 2012
Has thanked: 4 times
Been thanked: 1 time

Re: Count the historical crossovers of Moving Average Possib

Postby kajkanjan » 22 Apr 2013

Thanks.

That actually makes sense (i think!).

But just in trying to understand the (reverse?) logic of what I am trying to do then in 'looking forward, or, counting as I go ...

Doesn't that mean i need to have some sort of rolling/dynamic variable?
Not only do I need to then store the result for every period so that I can count how many times a cross over occured between two dates, or over a certain period range ... But I need to also be able store in a dynamic format. i.e I may want to look back at the past 12 periods, or I may want to look back at the past 200 periods; to see how many cross over 'true' events occurred.

I can just store a cross over event as a 1, and a non cross as a 0. Then just add up the sum of the array (i assume)?
Or are you proposing something else when you say "capture the date/time value"?

Regardless, how would I roll the variable? Don't I need to knock off the oldest value every new period?

Sorry if i am being slow here ... I am just trying to get my head around what needs to happen or else i have no clue what to even try to learn/do re the programming. My limited knowledge re arrays doesn't help.

Thanks though for the thought and direction! I never would have though of it in the 'think forward' way, even though that actually makes so much more sense!

I appreciate the feedback!

Kaj

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Count the historical crossovers of Moving Average Possib

Postby TJ » 22 Apr 2013

Thanks.

That actually makes sense (i think!).

But just in trying to understand the (reverse?) logic of what I am trying to do then in 'looking forward, or, counting as I go ...

Doesn't that mean i need to have some sort of rolling/dynamic variable?
Not only do I need to then store the result for every period so that I can count how many times a cross over occured between two dates, or over a certain period range ... But I need to also be able store in a dynamic format. i.e I may want to look back at the past 12 periods, or I may want to look back at the past 200 periods; to see how many cross over 'true' events occurred.
You are changing your requirements; which is not unexpected, most people have not clearly thought through their needs/wants when they start coding.
I can just store a cross over event as a 1, and a non cross as a 0. Then just add up the sum of the array (i assume)?
Or are you proposing something else when you say "capture the date/time value"?

Regardless, how would I roll the variable? Don't I need to knock off the oldest value every new period?

Sorry if i am being slow here ... I am just trying to get my head around what needs to happen or else i have no clue what to even try to learn/do re the programming. My limited knowledge re arrays doesn't help.

Thanks though for the thought and direction! I never would have though of it in the 'think forward' way, even though that actually makes so much more sense!

I appreciate the feedback!

Kaj
First, write down:
1. what you need,
2. when are you going to use the information,
3. how you are going to use the information,
4. WHAT are you going to use the information for...
then design a logic to meet that requirement.

A chart mock up with arrows and notes is always helpful.

kajkanjan
Posts: 33
Joined: 12 Feb 2012
Has thanked: 4 times
Been thanked: 1 time

Re: Count the historical crossovers of Moving Average Possib

Postby kajkanjan » 22 Apr 2013

I understand what I want, the end result is easy to know .... but I am struggling with how to translate that into the code and the (reverse)logic required, especially as I do not have much experience with arrays etc. even more so as I am not sure what is (or is not) possible... That is always the biggest problem. Hence the learning and questions.

From my current understanding, I need to count every time an 'event' is true within a set time frame, or, over a defined period.
As you elaborated, this needs to be a think forward as it happens process.
The only way I can see that working though is with a 'rolling variable/array' that will store the latest (x) values and knock off the oldest value for that period i am wanting to count.

Either the event happened, or it didn't, so a 1 and 0 format makes sense, especially if I can sum an array. A sum of all the 1’s will give me the total times the event occurred.

I am just unsure of how to create this rolling/dynamic array so that I can store e.g. 10 data points, or e.g. 200 data points.
I did some Googling and found a post re ‘shuffling’ an array and I think that is what I want to do....

The code (i think) would be something like:

Code: Select all

Array: myArray [10] (0);

Var: myArraySize(10);
Var: Counter1(0);
Var: HoldRow(0), SaveValue(0);
Var: MA1(3), MA2(20), ArraySum(0);

Condition1 = XAverage(Close, MA1) crosses above XAverage(close, MA2);
Condition2 = XAverage(Close, MA1) crosses below XAverage(close, MA2);

if condition1 or condition2 then
SaveValue = 1
else
SaveValue = 0;

For Counter1 = 1 to myArraySize begin
holdRow = myArray[Counter1];
myArray[Counter1] = SaveValue;
SaveValue = holdRow;
end;

ArraySum = sum(myArray[10],myArraySize);

Plot1(ArraySum);

So in the case above, I am storing data in an array every period. Either a 1 or a 0 will always be stored. I am only storing 10 data points though and each time a new period is added, the oldest value is removed.
I then am just adding up the array.

But this does not seem to be working as i want

Also:
a) is this the most efficient way to do this?
b) How do I get the above to be a dynamic variable. Right now it is locked/hard-coded at 10. But I want this to be a variable/input that will let me define the size e.g. 10 or 50 or even 200.

Thanks again,
I hope I am on the right track!

kajkanjan
Posts: 33
Joined: 12 Feb 2012
Has thanked: 4 times
Been thanked: 1 time

Re: Count the historical crossovers of Moving Average Possib

Postby kajkanjan » 22 Apr 2013

Is this how to do it? Again, with efficiency in mind?

Code: Select all

Input:PeriodLookBack(10);

Array: int myArray [] (0);

Var: myArraySize(0);
Var: Counter1(0);
Var: HoldRow(0), SaveValue(0);
Var: MA1(3), MA2(20), ExMA1(0), ExMA2(0), ArraySum(0);

myArraySize = PeriodLookBack;

Array_SetMaxIndex(myArray, myArraySize);

ExMA1 = XAverage(Close, MA1);
ExMA2 = XAverage(close, MA2);


if ExMA1 crosses above ExMA2 or ExMA1 crosses below ExMA2 then SaveValue = 1 else SaveValue = 0; {**}

For Counter1 = 1 to myArraySize begin {***}
holdRow = myArray[Counter1];
myArray[Counter1] = SaveValue;
SaveValue = holdRow;
end;


ArraySum = array_sum(myArray,1,myArraySize);{*}{***}

Plot1(ArraySum);


* I realised that needed to use 'array_sum' to sum the array.
** I assume that the 'If' statement is better than two separate conditions?
*** Do I start at 1, or do I start at 0 for the counter? I assume starting at 0 will actually give me 11 storage spaces if I set the PeriodLookBack to 10. So starting at 1, even though 0 is valid, is easier to follow?


Return to “MultiCharts”