from breeze_connect import BreezeConnect
from datetime import datetime,date,time,timedelta
import pandas as pd
import numpy as np
from credentials001 import *
import requests
import datetime
import time
from nsepython import *
from datetime import datetime

requests.packages.urllib3.util.connection.HAS_IPV6 = False

currentExpiry = nse_expirydetails(nse_optionchain_scrapper('BANKNIFTY'), 0)[0]
# print(currentExpiry)

breeze = BreezeConnect(api_key=api_key)
breeze.generate_session(api_secret=api_secret, session_token=session_token)

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None)

bn = breeze.get_quotes(stock_code="CNXBAN",
                       exchange_code="NSE",
                       expiry_date="",
                       product_type="cash",
                       right="others",
                       strike_price="0")
df = pd.DataFrame(bn['Success'])
# print(df)
actual_ltp = df[df['exchange_code'] == 'NSE']['ltp']
# print(actual_ltp)
rounded_ltp = round(actual_ltp, -2).to_string(index=False)
rounded_ltp = pd.to_numeric(rounded_ltp).astype(int)
# print("NIFTY BANK LTP:",rounded_ltp)

p =100
# Call - Premium of 100 - Strikeprice
dfce=[]
for i in range(rounded_ltp, rounded_ltp + 2000, 100):
    bnc = breeze.get_quotes(stock_code="CNXBAN",
                            exchange_code="NFO",
                            expiry_date=currentExpiry.strftime('%Y-%m-%dT06:00:00.000Z'),
                            product_type="options",
                            right="call",
                            strike_price=i)

    df = pd.DataFrame(bnc['Success'])
    # print(df)
    dfce.append(df)
dfce = pd.concat(dfce)
# print(dfce)
# print(dfce['ltp'])
dfce['sq.ltp'] = (dfce['ltp'] - p) * (dfce['ltp'] - p)
# print(df1)
dfce['sq.ltp.min']=dfce['sq.ltp'].min()
cestrikeprice = dfce.loc[dfce['sq.ltp'] == dfce['sq.ltp.min'], 'strike_price'].to_string(index=False)
ltpCE100 = dfce.loc[dfce['sq.ltp'] == dfce['sq.ltp.min'], 'ltp'].to_string(index=False)
# print("CE Price of LTP 100:",strikepriceCE100,ltpCE100)

# Put - Premium of 100 - Strikeprice
dfpe=[]
for j in range(rounded_ltp-2000, rounded_ltp, 100):
    bnp = breeze.get_quotes(stock_code="CNXBAN",
                            exchange_code="NFO",
                            expiry_date=currentExpiry.strftime('%Y-%m-%dT06:00:00.000Z'),
                            product_type="options",
                            right="put",
                            strike_price=j)

    df = pd.DataFrame(bnp['Success'])
    # print(df)
    dfpe.append(df)
dfpe = pd.concat(dfpe)
# print(dfpe)
dfpe['sq.ltp'] = (dfpe['ltp'] - p) * (dfpe['ltp'] - p)
# print(df1)
dfpe['sq.ltp.min']=dfpe['sq.ltp'].min()
pestrikeprice = dfpe.loc[dfpe['sq.ltp'] == dfpe['sq.ltp.min'], 'strike_price'].to_string(index=False)
ltpPE100 = dfpe.loc[dfpe['sq.ltp'] == dfpe['sq.ltp.min'], 'ltp'].to_string(index=False)
# print("PE Price of LTP 100:",strikepricePE100,ltpPE100)

bnce = breeze.get_historical_data(interval="1minute", from_date=datetime.now().strftime('%Y-%m-%dT09:00:00.000Z'),
                                  to_date=datetime.now().strftime('%Y-%m-%dT16:00:00.000Z'), stock_code="CNXBAN", exchange_code="NFO",
                                  product_type="options",expiry_date=currentExpiry.strftime('%Y-%m-%dT06:00:00.000Z'),
                                  right="call",strike_price=cestrikeprice)

bnpe = breeze.get_historical_data(interval="1minute", from_date=datetime.now().strftime('%Y-%m-%dT09:00:00.000Z'),
                                  to_date=datetime.now().strftime('%Y-%m-%dT16:00:00.000Z'), stock_code="CNXBAN", exchange_code="NFO",
                                  product_type="options",expiry_date=currentExpiry.strftime('%Y-%m-%dT06:00:00.000Z'),
                                  right="put",strike_price=pestrikeprice)
dfce100 = pd.DataFrame(bnce['Success'])
# print(dfce100)
dfpe100 = pd.DataFrame(bnpe['Success'])
# print(dfpe100)

def rma(x, n, y0):
    a = (n-1) / n
    ak = a**np.arange(len(x)-1, -1, -1)
    return np.r_[np.full(n, np.nan), y0, np.cumsum(ak * x) / ak / n + y0 * a**np.arange(1, len(x)+1)]
#
dfce100['change'] = pd.to_numeric(dfce100['close']).diff()
dfce100['gain'] = dfce100.change.mask(dfce100.change < 0, 0.0)
dfce100['loss'] = -dfce100.change.mask(dfce100.change > 0, -0.0)
n = 14
dfce100['avg_gain'] = rma(dfce100.gain[n+1:].to_numpy(), n, np.nansum(dfce100.gain.to_numpy()[:n+1])/n)
dfce100['avg_loss'] = rma(dfce100.loss[n+1:].to_numpy(), n, np.nansum(dfce100.loss.to_numpy()[:n+1])/n)
dfce100['rs'] = dfce100.avg_gain / dfce100.avg_loss
dfce100['rsi_14'] = 100 - (100 / (1 + dfce100.rs))
# print(dfce100)
# dfce100['sma'] = dfce100['close'].rolling(10).mean()
dfce100['lowp'] = dfce100['low'].iloc[-2]
dfce100['highp'] = dfce100['high'].iloc[-2]
dfce100ltp = pd.to_numeric(dfce100['close'].iloc[-1])
dfceltp100tp = round(dfce100ltp * 1.1, -1)
dfpe100ltp = pd.to_numeric(dfpe100['close'].iloc[-1])
dfpeltp100tp = round(dfpe100ltp * 1.1, -1)

rsi14ceprev = dfce100['rsi_14'].iloc[-2]

dfce100['lowc'] = dfce100['low'].iloc[-1]
dfce100['highc'] = dfce100['high'].iloc[-1]
rsi14cecur = dfce100['rsi_14'].iloc[-1]

# print(dfce100[["open","high","low","close","datetime","rsi_14"]])
# print(rsi14cecur,rsi14ceprev)

dfpe100['change'] = pd.to_numeric(dfpe100['close']).diff()
dfpe100['gain'] = dfpe100.change.mask(dfpe100.change < 0, 0.0)
dfpe100['loss'] = -dfpe100.change.mask(dfpe100.change > 0, -0.0)
n = 14
dfpe100['avg_gain'] = rma(dfpe100.gain[n+1:].to_numpy(), n, np.nansum(dfpe100.gain.to_numpy()[:n+1])/n)
dfpe100['avg_loss'] = rma(dfpe100.loss[n+1:].to_numpy(), n, np.nansum(dfpe100.loss.to_numpy()[:n+1])/n)
dfpe100['rs'] = dfpe100.avg_gain / dfpe100.avg_loss
dfpe100['rsi_14'] = 100 - (100 / (1 + dfpe100.rs))
# print(dfce100)
# dfce100['sma'] = dfce100['close'].rolling(10).mean()
dfpe100['lowp'] = dfpe100['low'].iloc[-2]
dfpe100['highp'] = dfpe100['high'].iloc[-2]
rsi14peprev = dfpe100['rsi_14'].iloc[-2]

dfpe100['lowc'] = dfpe100['low'].iloc[-1]
dfpe100['highc'] = dfpe100['high'].iloc[-1]
rsi14pecur = dfpe100['rsi_14'].iloc[-1]

# print(dfce100[["open","high","low","close","datetime","rsi_14"]])
print(rsi14cecur,rsi14ceprev,rsi14pecur,rsi14peprev,dfce100ltp,dfpe100ltp,dfceltp100tp,dfpeltp100tp)

if rsi14ceprev < 30 and rsi14cecur > 30:

    print(breeze.place_order(stock_code="CNXBAN",
                       exchange_code="NFO",
                       product="options",
                       action="buy",
                       order_type="market",
                       stoploss="",
                       quantity="25",
                       price="",
                       validity="day",
                       validity_date=datetime.now().strftime('%Y-%m-%dT06:00:00.000Z'),
                       disclosed_quantity="0",
                       expiry_date=currentExpiry.strftime('%Y-%m-%dT06:00:00.000Z'),
                       right="call",
                       strike_price=cestrikeprice))

    print(breeze.place_order(stock_code="CNXBAN",
                       exchange_code="NFO",
                       product="options",
                       action="sell",
                       order_type="limit",
                       stoploss="",
                       quantity="25",
                       price=dfceltp100tp,
                       validity="day",
                       validity_date=datetime.now().strftime('%Y-%m-%dT06:00:00.000Z'),
                       disclosed_quantity="0",
                       expiry_date=currentExpiry.strftime('%Y-%m-%dT06:00:00.000Z'),
                       right="call",
                       strike_price=cestrikeprice))


if rsi14peprev < 30 and rsi14pecur > 30:

    print(breeze.place_order(stock_code="CNXBAN",
                       exchange_code="NFO",
                       product="options",
                       action="buy",
                       order_type="market",
                       stoploss="",
                       quantity="25",
                       price="",
                       validity="day",
                       validity_date=datetime.now().strftime('%Y-%m-%dT06:00:00.000Z'),
                       disclosed_quantity="0",
                       expiry_date=currentExpiry.strftime('%Y-%m-%dT06:00:00.000Z'),
                       right="put",
                       strike_price=pestrikeprice))

    print(breeze.place_order(stock_code="CNXBAN",
                       exchange_code="NFO",
                       product="options",
                       action="sell",
                       order_type="limit",
                       stoploss="",
                       quantity="25",
                       price=dfpeltp100tp,
                       validity="day",
                       validity_date=datetime.now().strftime('%Y-%m-%dT06:00:00.000Z'),
                       disclosed_quantity="0",
                       expiry_date=currentExpiry.strftime('%Y-%m-%dT06:00:00.000Z'),
                       right="put",
                       strike_price=pestrikeprice))

 

By AskMahe.com

Hi, AskMahe.com is the Brainchild of Mahendran Paramasivan who is interested in bringing the latest technology to the common people and corporates. We are a team and we are providing supports on Technology and innovation as per industrial standard. We are supporting with the help of analysis we made on Technology and innovation to help corporate companies and individuals. We bring the latest updates of technology and innovation in front of the eyes of our readers, clients, and people. We have resources in Cloud computing, IoT, Edge Computing, Dew Computing, 5G, SAP, Web Designing, UI/ UX Design. Regards AskMahe Team askmahe.com