an indicator which shows a change of direction

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
hendrix
Posts: 54
Joined: 27 Dec 2013
Has thanked: 21 times
Been thanked: 5 times

an indicator which shows a change of direction

Postby hendrix » 16 Apr 2014

Hi guys

I would create an indicator which show me when moving average changes direction.

For example : indicator=1 when moving average changes and going up
indicator=-1 when moving average is going down

I have written this code :

Code: Select all

Inputs: price(Close), length(10),
zeroLine(0.0), zeroVisible(false),
upColour(Green), downColour(Red), colourDeltaBar(1);

Variables: var0 (0) ;

var0 = jtHMA(price, length);

//when the average changes direction : (moving average is going down)
if var0[2] > var0[1] and var0[1] < var0 then
value2 = 1 ;

//when the average changes direction : (moving average is going up)
if var0[2] < var0[1] and var0[1] > var0 then
value2 = -1 ;

Plot1(Value2, "jtHMA");
I have posted a picture of this code here : http://www.heberger-image.fr/images/874 ... s.png.html

Anybody would have an idea please?

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

Re: an indicator which shows a change of direction

Postby TJ » 16 Apr 2014

Hi guys
I would create an indicator which show me when moving average changes direction.
::
Anybody would have an idea please?
First thing I can suggest to you is to avoid using generic variable names,
ie. var0, value1, value2, condition1, condition2, etc.,

You can give each variable a meaningful name, eg. jtHMA.dir, avg.dir, jtHMA.up, jtHMA.dn, etc.,
In the long run, it will save you lots of debugging time.

hendrix
Posts: 54
Joined: 27 Dec 2013
Has thanked: 21 times
Been thanked: 5 times

Re: an indicator which shows a change of direction

Postby hendrix » 17 Apr 2014

I revised the code like this :

Code: Select all

Inputs: price(Close), length(10),
zeroLine(0.0), zeroVisible(false),
upColour(Green), downColour(Red), colourDeltaBar(1);

Variables:
average_jtJMA (0) ,
indic (0) ;


//compute mobile average
average_jtJMA = jtHMA(price, length);

//when the average changes direction : (moving average is going down)
if average_jtJMA[2] > average_jtJMA[1] and average_jtJMA[1] < average_jtJMA then
indic = 1 ;

if indic[1] = 1 then
indic = 0 ;

//when the average changes direction : (moving average is going up)
if average_jtJMA[2] < average_jtJMA[1] and average_jtJMA[1] > average_jtJMA then
indic = -1 ;

if indic[1] = -1 then
indic = 0 ;


Plot1(indic, "change_direction");
you'll find a picture of this code here > http://www.heberger-image.fr/images/640 ... e.png.html

++

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

Re: an indicator which shows a change of direction

Postby TJ » 17 Apr 2014

Nice work. Thanks for sharing.

Here's my rendition; it runs slightly faster.

Code: Select all

Inputs: price(Close), length(10),
zeroLine(0.0), zeroVisible(false),
upColour(Green), downColour(Red), colourDeltaBar(1);

Variables:
average_jtJMA (0) ,
indic (0) ;


//compute mobile average
average_jtJMA = jtHMA(price, length);

//when the average changes direction : (moving average is going down)
if average_jtJMA[2] > average_jtJMA[1] and average_jtJMA[1] < average_jtJMA then
indic = 1

else

//when the average changes direction : (moving average is going up)
if average_jtJMA[2] < average_jtJMA[1] and average_jtJMA[1] > average_jtJMA then
indic = -1

else

indic = 0 ;

Plot1(indic, "change_direction");


Return to “User Contributed Studies and Indicator Library”