import time
import csv
import argparse
from nsetools import Nse
from colorama import Fore
nse = Nse()
t = time.time()
CSV file contains the following data, I added it so that others may know the contents of the file. In the actual program I use a real CSV file in FILE_LOCATION variable.
CSV file contents:
Instrument,Qty,Avg price
APLAPOLLO,3,949.95
AVANTIFEED,6,554.55
BALAMINES,9,337.72
BANCOINDIA,15,217
DCMSHRIRAM,12,261.4
GHCL,12,267.2
GIPCL,27,101.95
JAMNAAUTO,15,182.1
JBCHEPHARM,15,344.85
KEI,24,143.95
KPRMILL,6,569.65
KRBL,9,312
MPHASIS,6,533.95
SHEMAROO,2,413.25
Code:
FILE_LOCATION = '' # empty because path not relevant to others
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--realtime', help='Obtain realtime stock\
quotes', action='store_true')
args = parser.parse_args()
def get_closing(stock):
"""Function to obtain closePrice or buyPrice1 of stocks"""
if args.realtime:
return nse.get_quote(stock)['buyPrice1']
else:
return nse.get_quote(stock)['closePrice']
def expenses_calc(buy, sell, qty):
"""Function to calculate the relevant expenses"""
stt = 0.10
ttc = 0.00325
service_tax = 15.0
sebi_charges = 0.0002
stamp_duty = 0.01
cdsl = 13.5
turnover = (buy * qty) + (sell * qty)
stt_total = (stt / 100) * turnover
total_tran_charge = (ttc / 100) * turnover
service_tax = (service_tax / 100) * total_tran_charge
sebi_charges = (sebi_charges / 100) * turnover
stamp_duty = (stamp_duty / 100) * turnover
total_tax_and_charges = cdsl + stt_total + total_tran_charge + service_tax
+ sebi_charges + stamp_duty
net_profit = (sell * qty) - (buy * qty) - total_tax_and_charges
return net_profit
# nifty
nifty_buy = 8262.70
nifty = nse.get_index_quote('nifty 50')
nifty_close = nifty['lastPrice']
nifty_percent = ((nifty_close - nifty_buy) / nifty_buy) * 100
# calculating total investment
with open(FILE_LOCATION, 'r') as f:
next(f)
total_investment = sum(int(row[1]) * float(row[2]) for row in
csv.reader(f))
# calculating current value of investment
with open(FILE_LOCATION, 'r') as f:
next(f)
current_value = sum(get_closing(row[0]) * int(row[1]) for row in
csv.reader(f))
gross_profit = current_value - total_investment
profit_loss_percent = (
(current_value - total_investment) / total_investment) * 100
# calculating net profit post expenses/taxes
with open(FILE_LOCATION, 'r') as f:
next(f)
net_profit = sum((expenses_calc(float(row[2]), get_closing(
row[0]), int(row[1]))) for row in csv.reader(f))
print('-' * 80)
# printing total investment
print('Total Investment: ' + unicode(u"\u20B9") +
'{:,.2f}'.format(total_investment))
# checking if my portfolio is in profit
if current_value > total_investment:
print('Current Value: ' + unicode(u"\u20B9") + Fore.GREEN +
'{:,.2f}'.format(current_value) + Fore.RESET)
else:
print('Current Value: ' + unicode(u"\u20B9") + Fore.RED +
'{:,.2f}'.format(current_value) + Fore.RESET)
# printing gross profit
if current_value > total_investment:
print('Gross Profit: ' + unicode(u"\u20B9") + Fore.GREEN +
'{:.2f}'.format(gross_profit) + Fore.RESET)
else:
print('Gross Profit: ' + unicode(u"\u20B9") + Fore.RED +
'{:.2f}'.format(gross_profit) + Fore.RESET)
# printing net profit
if net_profit > gross_profit:
print('Net Profit: ' + unicode(u"\u20B9") + Fore.GREEN +
'{:.2f}'.format(net_profit) + Fore.RESET)
else:
print('Net Profit: ' + unicode(u"\u20B9") + Fore.RED +
'{:.2f}'.format(net_profit) + Fore.RESET)
print('-' * 80)
# printing protfolio profit/loss
if current_value > total_investment:
print('Portfolio P/L: ' + Fore.GREEN +
'{:.2f}%'.format(profit_loss_percent) + Fore.RESET)
else:
print('Portfolio P/L: ' + Fore.RED +
'{:.2f}%'.format(profit_loss_percent) + Fore.RESET)
# checking if market is in profit
if nifty_close > nifty_buy:
print('Nifty P/L: ' + Fore.GREEN +
'{:.2f}%'.format(nifty_percent) + Fore.RESET)
else:
print('Nifty P/L: ' + Fore.RED +
'{:,.2f}%'.format(nifty_percent) + Fore.RESET)
# Mocking or Congratulating myself if in loss or profit
if nifty_percent > profit_loss_percent:
print(Fore.RED + "Failure, the market beat you" + Fore.RESET)
else:
print(Fore.GREEN + "Congratulations, you beat the market!" + Fore.RESET)
print('-' * 80)
print("Completed in ", time.time() - t)
Average Speed of calculating Total Investment, Current Value and Net Profit:
Total Investment: ₹43182.63
completed in 0.00 seconds
Current Value: ₹43478.05
completed in 17.86 seconds
Net Profit: ₹16.52
completed in 12.31 seconds
As of now the script does its job, but takes anything from 26-34 seconds to finish running. To speed things up I used generator expressions where applicable instead of for loops, but did not notice any major speed gains.