I created a strategy to sell SPXW DTE 0 call or put at delta 0.01 at price > 0.999 spx day high or price < 1.001 spx day low.
Sharpe ratio is super high but return is bad < 2.5% annual return.
Any suggestion on how to improve return to more than 15% but sharpe ratio still above 3? Maybe i should sell spread instead?
Yuri Lopukhov
Returns are low because you only trade 1 contract. This code assigns 5% of balance, which is still too much I guess, DD is 65%, but Sharpe is way above 3 ;)
I would strongly recommend testing on larger time period though.
Yuri Lopukhov
Actually, I shouldn't have changed
to
to cancel the deprecation message. Because this is called for both SPX and each SPXW contract. Not sure how the deprecated version is called though, need to check this.
Thomas
Thank you so much Yuri.
I think we should use schedule on . OnEndofDay trigger actions at 15:50 everyday (it took a newbie like me half a day to figure out :( .
Thomas
Thank you also for the suggestion Yuri , DD is 65% on Jan 23 is not fun .. open at 183k and low at 63k :)) ,,, I wonder what would happen during covid . Will run the backtest tomorrow
Yuri Lopukhov
Yes, it is better to just use self.Schedule.On() if you know exact time. You should also add symbol in self.DateRules.EveryDay() as a parameter to skip non-trading days for that symbol. But this is optional, in your case OnData simply won't run when the market is off anyway.
You can play with position size, of course, to get the risk/reward ratio that suits you.
Other possible option would be to add risk management models (https://www.quantconnect.com/docs/v2/writing-algorithms/algorithm-framework/risk-management/supported-models):
or just use stop/limit orders to reduce slippage (https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/order-types).
Non Compete
note https://github.com/QuantConnect/Lean/blob/master/Algorithm.Framework/Risk/MaximumDrawdownPercentPerSecurity.py
uses negative unrealized profit, so i dont think it takes profit, just protects from losses. I created this issue but maybe i missed something.
https://github.com/QuantConnect/Lean/issues/6841
Yuri Lopukhov
Yes, I just misplaced comments to MaximumDrawdownPercentPerSecurity (stop loss) and MaximumUnrealizedProfitPercentPerSecurity (take profit).
Thomas
Not a very good idea to run an option selling strategy with stop loss.
Option price is much more volatile than stock and when index is down , the downtrend on option price is much more significant than when index is up.
I won't recommend at all. See code and result below
Thomas
Also would u know if Quantconnect support option price for the last 3trading days? The algo didn't show anything whne I backtested on 15 , 16, 17 Feb
Thomas
in addition even without the stop loss/take profit the algo suggests selling >60 contracts of SPX :))
In reality that would require a >$1million account with > $10million buying power . This is not executable.
Thomas
In addition , Yuri & Non-compete , may i know if you know of a simple function to take a day high / day low?
Yuri Lopukhov
There is no simple function for that AFAIK, you can do it the way it was done in this algo, or use Identity indicator with High or Low fields.
So you mean QuantConnect is not calculating required buying power correctly here?
Yuri Lopukhov
it usually takes couple of days before real-time data becomes available for backtesting.
Yuri Lopukhov
sorry, Identity won't work here, you can use self.MAX, self.MIN indicators like this:
and in the beginning of each day call self.max.Reset() / self.min.Reset().
Or use and update Maximum()/Minimum() indicators manually in OnData().
Either way is hardly easier.
There is actually a feature request of this functionality on github here:
Thomas
Thank you very much Yuri.. I decided to use min/max & modify the code to selling spread to control the risk. However the risk is still quite significant . Stoploss doesn't work in the case of options due to super high volatility of the option price.
Still - I risk loosing like 15k on an account of 50k. The probability is low <3% but still… not for those with the fainted heart. Below shows the testing from 3 Jan , if i tested on Dec 22 data, it will result in significant loss.
And regarding your question Yuri, Yes I think quantconnect did not calculate the buying power correctly. Because a IBRK would require a buying power of 80k for selling an spx put/call naked . Meaning that for an account size of 100k i can sell max 1 contract but in the algorithm it allow me to sell 60 contracts !
Thomas
To illustrate my point please see the below backtest, on 12 Dec 2022, market was relatively calm still 14:30. Form 14:30 to 16:00 market jumped more than 30 points resulting in 10% loss to the portfolio. The algo will then take approximately 3months to recover
Yuri Lopukhov
hmm, I guess it can work like this too, but this does not look optimal. I think you can just replace this code with
to reset previous day values.
Thank you for clarifying options margin requirements for me, I am not experienced with options. I hope QC eventually will handle these nuances properly, because currently it shows very little exposure and drawdown, even with positions for hundreds of contracts.
If probability of crash is very low, perhaps it makes sense to extract your profit from trading account regularly and just accept the fact that someday you can lose your trading balance? If this happens, you will keep the profit and can restart. Because if you keep your trading balance low to prevent crashing it, profits are also going to be low. Just maybe something to consider.
David Eldringhoff
One thing with this algo is you're selling like 1000 options at a time. The margin required for that would make this algo not workable. You could only sell like 1% of what you're actually selling.
David Eldringhoff
Yeah I just pulled it up to manually put in a trad eto calculate the margin required. To sell 1 contract requires $58k in margin. So you'll make 0.30 on 58k of used margin and there is no way you can sell the amount on contracts the algo is trying to sell. In fact, on the clone of that last post, the starting amount of $50k, you cant sell even 1 contract with that.
David Eldringhoff
maybe try this on SPY instead? You can sell a contract on SPY for 1/10th the required margin.
Thomas
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!