Free stuff for trading

Phython Code to fetch OI data for FnO


Output Files
1) combined_ce_and_pe_open_int.csv
2) combined_future_open_int.csv
3) price_change.csv

Libraries
import requests, zipfile, StringIO
import sys
import argparse
import os
import convertCSVToXLSX as csvtoxlsx


Variables to declare
month_dict = {
"01": "JAN",
"02": "FEB",
"03": "MAR",
"04": "APR",
"05": "MAY",
"06": "JUN",
"07": "JUL",
"08": "AUG",
"09": "SEP",
"10": "OCT",
"11": "NOV",
"12": "DEC"
}
future_index_stock = [
'FUTIDX',
'FUTSTK',
]
instrument = 'OPTSTK'


Download Bhavcopy from NSE website
def download_bhavcopy(formated_date):
url = "https://www1.nseindia.com/content/historical/DERIVATIVES/{0}/{1}/fo{2}{1}{0}bhav.csv.zip".format(
formated_date.split('-')[2],
month_dict[formated_date.split('-')[1]],
formated_date.split('-')[0])
r = requests.get(url, stream=True)
if r.status_code == 404:
print "No data found for date {0}".format(formated_date)
return
z = zipfile.ZipFile(StringIO.StringIO(r.content))
z.extractall()
file_name = z.filelist[0].filename
# print file_name
return file_name


Parse arguments to be passed from command prompt or in eclipse argument
configuration
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--compare", help = "This compare the data", dest = "hostname", required = True)



Get OI change
def get_option_oi_change(current_date_file, previous_date_file):
filtered_current_date_file = {}
filtered_privious_date_file = {}
if not current_date_file or not os.path.isfile(current_date_file):
return
if not previous_date_file or not os.path.isfile(previous_date_file):
return
with open(current_date_file, 'r') as f:
for line in f:
if line.split(',')[0] == instrument:
symbol = line.split(',')[1]
OPTION_TYP = line.split(',')[4]
open_int = line.split(',')[12]
if symbol in filtered_current_date_file:
filtered_current_date_file[symbol].append(
"{},{},{}".format(symbol, open_int, OPTION_TYP))
else:
filtered_current_date_file[symbol] = ["{},{},{}".format(symbol, open_int, OPTION_TYP)]
with open(previous_date_file, 'r') as f:
for line in f:
if line.split(',')[0] == instrument:
symbol = line.split(',')[1]
OPTION_TYP = line.split(',')[4]
open_int = line.split(',')[12]
if symbol in filtered_privious_date_file:
filtered_privious_date_file[symbol].append(
"{},{},{}".format(symbol, open_int, OPTION_TYP))
else:
filtered_privious_date_file[symbol] = ["{},{},{}".format(symbol, open_int, OPTION_TYP)]
# print filtered_privious_date_file
with open('combined_ce_and_pe_open_int.csv', 'a') as f:
s1 = "{},{},{},{},{},{},{}".format("symbol".upper(),
"ce_privious".upper(),
"ce_current".upper(),
"ce_percentage_change".upper(),
"pe_privious".upper(),
"pe_current".upper(),
"pe_percentage_change".upper(),
f.write(s1+"\n")
for symbol, list_of_line in filtered_privious_date_file.items():
combined_ce_privious_open_int = 0
for v in list_of_line:
if 'CE' == v.split(',')[2]:
if '.' in v.split(',')[1]:
privious_ce_open_int = float(v.split(',')[1])
else:
try:
privious_ce_open_int = int(v.split(',')[1])
except ValueError:
print v.split(',')[1]
privious_ce_open_int = 0
combined_ce_privious_open_int = combined_ce_privious_open_int + privious_ce_open_int
# print combined_ce_privious_open_int
combined_pe_privious_open_int = 0
for v in list_of_line:
if 'PE' == v.split(',')[2]:
if '.' in v.split(',')[1]:
privious_pe_open_int = float(v.split(',')[1])
else:
try:
privious_pe_open_int = int(v.split(',')[1])
except ValueError:
print v.split(',')[1]
privious_pe_open_int = 0
combined_pe_privious_open_int = combined_pe_privious_open_int + privious_pe_open_int
combined_ce_current_open_int = 0
for v in filtered_current_date_file[symbol]:
if 'CE' == v.split(',')[2]:
if '.' in v.split(',')[1]:
current_pe_open_int = float(v.split(',')[1])
else:
try:
current_pe_open_int = int(v.split(',')[1])
except ValueError:
print v.split(',')[1]
current_pe_open_int = 0
combined_ce_current_open_int = combined_ce_current_open_int + current_pe_open_int
combined_pe_current_open_int = 0
for v in filtered_current_date_file[symbol]:
if 'PE' == v.split(',')[2]:
if '.' in v.split(',')[1]:
current_pe_open_int = float(v.split(',')[1])
else:
try:
current_pe_open_int = int(v.split(',')[1])
except ValueError:
print v.split(',')[1]
current_pe_open_int = 0
combined_pe_current_open_int = combined_pe_current_open_int + current_pe_open_int
try:
combined_pe_current_open_int_percentage_change =
((combined_ce_current_open_int/(combined_ce_privious_open_int*1.0) - 1)*100)
except (ZeroDivisionError):
combined_pe_current_open_int_percentage_change = "NA"
try:
combined_ce_current_open_int_percentage_change =
((combined_pe_current_open_int/(combined_pe_privious_open_int*1.0) - 1)*100)
except (ZeroDivisionError):
combined_ce_current_open_int_percentage_change = "NA"
# with open('combined_ce_open_int.csv', 'a') as f:
# s1 = "{},{},{},{}".format(symbol, combined_ce_privious_open_int, combined_ce_current_open_int,
combined_pe_current_open_int_percentage_change)
# f.write(s+"\n")
with open('combined_ce_and_pe_open_int.csv', 'a') as f:
s1 = "{},{},{},{},{},{},{}".format(symbol,
combined_ce_privious_open_int,
combined_ce_current_open_int,
combined_pe_current_open_int_percentage_change,
combined_pe_privious_open_int,
combined_pe_current_open_int,
combined_ce_current_open_int_percentage_change)
f.write(s1+"\n")


Get Futures OI
def get_future_oi_change(current_date_file, previous_date_file):
filtered_current_date_file = {}
filtered_privious_date_file = {}
with open('combined_future_open_int.csv', 'a') as f:
l = "{},{},{},{}".format("symbol".upper(), "combined_privious_open_int".upper(),
"combined_current_open_int".upper(), "combined_open_int_percentage_change".upper())
f.write(l+"\n")
if not current_date_file or not os.path.isfile(current_date_file):
return
if not previous_date_file or not os.path.isfile(previous_date_file):
return
with open(current_date_file, 'r') as f:
for line in f:
if line.split(',')[0] in future_index_stock:
symbol = line.split(',')[1]
open_int = line.split(',')[12]
close = line.split(',')[8]
expiry_date = line.split(',')[2]
if symbol in filtered_current_date_file:
filtered_current_date_file[symbol].append(
"{},{},{},{}".format(symbol, open_int, close, expiry_date))
else:
filtered_current_date_file[symbol] = ["{},{},{},{}".format(symbol, open_int, close, expiry_date)]
with open(previous_date_file, 'r') as f:
for line in f:
if line.split(',')[0] in future_index_stock:
symbol = line.split(',')[1]
open_int = line.split(',')[12]
close = line.split(',')[8]
expiry_date = line.split(',')[2]
if symbol in filtered_privious_date_file:
filtered_privious_date_file[symbol].append(
"{},{},{},{}".format(symbol, open_int, close, expiry_date))
else:
filtered_privious_date_file[symbol] = ["{},{},{},{}".format(symbol, open_int, close, expiry_date)]
for symbol, list_of_line in filtered_privious_date_file.items():
combined_privious_open_int = 0
for v in list_of_line:
if '.' in v.split(',')[1]:
privious_open_int = float(v.split(',')[1])
else:
try:
privious_open_int = int(v.split(',')[1])
except ValueError:
print v.split(',')[1]
privious_open_int = 0
combined_privious_open_int = combined_privious_open_int + privious_open_int
combined_current_open_int = 0
for v in filtered_current_date_file[symbol]:
if '.' in v.split(',')[1]:
current_open_int = float(v.split(',')[1])
else:
try:
current_open_int = int(v.split(',')[1])
except ValueError:
print v.split(',')[1]
current_open_int = 0
combined_current_open_int = combined_current_open_int + current_open_int
try:
combined_open_int_percentage_change = ((combined_current_open_int/(combined_privious_open_int*1.0)
- 1)*100)
except (ZeroDivisionError):
combined_open_int_percentage_change = "NA"
with open('combined_future_open_int.csv', 'a') as f:
s = "{},{},{},{}".format(symbol, combined_privious_open_int, combined_current_open_int,
combined_open_int_percentage_change)
f.write(s+"\n")


Get Price Change
def compare_price_change(current_date_file, previous_date_file):
filtered_current_date_file = {}
with open('price_change.csv', 'a') as f:
s1 = "{},{},{},{},{}".format("symbol".upper(),
"previous_close".upper(),
"current_close".upper(),
"close_percentage_change".upper(),
"expiry_date".upper()
)
f.write(s1+"\n")
if not current_date_file or not os.path.isfile(current_date_file):
return
if not previous_date_file or not os.path.isfile(previous_date_file):
return
with open(current_date_file, 'r') as f:
for line in f:
if line.split(',')[0] in future_index_stock:
symbol = line.split(',')[1]
open_int = line.split(',')[12]
close = line.split(',')[8]
expiry_date = line.split(',')[2]
if symbol in filtered_current_date_file:
filtered_current_date_file[symbol].append(
"{},{},{},{}".format(symbol, open_int, close, expiry_date))
else:
filtered_current_date_file[symbol] = ["{},{},{},{}".format(symbol, open_int, close, expiry_date)]
with open(previous_date_file, 'r') as f:
for line in f:
if line.split(',')[0] in future_index_stock:
symbol = line.split(',')[1]
open_int = line.split(',')[12]
close = line.split(',')[8]
expiry_date = line.split(',')[2]
current_data=""
for v in filtered_current_date_file[symbol]:
if expiry_date in v:
current_data = v
# print expiry_date, symbol, v
else:
continue
if '.' in current_data.split(',')[2]:
current_close = float(current_data.split(',')[2])
else:
try:
current_close = int(current_data.split(',')[2])
except ValueError:
# print current_data.split(',')[2]
current_close = 0
if '.' in close:
previous_close = float(close)
else:
try:
previous_close = int(close)
except ValueError:
previous_close = 0
try:
close_percentage_change = (((current_close-previous_close)/previous_close)*100)
except (ZeroDivisionError):
close_percentage_change = "NA"
# print "open_int_percentage_change"
# print current_data.split(',')[1], open_int
# open_int_percentage_change = "N/A"
with open('price_change.csv', 'a') as f:
s = "{},{},{},{},{}".format(symbol, previous_close, current_close, close_percentage_change, expiry_date)
f.write(s+"\n")


Main Method
def main():
try:
previous_date = sys.argv[1]
current_date = sys.argv[2]
except:
print "Please pass the previous date and current date argument"
print "Example 16-05-2018 17-05-2018"
sys.exit(1)
remove_file('combined_ce_open_int.csv')
remove_file('combined_pe_open_int.csv')
remove_file('combined_future_open_int.csv')
remove_file('price_change.csv')
remove_file('combined_ce_and_pe_open_int.csv')
current_date_file = download_bhavcopy(current_date)
previous_date_file = download_bhavcopy(previous_date)
compare_price_change(current_date_file, previous_date_file)
get_future_oi_change(current_date_file, previous_date_file)
get_option_oi_change(current_date_file, previous_date_file)
#imported function from convertCSVToXLSX.py file
remove_file(current_date_file)
remove_file(previous_date_file)
csvtoxlsx.csvToxlsx()
#remove_file('unFormatedStock_Details.xlsx')


Remove File
def remove_file(file_name):
if os.path.isfile(file_name):
os.remove(file_name)


Call Main Method
if __name__ == '__main__':
main()