Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Backtestproblems, 2x entershortstopmarket / enterlongstopmarket orders

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Backtestproblems, 2x entershortstopmarket / enterlongstopmarket orders

    Hello, community,

    for some time now I've been trying to solve the following problem:

    I want to enter the market by limit order at a fixed price, whether long or short is not relevant at first.
    The important thing is that I can manage the position differently after entering the market (at least 2 contracts). To realize this, I enter the market with two EnterShort/LongLimitMarket orders, and give the respective order a string tag, so that I can later use the stoploss or takeprofit method to scale out.

    This works in realtime trading, but not in the backtest function, where both trades are not identical, even though entered at the same price. Most of the time one of them runs into profit and the other one realizes a loss. How is this possible? How can I fix this?


    I have attached two sample codes:
    First of all, it doesn't matter if the strategy makes sense or is profitable (They're just code snippets), I also know that the backtest is not reliable when I open and close intrabar, the only important point I want to solve is that both trades are executed like in example 2, but the possibility exists to manage them differently.

    Candlestickchart, Calculate : OnBarClose, OnBarUpdate


    Code 1 (with exisiting problem):



    Code:
        
    
            protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                Cbi.MarketPosition marketPosition, string orderId, DateTime time)
            {
    
                    SetStopLoss("S1",CalculationMode.Ticks, 10,false);
                    SetProfitTarget("S1",CalculationMode.Ticks,10);
    
                    SetStopLoss("S2",CalculationMode.Ticks, 10,false);
                    SetProfitTarget("S2",CalculationMode.Ticks,10);
    
            }
    
            protected override void OnBarUpdate()
            {
                if((Position.MarketPosition==MarketPosition.Flat) && (Low[0]>Low[1]))                            
    
                                {        
    
                                        EnterShortStopMarket(0, false, 1, Low[1], "S1") ;
    
    
                                        EnterShortStopMarket(0, false, 1, Low[1], "S2") ;
    
                                }
    
            }



    I want that the code in example 1 gives the same results in backtest as the code below :




    Code:
            protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                Cbi.MarketPosition marketPosition, string orderId, DateTime time)
            {
    
                    SetStopLoss("S1",CalculationMode.Ticks, 10,false);
                    SetProfitTarget("S1",CalculationMode.Ticks,10);
    
            }
    
    
    
            protected override void OnBarUpdate()
            {
    
    
                if((Position.MarketPosition==MarketPosition.Flat) && (Low[0]>Low[1]))                            
    
                                {        
    
                                        EnterShortStopMarket(0, false, 2, Low[1], "S1") ;
    }



    Thank you very much for your help!

    Kevin






    Last edited by AutomatedTrading; 06-12-2020, 03:55 PM.

    #2
    Hello Kevin,

    Thank you for your reply.

    I think that what you're seeing here is due to how the Simulator handles fills on historical data.

    The simulation engine is not a simple algorithm that fills your order once the market trades at your order price. The engine uses a scientific approach to determine fill probability by including a number of variables including: ask/bid volume, trade volume, time (to simulate order queue position), and random time delays for switching between order states.

    If you go under Tools > Options > Trading and check the box for "Enforce Immediate Fills", then remove and re-add the strategy to a chart, do you see the orders being taken as you'd expect?

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      Hello Kate,

      Thank you for your detailed answer!
      I have re-inserted a part of my question that I had already removed, maybe this topic will help another forum member! It was a bit selfish to remove the question as soon as I had found a solution for me, although there were no answers yet.

      Back to the topic:

      The complexity of the filling algorithm is a good thing! I think that a "simple algorithm" would create even more "wrong executions".

      But as you can see in my code, the two limit orders are executed directly after each other, so there should be a nearly simultaneous filling, I think. In live trading this also works without any problems!

      So far I have only tested the code in the Strategy Analyzer, there the mentioned problems occur. To test it, I have just loaded the strategy into a chart and looked at the fillings of the past, even there sometimes one trade goes into profit and the other into loss. (with the first code example)

      Originally posted by NinjaTrader_Kate View Post

      If you go under Tools > Options > Trading and check the box for "Enforce Immediate Fills", then remove and re-add the strategy to a chart, do you see the orders being taken as you'd expect?

      I have now set the checkbox, but unfortunately this is not a solution either, it happens as described above, no change in the executions.

      My solution:
      I have set a limit order for the first order part and enter the market with the second order part via market order as soon as the limit order has been filled.
      This code is only executed in historical mode. Backtest works. Live trading also. I am satisfied so far, but I am also very interested in a more elegant solution or would appreciate it if we could find a better solution together.

      Can you reproduce the problem on your system?

      There should be the same results with both codes (both orders, same TP and same SL):

      1.
      EnterShortStopMarket(0, false, 1, Low[1], "S1") ;
      EnterShortStopMarket(0, false, 1, Low[1], "S2") ;



      2.

      EnterShortStopMarket(0, false, 2, Low[1], "S1") ;



      Best wishes, have a nice weekend!
      Kevin

      Comment


        #4
        Hello AutomatedTrading,

        Thank you for your reply.

        So you keep mentioning limit orders, but the code you've provided shows Stop Market orders being used. I'd expect that even using fill on touch wouldn't work in the Strategy Analyzer because since these are Market orders once the stop is hit, they're pretty likely to fill at different prices due to how the Strategy Analyzer fills orders. You can find an overview of how that works here: https://ninjatrader.com/support/help...fill_logic.htm

        If you use EnterShortStopLimit instead, does that give you the fills a little more accurately?

        Please let us know if we may be of further assistance to you.

        Comment


          #5
          Originally posted by NinjaTrader_Kate View Post
          Hello AutomatedTrading,

          Thank you for your reply.

          So you keep mentioning limit orders, but the code you've provided shows Stop Market orders being used. I'd expect that even using fill on touch wouldn't work in the Strategy Analyzer because since these are Market orders once the stop is hit, they're pretty likely to fill at different prices due to how the Strategy Analyzer fills orders. You can find an overview of how that works here: https://ninjatrader.com/support/help...fill_logic.htm

          If you use EnterShortStopLimit instead, does that give you the fills a little more accurately?

          Please let us know if we may be of further assistance to you.


          Hello Kate,

          thank you very much for your detailed answer.
          Sorry if I didn't make myself clear.
          My intention was to place an entry order above or below the market price. Above in long direction and below in short direction, so a limit order is not the desired type, I have expressed myself wrong.

          I have looked at the filling logic and I think I got it. I have also tested your suggested method with EnterShortStopLimit Orders.
          There are both trades actually executed equally and two successive entries, each with one contract, give the same result as an entry order with 2 contracts.(That was requested)

          However, the problem with this method is that, if the entry and exit are both in one bar, the strategy actually realizes a lot of unrealistic loss trades in the backtest, as well as a few trades that do not use the correct stoploss and have realized far more loss than the SL actually allows.

          I do not trust this calculation, it is sometimes wrong if I check the trades in a higher resolution.
          I still consider this topic to be strange, especially it is a mystery to me why the filling algorithm calculates so complicated, when both entries are at the same entry/exit price...

          I think that all questions have been answered so far, I will still stick to my "Short/LongStopMarket and enter market in backtest" variant.

          Thank you for your help!
          Kevin


          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          44 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          124 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          65 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          42 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          46 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X