Designing a trading algorithm

Het Dagli
Het Dagli
Nov 1 · 4 min read

It is assumed that trading in markets like Nifty, Sensex, etc, is carried out by individuals(human beings) with knowledge and required qualification. But with the advent of computers things have changed drastically.

Algorithmic trading is automated trading by computers that are programmed to take certain actions in response to varying market data.

Every day my algorithm will produce a trade sheet to decide on the stocks to buy and sell on that particular trading day. The trading strategy will generate the trade sheet based on the algorithm coded in python.

Each and every individual at some point in time will start earning money. The question of what to do with extra money i.e savings will surely come to their mind. Many will go for a fixed deposit or mutual fund etc. But the returns given by the above methods are not attractive enough and also the fact remains that in the above methods you are not in control of your own money. Algorithm trading may be a solution to the above problem. It is a better way to handle the savings and also more profitable if done right with extensive research.

Algorithm trading is generally done by HFTs and hedge funds. There are very few retail investors who do it.

Most of the algorithms which are open source are not profitable enough to invest upon. If I am able to minimize risk and maximize profit then I can open-source my algorithm for further improvement.

Even if I am not able to complete and test my algorithm in a given time period, the insights generated by me by analyzing historical data may prove to be valuable. It is difficult to innovate in this field as it requires a combination of logical decision making with a little bit of luck as part of the trial and error method. I need to generate multiple hypotheses based on a mathematical or a statistical model and work upon to prove my hypothesis right or wrong. Based on the results of the hypothesis testing I will make an informed decision of whether to use the given hypothesis in my algorithm.

I divide my approach into two phases:

  1. Researching and applying various machine learning models:

The first model I applied was the LSTM model on Nifty data. Firstly, I started out by importing all the libraries and also the data in an IPython notebook. The data used was nifty data from the year 1997 to 2019. The format of the data was OHCLV. The frequency of the data was daily. The next thing I did was to divide the data into train and test data and perform scaling to make all the values between 0 to 1.Next is just running the model and getting the results. I used matplotlib to visualize the predictions. Here is the result of the LSTM model.

The second model I applied was the HMM model to detect a regime change. The main hypothesis is that the hidden state of the hidden Markov model will change its state whenever regime change is detected.

The process started in a similar way as the previous model, where I imported the data. The data used was again the Nifty index’s day to day price.

I used different input variables to get better results.

Finally, I used the previous two models to construct a trading algorithm.

2. Constructing a trading algorithm

Pseudocode for the trading algorithm:

The function which carries out transaction:

Transaction_on_market_data(tick1,tick2)

{

buy_price=tick2.bid-s

If buy_signal is True

Replace it to buy_price

else

send another order at the buy_price

sell_price=ticks2.ask+s

if sell_order is True

replace it to sell_price

else

send new order at sellPrice

}

afterexecution

{

if buy execution happened in instr1

send sell order on instr2

else if sell execution happened on instr1

send buy order in instr2

}

The remaining work in the project is to take the algorithm live.

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade