Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to reset a dictionary when starting a strategy mid-day?

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

    How to reset a dictionary when starting a strategy mid-day?

    Hi there!

    I'm trying to reset a dictionary (called hypo_results) when I activate a strategy in the middle of the day. Currently, the strategy runs on previous data since I have the instrument set to 24 hours.

    I'm trying this with no luck:

    Code:
    // If this is the first tick of a new bar, start all this material
                if (IsFirstTickOfBar)                    
                {    
                    //  Set the proper time for the bar
                    if (ToTime(Time[0]) >= 83000 && ToTime(Time[0]) < 93000)        { currentHour = 0; }                                    
                    else if (ToTime(Time[0]) >= 93000 && ToTime(Time[0]) < 103000)        { currentHour = 1; }    
                    else if (ToTime(Time[0]) >= 103000 && ToTime(Time[0]) < 113000)        { currentHour = 2; }
                    else if (ToTime(Time[0]) >= 113000 && ToTime(Time[0]) < 123000)        { currentHour = 3; }
                    else if (ToTime(Time[0]) >= 123000 && ToTime(Time[0]) < 133000)        { currentHour = 4; }
                    else if (ToTime(Time[0]) >= 133000 && ToTime(Time[0]) < 143000)        { currentHour = 5; }
                    else if (ToTime(Time[0]) >= 143000)                                    { currentHour = 6; }
    
                    tickCount = 1;    
    
                    // Use this to reset all related dictionaries
                    if (currentHour == 0 && !dayReset)    
                    {
                        hypo_results.Clear();                                                // Clear all results from the main dictionary
                        dayReset = true;                                                    // Close out the reset
                    }
                }​
    I'm usually stopping, changing code, compiling and clicking the checkbox for the strategy to run it to the Output window. But it generates a lot of older computation on the day that's transpired so far. I'm running the strategy in real-time.

    Any suggestions? Is my code incorrect?

    #2
    Hello, thanks for writing in. The dictionary can be reset in OnStateChange, during State.Transition, or you can exclusively run your code in real time only by adding if(State == State.RealTime) within OnBarUpdate so that code does not run in State.Historical.

    Please let me know if this does not resolve.

    Comment


      #3
      State.Transition worked like a charm. Thanks!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      41 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      20 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      29 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      46 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      37 views
      0 likes
      Last Post CarlTrading  
      Working...
      X