Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Code works differently on historical data vs real-time

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

    Code works differently on historical data vs real-time

    Hello,

    I have an automated system trading futures and there is a simple piece of code to close open positions before the weekend inside OnMarketData() function.
    --------------------
    if ((Time[0].DayOfWeek == DayOfWeek.Friday) && (ToTime(Time[0]) >= 135800))
    {
    if (Position.MarketPosition == MarketPosition.Long)
    ExitLong();
    if (Position.MarketPosition == MarketPosition.Short)
    ExitShort();
    }
    ------------------
    This code work just fine in real-time and closes open positions right before 2pm PST on friday.

    During the weekend I usually reboot the system and restart NT. And after the restart of the same strategy on Sunday morning, this Friday position closing just does not happen.
    The strategy thinks that whatever position was open is still open.

    Tha data series has 'Tick Replay' flag set., and the strategy has 'Calculate on each tick' property set.

    It looks like historical data timestamps are processed differently in the system and the above mention code does not work correctly with historical data timestamp.

    Please let me know if I need to set any other flag/property or there is a workaround for the strategy to correctly close open positions on historical data.

    Thank you,

    #2
    Hello,

    Thank you for the post.

    The order was not executed because the OnMarketData() function is not called on historical data by default. You can enable this function in historical mode by activating Tick Replay. Fist enable the Tick Replay option via Control Center>Tools>Options>Market Data section> "Show Tick Replay". You may then activate Tick Replay for your data series by right-clicking the chart>Data Series>Tick Replay.

    A more detailed description of Tick Replay considerations while using OnMarketData() can be found here:


    More information on developing for Tick replay here:


    Please let us know if we may be of any further assistance.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick answer,

      As I mentioned in the original post, Data Series->Tick Replay is activated already and OnMarketData() function is called. I know that because my whole strategy is implemented within OnMarketData() function and it works fine on historical data except for that one piece that is supposed to close positions at the end of Friday.

      That is why I think something is not right with Timestamp in historical data comparing to real-time, so this particular check
      ----------
      if ((Time[0].DayOfWeek == DayOfWeek.Friday) && (ToTime(Time[0]) >= 135800))
      ----------
      is not working correctly on historical data.

      Thank you,

      Comment


        #4
        Hello,

        Thank you for the reply.

        Have you tried adding print statements for debugging? You may add Print() statements before and after your condition to see what the timestamp is at every call to OnMarketData. I was able to confirm that the condition works using this test snippet:

        Print("About to check the time, Time now: " + ToTime(Time[0])); //Print out Time[0] at every call to OnMarketData()
        if ((Time[0].DayOfWeek == DayOfWeek.Friday) && (ToTime(Time[0]) >= 135800))
        {

        Print("Condition met" + ToTime(Time[0]));

        }

        You can also turn on trace orders to print information about executions.

        https://ninjatrader.com/support/help...raceorders.htm - Trace Orders

        All Prints can be viewed from a NinjaScript output window (Control Center>New>Output window)

        I look forward to hearing of your test results.
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by kevinenergy, 02-17-2023, 12:42 PM
        118 responses
        2,778 views
        1 like
        Last Post kevinenergy  
        Started by briansaul, Today, 05:31 AM
        0 responses
        9 views
        0 likes
        Last Post briansaul  
        Started by traderqz, Yesterday, 12:06 AM
        11 responses
        28 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by PaulMohn, Today, 03:49 AM
        0 responses
        8 views
        0 likes
        Last Post PaulMohn  
        Started by inanazsocial, Today, 01:15 AM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Jason  
        Working...
        X