### Introduction to Automated Algorithmic Trading

Lets breakdown the topic backwards, “Automated Algorithmic trading”

Trading is buying and selling of stocks.

Algorithmic trading is buy and selling of stocks based on certain pre-set conditions.

For example, one would want to buy a stock when its previous day’s closing price is higher that the opening price for that day. So simply, a ratio of close/open > 1 would quantify the qualitative idea.

Automated algorithmic trading is buy and selling of stocks based on certain preset conditions automatically using compute codes.

As an example above, an automated algorithmic trading software will trade when the ratio of close/open is greater than 1. And as soon as that happens, the buy order is executed.

The concept sounds simple, isnt it.

The implementation is the tricky part.

### Introduction to Investment/Trading Strategy

A common **myth** before we proceed.

*"Investment and trading are the same."***They are not.**

One of the key difference is the

**time horizon.**

Investment is usually for a

**long-term holding period.**While trading can be monthly, weekly, daily or intraday.

You won't hear people saying they are an

*intraday investor*, or

*a long-term trader*often.

An investment/trading strategy is a set of

**decision points**which are used to achieve a particular target of desired returns from the stock market. Now, a strategy that buys and sells at the

**correct points**fulfilling the desired target returns evaluates to a good investment/trading strategy.

**A question comes**, how to develop one. There are multiple methods of developing an investment/trading strategy.

In general, people follow the news, use technical analysis, fundamental analysis, and several other functions, different people have their own ways to do an analysis.

An extensive classification is available in lesson 7.

The next lesson throws light on how to develop one.

### Developing investment strategies for Algorithmic Trading

**Should it be a random formula?**Do you wish to invest your money into an unknown randomly generated formula?

No. A strategy is developed from an

**idea**. Any idea from any domain of life you like.

A 1-hour long boring meeting with the boss can be taken as a motivation for an investment idea.

What happens when you get out of that boring meeting?

You feel happy, right?

An analogy to this in terms of investment ideas can be: if the markets are cold for a specified period, neither moving up or down, but just hopping within low range, can you expect a "breakout" , i.e.

**can you expect that the "boring" period will end with a positive or a negative sentiment?**

That's a small invest idea.

An example of a line of thought to qunatify a qualitative idea is as follows:

A cold period in the markets will mean that the prices are not moving much. There is a very low range, meaning the recent standard deviation i.e. the amount of movement in the markets is lesser than the volatility in the longer range.

Our simulator has a predefined function called std(price, time period

**)**. It takes the price of the stock and the time period to be considered to calculate the standard deviation.

**gives the value of the standard deviation of open prices of the stock of last 10 minutes.**

*std(open,10)***gives the value of the standard deviation of open prices of the stock of last 30 minutes.**

*std(open,30)*Since the idea of the strategy is that if the recent movement in the market is lesser than the movement in its longer time period, a breakout is expected and you buy at that point.

**std(close, 10) < std(close, 30)**

Voila!

The left-hand side of the equation is the movement in recent times, the right-hand side of the equation is the movement in a longer time frame, then this formula causes our systems to buy when the recent 10 minutes volatility is lower than long-term 30 minutes volatility.

More about stock market simulators in the next lesson

### Stock Market Simulator

*"The stock market has seen all possible kinds of news and events that are possible. Any price movement that is about to happen in the future has already been seen in the history. "*

You have developed your idea in the previous lesson.

Its verification can be done through a

**stock market simulator**.

Now, what exactly is that?

It is a computer program that emulates the behavior of the stock markets in the artificial environment.

It takes in your strategy and thinks as it what would have been the results if you had applied that strategy live in a previous time frame. How would have it performed during the global crisis or during elections or a natural calamity and many more events which affect the movement of the stock market?

**Why is it necessary?**

It lets you know the worth of your investment strategy. It allows you know whether this type of approach would have worked ever in the historical markets or not.

One shouldn't live trade a strategy that has no history of giving good returns.

A question is often asked, are the results of the simulator a proof that the strategy will run in the future?

No, an excellent historical result does not guarantee similar profits in the future. But it does guide you to segregate good and bad investment ideas

Give it a thought.

The simulator is for strategy testing.

Monte Carlo Simulations are for checking the robustness of the same.

### Monte Carlo Simulations

It is a tool to stress test your investment strategy in different

**"universes"**. Seems weird. Don't worry it will all make sense in some time.

The concept of

**"different universe"**is an exciting read and is driven by philosophy.

*It states that different versions of you exist simultaneously in a parallel domain.*

For example, in one universe, you are a software engineer, in other, you are a Broker.

A Monte Carlo simulation is used after backtesting a strategy having good result metrics.

It is a tool to test the robustness of the strategy in different environments

**Hundreds and Thousands**of new mathematically generated samples replace the actual stock data to a form in which it might exist in a different universe.

This is the data

**no one has seen till now**and you can simulate on a thousand samples of it.

By our knowledge, it is one of the

**most sophisticated tools**for risk management in today's time. Once a strategy has been backtested on multiple stocks, and Monte Carlo simulations have given good results, the next important step is to create an optimized portfolio, i.e. develop a collection of stocks such that the money is distributed to each stock in the most optimized manner.

### Portfolio Optimization

**"One size doesn't fit all.**" One can simulate all the stocks listed on NSE at a single go but all will not perform well.

Some stocks will respond nicely to the strategy and rest won't.

We need to filter out the non-performing stocks.

A quick question: Do all the well-performing stocks have the

**same returns?**No.

Some perform very well, some perform "only" well.

We would need to

**optimize**the money allocation to different stocks so that out of the well-performing stocks,

**the best-performing stocks**get the highest allocation and the lower well-performing stocks a bit less.

In very broad terms, this is what is called

**portfolio optimization**.

### Classifications of different domains in trading

This module is generally related to different types and aspects of investment strategy, trading, risk management and any other aspect that we thought might be helpful.

**Classification of strategies based on holding period:**

- Long-term investments
- Medium-term investments
- Intraday strategies
- High-Frequency Trading

**Classification of stocks:**

- Growth stock
- Value stocks

**Classification of methods of stock analysis:**

- Fundamental analysis
- Technical analysis

**Basic quant strategies:**

- Technical indicators
- Price-Volume based strategies

**Advanced quant strategies:**

- Sentiment analysis
- Support Vector Machines
- Neural Networks
- Stochastic processes
- Analysis of Variance
- Multivariate regression
- Logistic regression
- Dynamic live strategy and stock selection out of a bucket of stocks and strategies

**Stochastic processes**

- Random walk
- UB process
- Markov Model

**Portfolio allocations**

- Markovitz model
- Sharpe model
- Dynamic Allocation
- Static logic based allocation with rebalancing

**Components of Risk Management:**

- Stop loss
- Limit, order
- Bracket order
- Probabilistic approach
- Classification algorithms
- Monte Carlo simulations
- Sensitivity analysis
- Statistical process control

**Types of stock selection methods:**

- For long-term investing: Fundamental Ratios
- For medium term: Sentiment
- For day trading: Time Series
- For intraday: Technical Analysis

### Classification of strategies based on holding period

*Let us begin with the first classification, the holding period of a trading strategy.*Holding period is the amount of time you are in the trade, i.e. the time gap between a buy and sell order of a stock. Simple isn’t it.

Levels of holding period :

**Long-term investments**There are the investments, focused on future high growth stocks. These strategies are developed by a thorough analysis of a company’s profile, team, domain, mentors etc. More focus is on the current stability and future growth prospects of the company. Fundamental analysis and ratios play an important part in this. It has some risk that if you can identify the correct stocks with a diversified portfolio, it is good. You can look up some key fundamentals of the company like P/E ratio, Revenue growth, and profitability metrics for the company.

**Medium-term investments**We would classify it as just trading. Buy a stock, hold for some days and then exit. Strategies based on such holding periods are usually developed by a combination of fundamental ratios, price movement, and qualitative market sentiment. It's a medium risk method, as market sentiment can change fast based on news and yet again, all depends on how well your approach your analysis. You can look up the current market news related to different sectors to identify the stocks.

**Intraday trading**It’s called intraday trading when the buy and sell orders for a particular stock are executed the same day. Strategies based on such holding periods are usually developed by using technical analysis, charting and predictive analytical models like time series analysis, neural nets, sentiment analysis etc. These are profitable when the stock has a lot of movement, either upward or downward and corresponding correct positions can be identified. It's a high-risk high-return proposition.

**High-Frequency Trading**. It requires extremely good hardware and software support to identify the profit opportunities, perform calculations and generate profits in an extremely short time.

### Classification of stocks

**Growth Stocks**

Stocks that have very strong fundamentals and are expected to outperform the market in the future. Such stocks are usually of the well-established companies that the proved their mettle. Investors investment in these with regard to gains through dividends and with regard to stock prices.

Often the prices of these stocks are high and they still continue to command good positions in the future also.

Usually, long-term holding of such stocks lead to more gains rather than playing them at a medium or intraday level.

**Value stocks**

These are the companies whose fundamentals are good but the same is not reflected in the stock prices. Their prices are lesser than what it should have been.

Indirectly meaning that such stocks might have a high potential for growth and now might be a good time to get in. To search for such stocks needs a sectoral approach, wherein an individual identifies and benchmarks of key financial ratios for that sector and then does a stock by stock filtering to identify under-valued stocks

### Classification by Methods of Stock Analysis

**Fundamental Analysis**

Fundamental Analysis refers to investing or trading based on the core stability of a company with is reflected in its financials like revenue, income, net profit, capital expenditure etc. The core genesis is to identify a company that is stable and has high prospects of consistent growth in the future. It can be used to identify both value and growth stocks.

A list of popular company financials is as follows:

- Revenue
- Retained Earnings
- Total Current Liabilities
- Non-Current Liabilites
- Short-Term Debt
- Long-Term Debt
- Total Non-Current Assets
- Inventories
- Cash And Equivalents
- Goodwill
- Amounts Receivable
- Amounts Payable
- Operating Expenses
- Profit before Taxes
- Cost of Goods Sold
- Net Revenue
- Net Income
- Capital Expenditure

**Technical Analysis**

Technical Analysis is trading activity based on study of recent movement of stocks, i.e. whether the stock is moving up or down. The price-volume data like Opening price, closing price, the high or the low of a stock for a particular time frame ranging from a minute to a day are considered before making any trade.

It usually falls in the short term trading activities which can be intraday or a weekly holding activities. Mostly the key decision to buy or sell is made based on what is called "Technical Indicator". These are fixed formulas which have a particular value. Basing on that value leads to a decision. It is very important to note the logic behind a particular technical indicator.

Without have the key knowledge of the logic behind in indicators might lead to lack of innovation which holds the key in the stock markets. A list of popular technical indicators and their definition is given in the next section

### Basic Quant Strategies

**Technical Indicators**

**Simple Moving Average**

**Syntax:sma(parameter,n)**

As the name, it gives the average of the parameter for the last n values of the parameter.It is called moving average because it is calculated at every minute, and a seperate series, same as that of price is formed in this indicator.

**Example:**sma(close,5) > sma(close,10)

**Exponential Moving Average**

**Syntax: ema(parameter,n)**

Almost similar to the simple moving average, the difference lies that in the Exponential Moving Average, rather than simple avearge, a weighted average is evaluated, which gives higher priority to the latest prices than the earlier prices. It leads to the series of Exponential Moving Average moving more closely to the price series than simple moving average.

**Example:**ema(close,5) > ema(close,10)

**Relative Strength Index (RSI)**

**Syntax:rsi(n)**

It gives an indication of reversal of a particular trend in a scale of 0 to 100. A value > 80 signifies the price of the stock is more than what it should have and will move down from then, and a value of < 30 indicates the reverse. It calculates the frequency of time frame when the prices have moved up and down within n time frame and converts to a scale of 100.

**Example:**rsi(10) < 30

**Moving Acearge Convergence-Divergence (MACD)**

**Syntax:macd(parameter)**

As the name, it gives an indication whether 2 differently times moving averages are moving close to each other or the opposite.It calculates 2 moving averages series of 9 and 14 time frames, i.e. sma(close,9) and sma(close,14) ; then creates a moving average of the difference over 9 time periods, i.e. sma( sma(close,9) - sma(close,14),9) and returns the difference between this and the latest difference between moving averages, i.e. (sma( close, 9 ) - sma ( close , 14) ) - sma( sma(close,9) - sma(close,14),9). Higher value indicates upward momentum and vice versa.

**Example:**macd(close) > 0

**Bollinger Bands**

**Syntax:bb(parameter,n)**

Its a reversal indicator that gives you an indication that the price has moved farther than the mean by 1-standard deviation and has higher probability to come back. It works well with RSI indicator.

**Example:**(bb(close,10) > 0) & ( rsi(10) > 70)

**Volume Weighted Moving Average**

**Syntax:vwap(n)**

Evaluates the average of recent n time period of prices with volume as weights to the corresponding price. It is considered to be a very strong parameter and can be used with multiple indicators to give a balance strong strategy.

**Example:**ema(vwap(10), 10) > ema(vwap(10), 20)

**Rate of Change (ROC)**

**Syntax:roc(parameter,n)**

Evaluates the change in value of a paramter in the last n time periods

**Example:**roc(sma(close,10),10) > 1

**Money Flow Index (MFI)**

**Syntax**:mfi(10)

Its an analogy of Relative Strength Index indicator in volume, wherein the ratio of frequency of ups and downs are adjusted with volume.

**Example:**mfi(10) > 30

**Force Index**

**Syntax**:force(10)

It tells you about the strength of the momentum with volume. Higher the value, more is probability of prices moving in a particular direction.

**Example:**mfi(10) > fi(20)

**Average True Range(ATR)**

**Syntax**:atr(n)

It tells you about the average range seen in the prices in the recent n time frames.

**Example:**atr(10) > atr(20)

**On Balance Volume**

**Syntax**:obv(n)

It is a ratio of sum of volumes at time frames of upward price movements and at down time movements

**Example:**obv(10) > 0

A few examples of Price-Volume based strategies are

**Example:**sma(rank(vwap(5),10),5) > sma(rank(vwap(5),10),10)

**Logic:**The idea behind this strategy is also to buy when the pricess are moving up. The above formula is an implementation for the same and combines the 4 point framework of a strategy in a single formula.

A stock is moving up can be indicated by the fact that its volume weighted moving avreage is moving upwards. So we have used vwap to represent the same. Now vwap would increase if its rank is increasing, i.e. the highest values of vwap are seen in the latest data. Hence if rank of vwap for n time frames is higher than rank of vwap for (n+ 1) time frames, it indicates that the price is rising. To remove any points that might come up due to a small erratic movement in price, we added the simple moving average term, i.e. sma. What it does is that it smoothens the data series. Hence, implementing the complete formula for the strategy, it might be possible to identify the stocks that are about to move up and buy them

**Example:**close > vwap(5)

**Logic:**Based on indentifying prices that are higher than they should be.

**Example:**(std(close,10) > std(close,15) ) & ( sma(close,5) > sma(close,10)

**Logic:**Based on indentifying the movement is there in the market and prices are moving up..

### Components of Risk Management

Risk management is one of the most important things to do in the stock markets. One key work of the strategy is to make sure that losses are redues, because, losses will always be there no matter how good the strategy is, but a good strategy is able to ensure that losses are lesser than the profits.

We have highlighted some key components that are part of risk management

- Always setting a target and stoploss for every trade that happens. And the range in the target and stoploss should be such that the target is always more than the stoploss and maybe even a bit higher so as to accomodate the losses in transactional costs.
- Having atleast 5-10 stocks in a portfolio. It is a not a good habit to trade on a single stocl only. For when it falls, the entire capital will be eroded. Having multiple stocks, especially from diffenent sectors is always a good idea to reduce the risks.
- Be hopeful for the best and prepared for the worst. Using this ideology, set the ranges of the targets and stoploss. One should know how much money can he/she afford to loss.
- Using Stress Testing to get to know how bad can a strategy can go. Stress Testing is a tool which is very similar to a backtesting system but on artificially created data, which no one has seen. Its always a good practice to test your strategy on stress testing modules also before taking it live.

### Portfolio Allocations

A well diversified portfolio can save you from risks which might not have been expected. And stock markets are always un-predictable. It can never be said in surity that the prices will behave a particular manner.

A simple way to diversify your portfolio to pick and chose stocks from different sectors and industries that are independent of each other. For example: Pharmaceuticals and Tires.

This helps in ensure that if Pharmaceuticals is down, Tires is there to save you.

Another problem people face is that how much capital they should allocate to which sector or stock. A simple intuitive logic suggests that allocate more capital of the stocks that have a higher probability of giving stable returns.

For example, there's a stock that gives a backtest Sharpe ratio of 2.1 and a stock that gives a backtest result of 1.1, a simple allocation of the capital can be in the ratio of 2:1, i.e. you are allocating double the capital to the stock having higher Sharpe against the other.

On the technical side, there are some methods to that can automatically decided on the capital allocation based on parameters like Expected Annualized Returns and Volatility. We have incorporated 2 such methods in our backtesting system which are:

- Markovitz Portfolio Optimization
- Sharpe Model

### Framework of an Algorithmic Trading Strategy

An Algorithmic Trading Strategy must have the following key components

**Driving Force****Confirmation****Movement CheckPoint****Noise Reduction**

Lets study the components one by one.

**Driving Force:**

It is the core logic behind an trading algorithm at a broad level that sets the tone of further development.

An example of a driving force can be that the user wants to develop a momentum based trading strategy when the stock has risen a few points and is expected to move up further. This logic defines the entry conditions for you and you have to write the equation corresponding to the same.

**Confirmation**

In this step, you are simple confirming that your driving force conditions are being met

**Movement Checkout**

You need to ensure that there would be sufficient movement in the prices after an order is placed.

**Noise Reduction**

This step ensures that when an entry point is identified by an algorithm, it is not based on the very short term erratic movement of the prices but on an actual movement.

Lets take an example to potray each of these components

The driving force for a stratgy is that it buys when there is upward price movement in stocks and is expected to continue for some time. The core algorithm for this can be

**sma(close,5) > sma(close,10)**

Next to confirm that the upward movement is significant, we can add a volume check as

**sma(volume,5) > sma(volume,10)**

that verifies that volume is also increasing as the prices are increasing and confirms a momentum. To ensure that this momentum will further continue, we add a volatility parameter as

**sma(std(close,5),5) > sma(std(close,10),5)**

which ensure that the standard deviation, which can be taken as a reflection of price movement is gradually increasing.

And finally to ensure that buy decision is on the actually movement rather than a short term noise, we can add an algorithm like **sma(high - close ,5) < sma(high -close,10) **

In totallity the algorithm turns out to be

**(sma(close,5) > sma(close,10)) & ( sma(volume,5) > sma(volume,10))& (sma(std(close,5),5) > sma(std(close,5),10) & (sma(high - close ,5) < sma(high -close,10))**

### Statistics used

Basic Statistics is very useful in developing a trading algorithm. Some of the most widely used functions are as follows

**Mean****Standard Deviation****Skewness****Correlation**

Lets study the functions one by one.

**Mean**

Simply returns the sum of parameters divided by the number of parameters

**Syntax:**mean(parameter,n)

**Standard Deviation**

Gives an idea about the movement in the parameters. Higher the value, more is the combined upward and downward movement

**Syntax:**std(parameter,n)

**Skewness**

Gives some information about how unsymmetrical have the values of the parameter have been. A positive value means that more parameters are higher than the mean and vice versa.

**Syntax:**skew(parameter,n)

**Correlation**

Gives an idea on how to different parameters are changing their value in synchronization, i.e. a high correlation means that parameters are changes in an almost same way and a negative correlation indicates opposite changes in the parameters.

**Syntax:**corr(parameter1,parameter2,n)