Hi ,
The Course "Options Trading Strategies In Python: Advanced" is very helpful . Especially "
Dispersion Trading Strategy" topic and code.
Based on the given code , I tried to develop 1 with equal weights for all Nifty 50 and its constitutents .
However the results are working for NIFTY INDEX, but for its constitutents the returns are not positive .
Meaning , if the signal is -1 , we Generate Buy signal for Nifty 50 Constitutents and when i check in backtesting manual way , the returns were negative .
Just wanted to check if I am doing something wrong .
I have loaded the data into oracle database and from there i am fetching .
I have given the code in this link . Please let me know if anything is wrong with the code/ strategy used by me with Equal weights for Constifutents .
The data used is in link https://docs.google.com/spreadsheets/d/1TP_tdub1TlqUC9Ba0DTbsdws-rkQCe4a/edit?usp=sharing&ouid=104578472694732943326&rtpof=true&sd=true
Please help
Thank you !!!
With Warm Regards
Nandagopal
Hey Nandagopal,
We are really glad that you found this course helpful.
The two files that you have shared above are currently private.
Could you please change the settings for the same to public so that we can have a look at it?
Alternatively, you can email the files to us at quantra@quantinsti.com (in case you do not wish to make the files public.)
Looking forward to helping you out with this.
Regards,
Kevin
hi ,
Kindly apologize . Please use the below links and let me know if you are able to access . Also , I have sent the same files in email .
Data link
-------------
https://docs.google.com/spreadsheets/d/1TP_tdub1TlqUC9Ba0DTbsdws-rkQCe4a/edit?usp=sharing&ouid=104578472694732943326&rtpof=true&sd=true
Code Link
--------------
QuantraDispersiontrading-SendtoQuantra.py - Google Drive
Thank you !!!
WIth Warm Regards
Nandagopal
Hey Nandagopal,
Thanks for sharing the access.
I did go through your code and I must say, it is really impressive that you have implemented the dispersion strategy on the Nifty50 index and its constituents.
As you might be aware, we follow the following steps in this strategy:
- Buy the index straddle and sell the index constituents’ straddle when the implied correlation is low.
- Sell the index straddle and buy the index constituents’ straddle when the implied correlation is high.
- Exit the positions when the implied correlation reverts to the mean.
However, when you calculate the profit and loss for the same (i.e. the daily pnl for the index straddle or even the index constituents straddle, it may be possible that the returns are not always positive on all the trading days.
Apart from this, I also noticed that the dataset you utilised contains data right from the year 2015.
It is very likely that the constituents of Nifty50 must have changed multiple times over these past few years.
For example:
In 2015, the Nifty50 index also included stocks like Idea and Yesbank, which are not a part of the index anymore.
Determining the constituents of the index too is an important point to keep in mind while implementing the dispersion trading strategy.
If you are wondering how to fetch the data of historical constituents of Nifty50, here's the link to get the same from NSE. You need to select the option 'indices - market capitalization & weightage' and choose a relevant date to fetch and download the data.
I hope this was helpful to you.
Hi ,
Thank you !!!
Understood . Is it ok if we use equal weights .
I have done for Nifty 50 and its constituents with equal weights
Thank you !!!
With Warm Regards
Nandagopal
Hey Nandagopal,
It would be best to maintain the weights as per the constituent stock's weightage within the index.
Also, I strongly recommend you explore some of the resources I have shared below.
You will definitely find these to be interesting and useful.
Blogs:
Dispersion Trading Using Options
Dispersion Strategy Based On Correlation Of Stocks And Volatility Of Index
Video:
How to Trade Volatility Dispersion
Regards,
Kevin
Hi Kevin ,
Thank you !!!
It is indeed every helpful.
Need one help
while calculating PNL i am getting the below error
File "C:\Users\NANDAG~1\AppData\Local\Temp/ipykernel_12280/1775862426.py", line 5, in <module>
& (opt.Date <= trade_date + datetime.timedelta(days=20)
TypeError: can only concatenate str (not "datetime.timedelta") to str
The above error is comming from the function . Please help to update .
def daily_pnl(opt, full_opt):
#opt=optdata
#full_opt=full_optdata
opt['next_day_close'] = np.nan
opt.sort_values('RCD_DATE', inplace=True, ascending=True)
#k=0
for k in range(0, len(opt)-2):
strike_price = opt.iloc[k]['STRIKE_PR']
trade_date = opt.iloc[k]['RCD_DATE']
#print("Trade Date="+str(trade_date))
next_trading_date = opt[(opt.RCD_DATE > trade_date)
& (opt.RCD_DATE <= trade_date + datetime.timedelta(days=20)
)].iloc[0]['RCD_DATE']
option_type = opt.iloc[k]['OPTION_TYP']
t=full_opt[(full_opt['STRIKE_PR'] == strike_price) & (full_opt['RCD_DATE'] == opt[(opt.RCD_DATE > trade_date)
& (opt.RCD_DATE <= trade_date + datetime.timedelta(days=20)
)].iloc[0]['RCD_DATE'])]
if opt.iloc[k]['time_to_expiry'] != 0 & t['SYMBOL'].count()>0 :
opt.iloc[k, opt.columns.get_loc('next_day_close')] = full_opt[(full_opt['STRIKE_PR'] == strike_price) &
(full_opt['RCD_DATE'] == next_trading_date) &
(full_opt['OPTION_TYP']== option_type)
].iloc[0]['CLSE']
else:
# This is done because on expiry day the next day price doesn't exists
opt.iloc[k, opt.columns.get_loc(
'next_day_close')] = opt.iloc[k]['CLSE']
opt['daily_straddle_pnl'] = opt.next_day_close - opt.CLSE
return opt
Thank you !!!
With Warm Regards
Nandagopal
Hi Nandagopal,
I couldn't replicate the exact same error on my end.
However, I presume that you are currently trying to concatenate a 'datetime' object to a 'string'.
Hence you are facing the following error:
'can only concatenate str (not "datetime.timedelta") to str'
To fix this, you can refer to the link I have shared here, you will learn how to convert the string to a datetime object.
However, if you're still facing any difficulty, you can share the complete notebook along with the data file and we will try our best to help you out with it.
Hope this helps!
Hi Kevin,
Thank you !!!
When i use the lower versions of Python the code is working !!!
Have one doubt . Regarding Dispersion trading .
Please find the below link containing Buy / Sell signal generated using our Course .
DF-position.xlsx - Google Sheets
data used is as below
https://docs.google.com/spreadsheets/d/1bDjxSgCY9LuNlnO8dGeJhbVBopH8uSMgAdNfx5fJiCk/edit?usp=sharing
As per our code , the Position is as follows
We will store:
- '+1' for long on index straddle and short on index constituents straddle
- Vice-versa, '-1' for short on index straddle and long on index constituents straddle
- '0' is stored to exit the positions in index and index constituents
Below are my doubts
1. If the +1 signal is generated today , we need to buy the index and short its constitutents the next day. Is my understanding correct?
2. As per the sheet in the above link , On 14-Jan-2015 there is a -1 positions and he next day it became exit signal . how to overcome these Whipsaws
3. If i need to take a position on the month beginig how to do this .
Thank you !!!
WIth Warm Regards
Nandagopal
Hey Nandagopal,
To summarise, below are the answers to your questions based on yesterday's discussion.
- If the +1 signal is generated today, we need to buy the index and short its constituents the next day. Is my understanding correct?
–> In the notebook, yes, we have taken the required positions on the next day, depending on the value of the signal generated on the previous day. But if you yourself are implementing the dispersion trading strategy, you can consider taking the required positions whenever you receive the valid signal.
For example, if you are implementing this strategy on an hourly close basis, there is no need to wait until the next day to take your positions. You can initiate the positions as soon as you receive a signal.
- As per the sheet in the above link, On 14-Jan-2015 there is a -1 position and the next day it became an exit signal. How to overcome these Whipsaws?
–> You can experiment by increasing the threshold value of the standard deviation bands, this might reduce the number of trades, but at the same time will improve the reliability of your generated signals.
- If I need to take a position on the month beginning how to do this.
–> We will take positions solely on the basis of the signals we receive from our strategy. If we receive a '+1' or a '-1' value at the beginning of the month, then we will simply take the required positions, as we do during the other days.
I hope all of your doubts have been cleared!
Hi Kevin ,
Thank you Very much!!!
Now I have some clarity .
Now I am Trying to use decision tree in the test data to predict whether the option price will go up (+1) or go down (-1) tomorrow. This is based on "Options Price Prediction Using Decision Tree- Advanced"- Section 3. Machine Learning Code.
Hi Kevin ,
Thank you Very much!!!
Now I have some clarity .
Now I am Trying to use decision tree in the test data to predict whether the option price will go up (+1) or go down (-1) tomorrow. This is based on "Options Price Prediction Using Decision Tree- Advanced"- Section 3. Machine Learning Code.
I am using the below data for test and train
DecissionTree.xlsx - Google Sheets
I am using the data of Current Expiry month as test data .
All the data before the expiry date for that Strike Price , i am using as Training data
cls = DecisionTreeClassifier(max_depth=6, min_samples_split=2, max_leaf_nodes=8)
cls.fit(Train_clpredictors, Train_cltarget)
I am getting the below error on the above code.
ValueError: Input contains NaN, infinity or a value too large for dtype('float32').
Any suggestion to resolve this error would be of great help.
Hey Nandagopal,
Sorry for the delay in getting back.
A very similar query had been answered before on the Quantra community.
Below is a link to the same for your reference
Input contains NaN, infinity or a value too large for dtype('float32').
You might be able to fix this issue by simply replacing the 'NaN' values with 'zero' and 'infinity' values with 'large finite numbers'.
After running the following block of code:
# Number of days to train algo
t = 70
# Train dataset
predictors_train = predictors[:t]
target_train = target[:t]
# Test dataset
predictors_test = predictors[t:]
target_test = target[t:]
You can try adding the following lines of code:
predictors_train[:] = np.nan_to_num(predictors_train)
predictors_test[:] = np.nan_to_num(predictors_test)
To read and understand more about what might be causing this problem, do check out this thread.
I hope this was helpful.
Hi Kevin ,
Thank you very much!!
As suggested by you removing NAN values and reset_index() fixed the issue .
As always , you have helped me a lot .
Have one more doubt !!!
The Nifty_ML_data.csv used for Prediction using Decision tree classification algorithim , contains Call or Put Greeks?
Because i think Put and call Greeks are calculated seperately .
Should i use only Call greeks or Put Greeks .
Thank you !!!
with Warm Regard
Nandagopal
Hey Nandagopal,
The dataset 'Nifty_ML_data.csv' used in the Notebook on 'Prediction using Decision Tree Classification Algorithm', contains data corresponding to the Call Greeks.
You too can proceed and use the same, for the purpose of analysis.
Best Regards,
Kevin
Hi Kevin,
Thank you !!!
one more doubt.
I have the data for straddle PL . I took the max Straddle PL and the number of days it took to reach the max profit . Below is the example
Symbol Expiry_date Purchase_date Max_profit Num_of_days_took_to_reach_max
TATAMOTORS 31-Apr-2016 15-Apr-2016 20 10
ACC 31-Apr-2016 10-Apr-2016 10 6
TATAMOTORS 31-May-2016 05-May-2016 15 8
Nee one help . How to use Decission tree classifications with this kind of data . Or what are the kind of algorithims that can be used to find in how many days the max profit will be reached based on the past data.
Thank you !!!
WIth Warm Regards
Nandagopal
Hi Kevin,
Thank you !!!
one more doubt.
I have the data for straddle PL . I took the max Straddle PL and the number of days it took to reach the max profit . Below is the example
Symbol Expiry_date Purchase_date Max_profit Num_of_days_took_to_reach_max
TATAMOTORS 31-Apr-2016 15-Apr-2016 20 10
ACC 31-Apr-2016 10-Apr-2016 10 6
TATAMOTORS 31-May-2016 05-May-2016 15 8
Nee one help . How to use Decission tree classifications with this kind of data . Or what are the kind of algorithims that can be used to find in how many days the max profit will be reached based on the past data.
with Accuracy_score function , i am not able create accuracy_score for targets with 2 columns .
Kindly let me know how can i calculate accuracy_score for targets with 2 columns .
2 Doubts :
1. algorithims that can be used to find in how many days the max profit will be reached based on the past data.
2. how can i calculate accuracy_score for targets with 2 columns .
Thank you !!!
WIth Warm Regards
Nandagopal
Hi Nandagopal,
It seems to me that the approach you are taking isn't the best one.
I say this because the 'no. of days to reach max profit' is a variable that can be computed easily while looking back at past data. However, when you try to reverse-engineer the same, you'll realise that there were too many aspects that may have affected this variable. For example, entry date, fundamental events, overall market sentiments, news articles and brokerage ratings, volatility and trading volumes, etc.
In your case, when it comes to straddles, even you would agree that 'volatility' plays a major impact role in driving your profits. Thus, studying the 'change in volatility' over a given period of time would be much more effective, if you are trying to better time your exits with straddles.
Also, regarding your second point, I feel using regression would be better suited for this case.
I have shared a couple of resources below that might be of help.
Hi kevin ,
Thank you !!!
Agree with you that Volatility and Greeks play important part . My features include open,high , low . close , IV , Delta , Gamma and Theta . Using these features i need to predict (no. of days to reach max profit, max profit).
As per the resources you shared , below is my understanding :
1. model accuracy can be computed as MAPE , MSE etc for multi output models
2. Regression is the only algorihim to be used for such predictions
is there a mapping of problems to algorithims . like for these kind of Problems , these algorithims are best .
Thank you !!!
WIth Warm Regards
Nandagopal
Hi Nandagopal,
You can refer to the article linked below to understand more about the use case for the algorithms.
I hope this was helpful.
Regards,
Kevin