def __fetch_data(self): history_data = get_history(symbol=self.symbol, start=self.from_date, end=self.to_date) # Date index is string type so we can't add freq # So, need to make Datetime index and add freq 1-Day # history_data = history_data.reset_index() history_data['Date'] = pd.to_datetime(history_data['Date']) history_data = history_data.set_index('Date') history_data = history_data.resample('1D').mean() return history_data
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_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 store_nse_data(ticker, start, end): print('Start fetching: ', ticker) if not os.path.exists('nse_data/{}.csv'.format(ticker)): try: df_nse = get_history(symbol=ticker, start=start, end=end) except Exception as e: # NSE_INDICES_TO_BE_SKIPPED.append(ticker) return None df_nse.to_csv('nse_data/{}.csv'.format(ticker)) print('Save data for {}'.format(ticker)) return 'Success' else: print('Already have {}'.format(ticker)) return 'Already there'
def get_index_call_test(self, expiry_date, strike): print("Input parameters") print(str(expiry_date) + " -- " + str(strike)) df = pd.DataFrame() print("df == " + str(df)) 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_date,strike_price=strike) except: continue return df
] sql = ''' SELECT max(CONVERT(varchar, [Date], 23)) as maxdate FROM IndexHistory ''' df = pd.read_sql_query(sql, engine) lastupdateddate = datetime.strptime(df['maxdate'][0].split()[0], "%Y-%m-%d").date() startdate = lastupdateddate + timedelta(days=1) enddate = datetime.now().date() + timedelta(days=1) print("For Range:", startdate, " to ", enddate) for idx in indexnames: try: indexvals = get_history(symbol=idx, start=startdate, end=enddate, index=True) indexvals["Index Name"] = idx indexvals.to_sql("IndexHistory", engine, if_exists='append', chunksize=1000) print(idx, " -- COMPLETED") except Exception as e: print(idx, " -- NOT COMPLETED") print(e)