google-site-verification=JHtzA60Hs5wN9d6gu1Bc2h1qX2UpYFwdSVz_pcE3C2A
top of page

Algo Trading

Algorithmic trading is the newest trend. Just like a smartphone or 1.5GB/day internet or electric cars. We sure use them. but how many can create them?

When it comes to making money, especially in the stock market, having an edge is important. Well, you might have an edge but how can you code it? How do you make it work while you do other things?

YOU DON'T NEED TO BE A PRO IN PYTHON!

IT IS SIMPLE...SEE BELOW

Due to the nature of content, this page is best viewed on desktop

 

Stationary photo

Advantages of Algo Trading

Strategy

You can not code something without a strategy. Algo helps you build a plan thinking over entries and exits

Emotions and Bias

Can an algo panic during erratic price movements? It just follows the coded instructions

Discipline

AI luckily does not think of its own yet. It follows the instructions. Sleeps and wakes with precision.

How I built the algo for the famous 9:20 straddle 

I am not a computer science guy but am a geek so I am good with computers. My exposure to a computer language was in my Bachelors when C/C++ was a subject in the curriculum. I understood how it works and how to use operators. A few years after my bachelors, I got introduced to stock markets. And then to algo trading and I learnt how to create an algo and there is not looking back. I built my first one with just basic knowledge within one day and started improving it. I even had to get help to install python and its environment.

I realized that the logic is important when it comes to creating algo than technical knowledge. So before you begin coding, you need to have some knowledge regarding the following:

  • Derivatives, especially Options in stock market

  • Experience in trading - Algo trading is not for beginners and you should have enough knowledge about trading. Algo helps you automize your strategy. I am assuming you are well aware of derivatives.

  • PyCharm (or any python application..I found pycharm easy to use)

  • creating functions, if else, while, try except, read/write functions and basic knowledge in python

  • KiteConnect Setup (because I am using Zerodha..every broker has their own initialization process)

Before I go into the coding process, let us think about how the strategy works. I am using the 920 straddle strategy to do the coding only as an example because it is very simple and many people know about it. So what is the 920 straddle? Let me give a brief introduction

What is 920 straddle?

920 straddle is a strategy where we enter the positions at 9:20 AM by selling ATM CE and PE options in Bank Nifty or Nifty with a stop-loss of 25% on individual leg. If CE Stop-loss is hit, PE still remains and if PE Stop-loss is hit, CE position still remains. And we keep the positions open until 15:20. 

Terminologies:

ATM - At The Money

LTP - Last Traded Price

CE - Call option

PE - Put Option

SL - Stop Loss

Let us think of it in terms of steps

  1. Wait until 9:20 am

  2. Sell ATM CE and ATM PE options

  3. Place SL or 25% on CE option

  4. Place SL of 25% on PE option

  5. Wait until 15:20

  6. Exit any open position

It has become very famous because it is easy to follow and the back test gave amazing results. Because many people are using it, it is easy to get slippages during execution which is why some people started doing it with their own variations. Some enter at 9:30, some at 9:29 some at 10:00 etc. 

 

The strategy appears pretty simple right? But when coding, the computer needs strict instructions to follow otherwise it may back fire creating huge losses or even gains based on how the markets are moving.

 

In the next section, I will explain how to code the 9:20 straddle
 

How to code 920 straddle?
Thought process behind developing the algo

First, we have to think all the various combinations that can happen. Remember Murphys Law. If something can go wrong, it will!

Lets look at the steps I mentioned above and think of what can be done in terms of coding

  1. Wait until 9:20 am

    • Start the python program at or before 9:20 am​

    • run a loop to check if the time is 9:20 or not

    • if time is 9:20, we go to step 2 else repeat the loop

  2. Sell ATM CE and ATM PE options

    • Time is 9:20​

    • We find what the LTP of Nifty/Bank Nifty is

    • We determine ATM based on LTP

    • We send instruction to broker to place CE and PE sell order for ATM

  3. Place SL of 25% on CE and PE options

    • Confirm CE and PE sell orders are placed​

    • Get the price at which CE and PE orders are sold

    • Determine the 25% SL for entered CE and PE options

    • Send instruction to broker to place CE and PE Stop-Loss orders at 25%

  4. Wait until 15:20

    • Run the loop to check for time​

    • if time is 15:20, go to next step, else repeat loop

  5. Exit any open position

    • time is 15:20​

    • check if CE SL is hit

    • if CE SL hit, go to next step, else exit CE position

    • check if PE SL is hit

    • if PE SL hit, go to next step, else exit PE position

    • check no positions are open

    • end program

See, so much to take care of for a simple strategy that can be explained in 2 lines. But this is how you prepare an algo. You need to think of every thing right from entry to exit which is why algo trading works, because you have a proper strategy with defined entry and exit.

I used pycharm to setup my python environment and started coding my logic based on the the above steps. 

I have an account with Zerodha so I am using the API provided by Zerodha to create my algo. It is called KiteConnect and is very reliable. Every broker has their own with their own methods to initialize it and establish a connection with your broker account. In the next section, lets see the actual code I used to create the program

Actual code

To make the page easy to read, I am directly jumping in to the code for each step above. Before initiating a code, we have to declare some variables and constants. I kept adding more variables and constants while I started coding as shown below. We also need to import some libraries which help us use the functions without having to write them all over again.

  1. Wait until 9:20 am

    • Start the python program at or before 9:20 am​





      Kite connect needs to be initialized before we can establish connection with you account. I will explain how to do it later.




      I defined the entry and exit times at the start of the program

    • run a loop to check if the time is 9:20 or not

    • if time is 9:20, we go to step 2 else repeat the loop







      I used a user defined function now() to get current time. I am checking if the current time > entry time and not = because my sleep time is 5 sec and there is great chance of missing the 9:20 AM time if we try to be exact. The loop closes after running 1 instance of entering the position.

  2. Sell ATM CE and ATM PE options

    • Time is 9:20​

    • We find what the LTP of Nifty/Bank Nifty is

    • We determine ATM based on LTP



      I rounded off the actual Bank Nifty value to the nearest 100 because the strike prices of Bank Nifty options are usually nearest 100. If you want to do this for Nifty, you will need to code it differently

    • We send instruction to broker to place CE and PE sell order for ATM









       

  3. Place SL of 25% on CE and PE options

    • Confirm CE and PE sell orders are placed​
      If the ce_order_id and pe_order_id from above orders is executed without any error, then it is a confirmation the orders are placed successfully

    • Get the price at which CE and PE orders are sold
      The price at which ATM CE position was sold is stored in the ce_price variable

      The price at which ATM PE position was sold is stored in the pe_price variable

    • Determine the 25% SL for entered CE and PE options
      This is pretty simple..multiple ce_price by 1.25 and pe_price by 1.25

    • Send instruction to broker to place CE and PE Stop-Loss orders at 25%









      If you observe, I placed the trigger_price at pe_price*1.25 but the actual price is pe_price*1.25 + 20. The extra Rs. 20 is added to the price to prevent the stop loss from not getting hit when there are extreme moves. Some times the price can go from Rs. 125 to Rs. 175 directly when there is erratic movement. So I kept a safety margin of Rs. 20. You can change it to Rs. 100 or any other number based on your thoughts. Research more about SL Orders to understand how this works.






      The very starting of the entry code, there is a break if all the entry and SL orders are placed. Initially they are declared as none.
       

  4. Wait until 15:20

    • Run the loop to check for time​










      This is a new while loop only for exit. The Entry while loop exits when all orders are placed

    • if time is 15:20, go to next step, else repeat loop

  5. Exit any open position

    • time is 15:20​





      When the time is 15:20, the status of Stop losses is obtained as from above
       

    • check if CE SL is hit

    • if CE SL hit, go to next step, else exit CE position as shown below






       

    • check if PE SL is hit

    • if PE SL hit, go to next step, else exit PE position as shown below






       

    • check no positions are open



       

    • end program with a simple break

At the end of the code, the total libraries I imported and all the constants variables I declared is as shown below


 







 

As you can clearly see, you need knowledge about python but you need not be a pro in it. You will spend a lot of time when creating it for the first time. But the 2nd one is going to be pretty easy. The above code is not the most optimized but it works and I am using it for more than 7 months without any issues until now. In the first few days, I suggest to trade using only 1 lot and keep an eye on how it is working. Also make sure you have enough capital to cover the margins of only 1 lot. If the loop is not properly coded, it is possible to keep sending the orders infinitely. Once you are confident, you can increase the lot size and leave it running while you create new strategies.

Once you finish coding shown above, you can change the stop loss percentage or entry time or exit time and develop your own method.

For any questions or feedback, you can contact me

Screen Shot 2022-05-15 at 02.57.46.png
Screen Shot 2022-05-15 at 03.01.39.png
Screen Shot 2022-05-15 at 03.07.15.png
Screen Shot 2022-05-15 at 03.11.15.png
Screen Shot 2022-05-15 at 03.14.13.png
Screen Shot 2022-05-15 at 03.48.58.png
Screen Shot 2022-05-15 at 03.51.48.png
Screen Shot 2022-05-15 at 05.05.01.png
Screen Shot 2022-05-15 at 05.08.49.png
Screen Shot 2022-05-15 at 05.23.19.png
Screen Shot 2022-05-15 at 05.26.02.png
Screen Shot 2022-05-15 at 05.26.22.png
Screen Shot 2022-05-15 at 05.34.06.png
Screen Shot 2022-05-15 at 05.45.48.png
Screen Shot 2022-05-15 at 07.32.51.png

©2017 by ptlman. Proudly created with Wix.com

bottom of page