Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Delay in Market Position update?

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

    Delay in Market Position update?

    Hi again,

    I have a strategy that I have backtested on a chart, and I noticed it is running fine except for one trade (out of about 70) where the strategy enters the same trade multiple times. This is on the 6E on 2 August at 4:50am. On further investigation, I think this is due to a delay in the Position.MarketPosition updating (I have used print statements in my strategy to determine this).

    I have re-downloaded the price data for this day, but I am still getting the error.

    Is this just a backtesting error or could it occur in real-time trading?

    Is there a way to access the order status data so I could add a line in the strategy?

    Thanks


    I have posted some of the output from the traceorder below. There is more that continues in the same vein:

    02/08/2012 4:50:15 AM Entered internal PlaceOrder() method at 02/08/2012 4:50:15 AM: BarsInProgress=2 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Trade293' FromEntrySignal=''
    02/08/2012 4:50:15 AM Entered internal PlaceOrder() method at 02/08/2012 4:50:15 AM: BarsInProgress=2 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Trade2293' FromEntrySignal=''
    02/08/2012 4:50:15 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Trade293' Mode=Ticks Value=17 Currency=0 Simulated=False
    02/08/2012 4:50:15 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Trade2293' Mode=Ticks Value=17 Currency=0 Simulated=False
    02/08/2012 4:50:15 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Trade293' Mode=Ticks Value=24 Currency=0 Simulated=False
    02/08/2012 4:50:15 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Trade2293' Mode=Ticks Value=72 Currency=0 Simulated=False
    MarketPosition = Flat
    02/08/2012 4:50:15 AM Cancelled pending exit order, since associated position is closed: Order='NT-00021/Sim101' Name='Profit target' State=Working Instrument='6E 09-12' Action=BuyToCover Limit price=1.2258 Stop price=0 Quantity=1 Strategy='ComboSVv3' Type=Limit Tif=Gtc Oco='NT-00012-436' Filled=0 Fill price=0 Token='6c3ea8b44c6f4e4983149a5b58cbeb4f' Gtd='01/12/2099 12:00:00 AM'
    02/08/2012 4:50:15 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00021/Sim101' Name='Profit target' State=Cancelled Instrument='6E 09-12' Action=BuyToCover Limit price=1.2258 Stop price=0 Quantity=1 Strategy='ComboSVv3' Type=Limit Tif=Gtc Oco='NT-00012-436' Filled=0 Fill price=0 Token='6c3ea8b44c6f4e4983149a5b58cbeb4f' Gtd='01/12/2099 12:00:00 AM'
    02/08/2012 4:50:15 AM Cancelled pending exit order, since associated position is closed: Order='NT-00023/Sim101' Name='Profit target' State=Working Instrument='6E 09-12' Action=BuyToCover Limit price=1.221 Stop price=0 Quantity=1 Strategy='ComboSVv3' Type=Limit Tif=Gtc Oco='NT-00013-436' Filled=0 Fill price=0 Token='8bccfe1b22f84cf898c756fa9d3efcea' Gtd='01/12/2099 12:00:00 AM'
    02/08/2012 4:50:15 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00023/Sim101' Name='Profit target' State=Cancelled Instrument='6E 09-12' Action=BuyToCover Limit price=1.221 Stop price=0 Quantity=1 Strategy='ComboSVv3' Type=Limit Tif=Gtc Oco='NT-00013-436' Filled=0 Fill price=0 Token='8bccfe1b22f84cf898c756fa9d3efcea' Gtd='01/12/2099 12:00:00 AM'
    02/08/2012 4:50:25 AM Entered internal PlaceOrder() method at 02/08/2012 4:50:25 AM: BarsInProgress=2 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Trade293' FromEntrySignal=''
    02/08/2012 4:50:25 AM Entered internal PlaceOrder() method at 02/08/2012 4:50:25 AM: BarsInProgress=2 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Trade2293' FromEntrySignal=''
    02/08/2012 4:50:25 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Trade293' Mode=Ticks Value=17 Currency=0 Simulated=False
    02/08/2012 4:50:25 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Trade2293' Mode=Ticks Value=17 Currency=0 Simulated=False
    02/08/2012 4:50:25 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Trade293' Mode=Ticks Value=24 Currency=0 Simulated=False
    02/08/2012 4:50:25 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Trade2293' Mode=Ticks Value=72 Currency=0 Simulated=False
    MarketPosition = Flat
    02/08/2012 4:50:25 AM Cancelled pending exit order, since associated position is closed: Order='NT-00027/Sim101' Name='Profit target' State=Working Instrument='6E 09-12' Action=BuyToCover Limit price=1.226 Stop price=0 Quantity=1 Strategy='ComboSVv3' Type=Limit Tif=Gtc Oco='NT-00016-436' Filled=0 Fill price=0 Token='dbec659a99a5422bb08272023e602a34' Gtd='01/12/2099 12:00:00 AM'
    02/08/2012 4:50:15 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00027/Sim101' Name='Profit target' State=Cancelled Instrument='6E 09-12' Action=BuyToCover Limit price=1.226 Stop price=0 Quantity=1 Strategy='ComboSVv3' Type=Limit Tif=Gtc Oco='NT-00016-436' Filled=0 Fill price=0 Token='dbec659a99a5422bb08272023e602a34' Gtd='01/12/2099 12:00:00 AM'
    02/08/2012 4:50:25 AM Cancelled pending exit order, since associated position is closed: Order='NT-00029/Sim101' Name='Profit target' State=Working Instrument='6E 09-12' Action=BuyToCover Limit price=1.2212 Stop price=0 Quantity=1 Strategy='ComboSVv3' Type=Limit Tif=Gtc Oco='NT-00017-436' Filled=0 Fill price=0 Token='1065558f391f4d4898ddd4ee4ae8ea86' Gtd='01/12/2099 12:00:00 AM'
    02/08/2012 4:50:15 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00029/Sim101' Name='Profit target' State=Cancelled Instrument='6E 09-12' Action=BuyToCover Limit price=1.2212 Stop price=0 Quantity=1 Strategy='ComboSVv3' Type=Limit Tif=Gtc Oco='NT-00017-436' Filled=0 Fill price=0 Token='1065558f391f4d4898ddd4ee4ae8ea86' Gtd='01/12/2099 12:00:00 AM'
    02/08/2012 4:50:26 AM Entered internal PlaceOrder() method at 02/08/2012 4:50:26 AM: BarsInProgress=2 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Trade293' FromEntrySignal=''
    02/08/2012 4:50:26 AM Entered internal PlaceOrder() method at 02/08/2012 4:50:26 AM: BarsInProgress=2 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Trade2293' FromEntrySignal=''
    02/08/2012 4:50:26 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Trade293' Mode=Ticks Value=17 Currency=0 Simulated=False
    02/08/2012 4:50:26 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Trade2293' Mode=Ticks Value=17 Currency=0 Simulated=False
    02/08/2012 4:50:26 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Trade293' Mode=Ticks Value=24 Currency=0 Simulated=False
    02/08/2012 4:50:26 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Trade2293' Mode=Ticks Value=72 Currency=0 Simulated=False

    #2
    Hello,

    Thanks for the note.

    With backtesting you need to wait at least one bar update before you check MarketPosition.

    Do you check MarketPosition in the same bar that you would submit an entry order?

    When you add Prints to the test what code shows its passing and allowing the enter trade again?

    -Brett

    Comment


      #3
      Thanks for the response Brett, I think you've solved the problem.

      I am running the strategy on a 1500tick chart, with CalculateOnBarClose set to true. However, I am running multi-timeframes, and the code that runs prior to trade entry is running against a smaller timeframe (100tick). The multiple entries are all on the same 1500tick bar (which is technically the next one because of how multiple timeframes reference each other with CalculateOnBarClose set to true), triggered when the 100tick bars close. I have put a print statement in to tell me the MarketPosition prior to running the code, which always comes back as Flat. Therefore, does this mean the MarketPosition isn't updating until the next 1500tick bar closes? And does this then mean this is only a problem during backtesting?
      (My MarketPosition check is running on the same 1500tick bar the trade entry requirements check)

      What I don't understand is why this hasn't caused multiple entries on all my trades? It seems to be isolated to this one only.

      Is there a way I can add another check or variable to get accurate backtesting results? I thought about adding a variable to tell me if a trade is entered (updated to true at the same time I enter a trade), but I'm not sure how to reset to false without using the MarketPosition... any ideas?

      Thanks in advance, you guys are always so helpful!

      Comment


        #4
        Right adding a variable is good, the reason why this happens is that your code executes much faster then the order gets filled.

        This could happen live occasionally as well for example.

        Code says:

        EnterLong();

        Code goes to next line and executes it whilee the code is executing NT has sent the order accross the internet to your broker. This can take anywhere from millesconds to longer for the order to transmit,the market conditions to decide to fill the order and then the return of filled status back to NinjaTrader. Only when the filled status is returned to NT would MarketPosition by updated to Long/Short.

        In backtesting all orders are filled at the end of the bar at the next open price on the BIP that the order is submitted on.

        It isnt until after that where MarketPosition would be updated.

        If your code needs to flag this somehow you can use your own variable or if you need realtime update in live on then you get an order filled you would use OnOrderUpdate(). A few sample of this here:



        -Brett

        Comment


          #5
          To further state:

          Backtesting all orders fill at the end of the bar at the next open price, in live the order fills when the market fills you.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by elirion, Today, 01:36 AM
          0 responses
          3 views
          0 likes
          Last Post elirion
          by elirion
           
          Started by gentlebenthebear, Today, 01:30 AM
          0 responses
          2 views
          0 likes
          Last Post gentlebenthebear  
          Started by samish18, Yesterday, 08:31 AM
          2 responses
          9 views
          0 likes
          Last Post elirion
          by elirion
           
          Started by Mestor, 03-10-2023, 01:50 AM
          16 responses
          389 views
          0 likes
          Last Post z.franck  
          Started by rtwave, 04-12-2024, 09:30 AM
          4 responses
          33 views
          0 likes
          Last Post rtwave
          by rtwave
           
          Working...
          X