# Free webinar series on algorithmic trading

I am happy to announce that I will be hosting a free webinar series on quantitative analysis and algorithmic trading. Dates and times for the first shows can be found over here: Tradesignal Webinar Series

## Date and Time based patterns

will be the topic of the first webinar. It will focus on the question if there are date based and time based patterns to be found in the markets. I will show some analysis, discuss the results and and provide the source codes to analyse your own markets.

It will go beyond “sell in may…” and I hope you will enjoy 30 minutes of useful information on how to find an edge in the markets using date and time. Register for free: Register

# A Neural Network based trading strategy

I always dreamed about the machine which tells me to enter long right before the market starts to go up. Might a neural network be this machine? Using Tradesignal and the free Python Neural Net library Pyrenn it is easy to find out…

### Part one: Classification of data

The first step in the process is to tell the Neural Network when it should give me a go. Therefore I designed me small indicator which returns 1 whenever the market has been rising for a given number of bars without falling back in between. This should have been an easy environment to make money and so I want the neural network to analyse the bars before this signal and see, if it can detect a pattern.

classification

### Part 2: Feeding data to neural network

After the classification indicator is working, the script which will do the trading has to take this information and prepare the inputs for the neural net.

data inputs to nn

I opted for a neural network with 5 inputs. These inputs will describe the market behaviour before the signal occurred by giving the %change of the market prior to the signal over 1,3,5,10&15 days. Be creative, the way you describe the market will massively influence the ability of the neural network to learn something from the past.

Beside the preparation of the training data in lines 12 to 19 the script also prepares the input data for the live trading in lines 23 to 27. These inputs, when applied on the trained neural net, will then hopefully give me a signal before the market has some good days.

### Part 3: Create and train the neural network

The codes above run on every bar of the chart and thus build a history of signals. On a specific date on the chart I want to use this data and train the network. Using the Pyrenn neural network module and the data prepared before, this is done with the following lines of code.

As defined in line 37 this is a network with 5 inputs, one output and 2 hidden layers with 3 neurons each.

Lines 44 to 49 transfer the data collected in Tradesignal to the Python environment.

Finally in line 49 the training is done. The network is trained until the max number of iterations or the minimum error has been reached.

Neural Net training

### Part 4: Testing the signals with a trading strategy

nn test

After the training has been done the neural net is fed with live data (lines 59 to 62) to calculate the prognosis for tomorrow. If the bars before today hint that I should buy, the neural net should return 1, otherwise 0.

The most simple test for the quality of the output is a simple trading strategy. It buys if the neural net signals a buy (1) and closes the position after the number of expected positive days (as demanded by classification script) have passed.

### Part 5: Results for daily S&P500 Index data

To obtain the results shown below the NN was trained with 2000 bars of daily data, prior to 2018. The out of sample trading simulation starts in 2018. About 68% of the trades have been positive, leading to a profit factor of 2.64. Not too bad for such a simple approach.

nn results

The strategy shows a low trading frequency, and as the drawdowns show, a more sophisticated exit strategy should have advantages and bring down the magnitude of the losing trades.

# A Simple Neural Network for Indicator Prognosis

## 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.

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

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

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.

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.

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.

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.

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 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

## 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

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

CFI2 Emissions overnight performance

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

F1BY overnight performance

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…

# Charting Probabilities

Charting is all about where you are and what might happen next. Seeing the statistical probabilities of further poves is surely a big help when thinking about the market. This article gives you a free set of indicator which will help you differ the likely from the unlikely. Continue reading

# The magic of implied volatility

VIX and implied volatility in general is a measure of the expected market move. If VIX is trading at 50, the option market expects that the market will stay within 50% up or down within the next year. Continue reading

# This is the end of the world as we know it

I don’t know what the future will bring, but there is one thing I know for sure. The bubble has burst and the party is over.

# Profit from large daily moves

Whenever the market shows an exceptional day ranges it is time to take bite. See how you can profit from large daily market moves.

## Open-Close Range

When looking at any chart, you will surely notice that the large candles tend to close near the high or low. This is due to herding. Once the market is moving significantly, everyone hops on and the large move becomes even larger. This is true for daily, weekly and intraday candles.

The chart shows an indicator which plots the daily move. Every opening is set to zero and the absolute move of the day is drawn. Around these normalised candles a long term 2 standard deviation volatility band is drawn.  Right now the 2 standard deviation volatility for SPX is about +/- 46 points.

## Take a bite before the market closes

As you can see this +/-46 point barrier above/below the opening of the day is a wonderful entry point. If you enter long 46 points above the opening and go short 46 points below the opening nearly all entries would have lead to a profitable trade. To get an even higher probability of success you can volume as a confirmation. Large moves must also show high volume. The exit is done at the end of the session. This analysis does not give any indication for the next days move. So be fast, take your bite and go home with a small profit and no overnight position.

## No free lunch

On the chart it looks easy, but be careful. As an example the last bar shown on the chart first crossed the band to the downside, reversed and crossed above the upper band. So you will need to use a trailing stop to lock in profits and avoid to take the full -46 to +46 points trade as a loss!

# Python Regression Analysis: Drivers of German Power Prices

German Power prices can be explained by supply and demand, but also by causal correlations to underlying energy future prices. A properly weighted basket of gas, coal and emissions should therefore be able to resemble the moves of the power price.  This article will introduce multivariate regression analysis to calculate the influence of the underlying markets on a given benchmark. It is an example of  a machine learning algorithm used in analysis and trading.