Hi there,
I was reading and asking Mia about why in backtesting the order fill price is correct but in live trading is not recovering it. I tried using Binance as my data provider, I tried not using the OnOrderEvent function, but the result if always the same. I backtest works but in Live trading not. I attach part of my code here so you can help me. In FiveMinuteReceiver function I send an order and save the order under a new variable like this:
self.LongEntryTicket = self.MarketOrder(self.symbol.Symbol, self.quantity, False, 'LONG')
In OnData function I get the fillprice like this:
if self.LongEntryTicket is not None:
order = self.Transactions.GetOrderById(self.LongEntryTicket.OrderId)
if order.Status == OrderStatus.Filled:
if order.Tag == 'LONG' or order.Tag == 'LONG NEXT':
if self.LongFillPrice is None or order.Price < self.LongFillPrice:
self.LongFillPrice = order.Price
Here I establish my stop loss later in the Ondata function (the main issue that I need to solve as Binance doesnt allow StopMarketOrder):
if self.LongFillPrice is not None and self.Portfolio[self.symbol.Symbol].Quantity > 0:
if self.Securities[self.symbol.Symbol].Price > self.LongFillPrice:
self.LongFillPrice = self.Securities[self.symbol.Symbol].Price
self.BUYorder_sl_price = self.LongFillPrice * (1 - self.slPercent)
self.Debug('Price: ' + str(self.Securities[self.symbol.Symbol].Price) + ' LONG_SL: ' + str(self.BUYorder_sl_price) )
First of all, this Debug works in Backtest but not in live trading (thats my main evidence)
Later in the Ondata fucntion I execute the stoploss like this:
if self.Portfolio[self.symbol.Symbol].Quantity > 0:
if self.BUYorder_sl_price != None:
if self.Securities[self.symbol.Symbol].Price <= self.BUYorder_sl_price:
self.quantity = self.Portfolio[self.symbol.Symbol].Quantity
self.MktOrder = self.MarketOrder(self.symbol.Symbol, -self.quantity, False, 'SL LONG')
self.BUYorder_sl_price = None
self.BUYorder_tp_price = None
self.LongFillPrice = None
Mia Alissi
Based on the information provided in the knowledge sources, it seems like the issue you're experiencing might be due to the differences between backtesting and live trading in QuantConnect.
In backtests, orders fill immediately. However, in live trading, they are sent to your brokerage and take about half a second to execute. If you fill an order in a backtest with stale data, deviations between backtesting and live trading can occur because the order is filled at a price that is likely different from the real market price (source).
In live trading, your brokerage provides the fill price of your orders. Since the backtesting brokerage models do not know the price at which live orders are filled, the fill price of backtest orders is based on the best price available in the current backtesting data (source).
For Binance, market orders are filled immediately and completely in backtests. In live trading, if the quantity of your market orders exceeds the quantity available at the top of the order book, your orders are filled according to what is available in the order book (source).
If you're still experiencing issues, it might be helpful to add an
OnOrderEvent
function to your algorithm to check the details of the order event. This function is triggered every time there's a change in an order status. You can use it to debug your order fills (source).Here's an example of how you can use
OnOrderEvent
:This will log the fill price every time an order is filled. You can compare this with the expected fill price to see if there are any discrepancies.
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.
EA_investing
I used the OnOrderEvent fucntion in the past but it is still not working. Is there something about Binance in live trading that is not working is there a workaround to set a stop loss (giving that I cant use StopMarketOrder)?
Mia Alissi
Based on the knowledge sources, it seems that there might be some differences in how orders are handled in backtesting and live trading, especially with certain brokerages like Binance.
In backtesting, the
StopMarketOrder
is a type of order that becomes a market order once the stop price has been hit. However, it seems that Binance might not support this type of order (source).As a workaround, you could manually implement a stop loss in your algorithm. You can do this by checking if the current price is less than your stop loss price, and if it is, you can place a market order to sell your holdings. Here's an example of how you can do this:
In this example, a market order is placed to buy 100 shares of SPY. A stop loss price is then set at 95% of the closing price. In the
OnData
method, it checks if the current price is less than the stop loss price, and if it is, it liquidates the holdings.Remember to adjust the stop loss logic to fit your specific trading strategy and risk tolerance.
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.
EA_investing
I need to use the fillprice in order to code a trailing stop loss, I cant use the close price at the time of the order as the fill price could be different and that can impact in the return (taking into account that crypto markets are quite volatile)
Mia Alissi
Based on the knowledge sources, you can implement a trailing stop loss using the fill price of an order. You can do this by updating the stop loss price every time a new order is filled. Here's an example of how you can do this:
In this example, every time an order is filled, the
OnOrderEvent
method is triggered and the trailing stop price is set to 95% of the fill price. Then, in theOnData
method, it checks if the current price is less than the trailing stop price, and if it is, it liquidates the holdings.Remember to adjust the trailing stop logic to fit your specific trading strategy and risk tolerance (source, source).
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.
EA_investing
Can you contact someone in the quantconnect team that gives me a new answer that I didnt hear yet?
Louis Szeto
Hi EA_investing
I believe Mia's last response is exactly what you were asking for. Make sure you use “order_event.fill_price” and that would be your average fill price instead of the market price at the time.
Best
Louis
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.
EA_investing
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!