Site Update Scheduled

I began restructuring this site a few months ago but due to a few projects that I was working on, hadn’t completed the process. Initially it appeared that I would post periodically dependent upon what I might be working on at the time. Moving forward, I intend to add content more regularly, yet with a focus on quality and not quantity. I have a keen interest in the cross section of financial modeling and valuation, technical analysis and quantitative analysis as well as research specific to quantitative trading and or portfolio management.

In the coming weeks, I will be adding more content specific to quantitative trading research across different asset classes and strategies.


Comments Off on Site Update Scheduled

Filed under Analysis

Using K-Means Clustering for Pair Selection Series in Python

I recently wrote a series on Quantinsti’s blog entitled “Using K-Means Clustering for Pair Selection in Python”. In this series I introduce what K-Means Clustering is and how it can be used to create a Statistical Arbitrage Strategy.  We also learn what it would be like to build a Statistical Arbitrage Strategy, having to solve the pair selection problem, without K-Means.

Once we’ve established a good foundation in what K-Means is and what our strategy development would look like without K-Means, we then get into developing our strategy. All the code is written in Python. We use a handful of features on the S&P 500 which contains 505 stocks, create clusters and write logic that will allow us to test every combination of pairs within each cluster toward the end of developing our strategy.

This was a interesting series to develop. Part 1 is now available here at Quantinsti’s blog. Stay tuned for future posts.


Leave a comment

Filed under Analysis

Protected: Trading Options Volatility In Indian Market with Python

This content is password protected. To view it please enter your password below:

Enter your password to view comments.

Filed under Analysis, Strategy

Quantitative Trading: FAANG Example Part II

In the first post in this series( read Part I here) we had the stated goal of introducing a simple quantitative trading workflow. We chose a group of familiar equities to be the subjects of our analysis. In Part I, our task was to conduct some general analysis of the securities, survey their respective descriptive statistics, and compare the behavior and character of each. During this initial exercise we took a look at how each of the securities were correlated to each other.

Surveying the correlation between securities is an initial step in developing some type of trading methodology. It is to be noted that the prices of securities follow a log normal distribution while the returns follow a normal distribution. In lieu of this, typically, the correlation of each securities returns are evaluated while with respect to prices, the log normal prices are used.

To continue with our methodology, while correlation was mentioned in Part I, it was later stated at the closing that further research had to be conducted prior to arriving at a trading strategy. This was the case as while securities may move similarly, it does not necessarily insinuate that there exists a tradeable relationship between them.  The presence of a tradeable relationship is contingent upon the portfolio of securities being cointegrated. Cointegration of two or more securities is the stationarity of their statistical properties. In other words, the prices of securities are said to follow a geometric random walk. A pair of securities can move in the same direction randomly, yet the relationship between the two securities can remain stable. This presents a trading opportunity with reference to the relationship between the two securities and hence a market neutral strategy.

The FAANG stocks typically all displayed extremely high correlations in Part I with the exception of AAPL. Our next step was to test each of the possible pairs for cointegration. The Augmented Dickey Fuller Test was employed to test for cointegration. It is noted that the Johansen test is a viable alternative and one that can be applied to more than one pair of securities. While the FAANG stocks displayed high correlations, the only pairs that displayed cointegration over the sample period were NFLX/FB and FB/NFLX. You may be wondering why this pair was seemingly mentioned twice. This is because though we are limited to these two stocks, the change in order of the two actually create a completely different portfolio. In other words, the relationship of, for instance NFLX as the independent variable relative to FB can yield different results to that of FB as the independent variable to NFLX and thus it is possible to trade two securities essentially as two completely different portfolios. Both of these pairs displayed cointegration at the 99% confidence interval.

One thing that was interesting is that in Part I, the correlation of FB to NFLX over the sample period was .95 and was second to that of FB and GOOG which came in at .96. However, both combinations of the FB/GOOG pairs (i.e. FB as both independent and dependent variable) failed the cointgration test at the 90% confidence interval. This shows that mere correlation does not secure a tradeable relationship, well at least in terms of rejecting the null hypothesis at the 1%, 5%, and 10% critical values. There may still exist a tradeable relationship between two securities even if we can not reject the null hypothesis at these levels but for the sake of this exercise we assume the contrary.

In Part III of this series, we’ll build upon our analysis and seek to build a linear mean reverting strategy in Python using both combinations of the pairs of NFLX/FB and FB/NFLX. To recap, thus far we’ve 1) analyzed the behavior and statistically properties of the FAANG stocks 2) formed a null and alternate hypothesis 3) conducted hypothesis testing employing the use of the ADF test, and now are ready to develop a linear mean reversion strategy and evaluate the performance of the pairs’ performance vs each other relative to the alteration of the order.

Leave a comment

Filed under Analysis

Strategy Snapshot: The Evolution of a System Across Different Market Regimes

In this snapshot we take a brief look at how an algorithmic trading system can evolve under different market regimes. The strategy is a swing trading strategy and derives signals from changes in momentum. Python was employed to conduct a backtest of this strategy over a YTD, One Year, Three Year, Five Year, and Historical Period. It is worth noting that the strategy was not reconfigured over these periods and thus avoids overfitting. The strategy is very simple in design which too limits data snooping bias. The logic for the system was derived from a theory that I had been testing for intraday trading. I simply applied that theory to a higher timeframe to gain understanding of its ability to scale.

Below are the returns for the eMini S&P YTD and Strategy Performance respectively.

ES YTD as of July 1 2017ES Strategy Performance YTD as Jul 1 2017

Merely from viewing the visualizations, one can deduce that drawdowns are typically moderate. One area that really stands out is that of mid-May when the eMini S&P retraced and yet the strategy performed inversely. Also, over the period of roughly March to mid-April, during a pullback by the market, the strategy resembles more of a sideways price movement.

Below are the eMini S&P Returns and Strategy Returns for the One Year Period.

ES One Year Performance as of Jul 1 2017ES Strategy Performance One Year as of Jul 1 2017

On a one year basis, we can see that the strategy remained consolidated over a period of a couple of months before appreciating substantially in Sept. 2016. During the period in which the strategy’s returns were muted, the market had appreciated  but was also consolidating. Notice how once the market began to drift lower, the strategy began to increase in profitability. Also, it appears that the bottom in the market, which occurs in Nov. 2016, was also a peak in the strategy. As the market began to rally on the back of the Presidential Election results, the strategy began to consolidate. The strategy experienced a drawdown, yet it appears to have been contained.

Below are the returns of the eMini S&P and Strategy Returns over the last three years.

ES Three Year Performance as of Jul 1 2017ES Three Year Strategy Performance as of Jul 1 2017

Looking at the performance of the strategy on a three-year basis provides  a greater sense of the potential longevity of the system. At the onset, the system experienced a drawdown of roughly (-5%). One thing that really jumped out at me was the fact that the market, while also experiencing a drawdown at the beginning of this period, had a larger peak to trough drawdown. The system appears to have gained some traction in early 2015 before experiencing a drawdown in coming months led by the market. Of the visualizations viewed thus far, this particular one appears to show the closest correlation to the market, primarily from around May 2015 onward. One thing to point out is that though showing a closer correlation to the market over this time frame, the strategy still managed to outperform the market.

Below is a look at the performance of the system on a five-year basis.

ES Five Year Performance as of Jul 1 2017ES Five Year Strategy Performance as of Jul 1 2017

This time frame paints a unique picture. To begin, the system which started trading in late 2012 experienced a relatively minor drawdown before essentially, catching pace with the market. It can also be seen that while the strategy was drawing down, the market was moving higher at the onset of this period. The first thing that really jumps out is that the strategy experienced a significant drawdown over mid 2013 to early 2014. Over this period the market was yet moving higher. The strategy managed to recover but not gain pace with the market over this time frame.

To provide some additional color to the performance and longevity of systems within different market regimes, I’ve included a longer dated historical period below.

ES RSI Historical Backtest

I find it necessary to be aware of not only how your strategy will perform within your specified time frame, but also to be cognizant of what market regimes could alter the performance of your system over time.

Leave a comment

Filed under Strategy

Textron(TXT): Combining Quantitative Analysis with Equity Financial Modeling and Valuation

This is the beginning of a series of posts that will combine the strengths of fundamental, technical, and quantitative analysis. Ultimately, I intend on designing a quantitative strategy as part of this project, yet the actual realization of this will be based on the merits of the analysis. This undertaking is an avid example of combining discretionary, algorithmic and quantitative skill sets. Conventionally, when coupled with valuation, quantitative analysis is more renown for the financial engineering subset relative to valuing derivatives. Equity valuation is less mathematically strenuous than derivative products which is why I believe combining such with a quantitative workflow should be interesting.

Textron(TXT) was an equity that I began researching as part of a much larger merger/acquisition modeling and valuation query. We’re going to take part of that analysis and begin by using it as the foundation for continued analysis of Textron(TXT) quantitatively.

Below you will find a slideshow of the pitch-book I created for this idea. Note, that some sections, namely that in which the actual merger analysis and proposal, and various means of structuring the transaction has been omitted. This analysis is the same analysis that you would conduct in IBD at an investment bank. I conducted a 3 Statement Model, Discounted Cash Flow, Comparable Companies, etc. Precedent Transactions Analysis is not included. Pitch-books similar to the one that I’ve provided are typically what you would spend the majority of your time doing as an investment banking analyst. The skill of developing merger models and valuations his highly invaluable particularly to those that desire to work in private equity or for a value or special situations hedge fund. This is so due to the extensive use of modeling. In an individual company analysis, you would build a 3 Statement Model, conduct Comps, PPaids, and DCF. In a merger model, however, this is multiplied by 3.5x. This is because you must do the aforementioned for both legs of the transaction in addition to actually modeling the close of the transaction, at which time you would conduct another valuation(ie. Comps, PPaids, DCF).

For those who would like to forego browsing the actual pitch-book, I’ve provided some summary snapshots from the analysis. Within the analysis, some quantitative tools were used. Namely Python and R were utilized to conduct visualizations, Monte Carlo Simulation, and other Risk Analysis. Excel was utilized for basic operational metrics display.

TXT Slide 41

TXT Slide 50


TXT Slide 51


TXT Slide 56

The complete excerpt of the presentation is below:

TXT Slide 2TXT Slide 3

This slideshow requires JavaScript.


Moving forward, in future posts we’ll begin by looking at Textron(TXT) from a quantitative trading perspective and survey how we can combine the spectrum of fundamental, technical, and quantitative analysis. Within the quantitative analysis, we will begin in the area of statistics, move toward probability and hypothesis testing, and reevaluate the merits of a quantitative trading strategy. Regarding the time-frame of the strategy, my initial assumptions would be to create a strategy that would trade on the intraday time-frame, but we shall see based on the analysis.

Leave a comment

Filed under Analysis

CL_F in Recent Times, Appears to Selloff as the Range Expands

I decided to take a look at the CL Futures to determine if there might be some tradeable relationship between the range and the returns, as well as the amount of volume and the returns. This post summarizes my findings, of which, I found the relationship between the daily returns and average daily range to be the most compelling of the queries.

I collected data over variety of periods on the CL. Inclusive to these were one month, three month, year-to-date, one year and five years. Below is a linear regression of the range to returns for the one year interval. The correlation within this period is 0.049

CL 1 Year Linear Regression Range to Returns

Next, I took a look at the year-to-date period. The YTD linear regression of range to returns is below and has a correlation of -0.49. A massive difference from that of the one year interval

CL YTD Linear Regression Range to Returns

Below is the three month range to returns linear regression. This period has a correlation of -0.56. which is a pretty significant increase above that of the YTD period.

CL 3 Month Linear Regression Range to Returns

The one month linear regression of range to returns in CL is below and has a correlation of -0.52. The correlation has increased somewhat, but still warrants some attention.

CL One Month Liinear Regression Range to Returns

I considered the above visualizations. It can be inferred that in more recent trading, when the CL does experience range expansion, it is most likely to be to the downside, or trade off. Just brainstorming a bit about this, this information could be useful in structuring a discretionary trading strategy, primarily intraday. I feel that the intraday timeframe would be the most advantageous as it would allow the trader to monitor the average range of the past n trading sessions and when the CL experienced a session that was above the average range of the lookback period, the trader could use this information to provide additional context on the intraday timeframe.

The below plot further justifies the use of this information for intraday trading current conditions as the longer term statistical significance for such an approach appears to be lacking.

The following is the five year linear regression of range to returns which has a correlation -0.016.

CL 5Yr Linear Regression Range to Returns

The relationship of volume to returns was less compelling, but as it was included in my analysis, I’ve listed my findings below. I surveyed the same period as that of range to returns. The following were the correlations for volume to returns: -0.038, -0.18, -0.11,  0.014, and 0.0032 for the one month, three months, year-to-date, one year and five year periods.

CL 1Month Linear Regression Volume to Returns

CL 3Mo Linear Regression Volume to Returns

CL YTD Linear Regression Volume to Returns

CL 1Yr Linear Regression Volume to Returns

CL 5Yr Linear Regression Volume to Returns

Leave a comment

Filed under Analysis

Quantitative Trading: FAANG Example Part I

In this post I introduce a basic workflow used to develop a quantitative trading strategy. We’re going to collect data on ‘FAANG’ stocks(Facebook, AAPL, AMZN, NFLX, and GOOG) and look for statistical significance in the underlying equities. We will then use this to base our hypothesis and develop a backtest of a quantitative trading strategy.

Once I collected the data, I initially wanted to take a look at the time series of each of these stocks.

FAANG Time Series

After getting a general idea of the performance of the stocks, I then decided to take a look at the return profiles of each underlying. Here my primary interest included understanding the typical intraday volatility as to develop grounds to better understand the risk associated with each underlying.

FAANG Aggregate Daily Returns

From looking at the above plot, it appears that NFLX is clearly the most volatile of the group. I decided to dig a little deeper and thus plotted each equity’s daily return profile separately for individual analysis.

Facebook Daily Return ProfileApple Daily Return ProfileAmazon Daily Return ProfileNetflix Daily Return ProfileGoogle Daily Return Profile

From looking at the plots of the individual equities, it can be seen that while NFLX is prone to some intraday price swings, the remaining equities are also prone to some, at times, more volatile intraday moves than NFLX. I furthered my analysis by calculating the standard deviations of returns for each of the stocks. Over the sample, Facebook’s standard deviation was 0.024, Apple’s was 0.016, Amazon’s was 0.0199, Netflix’s was 0.035, and Google’s was 0.015. Based on assessing the standard deviations of the daily returns for each, NFLX, with a standard deviation of 3.5% is clearly the more volatile stock.

As can be seen from the plot below, on a rolling standard deviation basis, NFLX again has been the more volatile stock intraday on a historical basis, but in more recent time, the stock, though still the most volatile, others in the group have somewhat closed the gap.

FAANG Rolling Standard Deviations

Now I’m ready to begin assessing the group to determine what relationships exist between each of the underlying stocks. I begin by looking at the group as whole.

FAANG Stocks PairPlot

At this point, I’m now looking for unique relationships within the group. It is a natural expectancy that the group be correlated to a degree. However, what I was really interested in was uncovering where any unique relationships between any two of the group really stood out from the consensus notion of all ‘FAANG’ stocks being correlated. After looking through the pairplot, which is a good function to get an overview of a data set, AMZN was the first row to really standout. The stock was positively correlated with the group but its correlation to Facebook and Google were the two that really jumped out. Moving on to Facebook’s row, after Amazon, Google appeared to have the strongest relationship to the stock. Moving to Google, Amazon then Facebook, appeared to have the strongest correlations. Lastly, surveying Netflix, each of the remaining stocks appeared to be roughly equally correlated with Netflix.

At this point, I believe that I know which pairs I’d like to take a deeper look at, but to further my analysis a bit more, I created a heatmap of the correlations of the underlying stocks.

FAANG Correlation Heatmap

The heatmap was a bit surprising, which is a good thing. It’s always desirable to seek to back-up your assumptions quantitatively. Before creating the heatmap, it appeared that Amazon’s strongest correlation was with Facebook, with Google coming in second. The heatmap of the correlations actually revealed that it was the other way around. Amazon’s strongest correlation has been to Google, with Facebook coming in second. My initial belief about Facebook was that it was most strongly correlated to Amazon with Google coming in second. Assessing the heatmap, yet again the actual numbers were inverted as Facebook has been most closely correlated with Google, with Netflix coming in second. On to Google, my initial belief was that Amazon had the strongest correlation followed by Facebook. After reviewing this assumption via the heatmap, it too was inverted with the strongest relationship being between Facebook and Amazon coming in second. Lastly, regarding Netflix, it was believed that this stock’s closest relationship was on balance equal to the remaining stocks in the group. This was a fair assumption as Netflix’s correlation to Amazon, Facebook, and Google, came in at .94, .95, and.94 respectively. However,take a look at its correlation to Apple, which came in at .81, and is the second lowest correlation between any two stocks in the group.

Based on this analysis, I must update my original hypothesis, and seek to design a quantitative strategy based on what this exercise has told me comprehensively. At this point, we’re still not quite ready to begin designing a trading strategy and backtesting it as there is yet some additional analysis that must be conducted, but we do have a better understanding about the group that will serve as a foundation for our continued analysis and later our strategy backtest.

Read Quantitative Trading: FAANG Example Part II

Leave a comment

Filed under Strategy

The ‘Hello World’ of Quantitative Trading

I am still amazed by the fact that so few traders are more welcoming of the shift toward algorithmic trading. This shift began decades ago, but has become more prevalent in recent years. Traders, to remain competitive, must continue to develop as the industry changes. I can actually see both sides of the paradigm, the traders who have no interest in programming or automating their trading strategies, and those who are either traditional computer scientists who are learning the financial industry or those who have industry experience and are learning computer science.

Typically, the first group, the traders who have no interest in programming, are characterized as being consistently profitable within their existing strategy and execution methodology and thus have no interest in algorithmic trading. Some traders in this space, if choosing to automate their strategies, simply lean on a broker sponsored language that doesn’t require programming knowledge. While this could save the trader time in the short run, I feel that it will be devastating in the long run as the markets become more competitive.

Actually learning to program not only affords you a greater level of autonomy in the financial markets, but also strengthens your logic and reasoning ability, of which are key in designing profitable trading strategies, both discretionary and systematic.

It is possible that some traders feel that they don’t have the time to learn algorithmic trading or maybe that it is too complicated. Unknowingly, many traders utilize indicators whose foundation is based upon quantitative methods. Think of MACD, moving averages, stochastics, etc. Even viewing candlestick charts are quantitative because they are merely a time series.

When viewed from this perspective, algorithmic trading doesn’t appear that intimidating. Below I’ve included analysis of a simple quantitative trading strategy coded in Python. In the quantitative trading world, this is considered to be the ‘hello world’ of quantitative trading, or a simple representation of quantitative methodology.

The strategy is a simple moving average crossover strategy executed on the SPY. The backtest period was start=’2017-01-01′, end=’2017-05-31′. The strategy executed a total of 94 trades over this period, 64 longs, and 30 shorts. The market volatility was 0.00448, while the strategy volatility was 0.00438.

MA Cross on SPY in Python 1

This plot shows how and when the buy and sell signals were generated.

MA Cross on SPY in Python 2

This plot shows that the strategy generated a return a little less than 7% while the market return was a little over 7%. This can possibly be explained by looking at the difference in volatility of the strategy and the market. The strategy’s volatility was a little less than that of the market.


MA Cross on SPY in Python 3

This plot shows the that the strategy has a 98% correlation to the SPY, which makes sense given that the strategy was momentum based and executed only on the SPY.

The purpose of this post was not to present a profitable trading strategy, but merely to illustrate the simplicity of algorithmic trading when often the opposite is the case.

Leave a comment

Filed under Strategy

My First Algorithmic Trading System

I was introduced to algorithmic trading a few years ago while trading discretionary across multiple asset classes. I started by using a broker native language to develop a intraday momentum strategy. Realizing that being limited to this native language would force me to only employ my ideas with this particular broker led to me beginning a self study route toward learning to program. The first language I was introduced to was C++. I later learned R, and Python. Also over the course of developing as a programmer, I picked up HTML, CSS, Javascript, NodeJS, MongoDB, and a lot of other libraries. While these additional languages weren’t directly related to trading, it helped to further develop my coding skills, of which would directly affect my trading.

I later began to look at the markets more quantitatively as a result of being introduced to data analysis, manipulation, and visualization capabilities via programming. Below is a slideshow of an excerpt of an initial backtest conducted while programming my first algorithmic idea.



This slideshow requires JavaScript.

Leave a comment

Filed under Strategy