I am trying to do some options backtesting.  I am finding that the MarketOrder fills for the options is very erratic.  Most likely it comes down to the low volume trading that occurs on the options.  Anyways, something I ususally do is just assume to lose the spread on every trade execution.  For example, when selling an option, you always assume you sell on the bid, and buying you always assume that you buy on the ask.  At QuantConnect we have bid/ask data so that seems possible, although I can't quite figure out how I would do the code.  From the docs we have this simple example:

# Set the fill models in initialize:
self.Securities["IBM"].SetFillModel(PartialFillModel())

# Custom fill model implementation stub
class PartialFillModel(ImmediateFillModel):
def MarketFill(self, asset, order):
# Override order event handler and return partial order fills
pass

So how would I create a default fill model for all options to say that MarketOrders are filled in the way I described above ( selling on bids and buying on asks ) ?  The documentation doesn't really explain what we need to return from this function.  Would something like this work?


# Custom fill model implementation stub
class MyCustomFillModel(ImmediateFillModel):
def MarketFill(self, asset, order):
if order.isSell:
order.fillPrice = asset.BidPrice
else:
order.fillPrice = asset.AskPrice
return order

I know that attributes of the order object are wrong but I can't find any documentation explaining the order object and which attributes we are supposed to override in the fill model.  Please help.

 

Thanks.