Suppose you would like to invest 1000$ in two stocks, and you have to decide how much money you should put into these two stocks. You could use some Modern Portfolio Theory for that, for example. However, I would like to talk about a second approach, which is quite curious in my opinion. Continue reading

# Using Brexit polls to build an algorithm to trade GBP/USD

Last 23rd of June a referendum took place in the United Kingdom, in which the voters decided to leave the European Union. After the results were published, the GBP/USD dropped significantly. But, could one have created a trading strategy for the GBP/USD, that was based solely in the polls that were periodically published?

After the referendum, I wanted test if this was possible. The model I wanted to build should have the following features:

- The strategy would only buy/short sell the GBP/USD, if in that day a new poll was published. Then, in the following day the operation would be closed.
- Positive polls for the remain campaign should have a positive impact for the pound, and viceversa. Moreover, the greater the difference between the percentage of remain and leave votes is, the bigger this impact should be.
- The closer the referendum day is, the higher the impact of the poll should be.
- If the polls suggest that the remain and leave campaign will receive the same amount of votes more or less, the GBP/USD should follow the trend it previously had.

Taking this features in mind, I tested the following model:

Here, denotes the exchange rate of GBP/USD, indicates the time at which the referendum will take place, and indicates the percentage of remain votes of the latest poll, for day . This differential equation satisfies all the features I asked to the model:

- If a poll is published on a day, and the poll is favorable for the remain campaign, then is positive, so that the GBP/USD will tend to go up. And viceversa, if it is favorable for the leave campaign, would be negative, so that the exchange rate will tend to go down.
- If is close to , that is, we are close to the referendum day, would be close to 1, and if is not close to , it would be close to 0. Thus, the closer we are to the referendum day, the greater the influence of the poll would be.
- The term makes the GBP/USD to follow the previous trend. The greater the derivative is, the higher the strength of the trend would be.

Intuitively, think of the differential equation as a moving bicycle that is speeding up. However, there is wind around the bicycle, that depending on its direction and strength would make the bicycle slower or faster. The bicycle represents the exchange rate GBP/USD, and the wind represents the polls: depending on the direction, it will move the rate in one direction or the other. But in all cases, the bicycle will try continue to move its current direction.

So, how could we solve the differential equation? An easy way consists of using finite differences, in order to numerically approximate the solution. The term would be approximated as

Where , and is the time step (since we are using daily data, ). Now, we could approximate as:

Using all this information, and denoting , we get

# Results

The poll data was obtained from the BBC’s site. The information of the exchange rate GBP/USD and the polls are gathered in this spreadsheet. The returns are shown in the following chart:

The returns from the 13th of September 2015 and the 22nd of June 2016 was 11.9%. In the same period of time, the S&P 500 lost -0.38%, and the FTSE 100 returned about 0.9%. Taking a look to the chart, one can see that until March 2016 more or less the returns remained quite flat. However, from March to June the returns increased a lot, probably due to the fact that in this period the polls became more important and relevant, when it comes to the GBP/USD exchange rate.

# The Code

The code is shown below, and it is also available in GitHub.

data<-read.csv("data.csv") money<-c(1000); #Start with 1000$ i0=247; for (i in i0:(nrow(data)-1)) { if (is.na(data$Remain[i])) { money<-c(money, money[length(money)]); cat("Stay still.\n") next(); } T=nrow(data)-i0; nextDay=2*data$Exchange[i]-data$Exchange[i-1]+(i-i0)/T * (data$Remain[i]-0.5)+(data$Exchange[i]-data$Exchange[i-1]) margin=0; if (nextDay>data$Exchange[i]*(1+margin) & data$Exchange[i]*0.01>m) { money<-c(money, money[length(money)]*as.numeric(data$Exchange[i+1])/as.numeric(data$Exchange[i])); cat("Buy. Money:" , money[length(money)], "\n"); } else if (nextDay<data$Exchange[i]*(1-margin) & data$Exchange[i]*0.01>m) { money<-c(money, money[length(money)]*as.numeric(data$Exchange[i])/as.numeric(data$Exchange[i+1])); cat("Sell. Money:" , money[length(money)], "\n"); } else { money<-c(money, money[length(money)]); cat("Stay still.\n") } } money.zoo<-zoo(money, as.Date(data$Date[-(1:i0)], "%d/%m/%Y")); exchange.zoo<-zoo(1000*data$Exchange[(i0):(nrow(data)-1)]/as.numeric(data$Exchange[i0]), as.Date(data$Date[-(1:i0)], "%d/%m/%Y")); z<-as.zoo(cbind(money.zoo, exchange.zoo)) plot(x = z, ylab = "Retrns", xlab="Date", main = "Cumulative Returns", screens=1, col=c("red", "blue")) legend(x = "topleft", legend = c("Strategy", "GBP/USD returns"), lty = 1,col = c("red", "blue")) |

# Further improvement

The model can be generalized a bit, in the following way:

Based on this model, the constants that offer the best fit could be found, in order to improve a bit the performance of the model.

# Does a good January for the stock market imply a good year, and viceversa?

There is a saying in Wall Street, that says that whatever happens to the stock market in January will determine the rest of the year. That is, a good January will imply a good year for the stock market, and viceversa.

In this post, I will try to show if there actually is any truth to this. Continue reading

# MonteCarlo and Arima for stock selection

A few days ago, in this post, I talked about how ARIMA models could be used to forecast the S&P 500 index, and use this information in order to buy or sell the index every day, if the algorithm predicts an increase or decrease in the price, respectively.

In this post, I will go a step further. The idea of the trading algorithm will be the following:

- Given a day, for each stock of a certain index, select the best ARIMA model for this stock.
- Then, simulate different possible trajectories of the price of this stock.
- Now, estimate the probability of the stock going up, dividing the number of trajectories that actually increased their price, by the total number of trajectories. This way of estimating probabilities is called Monte Carlo method.
- Now, since this process was repeated for a big number of stocks, we may sort them appropiately. If is the probability of a price rise for stock , this stock would be a good candidate to buy if was close to 1. However, if it is close to 0, it would also be a good candidate, but for short selling. Thus, we pick the stock such that is the maximum.
- Now, once we have calculated the stock where the maximum is attained, we go long or short depending if the probability was greater or less than 0.5.

Once I have explained how the algorithm will work Continue reading

# Trading strategy for the S&P 500 index based on ARIMA models

ARIMA models are a family of models for time series that are used to forecast future behaviour. It can be (and it is) used in finance, and in particular in trading. In this post I will try to show a specific use for a trading strategy based on these models, and it will be applied to the S&P 500 index.

ARIMA models are denoted by ARIMA(p,d,q), where:

- p is the order of the
*autorregresive factor*. - d is the order of
*differenciation*, in order to obtain a stationary time series. - q is the order of the
*moving-average*.

R will be used to implement this strategy. In particular, the timeSeries, quantmod and forecast libraries will be used Continue reading

# Differential equation that models the support and resistance strategy

Support and resistance indicators are widely used in technical analysis. What I tried to do is to model this strategy using a suitable differential equation, in order to test it with historical data.

A simple model could be the following: