def avg_return(symbol):
if(symbol=="NIFTY"):symbol="^NSEI"
if(symbol=="BANKNIFTY"):symbol="^NSEBANK"
params = {'range': '1y', 'interval': '1d'}
main_json = requests.get(f'https://query1.finance.yahoo.com/v8/finance/chart/{symbol}', params=params).json()
closing_prices = [i for i in main_json['chart']['result'][0]['indicators']['quote'][0]['close']]
ltp = closing_prices[-1]
daily_return = []
for i in range(len(closing_prices)-1):
try:
returns = ((closing_prices[i+1]-closing_prices[i])/closing_prices[i])*100
except TypeError:
returns = 0
daily_return.append(returns)
avg = statistics.mean(daily_return) #this is in percent as it is calculated over percent
std = statistics.stdev(daily_return) # since it is calculated over percent it is also in percent
return ltp, avg, std
This patch of code was used in Artemis
If You can make a function that uses NSEPython Library instead of this. You can raise a pull request in GitHub - aeron7/nsepython: The Unofficial Python Wrapper for NSEIndia API.
PS: I will prolly do it myself by tomorrow 12PM. Try your hands before that.