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 mattbsea, Today, 05:44 PM
        0 responses
        4 views
        0 likes
        Last Post mattbsea  
        Started by RideMe, 04-07-2024, 04:54 PM
        6 responses
        31 views
        0 likes
        Last Post RideMe
        by RideMe
         
        Started by tkaboris, Today, 05:13 PM
        0 responses
        2 views
        0 likes
        Last Post tkaboris  
        Started by GussJ, 03-04-2020, 03:11 PM
        16 responses
        3,282 views
        0 likes
        Last Post Leafcutter  
        Started by WHICKED, Today, 12:45 PM
        2 responses
        20 views
        0 likes
        Last Post WHICKED
        by WHICKED
         
        Working...
        X