Ok so I have done a couple of days work on this trend categorizing and I am making some progress.
I have put together a new indicator, currently called TrendF, that attempts to identify the trends. I thought about this a lot and tried many things. We obviously want to focus on the gradient of the trend so this needs to be highlighted somehow.
The indicator is more for research purposes but I think it has given me a good lead.
The chart below shows USDJPY for August 2013 and there is a clear downward trend. I’ve used similar logic to the Fractal indicator (though over a wider range) to capture the swing highs and swing lows. I’ve then joined those highs and lows together, with some intelligence to try to find the trends and ignore small dips in the price to smooth the trend out.
You can see the solid lines track the lower lows and higher highs showing the trends, and the dotted lines track the opposite – i.e. higher lows and lower highs. I’ve also plotted green mid points.
It works pretty well, there are some conditions that aren’t handled very well, mostly around extreme moves.
So how do we use this data to calculate the trend gradient? I think we can calculate the gradient of the line of best fit. I’m thinking we can either
Plot the line of best fit through the lows on a downtrend, or through the highs on a high trend, and calculate that angle for the trend
Plot the line of best fit through the green mid points and just use that
At this stage I’m going to go with the mid-points because that will cover both up and down trends.
I found a couple of links online that show how to calculate the line of best fit manually
But does it really work? Well I tested the data in Excel and plotted it. It looks pretty good don’t you think? I compared this to calculating the angle of the slope via the atan function for comparison’s sake.
However, it appears the way the function handles nils in LUA is not good. While attempting to calculate the lregSlope on output streams, my results where always off. It turns out when doing this, periods where there is no data are treated as zeros. This of course will throw off the calculation. Specifically, no data’s gradient is 0.012735 vs 0.007217 when the nils are converted to zero.
So, in the end I had to implement my own custom function to calculate the line of best fit.
Next! Sticking to the plan – must identify the trend angle for different trends to categorize them. Then see if there are any other factors required to accurately differentiate trends, such as the volatility of the curve etc.