def get_futures_data_LTP(symbol): current_date = datetime.datetime.now().date() current_year = current_date.year current_month = current_date.month current_expiry_date = get_expiry_date(year=current_year, month=current_month) if current_date > current_expiry_date: current_expiry_date = get_expiry_date(year=current_year, month=current_month+1) next_expiry_date = get_expiry_date(year=current_year, month=current_month + 2) else: next_expiry_date = get_expiry_date(year=current_year, month=current_month + 1) current_exp_quote = get_quote(symbol=symbol, instrument='FUTSTK', expiry=current_expiry_date, strike=450.00) next_exp_quote = get_quote(symbol=symbol, instrument='FUTSTK', expiry=next_expiry_date, strike=450.00) print(current_exp_quote) print(next_exp_quote) # current_exp_quote_lastPrice = current_exp_quote['lastPrice'] # next_exp_quote_lastPrice = next_exp_quote['lastPrice'] # make current_expiry_ltp = current_month_buyPrice1 from spread # make next_expiry_ltp = next_month_sellPrice1 from spread # becasue this will be only the sell spread: Buying current month, selling next month current_exp_quote_lastPrice = current_exp_quote['sellPrice1'] next_exp_quote_lastPrice = next_exp_quote['buyPrice1'] print(current_exp_quote_lastPrice) print(next_exp_quote_lastPrice) if current_exp_quote_lastPrice == '-': current_exp_quote_lastPrice = 0.0 if next_exp_quote_lastPrice == '-': next_exp_quote_lastPrice = 0.0 if current_exp_quote_lastPrice > 1 and next_exp_quote_lastPrice > 1: return current_exp_quote_lastPrice, next_exp_quote_lastPrice; else: return 0.0, 0.0;
def getDerivativeHistoryPrice(): df_comp = get_symbol() for row in df_comp[['CompanyId', 'Symbol']].iterrows(): try: result = row[1] symbol_name = str(result.get(key='Symbol')) company_id = int(result.get(key='CompanyId')) endDt = datetime.now().date() previousExipry = max( get_expiry_date(year=endDt.year, month=(endDt.month - 1))) currentExpiry = max( get_expiry_date(year=endDt.year, month=endDt.month)) print(company_id, symbol_name, endDt, previousExipry, currentExpiry) df_prices = get_history(symbol='TCS', start=date(previousExipry.year, previousExipry.month, (previousExipry.day + 1)), end=date(endDt.year, endDt.month, endDt.month), futures=True, expiry_date=date(currentExpiry.year, currentExpiry.month, currentExpiry.day)) df_prices['CompanyId'] = company_id print(df_prices.head()) except Exception as e: print('error', e)
def fetch_data(sym, expiry, start_date): df_next_temp = pd.DataFrame() df_curr_temp = pd.DataFrame() df_combined_temp = pd.DataFrame() if sym == "NIFTY" or sym == "BANKNIFTY": # add OR cond. for additional index index_bool = True else: index_bool = False curr_fut_data = get_history(symbol=sym, start=start_date, end=expiry, index=index_bool, futures=True, expiry_date=expiry) if expiry.month == 12: next_expiry = get_expiry_date(expiry.year + 1, 1) else: next_expiry = get_expiry_date(expiry.year, expiry.month + 1) # bug in nse site fetching wrong expiry for march, 2018 if next_expiry == date(2018, 3, 29): next_expiry = date(2018, 3, 28) next_fut_data = get_history(symbol=sym, start=start_date, end=expiry, index=index_bool, futures=True, expiry_date=next_expiry) df_curr_temp = curr_fut_data[["Last", "Close"]].copy() df_next_temp = next_fut_data[["Last", "Close"]].copy() df_curr_temp.reset_index(drop=False, inplace=True) df_curr_temp['Date'] = pd.to_datetime(df_curr_temp['Date']) df_curr_temp.set_index('Date', inplace=True) df_next_temp.reset_index(drop=False, inplace=True) df_next_temp['Date'] = pd.to_datetime(df_next_temp['Date']) df_next_temp.set_index('Date', inplace=True) # you can add/remove columns here df_combined_temp["Curr Close"] = df_curr_temp["Close"] # df_combined_temp["Curr Last"] = df_curr_temp["Last"] df_combined_temp["Next Close"] = df_next_temp["Close"] # df_combined_temp["Next Last"] = df_next_temp["Last"] return df_combined_temp
def fetch_nifty_options_data(args): year = args[0] month = args[1] logging.info(f"Start {year}-{month}") """ if "Date" in pd.read_csv(f"csv/{year}-{month}.csv").columns: logging.info(f"Done {year}-{month}") return """ try: df = pd.DataFrame() exp_dates = get_expiry_date(year=year, month=month) logging.info(f"Exp dates for {year}-{month}: {str(exp_dates)}") for exp_date in exp_dates: start_date = exp_date - timedelta(days=30 * 4) for i in range(0, 34000, 100): df_t = get_history(symbol="BANKNIFTY", start=start_date, end=exp_date, index=True, option_type='CE', strike_price=i, expiry_date=exp_date) df_t["Date"] = df_t.index df = pd.concat([df, df_t]) df.to_csv(f"csv/banknifty/{year}-{month}.csv", index=False) logging.info(f"Done {year}-{month}") except Exception as e: logging.error(f"Error: {year}-{month}") logging.error(e, exc_info=True) time.sleep(1) fetch_nifty_options_data(args)
def get_data(contract): df_combined = pd.DataFrame() month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] #add year at new year. Check in console whether old data is still fetched #if not fetched remove it and make suitable logic changes in line 59 year = [2016, 2017, 2018] #if you have already run this program before,change the filename below to # avoid overwriting filename = "_calendar_spread.xlsx" filename = contract + filename now_d = date.today() writer = pd.ExcelWriter(filename, engine='xlsxwriter') for yy in year: for mm in month: expiry = get_expiry_date(year=yy, month=mm) if expiry == date(2016, 1, 28): start_date = expiry + timedelta(days=1) else: #bug in nse site resulting in wrong expiry for march, 2018 #can be removed if fixed by nse # run get_expiry_date(2018, 3) in console to verify if expiry == date(2018, 3, 29): expiry = date(2018, 3, 28) df_combined = df_combined.append( fetch_data(contract, expiry, start_date)) start_date = expiry + timedelta(days=1) if start_date > now_d: df_combined.to_excel(writer, contract) break writer.save()
def getExpirydate(year,month, week,weekly, symbol): if symbol =='BANKNIFTY' and weekly: expiry = date(year,month,17) else : expiry = get_expiry_date(year=year, month=month) return expiry
def get_symbol_futures_price(sym, month_val ): today=date(datetime.now().year,datetime.now().month,datetime.now().day) day1=datetime(datetime.now().year,datetime.now().month,1) - timedelta(days=1) day1=get_expiry_date(datetime.now().year,day1.month) year_val = datetime.now().year if month_val == 13 : month_val = 1 year_val += 1 stock_fut = get_history(symbol=sym, start=day1, end=today, futures=True, expiry_date=get_expiry_date(year_val,month_val)) return float(stock_fut['Last'][-1])
def get_all_expirty_date_within_range(from_date,to_date): num_months = (to_date.year - from_date.year) * 12 + (to_date.month - from_date.month) + 2 current_date = from_date monthly_expiries = [] for month in range(0,num_months): monthly_expiries.append(get_expiry_date(current_date.year,current_date.month,False,True)) next_month = calendar.nextmonth(current_date.year,current_date.month) current_date = datetime(next_month[0],next_month[1],1).date() flttern_monthly_expiries = list(itertools.chain.from_iterable(monthly_expiries)) return flttern_monthly_expiries
def get_expiry_date_wrapper(year, month, index=True, stock=False, vix=False, recursion=0): return get_expiry_date(year=year, month=month, index=index, stock=stock, vix=vix, recursion=recursion)
def get_index_futures(self,expiry_year,expiry_month): expiry=get_expiry_date(expiry_year,expiry_month) df= pd.DataFrame() for i in range(8): if len(df)==0: try: df=history.get_history(self.stock,self.start,self.end,\ futures=True,index=True,\ expiry_date=expiry-timedelta(i)) except: continue return df
def get_index_call(self,expiry_year,expiry_month,strike): expiry=get_expiry_date(expiry_year,expiry_month) df= pd.DataFrame() for i in range(8): if len(df)==0: try: df=history.get_history(self.stock,self.start,self.end,\ option_type='CE',index=True,\ expiry_date=expiry-timedelta(i),strike_price=strike) except: continue return df
def get_expiry(year=2018, sm=1, m=12): ''' :param year: start year for contracts :param sm: start month for contract :param m: total month wise contracts :return: list of expiry till current month + 2 ''' from nsepy.derivatives import get_expiry_date now = datetime.now() y_now = now.year expiry = [] try: if year < y_now: expiry.extend( list( map(lambda x: get_expiry_date(year=year, month=x), range(sm, m + 1)))) year = year + 1 while (year < y_now): expiry.extend( list( map(lambda x: get_expiry_date(year=year, month=x), range(1, m + 1)))) year = year + 1 if year == y_now: expiry.extend( list( map( lambda x: get_expiry_date(year=y_now, month=x) if x <= now.month + 2 else None, range(1, m + 1)))) except Exception as e: print(e) if len(expiry) > 0: while (expiry[-1] == None): expiry.pop(-1) return (expiry) else: print("not sufficient expiry dates", expiry)
def get_list_of_futures_price_for_next_months(symbol): global totalprofit if not nse.is_valid_code(symbol): print ("Error : " + symbol + "is not valid") sys.exit(-1) month={1:"JAN", 2:"FEB",3:"MAR",4:"APR",5:"MAY",6:"JUN",7:"JUL",8:"AUG",9:"SEP",10:"OCT",11:"NOV",12:"DEC"}; lot_size = nse.get_fno_lot_sizes(cached=False)[symbol] #if lot_size < 1000 or lot_size > 10000: # return day_val = get_expiry_date(datetime.now().year,datetime.now().month) val = get_symbol_futures_price(symbol, datetime.now().month ) val_next_month = get_symbol_futures_price(symbol, datetime.now().month + 1) spot_price = nse.get_quote(symbol)['lastPrice'] diff_with_spot_price_curr_month = spot_price - val if diff_with_spot_price_curr_month < 0: diff_with_spot_price_curr_month = val - spot_price diff_with_spot_price_next_month = spot_price - val_next_month if diff_with_spot_price_next_month < 0: diff_with_spot_price_next_month = val_next_month - spot_price diff = val - val_next_month if diff < 0: diff = val_next_month - val expectedprofit = diff * lot_size if expectedprofit < 5000 : return totalprofit += int(expectedprofit) print("Symbol : "+symbol + " Lot Size : " + str(lot_size)) print("===================") print("Stock Price : "+str(spot_price)) print(month[datetime.now().month] + " Fut Price : " + str(val)) print(month[(datetime.now() + relativedelta( months=+1)).month] + " Fut Price : " + str(val_next_month)) print("Expected Profit: " + str(expectedprofit)) print("************************************************************")
def func_run_parallel(i): global curr_frames global near_frames curr_day = today - relativedelta(days=i) curr_day_str = curr_day.strftime('%Y-%m-%d') # print(curr_day_str) expiry_date_curr_month = list( get_expiry_date(curr_day.year, curr_day.month)) # try: # with time_limit(1): data_curr_month = downloadHistoricDataForFuture( symbol, start_date=curr_day, end_date=curr_day, expiry_date=max(expiry_date_curr_month), index_flag=index_flag) data_curr_month.reset_index(level=0, inplace=True) # print(curr_frames) curr_frames.append(data_curr_month) # except TimeoutException as e: # print("Timed out!") near_day = curr_day + relativedelta(months=1) expiry_date_near_month = list( get_expiry_date(near_day.year, near_day.month)) # try: # with time_limit(1): data_near_month = downloadHistoricDataForFuture( symbol, start_date=curr_day, end_date=curr_day, expiry_date=max(expiry_date_near_month), index_flag=index_flag) data_near_month.reset_index(level=0, inplace=True) # print(near_frames) near_frames.append(data_near_month)
def GetCurrentWeekExpiry(): now = datetime.now().date() curr_year = now.year curr_month = now.month month_expiries = get_expiry_date(year=curr_year, month=curr_month) closest_expiry = datetime(2050, 1, 1).date() month_expiries.add(datetime(2021, 4, 22).date()) for date in month_expiries: if date >= now: closest_expiry = min(date, closest_expiry) expiry_year = str(closest_expiry.year)[2:] expiry_month = str(closest_expiry.month).replace('0', '') expiry_day = str(closest_expiry.day) expiry = expiry_year + expiry_month + expiry_day return expiry
def main(self, args): from_date = Utils.get_date_from_string(args.beginDate) to_date = Utils.get_date_from_string(args.endDate) try: expiry_date = None if args.expiryDate is None else Utils.get_date_from_string( args.expiryDate) except ValueError: expiry_month_year = Utils.get_month_year_from_string( args.expiryDate) expiry_date = list( get_expiry_date(year=expiry_month_year.year, month=expiry_month_year.month))[0] strike_price = None if args.strike_price is None else int( args.strike_price) if args.optionType is None: data = get_history(symbol=args.symbol, start=from_date, end=to_date, index=args.index, futures=args.futures, expiry_date=expiry_date, strike_price=strike_price) else: data = get_history(symbol=args.symbol, start=from_date, end=to_date, index=args.index, futures=args.futures, expiry_date=expiry_date, option_type=args.optionType, strike_price=strike_price) print(data) if args.no_graph is False and len(data) > 0: Utils.plot_graph(args.plotType, data)
def get_data(): # try to keep the number of contracts less than 8 for optimal performance symbols = ["ZEEL"] month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] # add year at new year. Check in console whether old data is still fetched # if not fetched remove it and make suitable logic changes in line 95 year = [2018, 2019] # if you have already run this program before,change the filename below to # avoid overwriting filename = "calendar_spread.xlsx" now_d = date.today() writer = pd.ExcelWriter(filename, engine='xlsxwriter') for sym in symbols: df_combined = pd.DataFrame() for yy in year: for mm in month: expiry = get_expiry_date(year=yy, month=mm) if expiry == date(2018, 1, 25): start_date = expiry + timedelta(days=1) else: # bug in nse site resulting in wrong expiry for march, 2018 # can be removed if fixed by nse # run get_expiry_date(2018, 3) in console to verify if expiry == date(2018, 3, 29): expiry = date(2018, 3, 28) df_combined = df_combined.append(fetch_data(sym, expiry, start_date)) start_date = expiry + timedelta(days=1) if start_date > now_d: df_combined.to_excel(writer, sym) break writer.save()
from nsepy.liveurls import quote_eq_url, quote_derivative_url, option_chain_url from nsepy.derivatives import get_expiry_date import requests, bs4 import glob, datetime, os, sys import time from datetime import datetime import sqlalchemy import pyodbc import urllib from sqlalchemy.sql import text as sa_text symbols_stock = ['YESBANK','MARUTI','RELIANCE','BPCL','HDFC','SBIN','HDFCBANK','ICICIBANK','TATAMOTORS','BAJFINANCE','TATASTEEL','AXISBANK','TCS','TITAN','INFY','ITC','LT'] symbols_index = ['NIFTY'] sector_dict = {'YESBANK':'Banking','MARUTI':'Automobile','RELIANCE':'Energy','BPCL':'Energy','HDFC':'Financial Services','SBIN':'Banking','HDFCBANK':'Banking','ICICIBANK':'Banking','TATAMOTORS':'Automobile','BAJFINANCE':'Financial Services','TATASTEEL':'Metals','AXISBANK':'Banking','TCS':'Information Technology','TITAN':'Consumer Goods','INFY':'Information Technology','ITC':'Consumer Goods','LT':'Construction'} month_list = [10, 11, 12] expiries = [get_expiry_date(year=2019, month=k) for k in month_list] eq_quote_referer = "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol={}&illiquid=0&smeFlag=0&itpFlag=0" derivative_quote_referer = "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying={}&instrument={}&expiry={}&type={}&strike={}" option_chain_referer = "https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=-9999&symbol=NIFTY&symbol=BANKNIFTY&instrument=OPTIDX&date=-&segmentLink=17&segmentLink=17" quoted = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 11.0};SERVER=NKS\SQLEXPRESS;DATABASE=Derivative Analysis;Trusted_Connection=yes;") engine = sqlalchemy.create_engine('mssql+pyodbc:///?odbc_connect={}'.format(quoted)) def refresh_table(df, table_name): df.to_sql(table_name, con = engine, if_exists = 'append', index=False) print ("Refreshed "+ table_name) def get_quote(symbol, series='EQ', instrument=None, expiry=None, option_type=None, strike=None): """ 1. Underlying security (stock symbol or index name)
def plotTheGraph(stock): today = date.today() currentExpiryDate = get_expiry_date(today.year, today.month) # if(today>currentExpiryDate): # currentExpiryDate = get_expiry_date(today.year,today.month+1) # prevExpiryDate = get_expiry_date(today.year,today.month) # futureExpiryDate = get_expiry_date(today.year, today.month + 2) # else : # currentExpiryDate = get_expiry_date(today.year, today.month) # prevExpiryDate = get_expiry_date(today.year, today.month-1) # if(today.month != 12): # futureExpiryDate = get_expiry_date(today.year, today.month + 1) # else: # futureExpiryDate = get_expiry_date(today.year+1, 1) currentExpiryDateSet = get_expiry_date(today.year, today.month) prevExpiryDateSet = get_expiry_date(today.year, today.month - 1) if (today.month != 12): futureExpiryDateSet = get_expiry_date(today.year, today.month + 1) else: futureExpiryDateSet = get_expiry_date(today.year + 1, 1) currentExpiryDate = sorted(currentExpiryDateSet, reverse=True)[0] prevExpiryDate = sorted(prevExpiryDateSet, reverse=True)[0] futureExpiryDate = sorted(futureExpiryDateSet, reverse=True)[0] start = prevExpiryDate - timedelta(15) end = today #date(2018,12,27) print('start', start) print('end', end) end2 = today index = False future = True instrument = 'FUTSTK' instrumentArray = ['NIFTY', 'BANKNIFTY'] if stock in instrumentArray: instrument = 'FUTIDX' index = True future = False # Live Data liveData = get_quote(symbol=stock, instrument=instrument, expiry=currentExpiryDate, option_type='-', strike=700) #Pivot table code liveData = pd.DataFrame.from_dict(liveData, orient='index') # liveData = pd.DataFrame(liveData,columns=['column','values']) # liveData['index'] = np.arange(len(liveData)) # liveData = liveData.reset_index().set_index('index') # liveData.pivot(columns='level_0',values='0') # print(liveData) ltp = liveData.get('ltp', 0) volume = liveData.get('numberOfContractsTraded', 0) # print("Today's volume: ",volume) chngOI = liveData.get('changeinOpenInterest', 0) # print("Today's OI: ",chngOI) # print("live: ",liveData) # print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") data_fut = get_history(symbol=stock, index=index, futures=future, start=start, end=end, expiry_date=currentExpiryDate) data_fut2 = get_history(symbol=stock, index=index, futures=future, start=start, end=end2, expiry_date=futureExpiryDate) # pd.DataFrame(data_fut, index=data_fut['Date'].strftime("%b %d")).style.format("{:.2}") # pd.DataFrame(data_fut2, index=data_fut..strftime("%b %d")).style.format("{:.2}") if instrument == 'FUTSTK': OI_combined = pd.concat( [data_fut2['Open Interest'], data_fut['Open Interest']], axis=1, sort=False) OI_combined['OI_Combined'] = OI_combined.sum(axis=1) + int(chngOI) # print("Open Interest: ",OI_combined['OI_Combined']) # OI_combined['OI_Combined'] = OI_combined['OI_Combined'] + chngOI # No. of contracts volume_combined = pd.concat([ data_fut2['Number of Contracts'], data_fut['Number of Contracts'] ], axis=1, sort=False) volume_combined['volume_combined'] = volume_combined.sum( axis=1) + int(volume) # print("Volume : ",volume_combined['volume_combined']) # print("volume_combined['volume_combined'] = ",volume_combined['volume_combined']) # volume_combined['volume_combined'] = volume_combined['volume_combined'] + volume else: # No. of contracts volume_combined = pd.concat([data_fut2['Volume'], data_fut['Volume']], axis=1, sort=False) volume_combined['volume_combined'] = volume_combined.sum(axis=1) # print("volume_combined['volume_combined'] = ",volume_combined['volume_combined']) # volume_combined['volume_combined'] = volume_combined['volume_combined'] + volume # Rule to throw an alert # Display Underlying plt.subplot(222) plt.title('Underlying') # priceCombined= pd.concat([data_fut['Underlying'],data_fut2['Underlying']],axis=1,sort=False) # plt.plot(priceCombined,color='green') plt.plot(data_fut['Last'], color='green') # plt.plot(pd.concat(data_fut['Last'],liveData.get('ltp',0)), color='green') plt.legend(['Last']) plt.xlabel('Date') plt.ylabel('Price') # plt.xlabel.set_major_formatter(mdates.DateFormatter('%b %d')) # Display Volumes plt.subplot(224) plt.title('Volume') plt.plot(volume_combined.volume_combined, label='Volume', color='blue') plt.plot(volume_combined.volume_combined.rolling(5).mean(), label='Volume', color='orange') plt.legend(['Volume', 'Volume_mean']) plt.xlabel('Date') plt.ylabel('Volume') # plt.xlabel.set_major_formatter(mdates.DateFormatter('%b %d')) # Display Cumulative Open Interest plt.figure(1, figsize=(10, 9)) plt.subplot(221) plt.title('Open Interest') plt.plot(OI_combined.OI_Combined, label='OI') plt.plot(OI_combined.OI_Combined.rolling(5).mean(), label='OI') plt.xlabel('Date') plt.ylabel('Open Interest') # plt.xlabel.set_major_formatter(mdates.DateFormatter('%b %d')) fig = pylab.gcf() fig.canvas.set_window_title(stock) plt.legend(['OI', 'OI_mean']) plt.show() stock = input("Enter the Future Symbol here: ") if stock == "": plt.close(fig) return else: plt.close(fig) plotTheGraph(stock=stock)
from datetime import date, datetime, timedelta from nsepy import get_history import matplotlib.pyplot as plt from nsepy.derivatives import get_expiry_date now = datetime.now() today = now.date() today = today + timedelta(days=30) # options expire on last thursday of every month p1_expiry = get_expiry_date(year=today.year, month=today.month - 1) p2_expiry = get_expiry_date( year=today.year, month=today.month - 2) + timedelta(days=1) print(p1_expiry, p2_expiry) symbol = "NIFTY" close = 'Close' print(symbol) df = get_history( symbol=symbol, index=True, strike_price=9600, option_type='CE', start=p2_expiry, end=p1_expiry, expiry_date=p1_expiry, ) df = df.sort_index(ascending=True) print(df[[close, 'Underlying']])
print(data_spot_14_days_back.Deliverble[count]) count_this_week = count_this_week +1 print("Coun this week=", count_this_week) thisWeekAvgDeliverble = sum_this_week/count_this_week print(thisWeekAvgDeliverble) data_spot_7_days_back = get_history(symbol=str(sys.argv[1]),start=start_7,end=date(today.year,today.month,today.day)) dataSpotLength = data_spot_7_days_back.size/15 dataSpotRecords = int(dataSpotLength) -1 data_futures= get_history(symbol=str(sys.argv[1]),start=date(today.year,10,10),end=date(today.year,today.month,today.day),futures=True,expiry_date=get_expiry_date(year = today.year,month = today.month)) Length = data_futures.size print(Length) print(data_futures.columns) numRecords = Length/13 #number of fields in column = 13 NumOfRecords = int(numRecords) -1 print(NumOfRecords) if data_futures.Open[NumOfRecords] > data_futures.Close[NumOfRecords -1] and data_futures.Open[NumOfRecords] > data_futures.High[NumOfRecords -1]: print(data_futures.Symbol, " closed above yesterdays close and also breached yesterdays high") else: print(data_futures.Symbol, " closed above yesterdays high") #f data.Open() > data
def numberofDaysToExpiery(year, month): today = datetime.date.today() expiry = get_expiry_date(year=year, month=month) someday = expiry diff = someday - today return diff.days
### columns for the data frame which will store the below ADF statitics for all co-integrated pairs cols = ['Pair(y)','Pair(x)','ADF Test Statistics','ADF p-value', 'ADF at 1%','ADF at 5%','ADF at 10%', 'Status'] lst =[] ### loop to download the data and then check for co-integration for each symbol-pair for i in symbol_pairs: try: symb = i print("FOR THE PAIR => ",symb) # downloading the historical data from NSE: y= ns.get_history(symbol= symb[0], start= date(2016,10,10), end= date(2018,10,10), futures= True, expiry_date= nsd.get_expiry_date(2018,10)) x= ns.get_history(symbol= symb[1], start= date(2016,10,10), end= date(2018,10,10), futures= True, expiry_date= nsd.get_expiry_date(2018,10)) #to make sure Dataframes are the same length min_date= max(df.dropna().index[0] for df in [y,x]) max_date= min(df.dropna().index[-1] for df in [y,x]) y= y[(y.index>= min_date) & (y.index <= max_date)] x= x[(x.index>= min_date) & (x.index <= max_date)] # to run ordinary Least Squares regression to fing hedge ratio and then to create spread series df1= pd.DataFrame({'y':y['Close'],'x':x['Close']}) est= sm.OLS(df1.y,df1.x) est= est.fit()
def get_futures_data_continous(symbol, start_date, end_date): # try to keep the number of contracts less than 8 for optimal performance month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] start_month = start_date.month end_month = end_date.month start_year = start_date.year end_year = end_date.year # add year at new year. Check in console whether old data is still fetched # if not fetched remove it and make suitable logic changes in line 95 year = [2018, 2019] # if you have already run this program before,change the filename below to # avoid overwriting path_local = "../data/data_calendarSpreads/" + symbol + ".xlsx" path_heroku = "/app/data/data_calendarSpreads/" + symbol + ".xlsx" file = path_local if os.path.isfile(path_local) else path_heroku exists = os.path.isfile(file) if exists: writerA = pd.ExcelWriter(file, engine='openpyxl') stock_data_df = pd.read_excel(file, sheet_name=symbol) last_record = stock_data_df['Date'].tail(1) last_record_date_time = last_record.iloc[0] last_record_date = last_record_date_time.to_pydatetime().date() current_date = datetime.datetime.now().date() current_year = current_date.year current_month = current_date.month print(last_record_date, current_date- timedelta(days=1)) if last_record_date < current_date - timedelta(days=1): # if current_date > current_expiry_date: # current_expiry_date = get_expiry_date(year=current_year, month=current_month+1) expiry = get_expiry_date(year=current_year, month=current_month) if expiry < last_record_date: expiry = get_expiry_date(year=current_year, month=current_month + 1) print(symbol, expiry, last_record_date + timedelta(days=1)) new_data = fetch_data(symbol, expiry, last_record_date + timedelta(days=1)) # stock_data_df.append(fetch_data(symbol, expiry, last_record_date + timedelta(days=1))) if (len(new_data.index) > 0): append_df_to_excel(file, new_data, sheet_name=symbol) else: print(symbol, expiry, last_record_date + timedelta(days=1)) new_data = fetch_data(symbol, expiry, last_record_date + timedelta(days=1)) # stock_data_df.append(fetch_data(symbol, expiry, last_record_date + timedelta(days=1))) if (len(new_data.index) > 0): append_df_to_excel(file, new_data, sheet_name=symbol) else: writer = pd.ExcelWriter(file, engine='xlsxwriter') df_combined = pd.DataFrame() for yy in year: for mm in month: if ((mm >= start_month and yy == start_year) or yy > start_year): expiry = get_expiry_date(year=yy, month=mm) if expiry == date(2018, 1, 25): start_date = expiry + timedelta(days=1) else: # bug in nse site resulting in wrong expiry for march, 2018 # can be removed if fixed by nse # run get_expiry_date(2018, 3) in console to verify if expiry == date(2018, 3, 29): expiry = date(2018, 3, 28) df_combined = df_combined.append(fetch_data(symbol, expiry, start_date)) start_date = expiry + timedelta(days=1) if start_date > end_date: df_combined.to_excel(writer, symbol) break # writer.save() if (len(df_combined.index) > 200): writer.save()
niftyCodes['index'] = np.arange( len(niftyCodes)) # adding a column to assign row number # print(niftyCodes) niftyCodes = niftyCodes.reset_index().set_index('index') # print(niftyCodes) niftyCodes = niftyCodes.iloc[:, [0, 3, 4]] # Choosing 0th, 3rd, 4th column of df # print(niftyCodes) return niftyCodes nifty_codes = get_nifty_scrips(niftyCodesPath) print(nifty_codes[2:]) today = date.today() currentExpiryDateSet = get_expiry_date(today.year, today.month) currentExpiryDate = sorted(currentExpiryDateSet, reverse=True)[0] def fetch_periodic_data(nifty_codes): sourceFile = open(dyn_file_name, 'a+') source_file_consolidate = open('LiveDataConsolidated.txt', 'w') print(fmt.format('UNDERLYING', 'TIMESTAMP', 'OPEN', 'HIGH', 'LOW', 'LAST', 'ULAST', 'VOL', 'AVGVOL', 'VOLSGNL', 'CHOI', '%PREM', '%PR', '%OI', '%VOL', 'MON_INV_MLL'), file=source_file_consolidate) sourceFile.flush() ts = time.time() st = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') print("fetchPeriodicData started @ ", st)
from datetime import date, datetime, timedelta from nsepy import get_history import matplotlib.pyplot as plt from nsepy.derivatives import get_expiry_date now = datetime.now() today = now.date() today = today + timedelta(days=30) # options expire on last thursday of every month p1_expiry = get_expiry_date(year=today.year, month=today.month - 1) p2_expiry = get_expiry_date(year=today.year, month=today.month - 2) + timedelta(days=1) print(p1_expiry, p2_expiry) symbol = "NIFTY" close = 'Close' print(symbol) df = get_history( symbol=symbol, index=True, strike_price=9600, option_type='CE', start=p2_expiry, end=p1_expiry, expiry_date=p1_expiry, )
from nsepy import get_history from datetime import date import pandas as pd from nsepy.derivatives import get_expiry_date expiry = get_expiry_date(year=2015, month=12) print expiry tcs = get_history(symbol='TCS',start=date(2015,1,1),end=date(2015,12,31)) print tcs tcs.insert(0, 'Date', pd.to_datetime(tcs.index,format='%Y-%m-%d') ) type(tcs.index) c = type(tcs.Date) print c tcs.to_csv('tcs_stock.csv', encoding='utf-8', index=False) ############################################################# infy = get_history(symbol='INFY',start=date(2015,1,1),end=date(2015,12,31)) print infy infy.insert(0,'Date',pd.to_datetime(infy.index,format='%Y-%m-%d')) print(type(infy.index)) print(type(infy.Date)) infy.Date.dt infy.to_csv('infy_stock.csv', encoding='utf-8', index=False) ############################################################# nifty_it = get_history(symbol="NIFTYIT",start=date(2015,1,1),end=date(2015,12,31),index=True) print nifty_it nifty_it.insert(0, 'Date', pd.to_datetime(nifty_it.index,format='%Y-%m-%d')) print(type(nifty_it.index)) print(type(nifty_it.Date)) nifty_it.Date.dt nifty_it.to_csv('nifty_it_index.csv', encoding='utf-8', index=False)
def data_downloader_FnO_daily(day_range): with open('/home/parallax/algo_trading/app/wishlist.csv', 'r') as wishlist: symbol_count = 0 csv_reader = csv.reader(wishlist) for element in csv_reader: symbol = element[0] symbol.replace('_', " ") print(symbol) today = date.today() symbol_count = symbol_count + 1 first_day = today i = 0 curr_frames = [] near_frames = [] existing_current_month_data = pd.read_csv( '/home/parallax/algo_trading/app/F&O_data/' + symbol + '_curr_month.csv') existing_near_month_data = pd.read_csv( '/home/parallax/algo_trading/app/F&O_data/' + symbol + '_near_month.csv') while (i < day_range): curr_day = today - relativedelta(days=i) curr_day_str = curr_day.strftime('%Y-%m-%d') if ((curr_day_str not in existing_near_month_data['Date'].tolist()) or (curr_day_str not in existing_current_month_data['Date'].tolist())): expiry_date_curr_month = list( get_expiry_date(curr_day.year, curr_day.month)) data_curr_month = downloadHistoricDataForFuture( symbol, start_date=curr_day, end_date=curr_day, expiry_date=max(expiry_date_curr_month)) data_curr_month.reset_index(level=0, inplace=True) curr_frames.append(data_curr_month) near_day = curr_day + relativedelta(months=1) expiry_date_near_month = list( get_expiry_date(near_day.year, near_day.month)) data_near_month = downloadHistoricDataForFuture( symbol, start_date=curr_day, end_date=curr_day, expiry_date=max(expiry_date_near_month)) data_near_month.reset_index(level=0, inplace=True) near_frames.append(data_near_month) # break i = i + 1 curr_frames.append(existing_current_month_data) near_frames.append(existing_near_month_data) curr_month_data = pd.concat(curr_frames) curr_filename = "/home/parallax/algo_trading/app/F&O_data/" + symbol + "_curr_month.csv" curr_month_data.to_csv(curr_filename, index=False) near_month_data = pd.concat(near_frames) near_filename = "/home/parallax/algo_trading/app/F&O_data/" + symbol + "_near_month.csv" near_month_data.to_csv(near_filename, index=False) print("data downloaded for " + symbol) # break; return "success"
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Apr 28 14:31:44 2018 @author: sanjotraibagkar """ from datetime import date from nsepy.history import get_price_list from nsepy import get_history from datetime import date, timedelta import pandas as pd from matplotlib import pyplot as plt import datetime from nsepy.derivatives import get_expiry_date import numpy as np from utility import * prices = get_price_list(dt=date(2018, 4, 27)) print(prices) from nsepy.derivatives import get_expiry_date expiry = get_expiry_date(year=2018, month=3) print(expiry) li.get_option_chain('NIFTY', instrument='OPTIDX', expiry=expiry) Option_data_NIFTY.csv
import os from datetime import date from datetime import datetime from nsepy import get_history from nsepy.derivatives import get_expiry_date today = datetime.today() data = [] file = open('scripts.txt', 'r') for line in file: #int (line) data.append( get_history(symbol=line, start=date(today.year, today.month, today.day), end=date(today.year, today.month, today.day), futures=True, expiry_date=get_expiry_date(year=today.year, month=today.month + 1))) print(len(data)) print(data.pop(0), data.pop(1)) for line in file: print(line in data)