Back

Can someone help me with a C# implementation of this OptionChains Python code?

All, I need to iterate through OptionChains class members to select individual chains based upon criteria of Right/Expiry/Strike.  

I found this Python code in the Tutorial: Applied Options:Iron Condor.  Howerver, it appears that in C# we do not have the ability to use array indexing to select a member of an OptionsChains class. In Python we see the use of such indexing

 

def TradeOptions(self,slice):
# If there is undelying assets in portfolio at expiration, liquidate the stocks in order to roll into new contracts
if self.Portfolio["GOOG"].Quantity != 0:
self.Liquidate()

if not self.Portfolio.Invested and self.Time.hour != 0 and self.Time.minute != 0:
for i in slice.OptionChains:
chain = i.Value
contract_list = [x for x in chain]
# if there is no optionchain or no contracts in this optionchain, pass the instance
if (slice.OptionChains.Count == 0) or (len(contract_list) == 0):
return

# sorted the optionchain by expiration date and choose the furthest date
expiry = sorted(chain,key = lambda x: x.Expiry)[-1].Expiry
# filter the call and put options from the contracts
call = [i for i in chain if i.Expiry == expiry and i.Right == 0]
put = [i for i in chain if i.Expiry == expiry and i.Right == 1]

# sorted the contracts according to their strike prices
call_contracts = sorted(call,key = lambda x: x.Strike)
put_contracts = sorted(put,key = lambda x: x.Strike)
if len(call_contracts) == 0 or len(put_contracts) == 0 : continue

otm_put_lower = put_contracts[0]
otm_put = put_contracts[10]
otm_call = call_contracts[-10]
otm_call_higher = call_contracts[-1]
self.trade_contracts = [otm_call.Symbol,otm_call_higher.Symbol,otm_put.Symbol,otm_put_lower.Symbol]
When I try this in C# the error message is "Cannot apply Indexing with [] to an expression of type OptionContract".... it's an extension of IEnumerable. I cannot find a way to select the nth member.
Update Backtest







0

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.


Hi Craig,

Without seeing the C# code, it's difficult to understand where the issue lies. For convenience, I've translated the Iron Condor algorithm from Python to C# and attached it below. Note the algorithms are exactly the same except for the arguments provided to SetFilter.

Best,
Derek Melchin

1

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.


Update Backtest





0

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.


Loading...

This discussion is closed