Back

Algo not updating holding quantity correctly

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.
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.


Adding screenshots as the table didnt format correctly

0

Hey Christopher,

Where is your initial entry into AMD? Could you post a backtest with the full code? It will help us better understand what is happening.

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.


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