Framing out the foundation of a strategy where ive implemented a simple market order buy at market close (100% allocation) then liquidate at some limit price.  It looks as though the algo just keeps selling thinking that my holidings exist ... or something.  I believe the issue is adherance to this code block ... Im learning python so maybe Im not implementing this logic cleanly.  Im using limit orders here so they can execute in extended hours if needed.  The nested if acts as a stop limit, which Im not sure if is allowed in extended hours so its implemented in this way.

def OnData(self, data): if self.Securities["AMD"].Invested: if self.Securities["AMD"].Price <= self.sellPrice: quantity = self.Portfolio["AMD"].Quantity self.LimitOrder("AMD", -quantity, self.sellPrice) self.Debug("Sell " + str(quantity)) quantityPost = self.Portfolio["AMD"].Quantity self.Debug("Net holdings before: " + str(quantity) + " Net After: " + str(quantityPost))

I should only ever be long or zero, but my order logs are strange with multiple sells in a row of the full portfolio quantity:

 

TimeSymbolPriceQuantityTypeStatusValue2017-01-03T20:59:00ZAMD11.41587MarketFilled993.1052017-01-04T14:45:00ZAMD11.30085-87LimitFilled-983.173952017-01-04T20:59:00ZAMD11.4386MarketFilled982.982017-01-05T16:09:00ZAMD11.3157-86LimitFilled-973.15022017-01-05T16:10:00ZAMD11.3157-86LimitFilled-973.15022017-01-05T16:34:00ZAMD11.386LimitFilled971.82017-01-05T20:59:00ZAMD11.2487MarketFilled977.882017-01-06T14:35:00ZAMD11.1276-87LimitFilled-968.1012

 

This shouldn't happen because after the initial Limit Sell self.Securities["AMD"].Invested == False Have I missed something?  Any help here would be appreciated.

Author