A Simple Neural Network for Indicator Prognosis

Technical indicators are the base of algorithmic trading. So wouldn’t it be nice to know tomorrows indicator value in advance? This article is about how to use a simple neural network to do so. Python and Tradesignal will be used to do the programming.

A single linear neuron

A single neuron / perceptron is the most simple form of a neural network. It consists of several inputs which are weighted and summed up to give the desired output. The correct weighting of the inputs is done in iterations, stepping through the available test data. In the end the hope is, that a function like a*indicator1 +b*indicator2 +c*indicator3 +…=value of tomorrow. But as the real world is not a simple linear function, the correct weightings a,b,c,.. will always change and so will the accuracy of the prediction of the next day’s value.

To test the behaviour of such a simple one layer neural network, I implemented one in my charting software Tradesignal using the Python programming language.

Tradesignal Python Neural Network

The first steps when setting up a Python neural net in Tradesignal is to prepare the data for the training and the live prediction of the network.

The input section defines how much data to use and when to start the training and prediction. The price to predict can be any curve you drag and drop the indicator on.

The variables define the placeholders for the live and training data.

Neural Net: prepare data

To predict tomorrows value of a curve, lines 11 to 15 define the needed inputs. Due to the simplicity of the programming the input values have to be between 0 and 1. So has to be the output. With my example network I will use the change of a curve over time as the predictor values for tomorrows value of that curve.

Feel free to put all your creativity in these most essential lines of the network. You have to give the network the right data to do the prediction. I opted for some inputs on the change of the curve and the change over time. So the network knows where the curve to predict might go. Try your own luck by replacing these lines by other or more indicators and try to give the network a better understanding on where the curve is and where it came from.

Given this simplicity tf the single neuron it can only be trained to predict something like an oscillator, but not the market price itself (not running between 0 and 1)

Lines 20 to 24 take the values of the predictor variables s1-s5, delays them by one bar [1] and as st1-st5 are defined as a serialized variables they will always contain the last x values. The amount of data contained is set with the setbackbuffer command. The 1d array “ot1” will contain the learning goal, the prices of the curve itself.

Defining the Python Neural Net

The next step is to define the needed functions for the neural net. This is strictly Python programming, mostly stolen from kdnuggets.com. Please see the link for more information about this part of the python code.

In Tradesignal the Python code is set between the “once begin…end” command.

Python neural net definition

Passing Data and Training of Neural Network

As Tradesignal provides live data I decided to automatically re-train the network every month. Line 77 holds the needed command

The python code (line 80..) first converts the data from Tradesignal into the needed Python form. In Lines 83-87 the data defined previously is converted into a numpy array. This way it can be fed to the neural network function. Line 89 arranges the inputs ( historic predictor values), line 90 gets the values for the output (the curve to predict) from Tradesignal.

After doing the training in line 93, the new weights are printed. These weights, multiplied by the input values, will give the prediction for tomorrow.

If these weights change dramatically with each new training at the beginning of the month, the neural network most probably is not able to find a stationary linear function to predict the curve from historic values, but only curve-fits the historic data. In this case do not expect the prediction to be too useful.

Python neural net training

Plotting the output

The last task after the training is to use the latest value of the predictor variables to calculate the prediction for the next bar.

In line 113 this is done in python, and in line 108/109 this prediction is transferred from Python to Tradesignal and plotted on the chart.

Python neural net output

Testing the Neural Network

The first test of the neural net is done with a simple sinus curve. As it can be seen the prediction (magenta) is quite close to the actual curve (blue).

This can also nicely been seen on the scatter plot. It shows the correlation of the level of the actual curve vs. the prediction. If this would be a straight line you would have the perfect prediction. The more the prediction deviates from the straight line, the higher the error is.

The network is using the last 25 values of the predictor variables to calculate the prediction for the next bar. For some reason it could not capture the tops and bottoms, but in general it did a good job in learning to predict the sinus curve.

Python neural net sinus test

Predicting a real world indicator

Every cyclical signal is an easy task for a neural network. But oscillators in technical trading are only semi cyclical. Indicators like RSI or Stochastic oscillate between given values, but the time-distance between their highs and lows is not constant. (like it would be with a sinus signal) So it is questionable if a neural network really can learn to predict tomorrows value of the oscillator. But let’s give it a try.

On the chart below I used a momentum based oscillator as the curve to be predicted. Like demanded by the simple programming it is oscillating between 0 and 1. It shows some cyclical behaviour, so the neural network might be able to guess tomorrows value.

Python neural net momentum test

The network has been trained using the last 500 data points and is re-trained every beginning of a month. The visual examination of the curve and the prediction (blue & magenta) shows a close correlation. This is confirmed by the scatter chart. The shape is somehow cigar like, which is not the perfect fit of the sinus curve, but better than guessing and flipping a coin. So there might be some edge in using such a simple neural network in trading. Give it a try!

Detecting Support and Resistance Levels

Support & Resistance levels are essential for every trader. The define the decision points of the markets. If you are long and the market falls below the previous support level, you most probably have got the wrong position and better exit.

The detection of support and resistance levels is usually highly subjective and based on the analysts experience. In this article I will use a simple algorithm to detect the levels and show them on the chart.

The first and second deviation of a curve

If you remember your high school math class you have heard about the first and second deviation of a curve and how to use it to find the turning points and extremes of a curve,

The deviation of a curve is nothing else than the momentum of a curve. It defines the slope of the curve. If the slope of a curve is zero, you have found a local extreme. The curve will change from rising to falling or the other way round.

The second deviation, or the momentum of momentum, shows you the turning points of the first deviation. This is important, as at this point the original curve will switch from acceleration to break mode.

But don’t worry about the theory behind the indicator, see the chart below to get the idea.

First the market data is smoothed.

Second the momentum of the market is calculated. Third the momentum of the momentum is calculated.

If the second momentum crosses above zero, the first momentum will have local low and the market will be in a state when the trend comes to an end.

Support and Resistance explanation

Algorithmic turning points

Using the logic laid out above the support&resistance indicator will show the turning points of the market in a timely manner.

Depending on level of market-smoothing it will show the long term or short term turning points.

support and resistance

If you are a trend follower you might only be interested in support levels. Therefore I added the possibility to use the Wells-Wilder Directional Movement Index as a trend filter.

support and resistance DMI filter

Tradesignal indicator code

I used the Tradesignal software suite to program this indicator. The code used is shown below.

It first calculates the first and second deviation of the smoothed market, and in a second step runs the turning point detection.

Tradesignal indicator code

Analysis done using the Tradesignal software suite and data from Refinitiv. Thanks!

Overnight Risk Premium in Equity and Commodity Markets

Over the last 20 years equity markets and ETFs did a significant part of their total performance over night. This article will examine the relationship of in-session moves vs. the out-of-session moves of ETFs and commodities.

The overnight risk premium

As an investor you can expect to get paid for taking risk. If someone sell its stock to you he gets the risk free return for holding cash,  but you will have to finance the risk of the stock moving against you. This risk is quite low when the market is open and liquid, as you could always sell the stock in case of an adverse movement. But when markets are closed you have to bear a higher risk as you will be bound to your position until the markets opens on the following day.

According to this theory the market returns over night should be positive, to compensate you for the higher risk of holding a position you can’t liquidate immediately. Let’s see if there is some truth in this theory and how big the overnight risk premium might be.

Test code and data

To test the theory I took daily market data from Refinitiv and used the Tradesignal code below to sum up the percent values of daily and overnight moves.

Tradesignal overnight performance test code

Tradesignal overnight performance test code

ETFs overnight risk premium

The chart below shows the outcome of the calculation for the QQQ ETF.  As it can be seen, the good performance over the last years has happened mostly when markets where closed. Even during the financial crisis the overnight returns have mostly been positive.

QQQ overnight risk premium

QQQ overnight risk premium

 

Having a look at the distribution of returns you see quite a different behaviour on e.g. a rolling sum of 10 day or night moves. The daily open to close returns show a higher tendency for big moves than the overnight move. So from a risk perspective the day session bears more risk than the night session.

QQQ overnight returns distribution

QQQ overnight returns distribution

 

QQQ is not the only ETF showing this excess overnight performance. The overview below gives you the data. The numbers show the sum of percent moves since 2001. (starting at 100)

SPY ETFs overnight performance analysis

SPY ETFs overnight performance analysis

A look at commodities

With futures and commodities this overnight effect is not as prolonged as with equities. Sometimes it even is non existent at all.

The chart below shows the overnight and daytime performance of the German Bund future. Beside the phase from 2015 to 2017 the overnight movement did not add to the total performance.

Bund Future FGBL overnight risk premium

Bund Future FGBL overnight risk premium

 

The december future on emission certificates (chart below) shows no significant overnight movement at all.

CFI2 Emissions overnight performance

CFI2 Emissions overnight performance

German power, yearly contract, shows a strong negative overnight performance.

F1BY overnight performance

F1BY overnight performance

Implications for trading

As the overnight move has got a significant impact on the total performance of equity markets, it will also have implications on the design of a trading strategy. A first implication of this overnight effect might be that you should not be short over night in equity markets, and you might not want to open your long position at the beginning of the day.  But keep in mind, if everyone knows the trick, this overnight movement will have implications for the first and last hour of trading. This will be a topic for another article…

 

RSI Hellfire Heatmap Indicator

Chart analysis is all about visualizing data. The RSI hellfire indicator uses a heat-map to visualizes how overbought or oversold the market is on a broad scale. This helps to get a broad picture of the current market setup.

Multiple Time-frame Relative Strength Index

Wells Wilder’s RSI is an old timer of technical indicators. It tries to find out if markets are overbought or oversold. Usually it is calculated using a 14 bar setting. But a 14 bars RSI on a daily chart will give a different reading than 14 bars on an hourly or weekly chart. As it is always nice to see what traders on a different time-frame see on their charts, you could simply display several RSI settings on your chart. Continue reading

The Edge of an Entry Signal

When developing a new trading strategy you are usually confronted with multiple tasks: Design the entry, design the exit and design position sizing and overall risk control. This article is about how you can test the edge of your entry signal before thinking about your exit strategy. The results of these tests will guide you to the perfect exit for the tested entry signal (entry-exit combination)

Quality of an Entry Signal

When you develop a new idea for an entry signal there are two things you would like to see after the entry: no risk and fast profits. This would be the perfect entry with the highest possible edge. In reality the market response to your entry will be risk and chance. With a good entry the upside would outnumber the downside. Continue reading

A simple algorithm to detect complex chart patterns

Finding complex chart patterns has never been an easy task. This article will give you a simple algorithm and a ready to use indicator for complex chart pattern recognition. You will have the freedom to detect any pattern with any pattern length. It has been described as Fréchet distance in literature. This article shows a simple adaptation for chart pattern analysis.

Defining a chart pattern

Continue reading

Noisy Data strategy testing

Adding some random noise to historic market data can be a great way to test the stability of your trading strategy. A stable strategy will show similar profits with noisy and original data. If the noise has a great impact on your results, the strategy might be over fitted to the actual historic data.

Synthetic market data?

Generating completely synthetic market data to test algorithmic trading strategies is a dangerous endeavour.  You easily lose significant properties like classic chart patterns or the trend properties of your market. Continue reading

Monte Carlo Simulation of strategy returns

Monte Carlo Simulation uses the historic returns of your trading strategy to generate scenarios for future strategy returns. It provides a visual approach to volatility and can overcome limitations of other statistical methods.

Monte Carlo Simulation

Monte Carlo is the synonymous for a random process like the numbers picked by a roulette wheel. Continue reading

Overnight vs Daytime Performance & Volatility

Analysing the market performance of the day session vs. the overnight movement reveals some interesting facts.

Daytime vs. Overnight Performance

The chart below gives a visual impression on where the performance of the SPY ETF is coming from.

The grey line represents a simple buy and hold approach. The green line shows the performance if you would have held SPY only during daytime, closing out in the evening and re-opening the position in the morning. Continue reading

An Algorithmic Stock Picking Portfolio

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.

Alpha and Beta

Investing in assets with low volatility and high return is on a lot of peoples wish list. Portfolios which archive this goal will have a high Sharpe ratio and in the end get the investors money. By reverse engineering this criteria, one can find promising stocks to invest in and out perform a given capital weighted index.

Alpha and beta are measures to describe an assets performance relative to its index. Both are used in the CAPM – capital asset pricing model.

Alpha is a measure for an assets excess return compared to an index. Continue reading

Weis Wave indicator code for Tradesignal

The Weis Wave indicator combines trend and volume information. It seems to be of some interest for timing short term market reversals. Here comes a version of this indicator for usage in Tradesignal.

The Weis Wave indicator for Tradesignal

The basic idea of the Weis Wave indicator is to sum up the traded volume, as long as the market moves in the same direction. The bullish volume wave is displayed in green. As soon as the market changes direction, a red wave is constructed. by comparing the magnitude of the Weis wave with the magnitude of the market move, valuable insights for short term market timing can be found.

More information can be found via a web search or from the page I got the idea form: https://weisonwyckoff.com/weis-wave/ Continue reading

Tradesignal Implied Volatility and IV Percentile Scanner

Implied volatility data is key in options trading. This article shows how to access free volatility data in the Tradesignal software suite.

Implied Volatility and IV Percentile

Thanks to https://www.optionstrategist.com/calculators/free-volatility-data  implied volatility and IV percentile data is available. For free on a weekly basis. Using this data and the given code the data can be loaded into Tradesignal. This enables you to do your custom market scans, combining Tradesignal technical analysis and the implied volatility data from the optionstrategist website.

Free implied volatility data

The first step to use the optionstrategist data would be to safe it into a text file. Just copy and paste the data, no additional formatting is required. The free data on the website is updated every Continue reading

Implied vs. Realized Volatility for NASDAQ100 stocks

(1) You shall only trade when the chances are on your side

Comparing implied and realised volatility

Selling volatility can be a profitable game, but only if you sold a higher volatility than the market realises later on. Comparing realised and current implied volatility gives you an idea if the chances are on your side.

We already had a look at realised volatility and what the fair price for a straddle might be. Have a look at the kvolfair bet articles. These articles present a way to calculate the historically correct price for a straddle. Whenever you sell a straddle (to sell volatility), implied volatility should be higher than the fair bet price. Only then you will win on a statistical basis. Also have a look at the statistics of VIX, to get a clue when a downturn in volatility can be expected. Continue reading

Seasonal trouble ahead

If a bitchy prime minister and a crazy president weren’t enough, for the upcoming months the seasonal chart is also indicating further price setbacks.

Seasonality of DAX

Analyzing the average monthly performance of the German DAX index a distinct pattern of seasonality can be observed. On average June has been down 0.6%, but the big trouble is yet to come.

Continue reading

A graphical approach to indicator testing

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.

Scatter Charts

Continue reading

Ranking: percent performance and volatility

When ranking a market analysts usually pick the percent performance since a given date as their key figure. If a stock has been at 100 last year and trades at 150 today, percent performance would show you a 50% gain (A). If another stock would only give a 30% gain (B), most people now would draw the conclusion that stock A would have been the better investment. But does this reflect reality?

Percent Performance and Volatility

In reality and as a trader I would never just buy and hold my position, I would always adjust my position size somehow related to the risk in it. I like instruments that rise smoothly, not the roller coaster ones which will only ruin my nerves. So ranking a market solely by percent performance is an useless statistic for me.

Lets continue with our example from above: if stock A, the one who made 50% has had a 10% volatility, and stock B, the 30% gainer, only had a 5% volatility, I surely would like to see stock B on top of my ranking list, and not the high vola but also high gain stock A.

Risking the same amount of money would have given me a bigger win with stock B.

Combining Performance and Volatility

To get stock B up in my ranking list I will have to combine the absolute gain with the market volatility in between. This can be done quite simple. Just add up the daily changes of the stock, normalized by market volatility.Have a look at the formula of this new indicator:

index(today)=index(yesterday)+(price(today)-price(yesterday))/(1.95*stdev(price(yesterday)-price(2 days ago),21))

In plain English: Today’s Vola Return Index equals yesterdays Vola Return Index plus the daily gain normalized by volatility

So if the index has been at 100, the volatility (as a 95% confidence interval over 21 days) is 1 and the stock made 2 points since yesterday, then today’s index would be 100 + 2/1 = 3

Vola Return Index vs. Percent Return Index

Lets have a look at a sample chart to compare the 2 ranking methods. I therefore picked the J.P.Morgan stock.

The upper indicator shows you a percent gain index. It sums up the daily percent gains of the stock movement, basically giving you an impression what you would have won when you would have kept your invested money constant.

The indicator on the bottom is the Vola Return Index. It represents your wins if you would have kept the risk invested into the stock constant. (=e.g. always invest 100$ on the 21 day 95%confidence interval of the daily returns)

Have a closer look at the differences of these two indicators up to October 2016. JPM is slightly up, and that`s why the percent change index is also in the positive area. During the same time the Vola Return Index just fluctuates around the zero line, as the volatility of JPM picked up during this period of time. To keep your risk invested constant over this period of time you would have downsized your position when JPMs volatility picked up, usually during a draw down. No good.

The same can be observed on the upper chart, showing the last months movements of the index. Right now, after the recent correction the percent change index is, like the JPM stock, up again. On the other side the Vola Return Index is still down, due to the rising volatility in JPM.

Vola Return Index – Ranking

Lets put this to a test and rank the 30 Dow Jones industrial stocks according to the percent return index and using my Vola Return Index as a comparison, calculated since 01/01/2015.

The first three stocks are the same, they got the highest vola and highest percent return. But JPM and Visa would get a different sorting. Just see how low the JPM Vola Index is, it would not be the 4th best stock.

Percent returns says JPM and Visa are abou the same, only the Vola Return Index shows that VISA would have been the better investment vehicle compared to JPM. But see for yourself on the chart…

Conclusion

Make sure your indicators show what you actually can do on the market. There is no use in just showing the percent gains of a stock if you trade some kind of VAR adjusted trading style.

Keeping you risk under control is one of the most important things in trading, and using the Vola Return Index instead of just plotting the percent performance can give you some key insights and keep you away from bad investment vehicles. Also have a look at this stock picking portfolio based on similar ideas.

 

Tradesignal Equilla Code for Vola Return Index:

 

 

EEX Phelix Base Yearly – Buy Wednesday, short Thursday?

When it comes to simple trading strategies, the day of the week is surely one of the best things to start with. That’s nothing new when it comes to equity markets. Everybody knows about the calendar effects, based on when the big funds get and invest their money. I do not know about any fundamental reason for the day-of-week effect in German power trading, but is seems to be a fruitful approach.

First of all I have to point out that it is not only the day of the week which is important. A strategy that just buys on Wednesdays and sells 1 or 2 days later would be doomed. But if you add a little filter which confirms the original idea, you will end up with a profitable trading strategy.

This filter will just be a confirmation of the expected move: If you suspect that Wednesday ignites a bullish movement, then wait until Thursday and only buy if the market exceeds Wednesdays high. Same for the short side, wait for a new low before you enter!

Have a look at the chart. The strategy shown buys on Thursdays if Wednesdays high is exceeded. The position is closed 2 days after the entry.

If you run a simple test which day of the week is the best to get ready for a long trade the day after then the next chart shows the return on account of the strategy using data from 2012 up to now: (exit one day after entry)

Continue reading