Monday 21 March 2016

Risk

Performance Analysis                                                                      

Managing risk according to Michael Halls-Moore comes down to:
  1. identifying and mitigating intrinsic and extrinsic factors that can affect the performance or execution of the algo
  2. managing the portfolio to maximise growth rate and minimise capital drawdown

Strategy and Model risk

This includes the risk from backtesting bias and assumptions in the statistical model that can be ignored. e.g a linear regression assumes homoscedasticity. We can see that through constant variance in the residual plot. However if this is not tested then the linear regression will provide less accurate results in parameter estimation.

Other models use moments of the data such as mean, variance, skewness and kurtosis of strategy returns. This means moments should be constant in time. However if a market regime changes then the model will not fit as well as it should due to a change in these moments. Models with rolling parameters can help.

Portfolio risk

Portfolio allocation may be heavy in certain sectors and therefore it is often important for institutional investors to override particular strategies to account for overloaded factor risk when the preservation of capital is more important than the long term growth rate of the capital.

Institutional investors may also face the risk of causing market impact particularly in illiquid assets. A large percentage of the daily trading volume if traded can invalidate the results of the backtest which typically neglect market impact. Therefore setting a limit such as small percentage of the running average daily volume over a certain period can help.

Strategies in the portfolio should also not be correlated. This correlation can be measured by the Pearson Product Moment Correlation Coefficient (Pearson’s R), though it is best to design it specifically so that they are not correlated due to different asset classes or frequency, particularly as correlation can change during financial contagion. Rolling correlations can therefore be estimated over a long time frame to include in the backtest.

Counterparty risk

In the context of algo trading we are more concerned about the risk of default from suppliers such as an exchange or brokerage (though rare). Michael Halls Moore factors the risk of brokerage bankruptcy and recommends using multiple brokerages though this can make it difficult.
                                   
Money Management

Measurement of account drawdowns (drop in account equity) can allow the trader to realise how much they are able to tolerate because they find it is optimal for long term growth rate of the portfolio via leverage.

The Kelly Criterion enables control over this balance - it lets us see leverage and allocation towards various strategies.

Assumptions:

  • Each algo strategy will be assumed to have a returns of a Gaussian distribution. Furthermore each strategy has their own fixed and constant mean and standard deviation of returns.
  • The returns are excess returns, having subtracted margin account interest and transaction costs (and management and performance fees for institutions). i.e mean annual return - risk free borrowing rate
  • The strategies have no correlation and therefore the covariance matrix between strategy returns is diagonal.

The Kelly Criterion fi for optimal leverage for each strategy to maximise growth rate and minimise drawdowns is:

fi = μi/σi2    for each strategy i to N of vector f
                                               
i.e average excess returns/variance of excess returns for that strategy

fi = optimal leverage = optimal size of portfolio/own equity
                                                                                                           
growth rate = rf + S^2/2    
                                                                       
Where rf is the risk-free interest rate, (the rate at which you can borrow from the broker), and S is the annualised Sharpe Ratio of the strategy.
Sharpe Ratio = annualised mean excess returns/annualised standard deviation of excess returns

Kelly Criterion is obviously not static and therefore should be recalculated regularly via a lookback window of 3-6 months of daily returns. Sometimes maintaining this level of leverage means selling into a loss though this is mathematically the way to maximise long term growth rate. Because the average excess returns and variance of excess returns are uncertain in practice traders tend to use the half Kelly, i.e divide it by two, where the actual value serves as an upper bound of leverage to use. Otherwise using the actual Kelly value can lead to a complete wipeout of account equity given the non Gaussian nature of returns for each strategy.
                                   
Risk Management
Value at Risk
                                               
VaR is an estimate at a certain confidence level of the size of a loss from a portfolio over a certain timeframe. This time period is one that would lead to the least market impact if the portfolio were to be liquidated.
                                   
Where L is loss, the value of the loss is VaR and c is confidence level:
P(L <= -VaR) = 1-c

So to mathematically express the fact there is a 95% chance of losing no more than US$100 000 the next day:

P(L <= -1.0 x 105 ) = 0.05            

Though straightforward to calculate, it does not tell us by how much the loss can exceed the value (that is expected shortfall). It also assumes typical market conditions (not tail risk) and volatility and correlation of assets need to be known, which can be difficult especially if there is a change in market regime that changes these parameters significantly.

Three techniques to calculate VaR are:
1.      variance-covariance method: assumes normal distribution
2.      monte carlo: assumes non normal distribution
3.      historical bootstrapping: uses historical returns of the asset/entire strategy

For the variance-covariance method for an asset/strategy, the daily VaR of a portfolio of P dollars with confidence level c and alpha being the inverse of a cumulative distribution function* of a normal distribution:

VaR = P - (P(alpha(1-c)+1))

*Cumulative frequency analysis is the analysis of the frequency of occurrence of values of a phenomenon less than a reference value.

The ppy method under the SciPy library in Python enables us to generate the values for the inverse cumulative distribution function of a normal distribution with mean and standard deviation values obtained from historical daily returns of an asset (we would replace these with returns of a strategy).

More info that describes the various ways in which R can calculate VaR and ES: http://www.r-bloggers.com/the-estimation-of-value-at-risk-and-expected-shortfall/

#!/usr/bin/python
# -*- coding: utf-8 -*-

# var.py

#importing the required packages
from __future__ import print_function

import datetime

import numpy
import pandas.io.data
from scipy.stats import norm

#creating the function to calculate VaR with the following parameters
def var_cov_var(P, c, mu, sigma):
    """
    Variance-Covariance calculation of daily Value-at-Risk
    using confidence level c, with mean of returns mu
    and standard deviation of returns sigma, on a portfolio
    of value P.
    """
    alpha = norm.ppf(1-c, mu, sigma)
    return P - P*(alpha + 1)

#reading the table of historical prices from Yahoo Finance for JP Morgan
#over the period 2012 to 2016
if __name__ == "__main__":
    start = datetime.datetime(2012, 1, 1)
    end = datetime.datetime(2016, 1, 1)

    jp = pandas.io.data.DataReader("JPM", 'yahoo', start, end)
   
    #converting into percentage returns
    jp["rets"] = jp["Adj Close"].pct_change()

    P = 1e6   # 1,000,000 USD portfolio 
    c = 0.99  # 99% confidence interval
    mu = numpy.mean(jp["rets"])
    sigma = numpy.std(jp["rets"])

    var = var_cov_var(P, c, mu, sigma)
    print("Value-at-Risk: $%0.2f" % var)
                                               
Value-at-Risk: $31676.22
There is a 99% chance of losing no more than $31 676.22 from a $100 000 portfolio the next day.




Thursday 10 March 2016

Webinar with Algo Trader Cameron Madlani - Creating Scalping Robots

Recently we joined Meet Up and registered for memberships in groups related to Finance, Algorithmic Trading and Data Science.

Our first Webinar was hosted by Cameron Madlani, an algorithmic trader based in Sydney. The topic was "Making Daily Money with Automated Trading- Creating Scalping EAs". Here's what we learnt:


Types of Automation


- Fully Automated

This is where the robot calculates risk, places trade, manages stop losses and targets, and closes the trade. To set up trading robots to enter high probability set ups in fast moving markets, we would require a virtual private server (Equinix Data Centre is popular with brokers) to enable trades to execute in milliseconds. In January 2016, the first Artificial Intelligence Hedge Fund 'Aidyia' launched. They place all trades using artificial intelligence. San Fran based Santient Technologies also runs a $143mill hedge fund that uses AI since 2015.


- Semi Automated


This is where the trader chooses or places the trade manually though the robot identifies the opportunity with a signal. It is based on the trader's own fundamental analysis and risk aversion to accept the trade. If they do choose to go through with the trade, the robot still manages stop losses and targets and closes the trade as it would normally do if the entire process was fully automated.


- Radars


As the name suggests, this refers to the robot monitoring multiple markets with an onscreen view of the financial instruments universe, and provides signal on dashboard for traders.


- Indicators


Indicators identify trading opportunities and provides general trade signal.


The process of automation for beginners:

1. Clearly define mechanical strategy
2. Identify intuition/experience. Depending on your trading style, you are able to include experience in the code. For instance, if you were a day trader, you would have code to check market spreads and timing. However if you find your most profitable trading hours are from 9am to 10am from experience, you can still optimise the robot to find the best hours from 0-24 and days of the week as the bot can combat time zone differences, and for instance, finding 4am-5am is actually the most profitable. Something interesting to check out is XML feeds. These are show fundamental figures, and the robot can be plugged in to trade based on these fundamental figures, such as reading interest rates which then directs their trade accordingly.

3. Write out strategy in pseudo code. Pseudo code refers to using if and else statements to plan out the strategy in a logical method. For instance:


If Moving Average 1 > Moving Average 2

    then Buy
If Moving Average 1 < Moving Average 2
    then Sell
Else do nothing

4. Learn coding: You could hire a professional automation firm or code yourself using languages such as MQL, JForex, Ninja Script, EFS. Alternatively, use algorithmic creation tool (Drag & Drop).


5. Test for extreme situations such as market closing times

6. Optimise but avoid curve fitting
7. Back test
8. Run on Demo Account to ensure satisfactory results

These steps are outlined in greater detail in our other blog posts relating to Algo trading.


Real Life Examples


Moving Average Crossover


The below examples use AUD as one of the currencies in the currency pair. This market trends well due to carry trading. Carry trading refers to investors using their accounts to buy $A due to relatively high interest rates, which makes them long carry trade revenue.


GBP/AUD: Jan 2015 - Dec 2015




This chart shows 3 Moving Averages (MA1, MA2, MA3). These moving averages refer to averages over different time frames. For instance, MA1 is a short term moving average compared to MA2. With two moving averages, the philosophy is when the shorter MA crosses the longer MA on the upside, this is a buy signal. If the shorter MA crosses the longer MA on the downside, this generates a sell signal. Note this is a momentum strategy. The more space between the two MA lines, the more confident you can be that the signal is correct and will continue. 

The same thing applies to three moving averages. Imagine a trader plotting a 5 day, 10 day and 20 day MAs on a chart, then buy and sell signals generated from when the 5 day moving average cross the 10 and 20 day moving average. This approach is suitable for those who will accept some delay in entering new trades in exchange for more accuracy in signals. The 3 moving average model is useful in keeping out of the trade if the price movements stop trending and start going sideways, or becomes choppy and volatile, so that you would need an exceptionally long moving average to see the trend. 

The above example has a total profit of 830 pips. If you traded with $10/pip, that would total to a profit of $8300! The ratio of profit to loss is 2.23. This means you are making $2.23 for each dollar you risk in the markets. Generally, investment banks are happy with even $1.10 profit to loss, so this is a good return. A common question that people ask is why might it be unreasonable to scale out automatically - i.e. take some of the profit as soon as the trade proves successful. This adversely affects the profit to loss ratio as we are risking more money than we profit. 

AUD/JPY: Jan 2015 - Dec 2015



The second chart shows the same strategy being applied to a different currency pair. With a profit of 959 pips and profit to loss ratio of 7.27, it demonstrates the strategy is very effective in different currency pairs.



AUD/JPY: Jan 2012 - Dec 2015




To ensure the returns are not isolated to one period of time, the strategy is again tested on the AUD/JPY currency pair from 2012 to 2015. It is still profitable, making 3691 pips or points. The profit to loss ratio however is  now lower at 3.28, though this number is still acceptable.


Bollinger Bands AUD/JPY Jan 15- Dec 15 




This chart demonstrates the application of two strategies together on one currency pair- the AUD/JPY over the time period from Jan 2015 to Dec 2015. The two strategies are ranging and trending. This means that when the trend is falling, we are profiting from the ranging strategy (mean reversion). Here we are using moving average crossovers as signals. Profit is 583 pips, profit to loss ratio is 1.23.




Using additional filter - with Bollinger bands, when the price hits the upper band, we go short. When the price hits the lower band, we go long. Here Easy Trend is a filter developed and placed into this strategy. It shows up as the red line. When the price action is under the red line, go short, and if the price action is above, go long. It is looking for more than just the traditional hitting the band method- for instance, we are looking for the candle to close below the band, which suggests reversal. Compared to the initial strategy, the filter helps improve profits. Profitability is now 727 pips over one year, and profit to loss ratio is 3.75.


Watch List Signal Radar 





Automated Signal Indicator





Daily Income 




The above chart demonstrates hourly EU vs AUD. Bollinger bands identify where prices are at extremes. The middle is the moving average. Outer bands represent two standard deviations from the moving average. Bollinger bands act as rubber bands and works well with currency pairs. This chart is trading from 12th Feb to present, making 477 points. We are able to trade hourly charts and fully automate this process.


The circles represent the set up occurring. If the circle is at the bottom of the candle it indicates the candle closed below the Bollinger band, whilst a circle at the top indicates the candle closed above the Bollinger band. An up arrow indicates long position, down arrow is short position. Squares means it hit the pre-specified stop loss. Diamond means take profit. If the strategy involved closing out the trade when the price hits the next Bollinger band or stop loss, and taking profit where it hits baseline (middle of the band), then this is destroying the profit to loss ratio as we risk more money to make less.


As shown below, a filter is applied called "EasyTrend" that focuses on trading market direction i.e. "bullish", "bearish" or "ranging". This is applied through ticking the box 'Only trade with market direction' rather than simply going long/short with a high time frame. This means when the green line appears, go long, exit trade when trend changes and stop loss is on line or previous candle's low. The stop loss is variable - we can bring the trend line closer or further to the market. EasyTrend speed setting is 4, which makes the trading less sensitive to the market direction changes. As a result trades are slower to enter, though this strategy is good for high volatility. Cameron suggests an obscure time frame gives advantage against other traders. This changes the profit to loss ratio from 1.92 to 2.01.




Hourly Intraday Strategy: Ichimoku Indicator 



Ichimoku is intended as a at a glance indicator to allow traders to determine whether a trend is present or whether it is better to wait for a better entry time on a pair. Ichimoku can be used in bull and bear markets regardless of time frame or liquid instrument. The only condition which Ichimoku cannot be used is where there is no trend. There are essentially two dynamic lines that form a 'cloud'. These lines act as support and resistance lines, that is, when the price tries to go above, it will bounce back. This helps traders identify the trend of the current price relative to past price action. 
When the price is above the cloud, we enter a long trade as it indicates a start of a trend (cloud acts as support). When the price is below the cloud, we look for temporary corrections higher to enter a short position (cloud acts as resistance). Once this is set up, we then look at moving averages- Cameron has set up six moving averages, the fast moving average is a 6 period moving average and the slow moving average is 50 period moving average. These moving averages use mid prices rather than closing prices. 



Using Martingale's theory, the stake is increased each time we lose until there is a winning trade in which the amount at stake is reset. The results of this strategy are below:






The disadvantage of this strategy is the large drawdown experienced and greater volatility as shown in the results dashboard. However eventually this strategy was successful. It is advised against trading Euro Dollar as this currency pair is very noisy. A good hedging strategy could involve exploiting the Canadian and Australian dollar being almost mirror images to each other.


Overall we highly enjoyed the webinar meet up and highly recommend it.


Wednesday 2 March 2016

Time Series Analysis

We can assess whether the time series is prone to mean reversion through the hypothesis that it is statistically significantly different from a random walk. For a mean reverting time series, the change in the value in the next time period is proportional to the difference between mean and present price. This is a continuous time series, known as an Ornstein-Uhlenbeck process.


Screen Shot 2016-02-23 at 10.33.12 AM.png
θ is the rate of reversion to the mean,  μ, and σ is the variance of the process and Wt is a Wiener Process or Brownian Motion.


Python libraries pandas and statsmodels is useful to test whether the time series follows the Ornstein-Uhlenbeck process. Here I will be trying out R instead.


The Augmented Dickey-Fuller (ADF) test uses this Orsentein-Uhlenbeck process as an assumption and tests for the presence of a unit root in an autoregressive time series. If the process has a unit root, then it is a non-stationary time series. ie If y = 0 then it is a random walk process.


The linear lag model of order p indicates the change in the price is proportional to a constant, time, the previous p values and an error term. β will be 0 if there is a long term drift in the price.
Screen Shot 2016-02-23 at 12.08.01 PM.png
The ADF test follows these steps:
  1. set p = 1 which is sufficient according to trading research
  1. calculate the test statistic, DFτ
  2. use the distribution of this test statistic along with the selected critical value to decide whether to reject the null hypothesis that y = 0 (hence α = β = 0 and non stationary) ie when DFτ is smaller than the critical values at whichever chosen significance levels.
Screen Shot 2016-02-23 at 12.08.44 PM.png


We firstly download Yahoo finance data on Amazon OHLCV data as a .csv file, from 1/1/2010 to 23/4/2016.


Screen Shot 2016-02-24 at 1.51.11 PM.png
Screen Shot 2016-02-24 at 1.51.54 PM.png
We therefore cannot reject the null and conclude that a mean reversion strategy would most likely not work on this stock as it does not seem to exhibit mean reversion over the past 6 years.


Another approach can be to determine whether the time series is stationary - the mean and variance of the stochastic process does not change over time and therefore there is no trend. This occurs when the joint probability distribution is constant. The Hurst Exponent can help us in this way by providing a scalar value. Here we are measuring the rate in which prices diverge from their initial value to detect whether the nature of the time series is mean reverting or a random walk or a trend. If it is a stationary series then this rate is slower than a Geometric Brownian Motion (random walk).


More on the Hurst Exponent: http://www.jstor.org/stable/3213900?seq=1#page_scan_tab_contents


To get to this scalar value we use the variance of a log time series ( time lag T ) to assess the rate of diffusive behaviour.
Screen Shot 2016-02-24 at 5.50.42 PM.png
Where the brackets ⟨ and ⟩ refer to the average over all values of t.
For a random walk, when T is large the variance of T is proportional to T:
Screen Shot 2016-02-24 at 5.51.29 PM.png
If we find behaviour that differs from this, then we have identified either a trending or a mean-reverting series. This is because then the price changes are autocorrelated.
We then change it to the below equation with the Hurst Exponent value H:


Screen Shot 2016-02-24 at 5.51.53 PM.png
• H < 0.5 - The time series is mean reverting
• H = 0.5 - The time series is a Geometric Brownian Motion
• H > 0.5 - The time series is trending
The closer H is to 0 the higher it is mean reverting and the closer to 1 the higher it is trending.


This can be seen in R, where I create a random generation of numbers that follow GBM:
Screen Shot 2016-02-26 at 12.34.37 PM.png


Screen Shot 2016-02-26 at 11.51.56 AM.png
Screen Shot 2016-02-26 at 11.52.22 AM.png
Screen Shot 2016-02-26 at 11.52.44 AM.png
Screen Shot 2016-02-26 at 11.52.50 AM.png
We can conclude that Amazon’s adjusted close stock price over the past 6 years has followed a trend.


Equities are typically not an asset class that experiences mean reversion though we can create a portfolio of stocks with prices that are stationary. The pairs trade as Mary blogged about before for instance relies on mean reversion where relative prices diverge and converge to the mean. JP Morgan and BoA were used before and now I will demonstrate the research into their cointegrating nature to select such a portfolio.


This below plot is from Yahoo Finance and shows their respective price histories for the period Feb 28th 2015 to Feb 28th 2016.


Screen Shot 2016-02-29 at 8.36.56 PM.png
A scatterplot allows us to visually determine whether this pair are closely integrated.
Screen Shot 2016-03-01 at 10.44.50 PM.png
Screen Shot 2016-02-29 at 8.59.10 PM.png
The pairs trade should work with a linear model for a relationship between the two stock prices as supported by the scatterplot.
Screen Shot 2016-02-26 at 1.52.01 PM.png
Where y is the price of JPM Morgan stock and x is the price of BAC stock at time t.







By using the residuals of the linear regression we can perform the Cointegrated Augmented Dickey-Fuller Test to determine the optimal hedge ratio and test for stationarity under this beta required to create a linear combination. If so, then this pair trade assumption is fulfilled.

Update: I am experiencing a technical bug in performing this test in R and I will post the results as soon as I can.