Trying to figure out how to calculate Greeks at different underlying prices. The idea is to do it in advance of needing the actual trades executed. The method used here is pretty hacky. Would be open to trying a different way. Also not sure if the changes made to the self.option object have lasting effects.
The problem is that in the backtest it appears the delta calculation is being done wrong because once the price dips low enough to kick in the trade the delta doesn't become neutral, it's 99 off, which instigates another trade very soon after at almost the same price.
In the code below we need to get "this_trade_bar" because "SetMarketPrice" doesn't work without a BaseData object and I couldn't figure out how to initialize a BaseData object.
this_trade_bar = self.equity.GetLastData()
# update needs a bunch of dummy variables which I hope don't affect the calculation
this_trade_bar.Update(new_price, 1.0, 1.0, 1.0, 1.0, 1.0)
# We're not sure yet what lasting effects setting market price has on the option object
self.option.Underlying.SetMarketPrice(this_trade_bar)
this_option_model_result = self.option.PriceModel.Evaluate(self.option, slice, this_option)
this_delta = this_option_model_result.Greeks.Delta
# delta * num options * num stocks per contract - num stocks already - thresholds covered
num_shares_to_hedge = this_delta * self.Portfolio[this_option.Symbol].Quantity * 100 \
- self.Portfolio[self.stock].Quantity \
- thresholds_so_far * self.delta_threshold * sign
Hello
So what appears to be the issue is the original option contract hasn't been updated to reflect the new price and so the greek calculations are off.
Alethea Lin
Hello,
Thanks for sharing the algorithm. This is pretty tricky to achieve. Here are a few thoughts after looking at your code:
Sorry we could not be more helpful.
Hello
Hey!
So this idea comes directly from the code for the Evaluate function. In particular line 93 here;
public OptionPriceModelResult Evaluate(Security security, Slice slice, OptionContract contract) { try { // setting up option pricing parameters var calendar = new UnitedStates(); var dayCounter = new Actual365Fixed(); var optionSecurity = (Option)security; var settlementDate = contract.Time.Date.AddDays(Option.DefaultSettlementDays); var maturityDate = contract.Expiry.Date.AddDays(Option.DefaultSettlementDays); var underlyingQuoteValue = new SimpleQuote((double)optionSecurity.Underlying.Price);
A couple of questions about possible solutions if calling SetMarketPrice() is not a good idea
Let's say I did go down the route of adding an additional input to the Evaluate function which took the current price directly from an input variable instead of getting it from the OptionSecurity.Underlying.Price attribute. Would it even be possible to run this in the online IDE or on a live server?
Alethea Lin
Hi,
It is possible to run anything in the online IDE and on a live server if you do decide to pursue down the route. However, what you are trying to achieve is beyond what QC's basic framework intends to offer, and the problem is very tricky. If you want to continue on this mission, I have one advice. If you change the security price with either SetRealTimePrice() or SetMarketPrice() for Greeks calculations, you need to make sure to change it back once the calculation is done.
Hello
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!