0

I have a trade history in a dataframe called trades. I have options data for the past years, and I wanted to check if I had taken those trades in options instead of futures. What would be my return?

I have used the following code:

Create a dataframe for the ATM Option

atm = pd.DataFrame()
atm['Start Date'] = trades['Start Date']
atm['End Date'] = trades['End Date']
atm['Entry Time'] = trades['Time']
atm['Exit Time'] = trades['Time.1']
atm[['Buy Price', 'Sell Price']] = np.nan


for j, row in trades.iterrows():

# Creating dataframe of relevant option contract

    strike = (trades.at[j, 'Buy']//50)*50

    if trades.at[j, 'year'] == 2020:
        strike = (trades.at[j, 'Buy']//100)*100

path = str(trades.at[j, 'year']) + ' NIFTY/' + str(trades.at[j, 'month']) + '/NIFTY' + str(strike) + 'CE.csv'
df = pd.read_csv(path)

if len(df.columns) == 8:
    df.columns = ['Strike', 'Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Vol']
if len(df.columns) == 9:
    df.columns = ['Strike', 'Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Vol', 'OI']

# Formatting the dates of option to make it same as trade data

for i, row in df.iterrows():

    datetimeobject = dt.datetime.strptime(df.at[i, 'Date'],'%Y/%m/%d')
    df.at[i, 'Date'] = datetimeobject.strftime('%m/%d/%Y')
    datetimeobject = dt.datetime.strptime(df.at[i, 'Time'],'%H:%M')
    df.at[i, 'Time'] = datetimeobject.strftime('%H:%M:%S')

# Filling entry and exit price from options data

for i, row in df.iterrows():

    if df.at[i, 'Date'] == trades.at[j, 'Start Date']:
        if df.at[i, 'Time'] == trades.at[j, 'Time']:
            atm.at[j, 'Buy Price'] = df.at[i, 'Close']
            print(df.at[i, 'Close'])

for i, row in df.iterrows():

    if df.at[i, 'Date'] == trades.at[j, 'End Date']:
        if df.at[i, 'Time'] == trades.at[j, 'Time.1']:
            atm.at[j, 'Sell Price'] = df.at[i, 'Close']

For some reason, only the first entry of the ATM dataframe is getting filled.

Enter image description here

The trades dataframe looks like this:

Enter image description here

So far I've understood that the second last loop used to fetch the entry price is running for all iterations, but it does does not fetch the price and the last loop to fetch exit prices doesn't run at all.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

0 Answers0