## Performance Metrics, Risk Metrics, and Strategy Optimization – An Overview

The performance of a trading strategy is measured with a set of parameters. For example, if you are trading in equity then your returns are compared against the benchmark index. The consistency of returns of the strategy also proves to be a significant factor. (more…)

‘Looks can be deceiving,’ a wise person once said. The phrase holds true for Algorithmic Trading Strategies. The term Algorithmic trading strategies might sound very fancy or too complicated. However, the concept is very simple to understand, once the basics are clear. In this article, I will be telling you about algorithmic trading strategies with some interesting examples.

If you look at it from the outside, an algorithm is just a set of instructions or rules. These set of rules are then used on a stock exchange to automate the execution of orders without human intervention. This concept is called Algorithmic Trading.

Let me start with a very simple trading strategy. Those who are already into trading would know about S.M.A and for those who don’t; S.M.A is Simple Moving Average. S.M.A can be calculated using any predefined and fixed number of days. An algorithmic trading strategy based on S.M.A can be simplified in these four simple steps:

1. Calculate 5 day SMA
2. Calculate 20 day SMA
3. Take a long position when the 5 day SMA is larger than or equal to 20 day SMA
4. Take a short position when the 5 day SMA is smaller than 20 day SMA

We refer to this algorithmic trading strategy as Moving Average Crossover Strategy. This was just a simple example. Now don’t get down to thinking that it is all going to be a bed of roses. Even if it were, then be prepared for the thorns.In everyday trading, far more complex trading algorithms are used to generate algorithmic trading strategies.

All the algorithmic trading strategies that are being used today can be classified broadly into the following categories:

• Momentum/Trend Following
• Arbitrage
• Statistical Arbitrage
• Market Making

Let me go into some detail.

#### Momentum based Strategies

Assuming that there is a particular trend in the market. As an algo trader, you are following that trend. Further to our assumption, the markets fall within the week. Now, you can use stats to determine if this trend is going to continue. Or if it will change in the coming weeks. Accordingly, you will make your next move. You have based your algorithmic trading strategy on the market trends which you determined by using statistics.

This method of following trends is called Momentum Based Strategy.

There are numerous ways to implement this algorithmic trading strategy and I have discussed this in detail in one of our previous articles called “Methodology of Quantifying News for Automated Trading

#### Arbitrage

If we assume that a pharma-corp is to be bought by another company, then the stock price of our corp could go up. This is triggered by the acquisition which is a corporate event. If you are planning to invest based on the pricing inefficiencies that may happen during a corporate event (before or after), then you are using an event-driven strategy. Bankruptcy, acquisition, merger, spin-offs etc could be the event that drives such kind of an investment strategy.

These strategies can be market neutral and used by hedge fund and proprietary traders widely.

#### Statistical Arbitrage

When an arbitrage opportunity arises because of misquote in prices, it can be very advantageous to algo trading strategy. Although such opportunities exist for a very short duration as the prices in the market get adjusted quickly. And that’s why this is the best use of algorithmic trading strategies, as an automated machine can track such changes instantly.

For instance, if Apple’s price falls under $1 then Microsoft will fall by$0.5 but Microsoft has not fallen, so you will go and sell Microsoft to make profit. You can read about the common misconceptions people have about Statistical Arbitrage here.

#### Market Making

To understand Market making, let me first talk about Market Makers.

According to Wikipedia:

A market maker or liquidity provider is a company, or an individual, that quotes both a buy and a sell price in a financial instrument or commodity held in inventory, hoping to make a profit on the bid-offer spread, or turn.

Market making provides liquidity to securities which are not frequently traded on the stock exchange. Market maker can enhance the demand-supply equation of securities. Let me give you an example:

Let’s assume you have Martin, a market maker, who buys for Rs. 500 from the market and sell it at 505. He will give you a bid-ask quote of Rs. 505-500. The profit of Rs. 5 cannot be sold or exchanged for cash without substantial loss in value. When Martin takes a higher risk then the profit is also higher.

I found Michael Lewis’ book ‘Flash Boys’ in Indian Bull Market pretty interesting and it talks about liquidity, market making and HFT in great detail. Check it out after you finish reading this article.

Since you will need to be analytical & quantitative while getting into or upgrading to algorithmic trading it is imperative to learn programming (some if not all) and build foolproof systems and execute right algorithmic trading strategy. Reading this article on Automated Trading with Interactive Brokers using Python will be very beneficial for you. You can read for the article here.

Now that I have introduced you to algorithmic trading strategies, I will be throwing some light on the strategy paradigms and modeling ideas pertaining to each strategy.

• Market Making
• Statistical Arbtirage
• Momentum
• Machine Learning Based

#### Market Making

As I had mentioned earlier, the primary objective of Market making is to infuse liquidity in securities that are not traded on stock exchanges. In order to measure the liquidity, we take the bid-ask spread and trading volumes into consideration.

The trading algorithms tend to profit from the bid-ask spread. I will be referring to our buddy, Martin, again in this section. Martin being a market maker is a liquidity provider who can quote on both buy and sell side in a financial instrument hoping to profit from the bid-offer spread. Martin will accept the risk of holding the securities for which he has quoted the price for and once the order is received, he will often immediately sell from his own inventory. He might seek an offsetting offer in seconds and vice versa.

When it comes to illiquid securities, the spreads are usually higher and so are the profits. Martin will take a higher risk in this case. Several segments in the market lack investor interest due to lack of liquidity as they are unable to gain exit from several small- and mid-cap stocks at any given point in time.

Market Makers like Martin are helpful as they are always ready to buy and sell at the price quoted by them. In fact, much of high frequency trading (HFT) is passive market making. The strategies are present on both sides of the market (often simultaneously) competing with each other to provide liquidity to those who need.

So, when is this strategy most profitable?

This strategy is profitable as long as the model accurately predicts the future price variations.

Modeling ideas based on this Paradigm

The bid-ask spread and trade volume can be modeled together to get the liquidity cost curve which is the fee paid by the liquidity taker. If the liquidity taker only executes orders at the best bid and ask, the fee will be equal to the bid ask spread times the volume. When the traders go beyond best bid and ask taking more volume, the fee becomes a function of the volume as well.

Trade volume is difficult to model as it depends on the liquidity takers execution strategy. The objective should be to find a model for trade volumes that is consistent with price dynamics. Market making models are usually based on one of the two:

1. The first focuses on inventory risk. The model is based on preferred inventory position and prices based on the risk appetite.
2. The second is based on adverse selection which distinguishes between informed and noise trades. Noise trades do not possess any view on the market whereas informed trades do. When the view of the liquidity taker is short term, its aim is to make short term profit utilizing the statistical edge. In case of long term view, the objective is to minimize the transaction cost. The long-term strategies and liquidity constraints can be modeled as noise around the short-term execution strategies.

To know more about Market Makers, you can check out this interesting article on QuantInsti’s blog.

#### Statistical Arbitrage

If Market making is strategy that makes use of the bid-ask spread, Statistical Arbitrage seeks to profit from statistical mispricing of one or more assets based on the expected value of these assets.

A more academic way to explain statistical arbitrage is to spread the risk among thousand to million trades in a very short holding time to, expecting to gain profit from law of large numbers. Statistical Arbitrage Algorithms are based on mean reversion hypothesis, mostly as a pair.

Modelling ideas

Pairs trading is one of the several strategies collectively referred to as Statistical Arbitrage Strategies. In pairs trade strategy, stocks that exhibit historical co-movement in prices are paired using fundamental or market-based similarities. The strategy builds upon the notion that the relative prices in a market are in equilibrium, and that deviations from this equilibrium eventually will be corrected.

When one stock outperforms the other, the outperformer is sold short and the other stock is bought long with the expectation that the short term diversion will end in convergence. This often hedges market risk from adverse market movements i.e. makes the strategy beta neutral. However, the total market risk of a position depends on the amount of capital invested in each stock and the sensitivity of stocks to such risk.

#### Momentum

Momentum Strategies seek to profit from the continuance of existing trend by taking advantage of market swings.

“In simple words, buy high and sell higher and vice versa.”

And how do we achieve this?

In this particular algo-trading strategy we will take short-term positions in stocks that are going up or down until they show signs of reversal. It is counter-intuitive to almost all other well-known strategies. Value investing is generally based on long-term reversion to mean whereas momentum investing is based on the gap in time before mean reversion occurs.

Momentum is chasing performance, but in a systematic way taking advantage of other performance chasers who are making emotional decisions. There are usually two explanations given for any strategy that has been proven to work historically, either the strategy is compensated for the extra risk that it takes or there are behavioral factors due to which premium exists.

There is a long list of behavioral biases and emotional mistakes that investors exhibit due to which momentum works. However, this is easier said than done as trends don’t last forever and can exhibit swift reversals when they peak and come to an end. Momentum trading carries a higher degree of volatility than most other strategies and tries to capitalize on the market volatility. It is important to time the buys and sells correctly to avoid losses by using proper risk management techniques and stop losses. Momentum investing requires proper monitoring and appropriate diversification to safeguard against such severe crashes.

Modelling ideas

Firstly, you should know how to detect Price momentum or the trends. As you are already into trading, you know that trends can be detected by following stocks and ETFs that have been continuously going up for days, weeks or even several months in a row. For instance identify the stocks trading within 10% of their 52 week high or look at the percentage price change over the last 12 or 24 weeks. Similarly to spot a shorter trend, include a shorter term price change.

If you remember, back in 2008, the oil and energy sector was continuously ranked as one of the top sectors even while it was collapsing. We can also look at earnings to understand the movements in stock prices. Strategies based on either past returns (“price momentum strategies”) or on earnings surprise (known as “earnings momentum strategies”) exploit market under-reaction to different pieces of information. An earnings momentum strategy may profit from the under-reaction to information related to short-term earnings. Similarly a price momentum strategy may profit from market’s slow response to a broader set of information including longer-term profitability.

#### Machine Learning based

In Machine Learning based trading, algorithms are used to predict the range for very short term price movements at a certain confidence interval. The advantage of using Artificial Intelligence (AI) is that humans develop the initial software and the AI itself develops the model and improves it over time. A large number of funds rely on computer models built by data scientists and quants but they’re usually static, i.e. they don’t change with market. ML based models on the other hand can analyze large amounts of data at high speed and improve themselves through such analysis.

Modeling idea

A form of machine leaning called “Bayesian networks” can be used to predict market trends while utilizing a couple of machines. An AI which includes techniques such as evolutionary computation (which is inspired by genetics) and deep learning might run across hundreds or even thousands of machines. It can create a large and random collection of digital stock traders and test their performance on historical data. It then picks the best performers and uses their style/patterns to create a new of evolved traders. This process repeats multiple times and a digital trader that can fully operate on it’s own is created.

This process repeats multiple times and a digital trader that can fully operate on it’s own is created.

This process repeats multiple times and a digital trader that can fully operate on it’s own is created.

These were some important strategy paradigms and modelling ideas.  Next we will go through the step by step procedure to build a trading strategy.

You can learn these Paradigms in great detail in QuantInsti’s Executive Programme in Algorithmic Trading (EPAT), one of the most extensive algorithmic trading courses available online with lecture recordings and lifetime access and support.

### Building an algorithmic trading strategy

From algo trading strategies to paradigms and modeling ideas, I come to that section of the article where I will tell you how to build a basic algorithmic trading strategy.

That is the first question that must have come to your mind, I presume. The point is that you have already started by knowing about the basics and paradigms of algorithmic trading strategies while reading this article. Now, that our bandwagon has it’s engine turned on, it is time to press on the accelerator.

#### And how exactly is this done?

I will explain how an algorithmic trading strategy is built, step by step. The concise description will give you an idea about the entire process.

1. Decide upon the genre/strategy paradigm

The first step is to decide the strategy paradigm. It can be Market Making, Arbitrage based, Alpha generating, Hedging or Execution based strategy. For this particular instance, I will choose pair trading which is a statistical arbitrage strategy that is market neutral (Beta neutral) and generates alpha, i.e.makes money irrespective of market movement.

1. Establish Statistical significance

You can decide on the actual securities you want to trade based on market view or through visual correlation (in case of pair trading strategy). Establish if the strategy is statistically significant for the selected securities. For instance in case of pair trading, check for co-integration of the selected pairs.

Now, code the logic based on which you want to generate buy/sell signals in your strategy. For pair trading check for “mean reversion”; calculate the z-score for the spread of the pair and generate buy/sell signals when you expect it to revert to mean. Decide on the “Stop Loss” and “Profit Taking” conditions.

• Stop Loss– A stop-loss order limits an investor’s loss on a position in a security. It fires an order to square off the existing long or short position to avoid further losses and helps to take emotion out of trading decisions.
• Take Profit– take-profit orders are used to automatically close out existing positions in order to lock in profits when there is a move in a favorable direction.
1. Quoting or Hitting strategy

It is very important to decide if the strategy will be “quoting” or “hitting”. Execution strategy to a great extent decides how aggressive or passive your strategy is going to be.

• Quoting– In pair trading you quote for one security and depending on if that position gets filled or not you send out the order for the other. In this case, the probability of getting a fill is lesser but you save bid-ask on one side.
• Hitting- In this case, you send out simultaneous market orders for both securities. The probability of getting a fill is higher but at the same time slippage is more and you pay bid-ask on both sides.

The choice between probability of fill and Optimized execution in terms of slippage and timed executive is what this is, if I have to put it that way. If you choose to quote, then you need to decide what are quoting for, this is how pair trading works. If you decide to quote for the less liquid security, slippage will be less but the trading volumes will come down liquid securities on the other hand increase the risk of slippage but trading volumes will be high.

Using stats to check causality is another way of arrving at a decision, i.e. change in which security causes change in the other and which one leads. The causality test will determine the “lead-lag” pair; quote for the leading and cover the lagging security.

1. Backtesting & Optimization

How do you decide if the strategy you chose was good or bad?

How do you judge your hypothesis?

This is where back-testing the strategy comes as an essential tool for estimation of performance of the designed hypothesis based on historical data. A strategy can be considered to be good if the backtest results and performance statistics back the hypothesis.

Hence, it is important to choose historical data with sufficient number of data points. This is to create sufficient number of sample trades (at least 100+ trades) covering various market scenarios (bullish, bearish etc.). Ensure that you make provision for brokerage and slippage costs as well. This will get you more realistic results but you might still have to make some approximations while backtesting. For instance, while backtesting quoting strategies it is difficult to figure out when you get a fill. So, the common practice is to assume that the positions get filled with the last traded price.

###### What kind of tools should you go for, while backtesting?

Since backtesting for algorithmic trading strategies involves huge amount of data, especially if you are going to use tick by tick data. So, you should go for tools which can handle such mammoth load of data.

###### R or MATLAB?

R is excellent for dealing with huge amounts of data and has a high computation power as well. Thus, making it one of the better tools for backtesting. Also, R is open source and free of cost. We can use MATLAB as well but it comes with a licensing cost.

1. Risk and Performance Evaluation

“With great strategy, comes greater scrutiny,” A Great Thinker once quoted.

Fine, I just ripped off Ben Parker’s famous quotation from the Spiderman movie (not the Amazing one). But trust me, it is 100% true. No matter how confident you seem with your strategy or how successful it might turned out previously, you must go down and evaluate each and everything in detail.  There are several parameters that you would need to monitor when analyzing a strategy’s performance and risk. Some important metrics/ratios are mentioned below:

• Total Returns (CAGR)- Compound Annual Growth Rate (CAGR). It is the mean annual growth rate of an investment over a specified period of time longer than one year.
• Hit Ratio- Order to trade ratio.
• Average Profit per Trade- Total profit divided by the total number of trades
• Average Loss per trade- Total loss divided by the total number of trades
• Maximum Drawdown– Maximum loss in any trade
• Volatility of Returns- Standard deviation of the “returns”
• Sharpe Ratio- Risk adjusted returns, i.e. excess returns (over risk free rate) per unit volatility or total risk.

## Vectorised Backtesting in Excel

By Jacques Joubert

Now for those of you who know me as a blogger might find this post a little unorthodox to my traditional style of writing, however in the spirit of evolution, inspired by a friend of mine Stuart Reid (TuringFinance.com), I will be following some of the tips suggested in the following blog post.

Being a student in the EPAT program I was excited to learn the methodology that others make use of when it comes to backtesting. As usual, we start off in Excel and then migrate to R.

Having previously written a blog series on backtesting on Excel and then moving to R, I was very interested to see a slightly different method used by the QuantInsti team. (more…)

## Pair Trading Strategy and Backtesting using Quantstrat

This article is the final project submitted by the author as a part of his coursework in Executive Programme in Algorithmic Trading (EPAT) at QuantInsti. Do check our Projects page and have a look at what our students are building.

### Author

Marco has spent his career as a trader and portfolio manager, with a particular focus in equity and derivatives markets. He specializes in quantitative finance and algorithmic trading and currently serves as head of the Quantitative Trading Desk and Vice-president of Argentina Valores S.A. Marco is also Co-Founder and CEO of Quanticko Trading SA, a firm devoted to the development of high frequency trading strategies and trading software. Marco holds a BS in Economics and an MSc in Finance from the University of San Andrés.

### Introduction

One of my favorite classes during EPAT was the one on statistical arbitrage, so the pair trading strategy seemed a nice idea for me. My strategy triggers new orders when the pair ratio of the prices of the stocks diverge from the mean. But in order to work, we first have to test for the pair to be cointegrated. If the pair ratio is cointegrated, the ratio is mean-reverting and the greater the dispersion from its mean, the higher the probability of a reversal, which makes the trade more attractive. I chose the following pair of stocks:

• Bank of America (BAC)
• Citigroup (C)

The idea is the following: If we find two stocks that are correlated (they correspond to the same sector), and the pair ratio diverges from a certain threshold, we short the stock that is expensive and buy the one that is cheap. Once they converge to the mean, we close the positions and profit from the reversal.

## Application of Sentiment Analysis in Trading: Where it works?

Sentiment Analysis. also known as opinion mining, is the process of computationally identifying and categorizing opinions expressed in a piece of text, especially in order to determine whether the writer’s attitude towards a particular topic, product, etc. is positive, negative, or neutral. The analysis finds significant prominence in social media, stock markets, law, policy making, sociology and even customer service.

Machines are faster at responding to events than humans and can process much vaster amount of information without any fatigue. In more volatile markets, people tend to react less strongly to positive news and react more strongly to negative news. Of course, there are situations where sentiment analysis, or sometimes also called News Analytics, works well for a position, while in other cases it fails. The following slide show is a summary of favourable and profitable outcomes of sentiment analysis in trading.

#### Next Steps:

Now that you have understood a little about how sentiment analysis works, you would also want to know the pitfalls. For this and other such questions, we have an upcoming webinar on “Quantitative Strategies using Sentiment Analysis” by Rajib Ranjan Borah. The webinar will give a brief overview of how sentiment analysis works and some historical perspective along with suggestions on how to develop trading strategies. Register for the webinar on this link.

## Automated Trading on Oanda platform by Dr. Yves Hilpisch

Automated trading on oanda platform by Dr. Yves Hilpisch

Python has emerged as one of the most popular language to code in Algorithmic Trading, owing to its ease of installation, free usage, easy structure, and availability of variety of modules. Globally, Algo Traders and researchers in Quant are extensively using Python for prototyping, backtesting, building their proprietary risk and order management system as well as in optimisation of testing modules.

This blog post highlights some of the key steps involved in Algorithmic Trading using Python as the programming language. The screenshots are taken from the webinar of Dr. Yves Hilpisch, in collaboration with QuantInsti. Dr. Hilpisch is world-renowned authority in the world of Python and is the founder of Python Quants GmbH, with several books on the subject under his belt. He also serves as a faculty at QuantInsti, one of Asia’s pioneer education training firm in Algorithmic Trading.

All examples shown are based on the platform and API of http://oanda.com. Background information about Python and the libraries used can be found in the O’Reilly book Hilpisch, Yves (2014): “Python for Finance – Analyze Big Financial Data”. The post is divided in two parts. The current post highlights the basics of connecting with the Oanda platform using python and backtesting the trading strategies. The next post will cover working with streaming data as well as automated trading in real-time.

## 4 High Frequency Trading (HFT) Order Types You Need To Know

High Frequency Trading (HFT) involves the concept of “Order Types”, which signals a trader to enter or exit a position. According to Reuters, Order types can reach an estimated 2,000 variations as a fully electronic market and more than 50 trading venues have multiplied the possibilities of how, when and with whom to trade.

Clearly, the rules of the game are redefined by the Order Types and their correct implementation in the marketplace. Amidst a long list, we touch upon some of the popular ones, frequently used by HFT practitioners. The following Infographic shows 4 HFT Order Types, and examples when a buy or sell order is placed.

## Introduction

Traders in the derivative market often exercise one of the following: Call option or Put Option.

“Call option” is a financial contract between a buyer and seller, whereby the buyer has the right, but not the obligation, to buy an agreed quantity of a financial instrument from the seller of the option at a certain time for a certain price (the strike price). The “Pull Option” serves the opposite.

In a “Covered Call”, the seller of the call options owns the corresponding amount of the underlying instrument.

A Covered Call is an income generating option strategy which involves two legs:

• Selling an Out of the money (OTM) call option

If the call is sold simultaneously along with the stock purchase, the strategy is referred to as a “buy-write” strategy.

In a Covered Call, the trader holds a neutral to a bullish outlook. Covered Call is a net debit transaction because you pay for the stock and receive a small premium for the call option sold.

The idea of this blog post is to elaborate on the covered call strategy by an example, and to plot its payoff using Python. The post also highlights “Calendar Call” as it is a modification of the Covered Call strategy.

## Sharpe Ratio and Its Applications in Algorithmic Trading

To measure the performance of a trading strategy, annualized returns are often a common metric. However comparing two strategies based on annualised returns may not always be a logical way due to several reasons. Some strategies might be directional, some market neutral and some might be leveraged which makes annualized return alone a futile measure of performance measurement. Also, even if two strategies have comparable annual returns, the risk is still an important aspect that needs to be measured. A strategy with high annual returns is not necessarily very attractive if it has a high-risk component; we generally prefer better risk-adjusted returns over just ‘better returns’. Sharpe Ratio takes care of risk assessment and the problem related to the comparison of strategies.

Sharpe ratio is a measure for calculating the risk-adjusted return. It is the ratio of the excess expected return of an investment (over risk-free rate) per unit of volatility or standard deviation. (more…)

## Sentiment Analysis in Trading Using R [WORKING MODEL]

In this post we discuss sentiment analysis in brief and then present a basic sentiment analysis model in R. Sentiment analysis is the analysis of the feelings (i.e. attitudes, emotions and opinions) which are expressed in the news reports/blog posts/twitter messages etc., using natural language processing tools.

Natural language processing (NLP) in simple terms refers to the use of computers to process sentences/text in a natural language such as English. The objective here is to extract information from unstructured or semi-structured data found in these tweets/blogs/articles. To enable this NLP makes use of artificial intelligence, computational linguistics, and computer science.

Using NLP models hundreds of text documents can be processed to ascertain the sentiment in seconds. These days sentiment analysis is a hot topic, and has found wide application in areas like Business intelligence, Politics, Finance, Policy making etc.

Sentiment analysis in Trading – Sentiments can often drive the direction of the markets. Hence, traders and other participants in the financial markets seek to gauge the sentiment expressed in news reports/tweets/blog posts. Traders build automatic trading systems which extract the sentiment from natural language. These trading systems take long/short positions in the markets based on the trading signals generated. The trading systems can also be combined with other trading systems. The objective at the end of the day is to generate superior returns from the extracted information.

There are various methods and models for sentimental analysis. Let us take a look at a very basic model in R for sentimental analysis.

### Sentiment analysis model in R

In this model we implement the “Bag-of-words” approach to sentiment analysis. The process identifies positive and negative words (or a string of words) within an article. For this it makes use of a large dictionary which contains words that carry sentiment.  Each word in this dictionary can be assigned a weight. The sum of the positive and negative words is the final sentiment score generated by the model.

We will test our model on the management commentary text taken from the latest earnings call transcript of Eicher Motors Ltd. Eicher Motors is a leading Indian automaker company which owns the Royal Enfield Motors. The objective of our model will be to gauge the opinion expressed in their fourth quarter 2015 earnings call.

To build this model we are using the “tm” and the “Rweka” package in R. We load the libraries and then read the two documents which contain the positive and the negative terms. To prepare these documents we have gone through four previous conference call transcripts prior to the fourth quarter 2015. We picked the positive/negative words from these transcripts to populate our dictionary. In addition to these words we have also added some general positive/negative words that relate to the Motorcycle industry.

We will be considering only the management’s commentary in our sentiment analysis model. We load the text document (fourth quarter 2015) containing the CEO’s prepared text commentary in R using the Corpus function. For this we have stored the commentary document in the TextMining folder in the R’s working directory.

Next step is to clean the text. We convert all words to lowercase, remove punctuations, remove numbers, and strip the whitespace. The writeLines function enables us to see the text post the cleansing.

In the code below, we tokenize the text which was cleaned above. Tokenization is the process of breaking a stream of text into words or a string of words. We are using the NGramTokenizer function here. This creates N-grams of text.

N-grams are basically a set of co-occuring words within a given text. For example, consider this sentence “The food is delicious”. If n= 2, then the ngrams would be:

• the food
• food is
• is delicious

Thereafter we create a term document matrix (called “terms” in the code) which a matrix that lists all occurrences of words in the corpus.

Below we check if the positive/negative words in the dictionary are present in the text document.

Now we extract all the positive/negative words from the text document which matched with the words in our dictionary.

The code lines below compute the positive/negative score, and finally the sentiment score.

Final result – Sentiment score

The model found 14 positive words and 4 negative words, and the final sentiment score was 10. This tells us that the quarterly result for Q4 2015 was good from the management’s perspective. The word cloud below shows some of the positive/negative words that were picked from the text document on which we ran the model.

Validate our sentiment analysis model – let us check the quarterly performance numbers to confirm the positive sentiment score generated by our model. As can be seen, Eicher Motors posted a strong quarter. EBIT growth was around 72% y/y on a strong sales volume of 125,690 motorcycles. The strong results were despite the production shutdown for few days which was caused by the floods experienced during the quarter at its production facility.

The chart on the right shows the stock market’s reaction to Eicher Motors strong results on the day of earnings result announcement. The stock opened at around 17100 levels, made a big move touching an intraday high of around Rs.18500, and finally closed at Rs. 18,175.

### Conclusion

This was a basic introduction to sentiment analysis. The model above can be made more robust and fine-tuned further. In future posts we will try to cover other sentiment analysis approaches and attempt to build a model around them.

QuantInsti has been actively participating in conferences on sentiment analysis, and was one of the lead marketing and education partner at the recently held “Sentiment analysis in Finance” conference in Singapore, 2016. Rajib Ranjan Borah, Co-founder & Director of iRageCapital Advisory Pvt. Ltd, & QuantInsti was one of the esteemed panelists for the session, “New Paradigms for Sentiment Analysis Applied to Finance” at the conference.

To know more about QuantInsti and the Executive Programme in Algorithmic Trading (EPAT) course offered by QuantInsti, check our website and the EPAT course page. Feel free to contact our team at contact@quantinsti.com for queries on EPAT.