def PE_list(i, date_latest, years): PE_of_years = get_index_pe_history( symbol=i, start=date.fromordinal(date_latest.toordinal() - years * 365), end=date_latest) return PE_of_years.index.values, PE_of_years['P/E'].tolist( ), PE_of_years['P/B'].tolist(), PE_of_years['Div Yield'].tolist()
def PE(i, date_latest, xdays): PE_of_xdays = get_index_pe_history( symbol=i, start=date.fromordinal(date_latest.toordinal() - xdays), end=date_latest) return PE_of_xdays['P/E'].tolist()[0], PE_of_xdays['P/B'].tolist( )[0], PE_of_xdays['Div Yield'].tolist()[0]
def PEhistory(indexsymbol): PEhistyear = pd.DataFrame() years = range(STARTYEAR, ENDYEAR) for year in years: PEhistyear = PEhistyear.append(PEhistoryyear(indexsymbol, year)) PEhistyear = PEhistyear.append( get_index_pe_history(symbol=indexsymbol, start=date(2020, 3, 31), end=date(2020, 3, 31))) return PEhistyear
def PEhistoryyear(indexsymbol, year): quarter = [3, 6, 9, 12] PE_history = pd.DataFrame() for n in quarter: PE_month_history = get_index_pe_history( symbol=indexsymbol, start=date(year, n, 1), end=date(year, n, calendar.monthrange(year, n)[1])) PE_history = PE_history.append(PE_month_history.iloc[[-1]]) return PE_history
nifty['SMA'] = ta.SMA(nifty['Close'], 50) nifty['EMA'] = ta.EMA(nifty['Close'], timeperiod=5) nifty['upper_band'], nifty['middle_band'], nifty['lower_band'] = ta.BBANDS( nifty['Close'], timeperiod=20) nifty['Percentage Change'] = nifty['Close'].diff() * 100 / nifty['Close'] nifty.to_csv('nifty{0}_{1}.csv'.format(start.year, end.year), sep='\t') start = date(2010, 1, 1) end = date(2020, 7, 22) nifty50Data(start, end) vix = get_history(symbol="INDIAVIX", start=start, end=end, index=True) nifty_pe = get_index_pe_history(symbol="NIFTY", start=start, end=end) vix.to_csv('vix{0}_{1}.csv'.format(start.year, end.year), sep='\t', columns=['Close']) nifty_pe.to_csv('niftyPE{0}_{1}.csv'.format(start.year, end.year), sep='\t', columns=['P/E']) dowJones = pd.read_csv('Dow Jones{0}_{1}.csv'.format(2010, end.year), thousands=',') dowJones['Date'] = dowJones['Date'].map( lambda x: datetime.strptime(x, "%b %d, %Y")) dowJones.to_csv('DowJones{0}_{1}.csv'.format(start.year, end.year), sep='\t', columns=['Date', 'Price'],
def PEhistory(indexsymbol): pe_history = get_index_pe_history(symbol=indexsymbol, start=date(2009, 3, 31), end=date(2009, 3, 31)) pe_history = pe_history.append( get_index_pe_history(symbol=indexsymbol, start=date(2010, 3, 31), end=date(2010, 3, 31))) pe_history = pe_history.append( get_index_pe_history(symbol=indexsymbol, start=date(2011, 3, 31), end=date(2011, 3, 31))) pe_history = pe_history.append( get_index_pe_history(symbol=indexsymbol, start=date(2012, 3, 30), end=date(2012, 3, 30))) pe_history = pe_history.append( get_index_pe_history(symbol=indexsymbol, start=date(2013, 3, 28), end=date(2013, 3, 28))) pe_history = pe_history.append( get_index_pe_history(symbol=indexsymbol, start=date(2014, 3, 31), end=date(2014, 3, 31))) pe_history = pe_history.append( get_index_pe_history(symbol=indexsymbol, start=date(2015, 3, 31), end=date(2015, 3, 31))) pe_history = pe_history.append( get_index_pe_history(symbol=indexsymbol, start=date(2016, 3, 31), end=date(2016, 3, 31))) pe_history = pe_history.append( get_index_pe_history(symbol=indexsymbol, start=date(2017, 3, 31), end=date(2017, 3, 31))) pe_history = pe_history.append( get_index_pe_history(symbol=indexsymbol, start=date(2018, 3, 28), end=date(2018, 3, 28))) pe_history = pe_history.append( get_index_pe_history(symbol=indexsymbol, start=date(2019, 3, 29), end=date(2019, 3, 29))) pe_history = pe_history.append( get_index_pe_history(symbol=indexsymbol, start=date(2020, 3, 31), end=date(2020, 3, 31))) print(pe_history) return pe_history
def PEhistory(indexsymbol): nifty_pe = get_index_pe_history(symbol=indexsymbol, start=date(2009, 3, 31), end=date(2009, 3, 31)) nifty_pe = nifty_pe.append( get_index_pe_history(symbol=indexsymbol, start=date(2010, 3, 31), end=date(2010, 3, 31))) nifty_pe = nifty_pe.append( get_index_pe_history(symbol=indexsymbol, start=date(2011, 3, 31), end=date(2011, 3, 31))) nifty_pe = nifty_pe.append( get_index_pe_history(symbol=indexsymbol, start=date(2012, 3, 30), end=date(2012, 3, 30))) nifty_pe = nifty_pe.append( get_index_pe_history(symbol=indexsymbol, start=date(2013, 3, 28), end=date(2013, 3, 28))) nifty_pe = nifty_pe.append( get_index_pe_history(symbol=indexsymbol, start=date(2014, 3, 31), end=date(2014, 3, 31))) nifty_pe = nifty_pe.append( get_index_pe_history(symbol=indexsymbol, start=date(2015, 3, 31), end=date(2015, 3, 31))) nifty_pe = nifty_pe.append( get_index_pe_history(symbol=indexsymbol, start=date(2016, 3, 31), end=date(2016, 3, 31))) nifty_pe = nifty_pe.append( get_index_pe_history(symbol=indexsymbol, start=date(2017, 3, 31), end=date(2017, 3, 31))) nifty_pe = nifty_pe.append( get_index_pe_history(symbol=indexsymbol, start=date(2018, 3, 28), end=date(2018, 3, 28))) nifty_pe = nifty_pe.append( get_index_pe_history(symbol=indexsymbol, start=date(2019, 3, 29), end=date(2019, 3, 29))) nifty_pe = nifty_pe.append( get_index_pe_history(symbol=indexsymbol, start=date(2020, 3, 31), end=date(2020, 3, 31))) print(nifty_pe) return nifty_pe
from nsepy import get_index_pe_history from datetime import date, datetime, timedelta import os dir_path = os.path.dirname(os.path.realpath(__file__)) os.chdir(dir_path) thisyear = datetime.now().year thismonth = datetime.now().month thisday = datetime.now().day Indices = [] with open("BroadMarketIndices.txt", "rt") as f: for line in f: if (line[0] != "#"): line = line.rstrip('\n') Indices.append(line) i = 1 for broadindex in Indices: broadindex = broadindex.upper() pe = get_index_pe_history(symbol=broadindex, start=date(thisyear, thismonth - 1, 1), end=date(thisyear, thismonth, thisday)) print(pe) pe.to_csv('data/%d_%s.csv' % (i, broadindex)) i += 1
end=date(2015, 1, 31)) data[['Close']].plot() sbin = get_history(symbol='SBIN', start=date(2020, 8, 1), end=date(2020, 9, 10)) print(sbin) print(sbin.columns) ax = plt.gca() sbin.plot(kind='line', x='%Deliverble', y='Date', ax=ax) #df.plot(kind='line',x='name',y='num_pets', color='red', ax=ax) #plt.show() stock_opt = get_history(symbol="SBIN", start=date(2020, 9, 1), end=date(2020, 9, 10), option_type="CE", strike_price=300, expiry_date=date(2015, 1, 29)) prices = get_price_list(dt=date(2020, 9, 8)) print(prices) nifty_pe = get_index_pe_history(symbol="NIFTY", start=date(2020, 9, 1), end=date(2020, 9, 10)) print(nifty_pe) from nsepy import get_rbi_ref_history rbi_ref = get_rbi_ref_history(date(2015, 1, 1), date(2020, 10, 10)) print(rbi_ref)
from nsepy import get_index_pe_history from datetime import date nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2009,3,31), end=date(2009,3,31)) print(nifty_pe) nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2010,3,31), end=date(2010,3,31)) print(nifty_pe) nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2011,3,31), end=date(2011,3,31)) print(nifty_pe) nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2012,3,30), end=date(2012,3,30)) print(nifty_pe) nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2013,3,28), end=date(2013,3,28)) print(nifty_pe) nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2014,3,31), end=date(2014,3,31)) print(nifty_pe) nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2015,3,31), end=date(2015,3,31)) print(nifty_pe) nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2016,3,31), end=date(2016,3,31)) print(nifty_pe) nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2017,3,31), end=date(2017,3,31)) print(nifty_pe) nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2018,3,28), end=date(2018,3,28)) print(nifty_pe) nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2019,3,29), end=date(2019,3,29)) print(nifty_pe) nifty_pe = get_index_pe_history(symbol="NIFTY 50", start=date(2020,3,31), end=date(2020,3,31)) print(nifty_pe)
#https://swapniljariwala.github.io/2017/11/03/NSEPy-with-backtrader.html #%matplotlib inline from datetime import date import pandas as pd from nsepy import get_history from nsepy import get_index_pe_history try: nifty = pd.read_csv('data/nifty17years_withPE.csv') print('Read from disk successful') except: print('Downloading from NSE') nifty = get_history('NIFTY', date(2000, 1, 1), date.today(), index=True) pe = get_index_pe_history('NIFTY', date(2000, 1, 1), date.today()) nifty['PE'] = pe['P/E'] nifty.to_csv('data/nifty17years_withPE.csv') print(nifty) from backtrader.feeds import GenericCSVData # Define the new parameter class GenericCSV_PE(GenericCSVData): # Add a 'pe' line to the inherited ones from the base class lines = ('pe', ) # add the parameter to the parameters inherited from the base class params = (('pe', 8), ) # Declare position of each column in csv file
sql = ''' SELECT max(CONVERT(varchar, [Date], 23)) as maxdate FROM IndexPEHistory ''' 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("Last Updated Date = ", lastupdateddate) for idx in indexnames: try: indexpevals = get_index_pe_history(symbol=idx, start=startdate, end=enddate) indexpevals["Index Name"] = idx indexpevals.to_sql("IndexPEHistory", engine, if_exists='append', chunksize=1000) print(idx, " -- COMPLETED") except Exception as e: print(idx, " -- NOT COMPLETED") print(e)
"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)
'''sbin=get_history(symbol="SBIN",start=date(2021,1,1), end=date(2021,1,29)) print(sbin)''' import pandas as pd '''try: nifty = pd.read_csv('C:\\Mydrive\\Study\\workspace\\data\\nifty2020year_withPE.csv') print('Read from disk successful') except:''' datadirectory = "C:\\data\\" startyear = 2019 endyear = 2020 '''scriptName='NIFTY' istheScriptIndex=True''' scriptName = 'SBIN' istheScriptIndex = False #change this to True if fetch is for Index data for x in range(startyear, endyear): print('Downloading from NSE' + str(x)) if (istheScriptIndex): sciptData = get_history(scriptName, date(x, 1, 1), date(x, 12, 31), index=True) else: sciptData = get_history(scriptName, date(x, 1, 1), date(x, 12, 31)) pe = get_index_pe_history(scriptName, date(x, 1, 1), date(x, 12, 31)) sciptData['PE'] = pe['P/E'] sciptData.to_csv(datadirectory + scriptName + str(x) + 'year_withPE.csv') print('completed for year ' + str(x)) print('completed')