My algorithm responds to an Insight by entering a trade with a MarketOrder, and also setting target and stop prices with StopMarkerOrder and LimitOrder like so:
def onInsightEmmitted(self, insight, currentPrice, targetPrice, stopPrice):
stopSize = abs(currentPrice - stopPrice)
positionSize = self.CalculatePositionSize(stopSize)
if not self.Portfolio[insight.Symbol].Invested:
if insight.Direction == InsightDirection.Up:
self.MarketOrder(insight.Symbol, positionSize)
self.StopMarketOrder(insight.Symbol, -positionSize, stopPrice)
self.LimitOrder(insight.Symbol, -positionSize, targetPrice)
if insight.Direction == InsightDirection.Down:
self.MarketOrder(insight.Symbol, -positionSize)
self.LimitOrder(insight.Symbol, positionSize, stopPrice)
self.StopMarketOrder(insight.Symbol, positionSize, targetPrice)
def OnOrderEvent(self, orderEvent):
order = self.Transactions.GetOrderById(orderEvent.OrderId)
if orderEvent.Status == OrderStatus.Filled:
if order.Type == 2 or order.Type == 1:
self.Transactions.CancelOpenOrders(order.Symbol)
This seems to work well in most cases, but it looks like I had a case in my last backtest where the MarketOrder was turned into a MarketOnOpen order, there was a price gap on market open, and the MarketOrder, LimitOrder and StopMarketOrder for each security were filled all at the same time. How can I prevent something like this from happening? Can I use some logic to say that if multiple orders for the same security are going to be filled at the same time, then they should all just be cancelled?
Alexandre Catarino
Hi Riley Bolen ,
We don't have enough information to help you.
Please attach a backtest that exemplifies the issue.
When the market, limit and stop orders are filled at the same time, it means that their price range (limit price to stop price) lies within the current bar High and Low range. It means that the algorithm is not taking into account the market volatility when those prices are set.
I guess that the algorithm is using daily-resolution data because you said that the market order was converted into a market on open order which is common when daily resolution data is selected. Maybe the limit price to stop price range is optimal for minute-resolution data but too short for daily data.
Riley Bolen
Alexandre,
Thanks for the response. I was just curious, does posting a backtest on here and making my algorithm public disqualify that algorithm from submission to the alpha streams market?
Rahul Chowdhury
Hey Riley,
You should refrain from posting your full algorithm that you intend to submit to AlphaStreams because it will disqualify it. We recommend you post just enough to reproduce your issue. This way we can help you while you can keep your ideas private. If you want more personalized support and have a prime subscription, you can contact support@quantconnect.com.
Best
Rahul
Riley Bolen
I am thinking that the issue I am having here is because of my own errors in the way I built my algorithm. I am going to rebuild it, and if I run into the same issue again i will open a new issue with more info. Feel free to close this one.
Riley Bolen
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!