def test_get_price_list(self): testdate = date(2019, 7, 26) testsymbol = 'IDFCFIRSTB' # Check Stock dfpleq = get_price_list(testdate, 'EQ') stk = dfpleq[dfpleq['SYMBOL'] == 'IDFCFIRSTB'].squeeze() self.assertEqual(stk['CLOSE'], 42.35) # Check Bond dfpln1 = get_price_list(testdate, 'N1') bond = dfpln1[dfpln1['SYMBOL'] == testsymbol].squeeze() self.assertEqual(bond['CLOSE'], 5085.00)
def today_eod(): dt = date.today() year = str(dt.year) month_no = dt.month if not os.path.exists(year): os.makedirs(year) print(year + ' : Dir Created.') else: print(year + ': dir already exists.') month = month_name[month_no] if not os.path.exists(year + '/' + str(month_no) + '-' + month): os.makedirs(year + '/' + str(month_no) + '-' + month) print(year + '/' + str(month_no) + '-' + month + ' : Dir Created.') else: print(year + '/' + str(month_no) + '-' + month + ': dir already exists.') try: if not os.path.exists(year + '/' + str(month_no) + '-' + month + '/%s.csv' % (dt)): df = get_price_list(dt) df = df[(df.SERIES == 'EQ')] df = df.drop(['TOTTRDVAL', 'ISIN', 'PREVCLOSE'], axis=1) df.to_csv(year + '/' + str(month_no) + '-' + month + '/%s.csv' % (dt), index=False) print('Downloaded : %s' % dt) else: print(year + '/' + month + '/%s.csv: file already exists.' % (dt)) except Exception as e: print(e)
def getNSEDailyQuote(start_date, end_date, filename): day_count = (end_date - start_date).days + 1 one_day = timedelta(1, 0, 0) for dayloop in range(0, day_count, 1): tradingdate = start_date + timedelta(dayloop, 0, 0) if (tradingdate in holidaylist or tradingdate.weekday() in (5, 6)): continue print('trading date ->', tradingdate) prices = get_price_list(tradingdate) # writing to CSV prices.to_csv('{}.csv'.format(filename), index=False, mode='a', header=None)
def get_latest_bhavcopy(): from_period=datetime.datetime.now().date()+datetime.timedelta(days=-3) to_period=datetime.datetime.now().date() prices=None while from_period<=to_period: try: prices = get_price_list(dt=from_period) except: print (" update_bhavcopy Unexpected error:", sys.exc_info()) pass from_period=from_period+datetime.timedelta(days=1) print(from_period) return prices
def update_bhavcopy_df(): to_period=datetime.datetime.now().date() from_period=to_period+datetime.timedelta(days=-4) prices_all=pd.DataFrame() while from_period<=to_period: try: prices = get_price_list(dt=from_period) prices_all=prices_all.append(prices) from_period=from_period+datetime.timedelta(days=1) print(from_period) except: print (" update_bhavcopy Unexpected error:", sys.exc_info()) pass return
def month_eod(): year = input('Year: ') month_no = int(input('Month(1-12): ')) if not os.path.exists(year): os.makedirs(year) print(year + ' : Dir Created.') else: print(year + ': dir already exists.') month = month_name[int(month_no)] if not os.path.exists(year + '/' + str(month_no) + '-' + month): os.makedirs(year + '/' + str(month_no) + '-' + month) print(year + '/' + str(month_no) + '-' + month + ' : Dir Created.') else: print(year + '/' + str(month_no) + '-' + month + ': dir already exists.') days = monthrange(int(year), month_no)[1] print('No. of days in %s: %d' % (month, days)) for dy in range(1, days + 1): dt = date(int(year), month_no, dy) try: if not os.path.exists(year + '/' + str(month_no) + '-' + month + '/%s.csv' % (dt)): df = get_price_list(dt) df = df[(df.SERIES == 'EQ')] df = df.drop(['TOTTRDVAL', 'ISIN', 'PREVCLOSE'], axis=1) df.to_csv(year + '/' + str(month_no) + '-' + month + '/%s.csv' % (dt), index=False) print('Downloaded : %s' % dt) else: print(year + '/' + month + '/%s.csv: file already exists.' % (dt)) except Exception as e: print(e) continue # 'Error While Dwnlding: ' + else: print('Done: ' + month)
def year_eod(): y = input('Year: ') if not os.path.exists(y): os.makedirs(y) print(y + ' : Dir Created.') else: print(y + ': dir already exists.') for mon in range(1, 13): month = month_name[mon] if not os.path.exists(y + '/' + str(mon) + '-' + month): os.makedirs(y + '/' + str(mon) + '-' + month) print(y + '/' + str(mon) + '-' + month + ' : Dir Created.') else: print(y + '/' + str(mon) + '-' + month + ': dir already exists.') days = monthrange(int(y), mon)[1] print('No. of days in %s: %d' % (month, days)) for dy in range(1, days + 1): dt = date(int(y), mon, dy) try: if not os.path.exists(y + '/' + str(mon) + '-' + month + '/%s.csv' % (dt)): df = get_price_list(dt) df = df[(df.SERIES == 'EQ')] df = df.drop(['TOTTRDVAL', 'ISIN', 'PREVCLOSE'], axis=1) df.to_csv(y + '/' + str(mon) + '-' + month + '/%s.csv' % (dt), index=False) else: print(y + '/' + month + + '/%s.csv: file already exists.' % (dt)) except Exception as e: print(e) continue else: print('Downloaded : %s' % dt) else: print('Done: ' + month) else: print('Done: ' + y)
def main(): wb = xw.Book.caller() DATE = wb.sheets[0].range("B1").value # Get the price list for defined Date above price_list = get_price_list(DATE) price_list = price_list[["SYMBOL", "CLOSE", "ISIN"]] # A one line function to return close price given ISIN get_close = lambda x: price_list[price_list.ISIN == x]["CLOSE"] Name = "XXX" share_row = 4 while True: sheet_name = "dly" # Get the name of the stock Name = wb.sheets[sheet_name].range(f"B{share_row}").value # If the name is blank, then end the update if not Name: break # Get the ISIN Number of the stock ISIN = wb.sheets[sheet_name].range(f"A{share_row}").value ISIN = ISIN.strip() close_price = get_close(ISIN) # Fetch the current closing and correspondingly populate the closing column for the day if len(close_price) > 0: wb.sheets[sheet_name].range(f"C{share_row}").value = float(close_price) else: wb.sheets[sheet_name].range(f"C{share_row}").value = "#NA" share_row += 1
pd.set_option("display.width", 1500) pd.set_option("display.max_columns", 75) pd.set_option("display.max_rows", 1500) abDatabase = 'D:\\Data\\Amibroker\\Databases\\NSEDAILY' #abFormat = 'D:\\Data\\Amibroker\\Formats\\pyImport.format' abFormat = 'D:\\Data\\Amibroker\\Formats\\pyBhavCopy.format' AB = wcl.Dispatch('Broker.Application') AB.LoadDatabase(abDatabase) pd.set_option('display.max_columns', None) fromDate = date(2020, 1, 1) toDate = date(2020, 5, 8) prices = get_price_list(dt=date(2015, 1, 1)) columnsTitles = [ 'SYMBOL', 'TIMESTAMP', 'OPEN', 'HIGH', 'LOW', 'CLOSE', 'TOTTRDQTY' ] prices.drop( ["SERIES", "LAST", "PREVCLOSE", "TOTTRDVAL", "TOTALTRADES", "ISIN"], axis=1, inplace=True) prices = prices[columnsTitles] #print(prices.dtypes) prices['TIMESTAMP'] = pd.to_datetime(prices['TIMESTAMP']) print(prices.dtypes) prices['TIMESTAMP'].apply(lambda x: x.strftime('%Y%m%d')) #prices['TIMESTAMP'] = prices['TIMESTAMP'].strftime('%Y%m%d') print(prices.dtypes) print(prices)
import plotly import webbrowser import plotly.graph_objs as go from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot from datetime import date from nsepy import get_history from nsepy.history import get_price_list from pandas_datareader import data as pdr import yfinance as yf import matplotlib.pyplot as plt import numpy as np prices = get_price_list(dt=date(2020, 3, 9)) #print(prices) #if row.TOTALTRADES>1000: #print(prices[prices.TOTALTRADES>300000]) df = get_history(symbol="NIFTY 50", start=date(2020, 6, 1), end=date(2020, 6, 17), index=True) # # def candle_df(df): #df_candle=first_letter_upper(df) df_candle = df.copy()
def NSE_TradedStocks(dateval): prices = get_price_list(dt=dateval) return prices['SYMBOL'].tolist()
#!/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 csv from datetime import date from nsepy.history import get_price_list script_data = get_price_list(dt=date(2020, 9, 28)) script_data.to_csv('out.csv') csv_out = csv.DictReader(open('out.csv', 'r')) script_dict = [] for line in csv_out: script_dict.append(line) data_list = [ 'SYMBOL', 'HIGH', 'LOW', 'CLOSE', 'R1', 'R2', 'PIVOT', 'S1', 'S2', '%', '>.6%', 'TOTTRDQTY', 'TOTTRDVAL', 'BUY', 'TGT', 'SELL', 'TGT' ] list_200_500 = [] for i in script_dict: dummy_dict = {} if 500 < float(i['CLOSE']) < 1000: for j in data_list: try: dummy_dict[j] = i[j] except: dummy_dict[j] = "" dummy_dict['PIVOT'] = (float(dummy_dict['HIGH']) + float( dummy_dict['LOW']) + float(dummy_dict['CLOSE'])) / float(3) dummy_dict['R1'] = (float(dummy_dict['PIVOT']) * float(2)) - float( dummy_dict['LOW'])
"India VIX is a volatility index which gives a measurement of market volatility based on NIFTY options contract. This servers as important parameter in option pricing" ) if st.button("Check Volatility"): vix = get_history(symbol="INDIAVIX", start=date(sy, sm, sd), end=date(ey, em, ed), index=True) st.write(vix) st.area_chart(vix[["Close"]]) st.header("Market Valuation") st.markdown( "P/E ratio of a security helps to estimate if the security is over-priced or under-priced and further make investment decisions. P/B value helps determine the intrinsic value of a security with regards to its market price" ) if st.button("Check Valuation"): pe = get_index_pe_history(symbol="NIFTY", start=date(sy, sm, sd), end=date(ey, em, ed)) st.write(pe) st.area_chart(pe[["P/E"]]) st.line_chart(pe[["P/B"]]) st.header("All stock quotes") st.markdown("Enter year,date and time to get stock quotes of entire market") y = int(st.number_input("Enter year")) m = int(st.number_input("Enter month")) d = int(st.number_input("Enter date")) if st.button("Check Quotes"): prices = get_price_list(dt=date(y, m, d)) st.write(prices)
# importing datetime module import datetime from datetime import date import calendar from nsepy.history import get_price_list #Taking date as input from the user d1, m1, y1 = [int(x) for x in input("Enter date(DD/MM/YYYY) : ").split('/')] b1 = date(y1, m1, d1) # Check the dates if b1.weekday() == 6: print("Bhavcopy isn't available on sundays") elif b1 == date.today(): prices = get_price_list(dt=date.today()) prices.to_csv('bhavcopy.csv') elif b1 < date.today(): prices = get_price_list(dt=datetime.datetime(int(y1), int(m1), int(d1))) prices.to_csv('bhavcopy.csv') else: print("Entered date is future date")
import csv from datetime import date from nsepy.history import get_price_list script_data = get_price_list(dt=date(2018,5,8)) script_data.to_csv('out.csv') csv_out = csv.DictReader(open('out.csv', 'r')) script_dict = [] for line in csv_out: script_dict.append(line) data_list = ['SYMBOL','HIGH','LOW','CLOSE','R1','R2','PIVOT','S1','S2','%','>.6%','TOTTRDQTY','TOTTRDVAL', 'BUY', 'SELL'] list_200_500 = [] for i in script_dict: dummy_dict = {} if 100 < float(i['CLOSE']) < 700: for j in data_list: try: dummy_dict[j] = i[j] except: dummy_dict[j] = "" dummy_dict['PIVOT'] = (float(dummy_dict['HIGH'])+float(dummy_dict['LOW'])+float(dummy_dict['CLOSE']))/float(3) dummy_dict['R1'] = (float(dummy_dict['PIVOT'])*float(2))-float(dummy_dict['LOW']) dummy_dict['R2'] = float(dummy_dict['PIVOT'])+(float(dummy_dict['HIGH'])-float(dummy_dict['LOW'])) dummy_dict['S1'] = (float(dummy_dict['PIVOT'])*float(2))-float(dummy_dict['HIGH']) dummy_dict['S2'] = float(dummy_dict['PIVOT'])-(float(dummy_dict['HIGH'])-float(dummy_dict['LOW'])) dummy_dict['%'] = ((float(dummy_dict['CLOSE'])-float(dummy_dict['PIVOT']))/float(dummy_dict['PIVOT']))*100 dummy_dict['BUY'] = (float(dummy_dict['CLOSE'])) + 1 dummy_dict['SELL'] = (float(dummy_dict['PIVOT'])) -1
auth_plugin="mysql_native_password") start_date = date(2012, 1, 1) end_date = date(2012, 1, 1) #prices = get_price_list(start_date) day_count = (end_date - start_date).days + 1 one_day = timedelta(1, 0, 0) for dayloop in range(0, day_count, 1): tradingdate = start_date + timedelta(dayloop, 0, 0) print() if (tradingdate in holidaylist or tradingdate.weekday() in (5, 6)): continue print('trading date ->', tradingdate) prices = get_price_list(tradingdate) #prices.to_sql(con=mydbconn, name='stock_price_history', if_exists='replace', flavor='mysql') prices.to_sql('stock_price_hist', con=engine, if_exists='append') mydbconn.commit() print("Record inserted successfully into python_users table") # mycursor = mydbconn.cursor() # sql_insert_query = "INSERT INTO stockdaily_raw(nse_symbol,load_datetime,stock_rawdata) VALUES (%s,%s,%s)" # val1='INFY' # insert_tuple = (val1) # for stock_row_string in stock_rawdata: # json_stock_row = json.loads(stock_row_string); # result = mycursor.execute(sql_insert_query, (json_stock_row['symbol'] ,currdatetime, stock_row_string)) except mysql.connector.Error as error: mydbconn.rollback()
from time import sleep from json import dumps from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers=['localhost:9092'], value_serializer=lambda x: dumps(x).encode('utf-8')) from nsepy.history import get_price_list from datetime import date for i in range(15, 26, 1): print(i) try: prices = get_price_list(dt=date(2020, 9, i)) records - prices.to_dict('records') for rec in records: producer.send('stocks', value=rec) except: print("NO DATA FOUND ON {} DECEMBER ".format(i))
from nsepy.history import get_price_list from datetime import date import pandas as pd ##stock_opt = get_history(symbol="SBIN", ## start=date(2015,1,1), ## end=date(2015,1,10), ## option_type="CE", ## strike_price=300, ## expiry_date=date(2015,1,29)) ##print(type(stock_opt)) ##stock_opt.to_csv("options.csv") prices = get_price_list(dt=date(2018, 11, 2)) prices.to_csv("Bhav.csv")
def getEodData(self, dt=None): if dt is None: dt = date.today() return get_price_list(dt=dt)
def select_stock(data): for i in range(len(data)): try: if data['CLOSE'].loc[i] <= 50.0: data.drop(i, axis=0, inplace = True) elif data['CLOSE'].loc[i] >= 10000.0: data.drop(i, axis=0, inplace = True) except: continue return data pd.set_option('display.max_rows', 500) pd.set_option('display.max_columns', 500) pd.set_option('display.width', 1000) data = get_price_list(dt=date(2019,1,4)) data.drop(['SERIES'], 1, inplace=True) data.drop(['LAST'], 1, inplace=True) data.drop(['TOTTRDQTY'], 1, inplace=True) data.drop(['TOTTRDVAL'], 1, inplace=True) data.drop(['TIMESTAMP'], 1, inplace=True) data.drop(['TOTALTRADES'], 1, inplace=True) data.drop(['ISIN'], 1, inplace=True) data = select_stock(data) rsi = [] predicts = [] # for i in range(len(data)): data = data[:50] for i in range(50): try:
lastupdateddate = datetime.strptime(df['maxdate'][0].split()[0], "%Y-%m-%d").date() startdate = lastupdateddate + timedelta(days=1) enddate = datetime.now().date() + timedelta(days=1) dtrange = daterange(startdate, enddate) starttime = datetime.now() print("Started : ", starttime) for dt in dtrange: yr = dt.year mth = dt.month dte = dt.day try: if (dt.weekday() < 5): prices = get_price_list(dt=date(yr, mth, dte)) prices['TIMESTAMP'] = pd.to_datetime(prices['TIMESTAMP']) prices.to_sql("bhavcopy", engine, if_exists='append', chunksize=1000) print(dt, " - Completed :: ", len(prices.index), " ROWS, ", len(prices.columns), " COLUMNS") except Exception as e: print(dt, " - Not Completed :: ", e) continue endtime = datetime.now() print("Finished : ", endtime) print("Completed in", endtime - starttime)
def extract_nse_daily(s_dt): print(s_dt) data = get_price_list(dt=date(s_dt.year, s_dt.month, s_dt.day)) data.columns = data.columns.str.strip().str.lower().str.replace(' ', '_').str.replace('(', '').str.replace(')', '') return data