Back

MarketOnClose() not working with .Daily resolution

Hello,

I want to test Python code, buying SPY daily at open and selling on close.

The algo places the trade but leaves it open and doesn't close it. (Hence in overview, 6 trades made but both win and loss rate are 0%)     Any ideas? Thank-you

Update Backtest








OnData receives daily bars when the market is closed, therefore MarketOnOpen and MarketOnClose orders should not be used with that resolution.
Please use a higher resolution and a consolidator instead:

from datetime import timedelta

# In Initialize
consolidator = TradeBarConsolidator(timedelta(1))
consolidator.DataConsolidated += self.OnDailyData
self.SubscriptionManager.AddConsolidator(self.sym, consolidator)

def OnData(self, data):
pass

def OnDailyData(self, sender, bar):
self.Log(">>> OnDailyData called.")
self.rWindow.Add(bar)

# Place market on open orders.
self.MarketOnOpenOrder(bar.Symbol, 1, "hello")

Finally, use OnOrderEvent handler to place a MarketOnCloseOrder once the MarketOnOpenOrder is filled.

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 Alex, thank-you again for yet another quick reply.

OK thanks, I've cloned the algo and ran it and it does work. When I'm back in on Monday I will go through in more detail and try to figure out these consolidators.  Thankyou and have a relaxing (or productive) weekend, whatever you are doing!

0

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