<jupyter_start><jupyter_text>Cells for the following information as per 08-07-2019: 1. CE dataframe: cell 127 2. PE dataframe: cell 195 3. Merged Graph of CE and PE: 217<jupyter_code>import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
%matplotlib inline
os.getcwd()<jupyter_output><empty_output><jupyter_text>FOR CE:   1. Extract info from 3rd Jan - 10th Jan csv file<jupyter_code>link = "C:\\Users\\MAHE\\Desktop\\Data Science\\HYD\\Projects\\data\\banknifty"
os.chdir(link)
bn = pd.read_csv("OPTIDX_BANKNIFTY_CE_03-Jan-2019_TO_10-Jan-2019.csv", sep = ",")
bn
bn = bn_ce.drop(["Symbol","Option Type","LTP","Settle Price","No. of contracts","Turnover in Lacs","Premium Turnover in Lacs","Open Int","Change in OI","Underlying Value"],axis = 1)
bn = bn.set_index(["Expiry","Date","Strike Price"])
bn<jupyter_output><empty_output><jupyter_text>2. Extra data from the data csv:<jupyter_code>link = "C:\\Users\\MAHE\\Desktop\\Data Science\\HYD\\Projects\\data"
os.chdir(link)
df = pd.read_csv("data.csv", sep = ",")
df
df = df.drop(["Shares Traded","Turnover (Rs. Cr)"],axis = 1)
df
df = df.set_index("Date")
df<jupyter_output><empty_output><jupyter_text>3. Trial Week: 3rd to 10th   Trial Date: 3rd Jan 2019  a. pick a date from the data.csv<jupyter_code>df.loc["03-Jan-2019"]["Close"]
x = df.loc["03-Jan-2019"]["Close"]
xr = np.round(x,decimals = -2)
xr<jupyter_output><empty_output><jupyter_text>b. map this to the strike price of the bn in this way<jupyter_code>bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr+100]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr+200]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr+300]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr+400]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr-100]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr-200]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr-300]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr-400]["Close"]<jupyter_output><empty_output><jupyter_text>c. Take all the dates from 03 to 10 from the data.csv to a list:<jupyter_code>tempdf = df
tempdf = tempdf.reset_index()
tempdf<jupyter_output><empty_output><jupyter_text>d. create a for loop for the required dates from the data.csv:<jupyter_code>datelist = []
for i in range(2,8):
    datelist.append(tempdf.loc[i]["Date"])
datelist<jupyter_output><empty_output><jupyter_text>4. Trial of the Complete solution:   a. Our target is to work with one week ( 03 - 10/1/19 ), so take the close of the 03 as it was the expiry of the previous week, do the addsubround and keep these 9 values constant till 10th:<jupyter_code>df.loc["03-Jan-2019"]["Close"]
firstclose = np.round(df.loc["03-Jan-2019"]["Close"], decimals = -2)
firstclose
[firstclose]
closelist = []
for i in range(len(datelist)):
    te = [firstclose]
    closelist+=te
closelist
addsublist = [-400.0,-300.0,-200.0,-100.0,0.0,100.0,200.0,300.0,400.0]
list3rd = []
for i in addsublist:
    list3rd.append(bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[firstclose+i]["Close"])
list3rd
list4th = []
for i in addsublist:
    list4th.append(bn.loc["10-Jan-2019"].loc["04-Jan-2019"].loc[firstclose+i]["Close"])
list4th
list7th = []
for i in addsublist:
    list7th.append(bn.loc["10-Jan-2019"].loc["07-Jan-2019"].loc[firstclose+i]["Close"])
list7th
list8th = []
for i in addsublist:
    list8th.append(bn.loc["10-Jan-2019"].loc["08-Jan-2019"].loc[firstclose+i]["Close"])
list8th
list9th = []
for i in addsublist:
    list9th.append(bn.loc["10-Jan-2019"].loc["09-Jan-2019"].loc[firstclose+i]["Close"])
list9th
list10th = []
for i in addsublist:
    list10th.append(bn.loc["10-Jan-2019"].loc["10-Jan-2019"].loc[firstclose+i]["Close"])
list10th<jupyter_output><empty_output><jupyter_text>These are the required closing price :<jupyter_code>print(list3rd)
print(list4th)
print(list7th)
print(list8th)
print(list9th)
print(list10th)<jupyter_output>[476.2, 403.7, 338.35, 282.45, 231.65, 187.3, 148.2, 114.5, 86.75]
[665.05, 566.85, 491.95, 414.95, 342.3, 280.0, 221.75, 171.7, 128.25]
[696.2, 594.6, 507.05, 421.0, 344.15, 275.75, 214.4, 161.95, 119.5]
[872.9, 776.0, 677.6, 590.8, 501.15, 404.45, 317.95, 244.45, 181.6]
[1044.45, 935.85, 845.25, 747.25, 650.75, 553.55, 458.05, 360.75, 275.05]
[918.45, 831.95, 725.15, 630.55, 521.35, 413.35, 318.7, 222.4, 124.15]<jupyter_text>Complete the trial dataframe:<jupyter_code>result = pd.DataFrame()
result["Expiry"] = np.tile(["10-Jan-2019"],(len(datelist)*9))
result
result_date = []
for i in datelist:
    temp = list(np.tile(i,9))
    result_date+=temp
result_date
len(result_date)
result["Date"] = result_date
result
result_strike_price = []
for i in closelist:
    for j in addsublist:
        temp = [i+j]
        result_strike_price+=temp
result_strike_price
result["Strike Price"] = result_strike_price
result
result
result = result.set_index(["Expiry","Date","Strike Price"])
result
result["Close"] = list3rd+list4th+list7th+list8th+list9th+list10th
result
["Strike Price"]+datelist
firstclose = int(firstclose)
firstclose
addsublist
closeaddsub = [firstclose+t for t in addsublist]
closeaddsub
nestedclose = [list3rd,list4th,list7th,list8th,list9th,list10th]
nestedclose
final = pd.DataFrame(columns = ["Strike Price"]+datelist+["Expiry Date"])
final["Strike Price"] = closeaddsub
final
k = 0
for i in datelist:
    final[i] = nestedclose[k]
    k+=1
final
final["Expiry Date"] = "10-Jan-2019"
final
final.set_index("Strike Price",inplace = True)
final<jupyter_output><empty_output><jupyter_text>5. Final dataframe for the trial week 03-10th Jan:<jupyter_code>final<jupyter_output><empty_output><jupyter_text>6. Use Matplotlib:<jupyter_code>datelist
closey = []
for i in datelist:
    te = bn.loc["10-Jan-2019"].loc[i].loc[27000]["Close"]
    closey+=[te]
closey<jupyter_output><empty_output><jupyter_text>Trial 1   Check the closing price of 3rd to 10th with strike price = 27000 ( since this was the rounded closing price of the data.csv file)<jupyter_code>x_axis = datelist
y_axis = closey
plt.plot(x_axis,y_axis,"go--",label="27000")
plt.xlabel("Date")
plt.ylabel("Strike Price")
plt.title("Trial Data")
plt.legend()<jupyter_output><empty_output><jupyter_text>Trial 2   Check the closing price of 3rd to 10th with strike price = 26600, 26700, 26800, 26900,27000,27100, 27200, 27300, 27400 ( since these were the rounded closing price of the data.csv file)<jupyter_code>x_list = datelist
len(x_list)
ya,yb,yc,yd,ye,yf,yg,yh,yi = [],[],[],[],[],[],[],[],[]
for i in datelist:
    te1 = bn.loc["10-Jan-2019"].loc[i].loc[26600]["Close"]
    te2 = bn.loc["10-Jan-2019"].loc[i].loc[26700]["Close"]
    te3 = bn.loc["10-Jan-2019"].loc[i].loc[26800]["Close"]
    te4 = bn.loc["10-Jan-2019"].loc[i].loc[26900]["Close"]
    te5 = bn.loc["10-Jan-2019"].loc[i].loc[27000]["Close"]
    te6 = bn.loc["10-Jan-2019"].loc[i].loc[27100]["Close"]
    te7 = bn.loc["10-Jan-2019"].loc[i].loc[27200]["Close"]
    te8 = bn.loc["10-Jan-2019"].loc[i].loc[27300]["Close"]
    te9 = bn.loc["10-Jan-2019"].loc[i].loc[27400]["Close"]        
    ya+=[te1]
    yb+=[te2]
    yc+=[te3]
    yd+=[te4]
    ye+=[te5]
    yf+=[te6]
    yg+=[te7]
    yh+=[te8]
    yi+=[te9]
ya
yb
yc
yd
ye
yf
yg
yh
yi
plt.plot(x_axis,ya,"bo--",label="26600")
plt.plot(x_axis,yb,"go--",label="26700")
plt.plot(x_axis,yc,"ro--",label="26800")
plt.plot(x_axis,yd,"co--",label="26900")
plt.plot(x_axis,ye,"mo--",label="27000")
plt.plot(x_axis,yf,"yo--",label="27100")
plt.plot(x_axis,yg,"ko--",label="27200")
plt.plot(x_axis,yh,"wo--",label="27300")
plt.plot(x_axis,yi,"go--",label="27400")
plt.xlabel("Date")
plt.ylabel("Strike Price")
plt.title("Trial Data")
plt.legend()<jupyter_output><empty_output><jupyter_text>FOR PE:  1. Extract info from 3rd Jan - 10th Jan csv file<jupyter_code>link = "C:\\Users\\MAHE\\Desktop\\Data Science\\HYD\\Projects\\data\\banknifty"
os.chdir(link)
bn = pd.read_csv("OPTIDX_BANKNIFTY_PE_03-Jan-2019_TO_10-Jan-2019.csv", sep = ",")
bn
bn = bn.drop(["Symbol","Option Type","LTP","Settle Price","No. of contracts","Turnover in Lacs","Premium Turnover in Lacs","Open Int","Change in OI","Underlying Value"],axis = 1)
bn = bn.set_index(["Expiry","Date","Strike Price"])
bn<jupyter_output><empty_output><jupyter_text>2. Extra data from the data csv:<jupyter_code>link = "C:\\Users\\MAHE\\Desktop\\Data Science\\HYD\\Projects\\data"
os.chdir(link)
df = pd.read_csv("data.csv", sep = ",")
df
df = df.drop(["Shares Traded","Turnover (Rs. Cr)"],axis = 1)
df
df = df.set_index("Date")
df<jupyter_output><empty_output><jupyter_text>3. Trial Week: 3rd to 10th   Trial Date: 3rd Jan 2019  a. pick a date from the data.csv<jupyter_code>df.loc["03-Jan-2019"]["Close"]
x = df.loc["03-Jan-2019"]["Close"]
xr = np.round(x,decimals = -2)
xr<jupyter_output><empty_output><jupyter_text>b. map this to the strike price of the bn in this way<jupyter_code>bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr+100]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr+200]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr+300]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr+400]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr-100]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr-200]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr-300]["Close"]
bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[xr-400]["Close"]<jupyter_output><empty_output><jupyter_text>c. Take all the dates from 03 to 10 from the data.csv to a list:<jupyter_code>tempdf = df
tempdf = tempdf.reset_index()
tempdf<jupyter_output><empty_output><jupyter_text>d. create a for loop for the required dates from the data.csv:<jupyter_code>datelist = []
for i in range(2,8):
    datelist.append(tempdf.loc[i]["Date"])
datelist<jupyter_output><empty_output><jupyter_text>4. Trial of the Complete solution:   a. Our target is to work with one week ( 03 - 10/1/19 ), so take the close of the 03 as it was the expiry of the previous week, do the addsubround and keep these 9 values constant till 10th:<jupyter_code>df.loc["03-Jan-2019"]["Close"]
firstclose = np.round(df.loc["03-Jan-2019"]["Close"], decimals = -2)
firstclose
[firstclose]
closelist = []
for i in range(len(datelist)):
    te = [firstclose]
    closelist+=te
closelist
addsublist = [-400.0,-300.0,-200.0,-100.0,0.0,100.0,200.0,300.0,400.0]
list3rd = []
for i in addsublist:
    list3rd.append(bn.loc["10-Jan-2019"].loc["03-Jan-2019"].loc[firstclose+i]["Close"])
list3rd
list4th = []
for i in addsublist:
    list4th.append(bn.loc["10-Jan-2019"].loc["04-Jan-2019"].loc[firstclose+i]["Close"])
list4th
list7th = []
for i in addsublist:
    list7th.append(bn.loc["10-Jan-2019"].loc["07-Jan-2019"].loc[firstclose+i]["Close"])
list7th
list8th = []
for i in addsublist:
    list8th.append(bn.loc["10-Jan-2019"].loc["08-Jan-2019"].loc[firstclose+i]["Close"])
list8th
list9th = []
for i in addsublist:
    list9th.append(bn.loc["10-Jan-2019"].loc["09-Jan-2019"].loc[firstclose+i]["Close"])
list9th
list10th = []
for i in addsublist:
    list10th.append(bn.loc["10-Jan-2019"].loc["10-Jan-2019"].loc[firstclose+i]["Close"])
list10th<jupyter_output><empty_output><jupyter_text>These are the required closing price :<jupyter_code>print(list3rd)
print(list4th)
print(list7th)
print(list8th)
print(list9th)
print(list10th)<jupyter_output>[84.75, 110.95, 141.1, 180.3, 223.9, 277.75, 336.3, 402.35, 471.9]
[26.15, 37.35, 53.65, 74.0, 101.05, 134.6, 174.35, 219.7, 274.55]
[12.45, 18.6, 28.0, 40.45, 57.55, 79.95, 113.8, 156.35, 209.4]
[4.9, 6.6, 9.55, 14.15, 21.35, 31.25, 45.9, 68.3, 99.2]
[2.05, 2.25, 2.8, 3.3, 4.35, 5.75, 7.85, 12.05, 21.7]
[0.05, 0.05, 0.05, 0.05, 0.05, 0.1, 0.05, 0.05, 0.15]<jupyter_text>Complete the trial dataframe:<jupyter_code>result = pd.DataFrame()
result["Expiry"] = np.tile(["10-Jan-2019"],(len(datelist)*9))
result
result_date = []
for i in datelist:
    temp = list(np.tile(i,9))
    result_date+=temp
result_date
len(result_date)
result["Date"] = result_date
result
result_strike_price = []
for i in closelist:
    for j in addsublist:
        temp = [i+j]
        result_strike_price+=temp
result_strike_price
result["Strike Price"] = result_strike_price
result
result
result = result.set_index(["Expiry","Date","Strike Price"])
result
result["Close"] = list3rd+list4th+list7th+list8th+list9th+list10th
result
["Strike Price"]+datelist
firstclose = int(firstclose)
firstclose
addsublist
closeaddsub = [firstclose+t for t in addsublist]
closeaddsub
nestedclose = [list3rd,list4th,list7th,list8th,list9th,list10th]
nestedclose
final = pd.DataFrame(columns = ["Strike Price"]+datelist+["Expiry Date"])
final["Strike Price"] = closeaddsub
final
k = 0
for i in datelist:
    final[i] = nestedclose[k]
    k+=1
final
final["Expiry Date"] = "10-Jan-2019"
final
final.set_index("Strike Price",inplace = True)
final<jupyter_output><empty_output><jupyter_text>5. Final dataframe for the trial week 03-10th Jan:<jupyter_code>final<jupyter_output><empty_output><jupyter_text>6. Use Matplotlib:<jupyter_code>datelist
closey = []
for i in datelist:
    te = bn.loc["10-Jan-2019"].loc[i].loc[27000]["Close"]
    closey+=[te]
closey<jupyter_output><empty_output><jupyter_text>Trial 1   Check the closing price of 3rd to 10th with strike price = 27000 ( since this was the rounded closing price of the data.csv file)<jupyter_code>x_axis = datelist
y_axis = closey
plt.plot(x_axis,y_axis,"go--",label="27000")
plt.xlabel("Date")
plt.ylabel("Strike Price")
plt.title("Trial Data")
plt.legend()<jupyter_output><empty_output><jupyter_text>Trial 2   Check the closing price of 3rd to 10th with strike price = 26600, 26700, 26800, 26900,27000,27100, 27200, 27300, 27400 ( since these were the rounded closing price of the data.csv file)<jupyter_code>x_list = datelist
len(x_list)
ya,yb,yc,yd,ye,yf,yg,yh,yi = [],[],[],[],[],[],[],[],[]
for i in datelist:
    te1 = bn.loc["10-Jan-2019"].loc[i].loc[26600]["Close"]
    te2 = bn.loc["10-Jan-2019"].loc[i].loc[26700]["Close"]
    te3 = bn.loc["10-Jan-2019"].loc[i].loc[26800]["Close"]
    te4 = bn.loc["10-Jan-2019"].loc[i].loc[26900]["Close"]
    te5 = bn.loc["10-Jan-2019"].loc[i].loc[27000]["Close"]
    te6 = bn.loc["10-Jan-2019"].loc[i].loc[27100]["Close"]
    te7 = bn.loc["10-Jan-2019"].loc[i].loc[27200]["Close"]
    te8 = bn.loc["10-Jan-2019"].loc[i].loc[27300]["Close"]
    te9 = bn.loc["10-Jan-2019"].loc[i].loc[27400]["Close"]        
    ya+=[te1]
    yb+=[te2]
    yc+=[te3]
    yd+=[te4]
    ye+=[te5]
    yf+=[te6]
    yg+=[te7]
    yh+=[te8]
    yi+=[te9]
ya
yb
yc
yd
ye
yf
yg
yh
yi
plt.plot(x_axis,ya,"bo--",label="26600")
plt.plot(x_axis,yb,"go--",label="26700")
plt.plot(x_axis,yc,"ro--",label="26800")
plt.plot(x_axis,yd,"co--",label="26900")
plt.plot(x_axis,ye,"mo--",label="27000")
plt.plot(x_axis,yf,"yo--",label="27100")
plt.plot(x_axis,yg,"ko--",label="27200")
plt.plot(x_axis,yh,"wo--",label="27300")
plt.plot(x_axis,yi,"go--",label="27400")
plt.xlabel("Date")
plt.ylabel("Strike Price")
plt.title("Trial Data")
plt.legend()<jupyter_output><empty_output><jupyter_text>MERGE THE GRAPHS OF CE AND PE:<jupyter_code>x_axis = datelist
y_ce = [231.65, 342.3, 344.15, 501.15, 650.75, 521.35]
y_pe = [223.9, 101.05, 57.55, 21.35, 4.35, 0.05]
plt.plot(x_axis,y_ce,"ro--",label="CE 27000")
plt.plot(x_axis,y_pe,"go--",label="PE 27000")
plt.xlabel("Date")
plt.ylabel("Strike Price")
plt.title("Trial Data")
plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20018092530369605, 'fraction_numerical': 0.11281985009046265, 'mean_word_length': 5.400744416873449, 'pattern_counts': {'":': 0, '<': 111, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '28', 'dir': 'jupyter-structured-clean-dedup', 'id': '34369', 'license': 'MIT', 'n_tokens_mistral': 7706, 'n_tokens_neox': 6481, 'n_words': 1141, 'path': 'Data-Science-HYD-2k19/Projects/codes/PROJECT 1 (Jupiter ) ( Bank Nifty )/Merge BankNift and Data/.ipynb_checkpoints/REDO PROJECT JUPITER-checkpoint.ipynb', 'repo_name': 'Sanjay9921/Python', 'size': '15476'}