# how to detect a flat moving average

Questions about MultiCharts and user contributed studies. nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

### how to detect a flat moving average

Hi everyone

I am wondering if you could help me to detect a flat moving average

here a screenshot with a 100 moving average

in the rectangle the moving average is +/- flat
how to detect this situation with an indicator?

Thank you in advance
Attachments FLAT MOVING AVERAGE.png (44.46 KiB) Viewed 1125 times

Henry MultiСharts
Posts: 9083
Joined: 25 Aug 2011
Has thanked: 1258 times
Been thanked: 2920 times

### Re: how to detect a flat moving average

Hello nuno-online,

Do you have a particular difficulty with implementing your logic or you want someone to advise how the logic should look like to achieve your goal? sptrader
Posts: 710
Joined: 09 Apr 2010
Location: Colorado
Has thanked: 468 times
Been thanked: 261 times
Contact:

### Re: how to detect a flat moving average

Maybe compare the angle of a horizontal line with the angle of the 100 per MA over a given length and if the deviation is less than a given percentage(say 5%-10%), then the 100 per MA would be considered to be flat. nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

### Re: how to detect a flat moving average

Hi Sptrader

this is exactly what i want to do ... but i don't know how to do this!

I would to compare the angle of a horizontal line with the angle of the 100 per MA (example linear regression) over a given length and if the deviation is less than a given percentage(say 5%-10%), then the 100 per MA would be considered to be flat.

maybe you can help me?

thanks sptrader
Posts: 710
Joined: 09 Apr 2010
Location: Colorado
Has thanked: 468 times
Been thanked: 261 times
Contact:

### Re: how to detect a flat moving average

Hi Sptrader

this is exactly what i want to do ... but i don't know how to do this!

I would to compare the angle of a horizontal line with the angle of the 100 per MA (example linear regression) over a given length and if the deviation is less than a given percentage(say 5%-10%), then the 100 per MA would be considered to be flat.

maybe you can help me?

thanks
***********************************
That's probably beyond my limited coding abilities...
I was just trying to clarify a possible starting point for you. I do like the concept though. nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

### Re: how to detect a flat moving average

ok Sptrader, thank you for the idea!

Is there anyone who can help me to "translate" this idea en easylanguage?

I would to compare the angle of a horizontal line with the angle of the 100 per MA (example linear regression) over a given length and if the deviation is less than a given percentage(say 5%-10%), then the 100 per MA would be considered to be flat.

thank you

MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 279 times

### Re: how to detect a flat moving average

Couldn't you do all that with just calculating the angle of your moving average? If the angle exceeds an inputted amount then begin? nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

### Re: how to detect a flat moving average

Hi MAtricks

have you an example about calculating the angle of the moving average?

MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 279 times

### Re: how to detect a flat moving average

Try something like this:

Code: Select all

```inputs: MALength(20), EntryAngle(5), multiplier(5000) ; variables: MA(0), MA1(0), Angle(0); MA = Average( C, MALength ); MA1 = Average( C, MALength ); Angle = (MA - MA1)/MA1*multiplier ; ```
That's what I can think of on the fly, but I'm sure there are better ways.. hopefully that will get you started though.

MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 279 times

### Re: how to detect a flat moving average

I poked around a little bit to see how others would come up with the angle of a moving average. I found this.. it looks better than my little workup.

(not my code and it definitely needs to be cleaned up but it looks like it would do exactly what you want)

Code: Select all

```// Angle relative to "0" degrees (Horizontal) inputs: Note_Angle ("Insert Angle X-Axis bars"), AvgLen(20), upColour(blue), downColour(Red), FlatColour(White), CautionColour (Yellow), colourDeltaBar(1); variables: Avg( 0 ), RelAngle( 0 ), color (0), colorl (0); // Calculate the moving average //Avg = Average( Close, AvgLen ) ; // Calculate the Y-component of the tangent: The difference between the price movement // over the number of avg bars value1 = round( Close - Close[AvgLen],6); // y component value2 = AvgLen; // x component Value3 = round(Value1 / Value2,4) ; // Y/X component Value4 = averageFC (value3, AvgLen); // y/x when value1 = 0 // Calculate the relative angle between the price and average If value1 <> 0 then RelAngle = averageFC(round(ArcTangent(Value3),4),AvgLen) // smoothed else if value1 = 0 then RelAngle = averageFC(round(ArcTangent(Value4),4),AvgLen); // Plot the relative angle to "0" angle (pointing to the right) //if absvalue (Relangle) < MinAngle then // Color = FlatColour {else} if (RelAngle > RelAngle)and (Close - Close[AvgLen]) > 0 and close > close[AvgLen/2] then Color = upColour else if (RelAngle < RelAngle) and (Close - Close[AvgLen]) < 0 and close < close[AvgLen/2] then Color = downColour else Color = Color; //if absvalue (Relangle) < MinAngle then // ColorL = FlatColour {else} if (RelAngle > RelAngle)and close < close[AvgLen] then colorL = CautionColour else if (RelAngle < RelAngle)and close > close[AvgLen] then colorL = CautionColour else if (RelAngle > RelAngle) then ColorL = upColour else if (RelAngle < RelAngle) then ColorL = downColour else ColorL = ColorL; Plot1( RelAngle,"RelA_Histo",color ) ; Plot2( RelAngle,"RelA_Line",colorL ) ; Plot3(0,"zero", color); ``` bowlesj3
Posts: 2104
Joined: 21 Jul 2007
Has thanked: 200 times
Been thanked: 420 times

### Re: how to detect a flat moving average

There is a simpler way to do this with less code. Set up two arrays. Lets say their size is from 0 to 30. You have a pointer that is incremented on every barstatus = 2. The pointer is used to load the high into the high array of highs and the low into the array of lows. After incrementing the pointer you check to see if it is > 30 and if it is you set it to 0 then load the arrays. Your pointer is initialized to –1 at the very start. Now once the table is loaded the first time you run the highest function to find the highest value in the array of highs. You also run the lowest function to find the lowest value in the array of lows. You store these. Use these two values for future testing. Here is the important part. When the table is full and just before you are about to load a new element in (just after the pointer is adjusted for the next load) you test to see if the element in the array is = to the highest in the array of highs and you also test to see if the new element going in is < the highest value. If this is true you set a switch and once the new value is loaded you loop the array to find the new highest value. If the new value coming into the table is higher than the current highest you simply change the highest value you have stored. You do this same process for the array of lows. Okay so this covers keeping the two arrays up to date. Now all you have to do is at the end of this processing test the two fields that contain the highest value and the lowest value. If their difference is less than your parameter setting then you have a series of bars which are flat according to your specifications for array size and high to low difference. I have not tested this idea but I think you will find this code works.

By the way you an do the same thing with the moving average However you would use a single array and do the same two loops through the array for highest and lowest value. Of course the value you are loading is the value placed in the plot. You would probably have to adjust the test of the element in the array which is being clobbered by the new value coming into the array to be "if the element being clobbered is = to the lowest or = to the highest and the new element coming in is < the highest and > the lowest" then after loading the new element into the array you have to loop the array to find the revised highest value and lowest value. Again if the new value exceeds the highest value or exceeds the lowest value you simply update the appropriate value of these two.

I personally would use the first method. Having said that maybe you would want to have both running. Also the first method could be done with a single array as well using a similar test as the second method except you would need to test the high against the highest value you have stored and the lower against the lowest value you have stored. The main underlying idea is you have a rolling array (an array that is perpetually updated with a pointer that keeps getting set back to the beginning) that processed in such a way so as to reduce the number of calls to the two functions highest and lowest. In other words you only call those two functions if the channel is shrinking in size.

Come to think of it I believe you can do all of this without an array. You simply use the highest and lowest functions to scan back the number of bars your parameter is set to and as each new bar comes in at barstatus = 2 you have to test the bar that has just gone out of this range to see if it matched the highest or lowest values. If it did and the new bar coming in is also within this range (less than the highest and lowest from before) then this means your channel is shrinking and only at this time do you need to execute either the highest or lowest functions again. I have to laugh because I have old channel code and it is so much more complex than this. I guess that means we get smarter as we get older - LOL.

If you can not figure out how to do this you need to take a course on elementary programming. After that you should study the thread near the top of the user contributed studies which is called "MC learning strategies" and do everything it says. nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

### Re: how to detect a flat moving average

Bowlesj3

thank you very much for all your ideas
That's help me to create others indicators

But in reality, i only want a simple way to compare the angle of a horizontal line with the angle of the 100 per MA (example linear regression) over a given length and if the deviation is less than a given percentage(say 5%-10%), then the 100 per MA would be considered to be flat.

Return to “MultiCharts”