30 April 2020

Installation of TWS API - Interactive Brokers Part 1


One advantage of Interactive Brokers (referred to as IBKR or just IB hereafter) over Kite Connect or Upstock API is that IB provide's a Paper Trading Account.

If you are an Individual, you can signup for the free trial ofInteractive Brokers . This basically gives you a "Paper Trading Account" with access to all the IBKR platforms for a couple of months including the TWS and Mosaic platforms. For the purpose of live testing our trading strategy , we have access to  Market Data ( Delayed by 10-15 minutes) and the API for retrieving data  (Not all Data) and placing orders. So yeah, the trial version of IB provides "Limited Functionality" for testing your strategies using the API. 

Some of the things that you can do with ApplicationProgramming Interface(API) from IBKR  :
  • Automate your strategies
  • Create a custom trading terminal
  • Develop a Screener
  • Experiment with your own Trading Indicator
Basically if you are reading this, you know why you have opted for IBKR.

Its is important to note that there are other third party Libraries in Python like ib_insyncIbPy and IBridgePy. But these have been developed well before the release of the Python Native API which is developed by IBKR . Only the API developed by IBKR is officially supported by IBKR.  For more information on the API, Check the YouTubelink for the official videos of TWS Python API

It is always helpful to get a feel of the web interface of IB which they refer to as Client Portal and Trader Work Station(TWS) before we get started.

15 April 2020

Calculation of Supertrend (ST)

The Supertrend is one of the most widely used Indicator among the Indian traders. And unfortunately there is very little documentation on this subject at Investopedia or the chart school of Stockcharts or even with Zerodha Varsity. So it required quite some investigation to arrive at the right method to calculate the Supertrend.

In its simplest form, when the Supertrend is overlayed over a candlestick chart, if the Supertrend crosses to below the Price we take a Long Position and when the Supertrend crosses to above the Price, we take a Short position. Here is a snapshot of the Supertrend.

This article from Economic Times is probably a good place to start with to get a qucik overview of Supertrend.

So let us dive straight into and get the calculation of Supertrend. Check this Google Sheet for the Calculation of Supertrend. 

Given below is the pseudo code for the calculation of Supertrend:

Basic UpperrBand = (High + Low) / 2 + Multiplier * ATR
Basic LowerBand =  (High + Low) / 2 - Multiplier * ATR

Final UpperBand = IF((Current BasicUpperband < Previous Final UpperBand) OR 
                    (Previous Close > Previous Final UpperBand))  THEN 
                    (Current Basic UpperBand) ELSE
                    (Previous FinalUpperBand)

Final LowerBand = IF((Current Basic LowerBand > Previous Final LowerBand) OR
                    (Previous Close < Previous Final LowerBand)) THEN
                    (Current Basic LowerBand) ELSE
                    (Previous Final LowerBand)

SuperTrend = IF((Previous SuperTrend = Previous Final UpperBand) AND 
               (Current Close <= Current Final UpperBand)) THEN 
               Current Final UpperBand
             ELSE
                IF((Previous SuperTrend = Previous Final UpperBand) AND
                  (Current Close > Current Final UpperBand)) THEN
                  Current Final LowerBand
                ELSE
                   IF((Previous SuperTrend = Previous Final LowerBand) AND
                     (Current Close >= Current Final LowerBand)) THEN
                     Current Final LowerBand
                   ELSE
                      IF((Previous SuperTrend = Previous Final LowerBand) AND
                        (Current Close < Current Final LowerBand)) THEN
                        Current Final UpperBand

Now lets see how this is calculated in Python. Lets first download the OHLCV data from the above google sheet into a CSV file. Name it as "test data". We can use this as our input file for the Python program.

Lets first take a look at the Imports and Global Declarations that we will use in this program:
#imports used in the program
import numpy as np
import pandas as pd

#Global Declarations
input_file = "test data.csv"
multiplier = 2 # An integer to indicate the value to multiply the ATR.
period = 14
atr = 'ATR' + '_' + str(period)
st = 'SuperTrend' + '_' + str(period) + '_' + str(multiplier)