Search This Blog

Sunday, March 20, 2022

Gapup GapDown strategy

 #!/usr/bin/env python

# coding: utf-8


# In[ ]:



# below code to get OHLC of HDFCBANK, SBIN, ICICI

import pandas as pd

import yfinance as yf

import numpy as np

import math

from mplfinance.original_flavor import candlestick_ohlc

import matplotlib.dates as mpl_dates

import matplotlib.pyplot as plt



# In[ ]:



# get stock prices using yfinance library

def get_stock_price(symbol,stDate,endDate):

    df = yf.download(symbol, start=stDate,end = endDate, threads= False)

    df['Date'] = pd.to_datetime(df.index)

#     df['Date'] = df['Date'].apply(mpl_dates.date2num)

    df = df.loc[:,['Date', 'Open', 'High', 'Low', 'Close']]

    return df



def identifyGAPUPDOWN(startDate,endDate):


    symbol = 'HDFCBANK.NS'

    df_HDFC = get_stock_price(symbol,startDate,endDate)


    symbol = 'ICICIBANK.NS'

    df_ICICI = get_stock_price(symbol,startDate,endDate)


    symbol = 'SBIN.NS'

    df_SBIN = get_stock_price(symbol,startDate,endDate)


    symbol = 'KOTAKBANK.NS'

    df_KOTAK = get_stock_price(symbol,startDate,endDate)

    strState = ""

    x = 0

    if int(df_KOTAK['Close'][0]) >= int(df_KOTAK['High'][0]):

        strState = 'GAP UP'        

        x = x+1

    

    if int(df_SBIN['Close'][0]) >= int(df_SBIN['High'][0]):

        strState = 'GAP UP'        

        x = x+1

    

    if int(df_HDFC['Close'][0]) >= int(df_HDFC['High'][0]):

        strState = 'GAP UP'        

        x = x+1

    

    if int(df_ICICI['Close'][0]) >= int(df_ICICI['High'][0]):

        strState = 'GAP UP'        

        x = x+1

    

    KOTAK_highClose_PTS = str(int(df_KOTAK['High'][0]) - int(df_KOTAK['Close'][0]))

    SBIN_highClose_PTS = str(int(df_SBIN['High'][0]) - int(df_SBIN['Close'][0]))

    HDFC_highClose_PTS = str(int(df_HDFC['High'][0]) - int(df_HDFC['Close'][0]))

    ICICI_highClose_PTS = str(int(df_ICICI['High'][0]) - int(df_ICICI['Close'][0]))

    

    KOTAK_lowClose_PTS = str(int(df_KOTAK['Close'][0]) - int(df_KOTAK['Low'][0]))

    SBIN_lowClose_PTS = str(int(df_SBIN['Close'][0]) - int(df_SBIN['Low'][0]))

    HDFC_lowClose_PTS = str(int(df_HDFC['Close'][0]) - int(df_HDFC['Low'][0]))

    ICICI_lowClose_PTS = str(int(df_ICICI['Close'][0]) - int(df_ICICI['Low'][0]))

    

    

    

    print('x===='+str(x))

    print('HDFC_highClose:'+HDFC_highClose_PTS+'---ICICI_highClose:'+ICICI_highClose_PTS+'--SBIN_highClose:'+SBIN_highClose_PTS+'--KOTAK_highClose:'+KOTAK_highClose_PTS)


#     y = 1

#     if int(df_KOTAK['Close'][0]) >= int(df_KOTAK['Low'][0]):        

#         strState = 'GAP DOWN'

#         y = y+1

    

#     if int(df_SBIN['Close'][0]) >= int(df_SBIN['Low'][0]):

#         strState = 'GAP DOWN'

#         y = y+1

    

#     if int(df_HDFC['Close'][0]) >= int(df_HDFC['Low'][0]):

#         strState = 'GAP DOWN'        

#         y = y+1

    

#     if int(df_ICICI['Close'][0]) >= int(df_ICICI['Low'][0]):

#         strState = 'GAP DOWN'        

#         y = y+1

    

   

    # HDFC_highClose:7---ICICI_highClose:3--SBIN_highClose:2--KOTAK_highClose:14

    # HDFC_lowClose:12---ICICI_lowClose:7--SBIN_lowClose:3--KOTAK_lowClose:8

                

    print('HDFC_lowClose:'+HDFC_lowClose_PTS+'---ICICI_lowClose:'+ICICI_lowClose_PTS+'--SBIN_lowClose:'+SBIN_lowClose_PTS+'--KOTAK_lowClose:'+KOTAK_lowClose_PTS)

    

    if int(KOTAK_highClose_PTS) <= int(KOTAK_lowClose_PTS) and int(SBIN_highClose_PTS) <= int(SBIN_lowClose_PTS) and int(HDFC_highClose_PTS) <= int(HDFC_lowClose_PTS) and int(ICICI_highClose_PTS) <= int(ICICI_lowClose_PTS):

        strState = 'GAP-UP'

    

    if int(KOTAK_highClose_PTS) >= int(KOTAK_lowClose_PTS) and int(SBIN_highClose_PTS) >= int(SBIN_lowClose_PTS) and int(HDFC_highClose_PTS) >= int(HDFC_lowClose_PTS) and int(ICICI_highClose_PTS) >= int(ICICI_lowClose_PTS):

        strState = 'GAP-DOWN'

        

    if strState == '':


        if int(SBIN_highClose_PTS) <= int(SBIN_lowClose_PTS) and int(HDFC_highClose_PTS) <= int(HDFC_lowClose_PTS) and int(ICICI_highClose_PTS) <= int(ICICI_lowClose_PTS):

            strState = 'GAP-UP'


        if int(SBIN_highClose_PTS) >= int(SBIN_lowClose_PTS) and int(HDFC_highClose_PTS) >= int(HDFC_lowClose_PTS) and int(ICICI_highClose_PTS) >= int(ICICI_lowClose_PTS):

            strState = 'GAP-DOWN'


        

    return 'BANK NIFTY on DATE '+endDate  +' is ---->'+strState



# In[56]:



stock_fut = get_history(symbol="SBIN",start=date(2022,3,16),end=date(2022,3,17),futures=True, expiry_date=date(2022,3,31))

stock_fut



# In[58]:



vix = get_history(symbol="INDIAVIX",start=date(2022,3,15),end=date(2022,3,17), index=True)

vix



# In[51]:



from nsepy import get_history

from datetime import date

data = get_history(symbol="BANKNIFTY", start=date(2022,3,17), end=date(2022,3,18),index=True)

data

# data[['Close']].plot()



# In[ ]:



# high = close -----> GAPUP

# low  = close -----> GAPDOWN


# if today (startDate) is GAP DOWN and HIGH = CLOSE ----> then GAP-DOWN on NEXT DAY)

# if today (startDate) is GAP DOWN and LOW = CLOSE ----> then GAP-UP on NEXT DAY)

# if today (startDate) is FLAT and LOW = CLOSE ----> then GAP - UP on NEXT DAY)

# if today (startDate) is FLAT and HIGH = CLOSE ----> then GAP - DOWN on NEXT DAY)


# if CLOSE is symmetrical pattern to HIGH and LOW and for today if it is GAP-UP,

#                                           then for next day it should also be GAP-UP


# if CLOSE is symmetrical pattern to HIGH and LOW and for today if it is GAP-DOWN,

#                                           then for next day it should also be GAP-DOWN



# In[ ]:



rc = identifyGAPUPDOWN('2022-03-04','2022-03-05')

print(rc)

# please recheck on 01-feb-2022, 05th JAN'22 ,31 DEC '21,



# In[ ]:



calculate high close diff points and low close diff points



# In[ ]:



df_SBIN



# In[ ]:



# df_KOTAK = df_KOTAK.between_time('09:15', '15:25')

# df_KOTAK