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:




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.