Back

Stop-loss not getting hit

Hi,

I've hit a problem where the backtesting server seems to ignore my order's stop loss and take profit levels. Judging from the logs the price have long since passed the stop-loss level, yet the trade is not being closed. Any idea why this would happen?

Also, still unsure of how to check whether a ticket is closed. The OrderEvent.Status enumeration only specified whether an order got filled or not, but contains no detail on when an order is automatically closed by the server.

Any ideas?

Kind regards,

Kepler

Update Backtest








Hi there Kepler,

When I use stop loss orders, first I make the entry to the market using a MarketOrder, then I use StopMarketOrder order to set the stoploss.

Check lines 81 and 82 in the main.cs file in this example.

Also check this clearer example by Jonathan Evans.

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.


Please put the following code line in the OnOrderEvent method:

public override void OnOrderEvent(OrderEvent orderEvent)
{
Log(orderEvent.ToString());
}

and check the logs.
We should have seen a line with the information about the submission of the StopLimit orders, but we couldn't. It seems that the code lines that place these orders are never hit.

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 JayJayD,

The engine doesn't allow that though?

 

Runtime Error: System.ArgumentException: Unable to get field StopPrice on order of type Market
at QuantConnect.Orders.OrderTicket.Get (QuantConnect.Orders.OrderField field) [0x001cf] in :0
at QuantConnect.Algorithm.CSharp.QCUMovingAverageCross.OnData (QuantConnect.Data.Slice data) [0x00275] in :0
at QuantConnect.Lean.Engine.AlgorithmManager.Run (QuantConnect.Packets.AlgorithmNodePacket job, QuantConnect.Interfaces.IAlgorithm algorithm, QuantConnect.Lean.Engine.DataFeeds.IDataFeed feed, QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler transactions, QuantConnect.Lean.Engine.Results.IResultHandler results, QuantConnect.Lean.Engine.RealTime.IRealTimeHandler realtime, QuantConnect.Interfaces.ICommandQueueHandler commands, System.Threading.CancellationToken token) [0x01346] in <2824cfe7150d4525ab66e90816c767c3>:0
0

Of course the engine is Unable to get field StopPrice on order of type Market

Before asking for the StopPrice, make sure you are refering to a StopMarketOrder or a StopLimitOrder.

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