Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

StopLimit Backtesting

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

    StopLimit Backtesting

    I have 1 minute Data for Backtesting.
    In my backtesting strategie orders should be filled on Prior Day High for Long an Prior Day Low for Short.

    These are the orders I'm using in my test strategie .

    EnterLongStopLimit(PriorDayOHLC().PriorHigh[0]+TickSize*1,PriorDayOHLC().PriorHigh[0],"Long");

    EnterShortStopLimit(PriorDayOHLC().PriorLow[0]-TickSize*1,PriorDayOHLC().PriorLow[0],"Short");

    Depending on what Time Frame I use they are filled or not. For instance in 60min Chart Longs are filled but only a fiew of the shorts. In a 30min Chart only some of the short(longs are filled) around. See pics. It doesn't matter if I change the order resolution.

    Is that an issue?


    Frank
    Attached Files

    #2
    Hello,

    I see that you have a custom OHLC indicator applied to the chart, but you are referencing the pre-loaded OHLC indicator in your code. Although one would expect OHLC values to be the same, it still couldn't hurt to debug a bit to make sure that PriorDayOHLC() and GansPriorDayOHLC() do indeed have the same values.

    Are you finding that the two do have consistent values at the points at which the trades are being missed?
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Hi Dave,

      yes, they are identical.

      Comment


        #4
        Hello,

        Alright, that eliminates that possibility. Can you share your order-entry condition with me, so I can replicate your setup on my end?
        Dave I.NinjaTrader Product Management

        Comment


          #5
          Hi Dave,
          just try:

          if( Position.MarketPosition !=MarketPosition.Long ) EnterLongStopLimit(PriorDayOHLC().PriorHigh[0]+TickSize*1,PriorDayOHLC().PriorHigh[0],"Long");

          if(Position.MarketPosition !=MarketPosition.Short ){ EnterShortStopLimit(PriorDayOHLC().PriorLow[0]-TickSize*1,PriorDayOHLC().PriorLow[0],"Short");

          Comment


            #6
            Hello,

            Thank you. Unfortunately, I'm not seeing the same issue when I run this on my end. It appears to be entering and exiting orders as would be expected with that code. Is there anything else you can think of that I would need to do to replicate the problem on my end?
            Dave I.NinjaTrader Product Management

            Comment


              #7
              Hi Dave another issue I noticed in this context.

              The Strategy stops working if you change the EnterShortStopLimit Order with a EnterShort Order incl. SetStopLoss (let the EnterLongStopLimit Order like it is):

              if (Position.MarketPosition !=MarketPosition.Short {
              EnterShort("Short");
              SetStopLoss("Short",CalculationMode.Price,High[1],false);
              }

              My Strategy Analyzer stops and set everything to 0. If I drop the SetStopLoss Order it works.
              The other way around (Long as EnterLong and Short as StopLimit works incl. StopLoss...)

              Comment


                #8
                Ah, okay. I'll do some more testing with SetStopLoss() and see what I can discover. You should hear back from me soon (possibly early tomorrow morning).
                Dave I.NinjaTrader Product Management

                Comment


                  #9
                  Hi Dave,

                  OK.

                  Concerning my first issue:
                  So you see an order on the chart every time a Bar is crossing PriorDayOHLC().PriorHigh[0] or PriorDayOHLC().PriorLow[0] when you are flat? Regardless of the Timeframe?

                  Could you please send me your test code and a Screenshot of the chart? I will check then further my site and will see if the problem still exists with your code.

                  Thanks Frank

                  Comment


                    #10
                    Hi Dave,

                    I figured out that placing a SetStopLoss for Short is the problem. But only in Combination with a Long Order.
                    Please find the strategy attached. On my system the Strategy analyser is showing 0 on all rows. If you comment out this:

                    SetStopLoss("Short",CalculationMode.Price,PriorDay OHLC().PriorHigh[0],true);

                    It will work. Then also the issue with the stop limit orders is gone...
                    Attached Files

                    Comment


                      #11
                      Here is a better example. The SetStopLoss in the short area is the problem.

                      Error message from console:

                      Strategie 'Test': Error on calling 'OnBarUpdate' method on bar 70: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

                      (I'm sorry it's half in German, even I use English as NT8 lang.)

                      If I comment the Short SetStopLoss or comment the EnterLongStopLimit the error message goes...
                      Attached Files

                      Comment


                        #12
                        Hello,

                        Thank you for that clarification. I believe I've spotted the issue and come up with a solution. It was the Managed Approach internal handling rules that were getting in your way. Due to the way that managed orders are handled in the background, you are not able to set up a bracket in the way that you were doing.

                        The solution is to set this up using the Unmanaged approach, instead, which gives you full control over your orders and does not look over your shoulder, so to speak, to make sure that everything lines up as expected. I've attached a modified version of your last script that will accomplish the same goal using the unmanaged approach.

                        You can find more information about managed internal handling rules at the link below:

                        http://www.ninjatrader.com/support/h...d_approach.htm

                        You can find more information about using Unmanaged methods in any of the sub-topics beneath the one in the link above.
                        Attached Files
                        Dave I.NinjaTrader Product Management

                        Comment


                          #13
                          Hi Dave,

                          thank's. I tried your version but I get strange results. Does it look the same on your Chart?
                          See Attachment.

                          Frank
                          Attached Files

                          Comment


                            #14
                            It did not look that way on my end, but I was only working with the Strategy Analyzer. I believe I can spot the cause of this easily enough -- I'll do a bit more testing and get back to you soon.
                            Dave I.NinjaTrader Product Management

                            Comment


                              #15
                              Hello,

                              Thank you for your patience on this one. I see what's happening now. With the Unmanaged approach, there is nothing like EntriesPerDirection to limit the number of trades we take. So, in my example code, as long as the market position is not long, we're going to be continually submitting new entry orders until one of them is filled, and vice versa for the other side. Then once one of them is filled, there could be a hundred more still sitting there to be filled at the same moment.

                              One way to get around this would be to use a boolean flag to determine whether we've placed the one order that we want to place, and check for that flag in our entry condition, like so:

                              Code:
                              if(Position.MarketPosition !=MarketPosition.Long && longEntryPlaced == false)
                              Then, you can set that flag to true just before you enter, like so:

                              Code:
                              longEntryPlaced = true;
                              //Long Entry
                              longOrder = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.StopLimit, 1, PriorDayOHLC().PriorHigh[0]+TickSize*1,PriorDayOHLC().PriorHigh[0],"", "Long");
                              //Stop Loss
                              longStopLoss = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, 1, 0, PriorDayOHLC().PriorLow[0], "", "Long Stop Loss");
                              Lastly, we can detect when the order has been filled (or check for some other condition at which you wish to start placing entry orders again) within the OnOrderUpdate() event method. If we detect that the long entry has been filled in OnOrderUpdate(), we can then set longEntryPlaced back to false, so that the entry condition can trigger once more.
                              Dave I.NinjaTrader Product Management

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by CortexZenUSA, Today, 12:53 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by CortexZenUSA, Today, 12:46 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by usazencortex, Today, 12:43 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post usazencortex  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              168 responses
                              2,266 views
                              0 likes
                              Last Post sidlercom80  
                              Started by Barry Milan, Yesterday, 10:35 PM
                              3 responses
                              13 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X