Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Backtest Limit Orders Fill on Following Candle

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

    Backtest Limit Orders Fill on Following Candle

    Hi Guys,
    when backtesting and using limit orders I am finding that if the candle immediately following the order passes through my EntryPrice then the order triggers correctly. However, if the price backtracks and goes no where near the EntryPrice the order is still filled at either the high or the low of that candle, whichever is closest.
    Attached is an example of 2 trades, one triggering correctly and the other incorrectly.
    My strategy uses the following;
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), EntryPrice, "Short Trade");
    TraceOrders output shows the order always requesting the correct limit price, however this is not always filled correctly.

    Thanks in advance for your help.
    Attached Files

    #2
    Hello ScottyP,

    Thank you for your note.

    If you use the following syntax, with true passed to isLiveUntilCanceled, do you see the results you expect? The syntax you're using, your limit order will be canceled on the next bar.

    Try,

    Code:
    EnterLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName)
    See EnterLongLimit section of our helpguide,


    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thank you Alan,
      I have tried this in the past and again now...
      EnterShortLimit(0, true, Convert.ToInt32(DefaultQuantity), EntryPrice, "Short Trade");
      ...but it yields the same result.
      I do wish the order to expire on the following bar each time so that my indicator can reevaluate market conditions and advise on whether to attempt another limit order to confirm directionality of the market.
      A typical TraceOrders snapshot is as follows;

      23/01/2013 9:55:00 PM Strategy 'ExhaustionStrat/-1': Entered internal SubmitOrderManaged() method at 23/01/2013 9:55:00 PM: BarsInProgress=0 Action=Buy OrderType=Limit Quantity=1 LimitPrice=98.06 StopPrice=0 SignalName='Long Trade' FromEntrySignal=''
      Ordered Long at 98.06 and got 98,

      I have just upgraded to NT8 and don't remember having this issue in NT7. Also I only have access to my old NT7 backtesting data so could this be interpreted differently by NT8? I don't wish to fund fresh data until I find an algorithm with a chance at working.

      Comment


        #4
        Hello ScottyP,

        Could your variable entryPrice be being reset on the new bar?

        I would suggest adding print statements to check whether and when your conditions are becoming true. I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:

        Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


        I’ve provided a link covering debugging which you may find helpful.
        Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

        If you try the above and still unable to figure out the issue, please leave the print statements in the script and you can upload a copy and I'll take a look and see if anything jumps out.

        To export a NinjaScript from NinjaTrader 8 do the following:
        From the Control Center window select Tools -> Export -> NinjaScript...
        Click Add>Select the indicator>OK>Export.
        Then attach that file you saved; under My Docs>NT8>Bin>Custom>Select the downloaded .zip file.

        I look forward to your reply.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Hi Alan,
          I actually had already used print statements - eg.the last line of the TraceOrder example in my last post was my feedback to myself to show that there was a miss-match of what I ordered vs what I was given, viz. "Ordered Long at 98.06 and got 98".

          To prove that the mystery behavior is not my script but may in fact be NT8's interpretation of my old data-set, I have run the sample script of "SampleMACrossOver" that comes with NT8.
          The only change I have made is in replacing EnterLong(): with the following....

          EnterLongLimit(Convert.ToInt32(DefaultQuantity), High[0] + 2*TickSize, "Long Limit Trade @ "+ Convert.ToString(High[0] + 2*TickSize));

          The same issue occurs, as can be seen in the attached graphic.
          For your interest I have also attached the exported Strategy script.
          Attached Files

          Comment


            #6
            Hello ScottyP,

            Thanks for your reply.

            In the example you posted and in the code you have provided you are using EnterLongLimit() above the current price which means that the limit order would fill at the limit price or better and in those cases where the fill price is different it would be a better price.

            If you want to go long above the current price then you can use EnterLongStopMarket() or EnterLongStopLimit().

            If you want to go short below the current price then you can use EnterShortStopMarket() or EnterShortStopLimit().
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Thanks Paul,
              You've nailed it!
              I am guessing that in a live stream the false entries I was getting (highs/lows) as shown in the backtest graphics, would not have occurred.......part of my confusion.

              Appreciate it.

              Scott

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by cre8able, 02-11-2023, 05:43 PM
              3 responses
              236 views
              0 likes
              Last Post rhubear
              by rhubear
               
              Started by frslvr, 04-11-2024, 07:26 AM
              8 responses
              113 views
              1 like
              Last Post NinjaTrader_BrandonH  
              Started by stafe, 04-15-2024, 08:34 PM
              10 responses
              46 views
              0 likes
              Last Post stafe
              by stafe
               
              Started by rocketman7, Today, 09:41 AM
              3 responses
              11 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by traderqz, Today, 09:44 AM
              2 responses
              10 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Working...
              X