Using Futures Data on BankNIFTY Golden Ratio Strategy

If you have followed here from my course of Coding an Algo Trading Bot: BankNIFTY Golden Ratio Strategy, You must have tried broker integrations following the Github Repo I had shared there.

Now, I had integrated the Golden Ratio Button in our Slack and Telegram Channels.

Problem 1:

I know I have shown in the course to use the spot for calculation of Golden Number and use that in BankNIFTY futures but here, What I was doing is taking the data purely from BankNIFTY Futures of Current Month.

Yesterday, The values derived from futures data was absurd!

             date     open      high       low    close   volume

10 2020-10-27 00:00:00+05:30 24311.0 24797.00 23894.85 24741.4 7850350
11 2020-10-28 00:00:00+05:30 23698.0 24794.25 23698.00 24620.0 1139925

As You can see here Chart Image — TradingView. The low is 23698.00 which means someone got ruined in their AMO order and the orders really got executed at very bad price!

Right? So, to avoid this, I decided to move back to using the spot data only.

Now, Let’s come to the second part.

When we are about to get the range of the first 10 mins - What we were doing was running the program at the 10th minute taking the high and low of the symbol!

That’s it.

Now, there is two way you can get the high and low of the symbol in Kite.

Using the Kite Historical API

def p_candle(token,time_period,candle):
    starting_date=(datetime.datetime.today() + datetime.timedelta(days=-15)).strftime("%Y-%m-%d")
    ending_date=(datetime.datetime.today() + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    zap=kite.historical_data(token,starting_date,ending_date,time_period,0)
    zapp=pd.DataFrame(zap).tail(1+abs(candle))
    print(zapp)
    return zapp['open'].iloc[-1+candle],zapp['high'].iloc[-1+candle],zapp['low'].iloc[-1+candle],zapp['close'].iloc[-1+candle]

The output of zapp will be like -

10 2020-10-28 00:00:00+05:30  11922.6  11929.4  11684.85  11729.6       0
11 2020-10-29 00:00:00+05:30  11633.3  11743.9  11606.75  11670.8       0

As you can see it will read the data from last day if we do - p_open,p_high,p_low,p_close=p_candle(get_insToken(var2.upper(),"NSE"),"day",0)

Using Kite LTP Function

def get_ohlc(var2,exchange="NFO"):
    var5='NFO:'+futname(var2)
    if(var2=="NIFTY"):var2="NIFTY 50"
    if(var2=="BANKNIFTY"):var2="NIFTY BANK"
    var6='NSE:'+var2.upper()
    if(exchange=="NSE"):
        return kite.ohlc(var6)[var6]['ohlc']['open'],kite.ohlc(var6)[var6]['ohlc']['high'],kite.ohlc(var6)[var6]['ohlc']['low'],kite.ohlc(var6)[var6]['ohlc']['close']
    else:
        return kite.ohlc(var5)[var5]['ohlc']['open'],kite.ohlc(var5)[var5]['ohlc']['high'],kite.ohlc(var5)[var5]['ohlc']['low'],kite.ohlc(var5)[var5]['ohlc']['close']

Anyways, the problem is the data will not be the same from both the places. The data that comes from Kite LTP function will match with Tradingview.

Now, Zerodha runs some problem that fixes this issue at night time every night. So, We need to use the Kite’s LTP Function only.

Another major change to the current strategy.

Well, Although the primary context of creating the Udemy Mini-Course was to introduce people to the trading API world but the strategy is chosen here is a pure diaster. Anyways,

In our last message, we talked about calculating using Spot and using that value in futures. It is wrong. Especially when the future and spot in a difference of premium i.e Spot is trading at x and futures is trading at x+100 or x-100.

Last month it became significant. So, Let’s calculate the golden number from the spot and use that data into futures using futures’ closing price.

Original Strategy:

Scrip = BANKNIFTY Futures

  • Golden Number = ((Previous Day High - Previous Day Low) + Opening Range of Today’s First 10 minutes))*61.8%
  • Buy Above = (Previous Day Close + Golden Number)
  • Sell Below = (Previous Day Close - Goldern Number)

Modified Strategy:

Scrip = BANKNIFTY

  • Golden Number = ((Previous Day High - Previous Day Low) + Opening Range of Today’s First 10 minutes))*61.8%
  • Buy Above = (Previous Day Close + Golden Number)
  • Sell Below = (Previous Day Close - Goldern Number)

Now, Again Modified:

This part is from BANKNIFTY Futures.
Golden Number = ((Previous Day High - Previous Day Low) + Opening Range of Today’s First 10 minutes))*61.8%

This part is from BANKNIFTY Spot.

  • Buy Above = (Previous Day Close + Golden Number)
  • Sell Below = (Previous Day Close - Goldern Number)

The code in the last was -

lotsize = instrumentList[(instrumentList.tradingsymbol.str.match(tradesymbolfut))]
lotsize = str(lotsize.lot_size.iloc[0])

p_open,p_high,p_low,p_close=p_candle(get_insToken(var2.upper(),"NSE"),"day",-1)
c_open,c_high,c_low,c_close=get_ohlc(var2,exchange="NSE")
eqltp,futltp=both_ltp(var2)
golden_number=((p_high-p_low)+(c_high-c_low))*.618
golden_number=round_down(float(golden_number),0.05)
buy_above=round_down((p_close+golden_number),0.05)
sell_below=round_down((p_close-golden_number),0.05)

Now we will change that to -

tradesymbolfut = futname(var2)

lotsize = instrumentList[(instrumentList.tradingsymbol.str.match(tradesymbolfut))]
lotsize = str(lotsize.lot_size.iloc[0])

p_futopen,p_futhigh,p_futlow,p_futclose=p_candle(get_insToken(tradesymbolfut,"NFO"),"day",-1)
p_open,p_high,p_low,p_close=p_candle(get_insToken(var2.upper(),"NSE"),"day",-1)
c_open,c_high,c_low,c_close=get_ohlc(var2,exchange="NSE")
eqltp,futltp=both_ltp(var2)
golden_number=((p_high-p_low)+(c_high-c_low))*.618
golden_number=round_down(float(golden_number),0.05)
buy_above=round_down((p_futclose+golden_number),0.05)
sell_below=round_down((p_futclose-golden_number),0.05)

This is something that should be noticed by a trader after facing a huge loss. Someone doing back test and selling lemons will not have any idea because the data provided by data vendors mismatch in high tune. Here is an example -

To code the above data, I casually checked for the closing price of the BankNIFTY Futures of current month.


It is 33807.25 here. The data is coming from Zerodha.


The data from Tradingview is a total joke! It is showing April Futures as current contract after May 1st is ended.

download - 2021-05-02T063139.206
Then there is Kite API having vertigo and made .05 disappeared

The same company Zerodha is showing 33807.2 and 33807.25. I know the argument will be that it is not significant. What I need to know is that, why it is there in the first place.

.05 in 255 trading days a year and in 10 years will make a deviation of 127.5 points which will be significant right? .0525510=127.5

Anyways…

There is always an error.

Then Murphy’s Law says, "Anything that can go wrong, will go wrong”.

Hi Amit, Is these latest fixes updated in github? or should we do that manually

1 Like

No bro. Not updated in Github because I made the course as a sole reason to show how to do coding on basic strategy. The strategy is not profitable when you add the trading costs and slippage yet some marketing guys who never traded in life sells this strategy to get brokerage.