# Trend Detection Indicator



## bingk6 (15 April 2007)

Hi all,

Just looking for some assistance with the formulation of an indicator which shows when a chart is in an uptrend, in a horizontal trading range or in a down trend. 

Just looking at a chart, these different zones are obvious, but coding an indicator is a little more tricky. When you overlay a 200day MA over the chart, it becomes clearer again. So I figured if I could calculate the slope of the MA, it will give me the answer, ie a slope of 0 - means flat trading range, positive slope means uptrend and negative slope means downtrend. I had very limited success with the linregslope function and have not been happy with the results. Just curious as to what the others use for this purpose.


----------



## mrWoodo (15 April 2007)

Hi bingk6, I'm looking for the same thing. Some indicators I've found on the web :

Average Peak Excursion

(Incidentally, if you simply change the month/year on the web address above, you can trawl through about 8 years of monthly amibroker/tradestation/etc. indicators)

Vertical Horizontal Filter

I haven't had a good look at them yet, tho - For now I've been filtering stocks using a very simplistic rate of change. I'm not keen on wading through piles of stocks that are on an ever downward slope, so I simply check that the rate of change is at least +20% over the last year.

Let me know if you find some others!


----------



## wabbit (15 April 2007)

bingk6 said:


> Just looking at a chart, these different zones are obvious, but coding an indicator is a little more tricky.




How are these zones obvious?  If you can "see" them, you can code them.

Don't get stuck on the one idea, like using MA etc.  You will probably have to combine many different bits of code together to achieve the right results.

Listen very closely to what your eyes and brain are telling you when you look at a chart.  Try looking at a chart for five seconds then close your eyes and tell yourself what the chart is doing and how you came to that conclusion.  Then code up your ideas, test and refine.


wabbit


----------



## constable (15 April 2007)

wabbit said:


> How are these zones obvious?  If you can "see" them, you can code them.
> 
> Don't get stuck on the one idea, like using MA etc.  You will probably have to combine many different bits of code together to achieve the right results.
> 
> ...




KISS


----------



## Wysiwyg (8 October 2009)

This is an AFL for trend detection/direction using a 140, 60 and 8 period MA. 
1) find stock in trend 2) assess chart/apply further analysis 3) make decision. 
Also available at the website.


----------



## mavirk (26 October 2009)

This is a very good AFL and can easily be customized for different periods. Thanks for sharing.


----------



## fodder-oz (26 October 2009)

hey wysiwyg can you provide link to website and how to use that script i.e what program to use?


----------



## Wysiwyg (27 October 2009)

fodder-oz said:


> hey wysiwyg can you provide link to website and how to use that script i.e what program to use?




Sure. http://www.amibroker.com/library/detail.php?id=724

and I just paste the code into Amibroker Formula Editor and save in the custom folder.
Thanks to Marcus Davidsson for sharing the code.


----------



## nomore4s (27 October 2009)

Wysiwyg,

Nice code, love some of the comments, lol.

How do you apply this code, as it just brings up every stock in the watchlist you are scanning?

Do you filter it someway? Or just quickly scan every stock in a trend?


----------



## nomore4s (27 October 2009)

Here is what I use as a trend filter. It is FrankD's channels.

Keep in mind these channels are worked out as the last trading day of each time frame is completed and then projected forward before the next trading period begins.

1st chart is monthly channels.
2nd chart is weekly & monthly together.
3rd is monthly zoomed out.

The arrows are not buy or sell signals - only alert signals.


----------



## Wysiwyg (27 October 2009)

nomore4s said:


> Wysiwyg,
> 
> Nice code, love some of the comments, lol.
> 
> ...



Only found the code 19 days ago myself and I`m new to Amibroker too. Perhaps removing some code so only the up trending stock are displayed would work. I don`t know code. I open the up trending chart and make an assessment on my perceived trend longevity, look for patterns, certain candles, liquidity (though not overly important) and if there is promise I peruse the company fundamentals.


----------



## nomore4s (27 October 2009)

Wysiwyg said:


> Only found the code 19 days ago myself and I`m new to Amibroker too. Perhaps removing some code so only the up trending stock are displayed would work. I don`t know code. I open the up the trending chart and make an assessment on my perceived trend longevity, look for patterns, certain candles, liquidity (though not overly important) and if there is promise I peruse the company fundamentals.




Ok thanks, was just curious.

I'm sure you could refine the code to narrow it down for you but instead of removing something from the code it might be better to add a basic buy signal when all 3 MA's are in an up trend and then run it through the scan function instead of the explore function, this should bring up all the strong up trends. This might remove the comments though and only come up buy/sell. Might play around with it if I get time this weekend(probably unlikely though).


----------



## Bowlane (28 October 2009)

Try googling 
"Trend direction & force index (TDF Index)"
and see if this indicator meets your needs. There are metastock and amibroker codes available at least. Seems to work well on a weekly time scale for me but you may need to tune it depending on your personal trading habits.


----------



## Tradesurfer (28 October 2009)

Thought I would share a video I did around a trend indicator Average Directional Movement.

http://www.youtube.com/watch?v=0LPagXSPxL0&feature=player_embedded

While some use as a stand alone entry/exit indicator, also can be used as a filter for other strategies

enjoy and hopefully gives some insight into how its used.

Derek


----------



## AlterEgo (28 October 2009)

nomore4s said:


> Here is what I use as a trend filter. It is FrankD's channels.
> 
> Keep in mind these channels are worked out as the last trading day of each time frame is completed and then projected forward before the next trading period begins.
> 
> ...




Any chance that you could post the code for that? Or do you know where I can get the code from?


----------



## pennies (9 February 2010)

Im also having trouble making some code for Amibroker so that I can make a buy condition that suits the following.

Buy when the slope of a moving average is at a % gradient pointing up. Ive tried having a reference to the moving average being greater than it was 2 days ago but it doesnt really help . Even a minor rise makes it buy. 

I really want it to be related to the slope of the moving average. I think there is a slope function in amibroker but I have no idea how to do it.

Can anyone please help me

Thanks


----------



## Wysiwyg (9 February 2010)

pennies said:


> Buy when the slope of a moving average is at a % gradient pointing up. Ive tried having a reference to the moving average being greater than it was 2 days ago but it doesnt really help .* Even a minor rise makes it buy. *
> 
> I really want it to be related to the slope of the moving average. I think there is a slope function in amibroker but I have no idea how to do it.
> 
> ...



Try using a rising RSI or Stochastic slope on normal range. So you have a long EMA maybe 100 or 200 to get a trend and a short range indicator to narrow the field down again. Here is a basic for exploration mode.


```
TS = TSF(C,200) > Ref(TSF(C,200), -5);
DE = DEMA(C,100) > Ref(DEMA(C, 100), -5);
ST = StochD(15) > Ref(StochD(15), -1);
SB = StochD(15) < 30;

Buy = TS && DE && ST && SB;
Sell = 0;

Filter = Buy;
AddColumn(Buy, "BUY CHECK", 1.2, 29, 54, -1);
```


----------



## skyQuake (9 February 2010)

pennies said:


> Im also having trouble making some code for Amibroker so that I can make a buy condition that suits the following.
> 
> Buy when the slope of a moving average is at a % gradient pointing up. Ive tried having a reference to the moving average being greater than it was 2 days ago but it doesnt really help . Even a minor rise makes it buy.
> 
> ...




The gradient problem is a fair problem because you have no frame of reference, If you adjust from 5min to 10min the gradient halves. Using a longer MA will be deceptive because if the underlying trends well, that MA will have a decent gradient.


----------



## Wysiwyg (9 February 2010)

For EOD exploration and using a long moving average. The problem with the long MA is the share price can be on it's way down before the long MA starts to decline. However, issues with this exploration can be chopped at discretion. 

You may want to move away from a MA as trending tool but most tools have their use.


```
DE = DEMA(C, 200) > Ref(DEMA(C, 200), -1);
SH = StochK() > 20; 
SL = StochK() < 30;
SU = StochK() > Ref(StochK(), -1);

Buy = DE && SH && SL && SU;
Sell = 0;

Filter = Buy;
AddColumn(Buy, "Let's Rock", 1.2, 24, 43, -1);
```


----------



## pennies (20 February 2010)

Thanks for that. That helped heaps.

Im finding AFL writing very hard even with the wizard.

Can someone please tell me how to code this.

Buy = 2 consecutive closes above an EMA

Sell = 2 consecutive closes below an EMA

I tried this but it didnt work.

Buy = 	Close > Ref( EMA(  Close , 20 ) , -2 );


It is price related and not number of bars.





If I can push my luck a little could someone also please tell me why this gives me 2 buy signals 1 day apart. I want it to buy when price is above the 20 day SMA and the SMA is pointing up. Ive spent hours on this and I really need some help.

http://i409.photobucket.com/albums/pp175/pennies_08/20 SMA/20SMA.png



Range1=Optimize("range1",20,20,50,1);


Buy = 	Close > Ref( MA(  Close , Range1 ) , -2 )
   AND MA( Close , 20 ) > Ref( MA( Close , 20 ) , -1 ) ;   


Sell = MA( Close , 20 ) < Ref( MA( Close , 20 ) , -2 ) ; 


Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);


Thank you


----------



## pennies (20 February 2010)

Ive got the first one sorted.

If someone can help me with the second one one I would really appreciate it.
I need it to give just one buy signal not 2.

http://i409.photobucket.com/albums/p...0SMA/20SMA.png



Range1=Optimize("range1",20,20,50,1);


Buy = Close > Ref( MA( Close , Range1 ) , -2 )
AND MA( Close , 20 ) > Ref( MA( Close , 20 ) , -1 ) ;


Sell = MA( Close , 20 ) < Ref( MA( Close , 20 ) , -2 ) ;


Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);


----------



## pennies (21 February 2010)

Wysiwyg said:


> For EOD exploration and using a long moving average. The problem with the long MA is the share price can be on it's way down before the long MA starts to decline. However, issues with this exploration can be chopped at discretion.
> 
> You may want to move away from a MA as trending tool but most tools have their use.
> 
> ...




Thats a great bit of code. Thanks  I suppose I could just alter for a sell below 80?


----------



## pennies (21 February 2010)

pennies said:


> Thats a great bit of code. Thanks  I suppose I could just alter for a sell below 80?




I tried setting the sell to trigger by turning down from above 80. It works most times but its one of the reasons I havent really used the stochastic. A lot of times it doesnt make it to 80 so I dont get a sell signal. What do you use for a sell then? Trailing stop?

Sorry if this seems pretty basic


----------



## Wysiwyg (21 February 2010)

Hi pennies. Yes you are right the 80 does not get crossed all the time with those parameters so why not add another exit? The trailing stop may well be the exit that fits all trades but not always. 

I try to keep the entry and exit criteria simple. Prices go up or down or stay the same.


----------



## tech/a (21 February 2010)

If your using a cross of 80 in stochastic as a sell signal then your using stochastic in correctly.
Stochastic signals are only taken in the Direction of the trend.


----------



## pennies (21 February 2010)

Wysiwyg said:


> Hi pennies. Yes you are right the 80 does not get crossed all the time with those parameters so why not add another exit? The trailing stop may well be the exit that fits all trades but not always.
> 
> I try to keep the entry and exit criteria simple. Prices go up or down or stay the same.




Im trading over periods of a few days to several weeks. What types of stops work most effectively for this type of trading? Ive tried various moving average crossovers but they lag a lot and I give back a lot of profit. Trailing stops seem to have to be set at aound 5% to keep me in longer trends. I havent tried ATR although Im currently reading about it. Do you have any suggestions?

Also RE: Tech/A

"If your using a cross of 80 in stochastic as a sell signal then your using stochastic in correctly.
Stochastic signals are only taken in the Direction of the trend."

Isnt a sell signal triggered when the stochastic has been above 80 and then closes below it?


Thanks


----------



## tech/a (21 February 2010)

No it indicates over bought not sell.


From here

http://stockcharts.com/school/doku....icators:introduction_to_tech#oscillator_types




> Banded oscillators are designed to identify overbought and oversold extremes. Since these oscillators fluctuate between extremes, they can be difficult to use in trending markets. Banded oscillators are best used in trading ranges or with securities that are not trending. In a strong trend, users may see many signals that are not really valid. *If a stock is in a strong uptrend, buying on oversold conditions will work much better than selling on overbought conditions*




Its important to understand how to use and interpret indicators in this case your attempting to use an indicator which is designed for Range trading signals to identify trends!!
Not particularly helpful in your pursuit.


----------



## Wysiwyg (21 February 2010)

pennies said:


> Im trading over periods of a few days to several weeks. What types of stops work most effectively for this type of trading?
> Thanks



Apart from the initial stop to fix loss if the trade goes against, I don't know of any most effective stop. Perhaps someone else does????


----------



## tech/a (21 February 2010)

Wysiwyg said:


> Apart from the initial stop to fix loss if the trade goes against, I don't know of any most effective stop. Perhaps someone else does????




By far the most effective is the one you take!


----------



## pennies (21 February 2010)

tech/a said:


> By far the most effective is the one you take!




 Yes at least I now know thats true. Thanks for the help.


----------



## tech/a (22 February 2010)

As I understand the question placed here you are looking for a method of  TREND DETECTION as against trend identification.

Which makes sence---you want to get on an emerging trend not confirm that a trend is indeed in place.

That being the case price action itself is the best I have found.
Higher highs and higher lows.
Those who have looked into this in more depth will also find identification of Wave 3 setups in Elliott Wave Analysis handy.

Learning how to count waves will be benificial.
This helps you see the structure of a chart.


----------



## pennies (22 February 2010)

Wysiwyg said:


> For EOD exploration and using a long moving average. The problem with the long MA is the share price can be on it's way down before the long MA starts to decline. However, issues with this exploration can be chopped at discretion.
> 
> You may want to move away from a MA as trending tool but most tools have their use.
> 
> ...




Can you please tell me what && means in AFL. Is it AND?

Im wondering how I get a buy signal when the stochastic is above 30 and turns up. The way I read that formula it tells me the the stochastic is between 20 and 30 and heading up. Is that right. Curious as to why I get a buy signal when the stochastic only made it down to 43 and didnt get to between 20 and 30  


http://i409.photobucket.com/albums/pp175/pennies_08/Stochastic/Stochastic.png


----------



## Wysiwyg (22 February 2010)

pennies said:


> Can you please tell me what && means in AFL. Is it AND?
> 
> Im wondering how I get a buy signal when the stochastic is above 30 and turns up. The way I read that formula it tells me the the stochastic is between 20 and 30 and heading up. Is that right. Curious as to why I get a buy signal when the stochastic only made it down to 43 and didnt get to between 20 and 30



From the example code ---- yes  && is AND and my signals are generated exactly as the circumstance occurs. That is, it does as the code states. Amibroker does what the code says otherwise 99% of the time it is human error.


----------



## pennies (22 February 2010)

Wysiwyg said:


> From the example code ---- yes  && is AND and my signals are generated exactly as the circumstance occurs. That is, it does as the code states. Amibroker does what the code says otherwise 99% of the time it is human error.




Yours seems to be fine. Mine on the other hand.... 

DE = DEMA(C, 200) > Ref(DEMA(C, 200), -1);
SH = StochK() > 20; 
SL = StochK() < 30;
SU = StochK() > Ref(StochK(), -1);

Buy = DE && SH && SL && SU;

Was I supposed to put values in the brackets for the stochastic code above? It looks like ithe signals on my chart work well on the price but they dont seem to be right on the stochastic.


----------



## Boggo (22 February 2010)

I just use a simple formula (see chart) which changes the colour of the bars on my default template to give me a bit of a heads up of a potential change of direction.

(click to expand)


----------



## Wysiwyg (22 February 2010)

pennies said:


> Was I supposed to put values in the brackets for the stochastic code above? It looks like ithe signals on my chart work well on the price but they dont seem to be right on the stochastic.



My stoch %K standard settings are 15,3. You have 15,5.  

Note the code is my beginners effort.


----------



## pennies (22 February 2010)

Wysiwyg said:


> My stoch %K standard settings are 15,3. You have 15,5.
> 
> Note the code is my beginners effort.




Beginners effort. Pretty darn good. Dont know where that leaves me  

Yes I do use 15/5. Where can I adjust the AFL to suit that?


----------



## Wysiwyg (22 February 2010)

Boggo said:


> I just use a simple formula (see chart) which changes the colour of the bars on my default template to give me a bit of a heads up of a potential change of direction.
> 
> (click to expand)



Hi, Could you post the pre March 2009  chart. March 2008 to March 2009. Thanks.


----------



## tech/a (22 February 2010)

Whats with this fanatical devotion to Stochastic?
Total in difference to other suggestions?


----------



## brty (22 February 2010)

Tech/a,



> That being the case price action itself is the best I have found.
> Higher highs and higher lows.




Absolutely agree, all this other fancy stuff is just there to confuse.



> Whats with this fanatical devotion to Stochastic?
> Total in difference to other suggestions?




It appears to be a sign of learning. Most seem to go through different stages of needing 'indicators' or some other such. People seem to need the crutch until they are either out of the market by blowing up, or a survivor.

brty


----------



## Boggo (22 February 2010)

Wysiwyg said:


> Hi, Could you post the pre March 2009  chart. March 2008 to March 2009. Thanks.





No problem, have also included the XJO for the same period to maintain an overall view of where the market is at to eliminate any "an indicator is better than the market" false enthusiasm events.

(click to expand)


----------



## Wysiwyg (22 February 2010)

brty said:


> It appears to be a sign of learning. Most seem to go through different stages of needing 'indicators' or some other such. People seem to need the crutch until they are either out of the market by blowing up, or a survivor.
> 
> brty



I doubt anyone can walk before crawling. Some of you may have been there and done that so all constructive comment is welcome.



Boggo said:


> No problem, have also included the XJO for the same period to maintain an overall view of where the market is at to eliminate any "an indicator is better than the market" false enthusiasm events.



Thanks. Similar as I get.  Yes to me indicators are zones of interest. There is nothing definite in any one indicator. The primary trend for ones chosen time frame could be of interest.


----------



## pennies (22 February 2010)

tech/a said:


> Whats with this fanatical devotion to Stochastic?
> Total in difference to other suggestions?




Not sure what you are referring to here. If it is because of the sell arrows I have on the chart indicating that Im still using the stochastic as a sell signal then thats not what Im doing. They were placed on there initially before I realised that they werent really a suitable sell trigger. I had not taken them back off because I had not come up with a way of coding what you mentioned before. (Lower high and lower lows) As for an entry signal I think the stochastic works really well. Up until now I have always used a trailing stop or placed the stop below a recent low.


----------



## Boggo (22 February 2010)

Wysiwyg said:


> Thanks. Similar as I get.  Yes to me indicators are zones of interest. There is nothing definite in any one indicator. The primary trend for ones chosen time frame could be of interest.




Precisely, and following on from zones of interest you then look for bars/patterns of interest in the new direction.

Its simple in theory


----------



## tech/a (22 February 2010)

> I doubt anyone can walk before crawling. Some of you may have been there and done that so all constructive comment is welcome




Thats what they are getting however they havent taken the time to learn what it is they have evidently become convinced of as being the indicator of indicators.



> It appears to be a sign of learning. Most seem to go through different stages of needing 'indicators' or some other such.




Yes so true I was in the same boat once.
Still losing $20k in a few months got my attention!


----------



## brty (22 February 2010)

Wysiwyg,



> I doubt anyone can walk before crawling.




That was my point to Tech/a, that what some of us know about these things, others with the L plates need to go through it. It is a step along the learning curve. I think we should encourage/guide those still there to lose as little as possible at this point.

Stochastics often give good signals at turns, but they also give many other good signals, that don't turn. Eventually as you proceed with them, you notice other signals that separate the good from the bad. These then get added to the arsenal. Further along the line this is further refined until there is a plethora of signals lining up on both trades for the year, that has one of it's few failures.

Tech/a,


> Yes so true I was in the same boat once.
> Still losing $20k in a few months got my attention!




I lost $20k in about 20 minutes in the early '90's, taught me a few things very quickly, like trusting signals  maybe even a little about overtrading....

brty


----------



## pennies (22 February 2010)

"Stochastics often give good signals at turns, but they also give many other good signals, that don't turn. Eventually as you proceed with them, you notice other signals that separate the good from the bad. These then get added to the arsenal. Further along the line this is further refined until there is a plethora of signals lining up on both trades for the year, that has one of it's few failures."


OK I see what you are saying. So do you use any indicators at all? Im not asking what you use just curious if you do actually rely on any indicators. Or do you rely on price patterns, candles, fibs, repeating ranges and support and resistance?

Just curious. Theres no sense in myself wasting more time on this if its not going to improve on where Im already at.


----------



## tech/a (22 February 2010)

You have asked for a Trend Detection Indicator I thought and Stochastic in the veiw of those who have replied isnt it.

Indicators have their place but for live discretionary trading I dont use any.
In Systems yes but thats a different type of trading again.


----------



## brty (22 February 2010)

pennies,



> So do you use any indicators at all? Im not asking what you use just curious if you do actually rely on any indicators.




I don't use indicators at all. I tend to know what most indicators are doing from the price action, most are much of a muchness. I will buy during an uptrend, defined by higher highs and higher lows. I will only buy at the bottom of a retracement to support, not any support but what I deem to be 'good support'. How a stock approaches this support is important, it must 'meander' to it.

My point about...



> others with the L plates need to go through it. It is a step along the learning curve.




... was real. Perhaps it IS IMPORTANT to make mistakes as part of the learning curve, these type of mistakes. The trick is to lose as little as possible whilst learning, bet small, no matter how good a signal works, or seems to work.

If you don't actually make the mistakes, then you may never really believe they are mistakes, and therefore things tend to play on your mind when implementing a position, or staying with a position.

In trading over the years, I have made every mistake known to man, and probably invented a few new ones. The one thing I didn't do was bet the farm to regain losses, hence I was able to stay in the game, or come back to it.

brty


----------

