Factor investing has been around in portfolio management for some years. Based on algorithmic rules it became the big thing in trading and the ETF industry. But is there still some money to be made? Is small beta still smart or just beta? This article will give you a Tradesignal framework to test the factor investing ideas by your own. Continue reading
This article is about the dollar cost averaging investment strategy and the influence of luck in it. Continue reading
Analysing the market performance of the day session vs. the overnight movement reveals some interesting facts.
Daytime vs. Overnight Performance
“The stock market is never obvious. It is designed to fool most of the people, most of the time” Jesse Livermore Continue reading
In this article I will discuss a simple algorithmic stock picking approach based on momentum and volatility. The goal will be to generate excess returns versus a capital weighted stock basket. Continue reading
Usually it makes no sense to fight against normal distribution. But there are setups which have got a high probability of unexpected behaviour. Volatility can be the key to future market movements. Continue reading
I have been in search for a signal I could use for a short vertical spread or naked short option strategy. So my main concern has been to find a level, which will most probably not be penetrated over the next few bars.
This is what I came up with. Continue reading
A graphical approach to indicator testing
The first step in algorithmic strategy design usually is to find some indicators which give you an edge and tell you something about tomorrow’s market behaviour. You could use a lot of statistics to describe this edge, but I like to take a graphical approach in indicator testing first, and only later on worry about the maths and statistics.
A scatter chart is a simple to read chart style to see the correlation between two input values. A regression line on the scatter chart gives you a visual idea if the two securities are positively or negatively correlated, the “cloud structure” of the scatter points tell you if this correlation is tight or loose.
This sample scatter shows the correlation between the DAX and DOW levels, and it can be easily seen that these two markets are tightly correlated in a positively way.
The horizontal scale is used for the second security (DAX), the vertical scale is used for the first security (DOW). This chart type is predefined in Tradesignal, just drag&drop it onto the securities on the chart and select the right amount of data to get the analysis you want to see. (eg. 2000-now). If you see a tight and positive correlation like on the chart above, It might be used to select the instrument you want to trade. If market A is easier to predict than market B, select A.
Scatter on Indicators
Although a scatter chart is usually used to show the correlation between two markets, it can also be used to show the correlation between two indicators.
The chart above shows the correlation between digital stochastic and momentum. Have a look at the clustering of points in on the right side of the scatter, a high level in digital stochastic usually goes with a high momentum. This insight enables you to get rid of momentum, as digital stochastic is easier to read than the shaky momentum. Less indicators = less parameters = less curve fitting.
Doing this analysis and getting rid of parameters is great if you want to minimise the dangers of curve fitting, but it does not tell you if your indicator is of any use at all, when it come to describing tomorrows move of the market. Surely it is valuable insight that a high level of stochastics corresponds to a high momentum, but does a high momentum today also mean that the market will move up tomorrow? And this question about tomorrow is the key question I ask myself when searching for some edge.
To get a glimpse on the prognosis quality of an indicator we will have to add some colour to our scatter chart. This colour tells me what the market has done after a specific indicator level has been reached. Green for an up move, red for a down move, black for not decided by now.
This chart shows the prognosis quality of the stochastic indicator. The left chart shows the 1 day prognosis of a 5 day stochastic, the right chart gives you the 5 day prognosis of a 21 day stochastic. Observe the clustering of the red and green dots. (black for not decided by now) As you can see on the left chart, the one day prognosis using a 5 day stochastic is not the thing to do. Regardless if stochastic is high or low, you get a nice mixture of red and green dots. This means the market, at a given stochastic level, sometimes moved up, sometimes moved down. Not this behaviour is not very useful for trading. Only in the extreme, near 0 and 100, this indicator seems to implicate a bearish next day movement.
The right chart, showing the longer term prognosis of a long term stochastic seems to be more useful. High levels of the indicator also show positive returns on the 5 days after, unfortunately you can not reverse the logic, as low indicator levels give a rater mixed prognosis. This visual analysis can give you an idea which areas of the indicator might be useful for further analysis.
A one dimensional analysis like on the chart above could also be done without this scatter chart. Going from one dimension to two dimensions is more useful, as it directly can be translated to do a kNN machine learning trading strategy. Have a look at the following chart. It shows the scatter of two indicators and the implication on the next days market move.
Lets start with he right chart. As you can see the red and green dots are evenly distributed, meaning there is no useful correlation between the used indicators and the movement of the market on the day after. If you would use a kNN algorithm with these two indicators, I would bet it would not return great results. Even if you would get a positive return, it might just be a lucky hit or curve fitting.
The opposite is true for the chart on the left. Here you can see some nice clustering of the red and green dots. Low indicator levels seem to predict a bearish move, high indicator levels result in a bullish move on the next day. A distribution like this is the perfect starting point for investing some time in a kNN machine learning trading strategy. The kNN algorithm would give you a strong prognosis with high or low indicator levels, and most probably only a weak or no prognosis when the indicators are around 50. The returns will be stable, no curve fitting problems should be expected.
Using a scatter chart can give you a nice visual indication if your indicator might be useful for a prognosis of the next days market move. This is valuable insight, as you can see the whole data universe with one glimpse, even before you do a thoroughly statistical analysis. Numbers can deceive you, pictures usually tell the complete story.
Tradesignal Equilla code:
I always thought that inspiration and experience are key factors in trading. But every time my chess computer beats me without any inspiration, just by brute force, I get my doubts. This article will be about a brute force approach in trading. The kNN algorithm.
Rule based trading
Rule based trading – algorithmic trading, is just a name for a set of if..then rules which will define the machines trading decisions. e.g. if the market crosses below the 200 day line, then short 100 contracts. If the market rises by 2% then exit the position. Easy stuff like this… (for the beginning)
This article will be a short introduction to machine learning. I will use a classic algorithm of machine learning to let my computer find a prediction for tomorrows market move. In the meantime I’ll have a glass of wine with some friends and let the machine do the job; At least that’s the idea, but can it be that simple in real life trading?
Unsupervised machine learning – kNN algorithm
The kNN algorithm is one of the most simple machine learning algorithms. Learning, in this case, is only a nice sounding label, in reality kNN is more of a classification algorithm.
This is how it woks:
The scatter chart above is a visualisation of a two dimensional kNN data set. For this article I used a classical indicators of technical analysis to do the prediction: a long-term and a short-term RSI indicator. The dots on the two dimensional scatter chart represent the historic RSI values at a given point of time.
Now have a look at the fat circled point. This point represents today’s value. It means, that today’s RSI1 has a value of 63, and RSI2 got a value of 70.
Additionally to the position on the chart the dots have got colours. A green dot means that the market moved up on the following day, a red dot shows a falling market on the day after.
We already know what has happened in history, so it is easy to colour the historic dots. But we do not know the colour of today’s dot, as it is not known where tomorrow’s market will end.
Based on the chart above, will it be a red or green dot? Will tomorrow be up or down? Should I go long or should I go short?
kNN – k nearest neighbours
To do a prediction of tomorrow’s market move, the kNN algorithm uses the historic data shown on the scatter plot above and finds the k-nearest neighbours of today’s RSI values. As you can see, our current fat point is surrounded by red dots. This means, that every time the two RSI values have been in this area, the market fell on the day after. That’s why today’s data point is classified as red. Wish it would be that easy all the times…
Call it classification or prediction, the two dimensional kNN algorithm just has a look on what has happened in the past when the two indicators had a similar level. It then looks at the k nearest neighbours, sees their state and thus classifies today point.
kNN as Tradesignal Equilla Code
To implement the algorithm in Tradesignal we first have to do the shown scatter plot. The algorithm stores the values in an array.
8/9 calculates the value of the fast and slow RSI indicators
12/13 looks what will happen on the day after (for the training data set)
16/17/18 stores everything in an array.
The next task to complete is to calculate the distances of today’s RSI point to all the historic points in the training data set.
23/27 calculates the euclidean distance of today’s point to all historic points, line 29 then creates a sorted list of all these distances to find the k nearest historic data points in the training data set.
Nearly done. The next step is just to find out what classification (colour) the nearest points have got and use this information to create a prediction for tomorrow. This is done in lines 33 to 35
Have a look at the scatter chart at the beginning. If this would be the data stored in our training data set, the prediction, using the 5 nearest neighbours, would be -5. All the 5 nearest neighbours of our current data point are red.
Now that we got a prediction for tomorrow, we need to make use of this prediction and trade it. The returns then will show if everything works as predicted.
Over here I just do a simple long/short interpretation of the prediction, but of course you could also use the quality of the prediction (+5 or +1?) in some sort of way. Position sizing…?
kNN algorithm performance
The next chart shows 2000 bars of daily Brent data. It uses a 14 and 28 day RSI to predict the next day’s move in the Brent oil market. The training was on the first half of the data set, and the 5 nearest neighbours did the classification.
Underneath the chart the returns of this test are shown. (strategy equity). On the bottom of the char you see the two RSI indicators used for the generation of the prediction / buy-sell command.
kNN algorithm – conclusion
The kNN algorithm offers a framework to test all kind of indicators easily to see if they have got any predictive value. Judging on the shown graph it seems to work. It seems to be possible to use these two RSI indicators to predict tomorrow’s Brent move.
But unfortunately this also could be just completely useless curve fitting. It is you who has to select the indicators and their periods and you will have to define if you like the outcome of a selected parameter set. To many degrees of freedom to be sure. The kNN algorithm is useful, but its application in finance has to be treated carefully. Otherwise bad surprises are guaranteed
Not everything can be done by brute force, inspiration and experience are key factors in finance…
Autocorrelation is the correlation of the market with a delayed copy of itself. Usually calculated for a one day time-shift, it is a valuable indicator of the trendiness of the market.
If today is up and tomorrow is also up this would constitute a positive autocorrelation. If tomorrows market move is always in the opposite of today’s direction, the autocorrelation would be negative.
Autocorrelation and trendiness of markets
If autocorrelation is high it just means that yesterdays market direction is basically today’s market direction. And if the market has got the same direction every day we can call it a trend. The opposite would be true in a sideway market. Without an existing trend today’s direction will most probably not be tomorrows direction, thus we can speak about a sideway market.
Autocorrelation in German Power
But best to have a look at a chart. It shows a backward adjusted daily time series of German Power.
The indicator shows the close to close autocorrelation coefficient, calculated over 250 days. You will notice that it is always fluctuating around the zero line, never reaching +1 or -1, but let`s see if we can design a profitable trading strategy even with this little bit of autocorrelation.
The direction of autocorrelation
Waiting for an autocorrelation of +1 would be useless. There will never be the perfect trend in real world data. My working hypothesis is, that a rising autocorrelation means that the market is getting trendy, thus a rising autocorrelation would be the perfect environment for a trend following strategy. But first we have to define the direction of the autocorrelation:
To define the direction of the autocorrelation I am using my digital stochastic indicator, calculated over half of the period I calculated the autocorrelation. Digital stochastic has the big advantage that it is a quite smooth indicator without a lot of lag, thus making it easy to define its direction. The definition of a trending environment would just be: Trending market if digital stochastic is above it`s yesterdays value.
Putting autocorrelation phase detection to a test
The most simple trend following strategy I can think about is a moving average crossover strategy. It never works in reality, simply as markets are not trending all the time. But combined with the autocorrelation phase detection, it might have an edge.
Wooha! That`s pretty cool for such a simple strategy. It is trading (long/short) if the market is trending, but does nothing if the market is in a sideway phase. Exactly what I like when using a trend following strategy.
To compare it with the original moving average crossover strategy, the one without the autocorrelation phase detection, you will see the advantage of the autocorrelation phase filter immediately: The equity line is way more volatile than the filtered one and you got lots of drawdowns when the market is sideways.
Stability of parameters
German power has been a quite trendy market over the last years, that`s why even the unfiltered version of this simple trend following strategy shows a positive result, but let`s have a test on the period of the moving average.
Therefore I calculated the return on account of both strategies, the unfiltered and the autocorrelation filtered, for moving average lengths from 3 to 75 days.
Return on account (ROA) =100 if your max drawdown is as big as your return.
The left chart shows the autocorrelation filtered ROA, the right side the straight ahead moving average crossover strategy. You don`t have to be a genius to see the advantage of the autocorrelation filter. Whatever length of moving average you select, you will get a positive result. This stability of parameters can not be seen with the unfiltered strategy.
Trend following strategies are easy to trade, but only make sense when the market is trending. As shown with the tests above, autocorrelation seems to be a nice way to find out if the market is in the right phase to apply a trend following strategy.