• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help about Ninja8 IsInstantiatedOnEachOptimizationIteration = false;

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

    Help about Ninja8 IsInstantiatedOnEachOptimizationIteration = false;

    Hello,

    It is my intention to load only once in memory a data dictionary and to reuse it on each optimization iteration.
    I created a simple strategy to test IsInstantiatedOnEachOptimizationIteration=false
    The behavior is quite weird:

    I'm expecting in the output print something like this :

    Loading Start..14/11/2019 17:57:48 Dictionary Count 0
    Loading End..14/11/2019 17:57:50
    Data Dictionary Tot Rec1 7962
    Current BAR 0 my_iteration 1
    Data Dictionary Tot Rec2 7962
    Current BAR 0 my_iteration 0

    I see during the optimization run :

    Loading Start..14/11/2019 18:24:39 Dictionary Count 0
    Loading Start..14/11/2019 18:24:39 Dictionary Count 0 ?? why two?
    Loading End..14/11/2019 18:24:41
    Data Dictionary Tot Rec1 7962
    Loading End..14/11/2019 18:24:41
    Data Dictionary Tot Rec1 7962 ?? why? I was expecting Data Dictionary Tot Rec2 7962
    Current BAR 0 my_iteration 1
    Current BAR 0 my_iteration 0



    Can someone please help me to understand how the progression of the statuses of OnStateChange is working?

    Thanks

    Attached Files

    #2
    Hello gbulle,

    NinjaTrader creates several instances of the strategy (Strategy Pooling), and at least one for each core even if it does not get used.

    Strategies are run in parallel asynchronously.

    With IsInstantiatedOnEachOptimizationIteration those instances that were created will then be re-used after the first test iteration. Each instance will remain alive, the properties set for that test iteration and then the State will be set back to State.Configure and then a new test will be run on that instance that was already used for a previous test iteration.

    There is not set or expected number of instances that will be created. But once created, those instances will be re-used.

    https://ninjatrader.com/support/help...niteration.htm

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks.

      I want to have a csharp dictionary loaded from a JSON file and memory resident during the optimization process
      so all iterations read data (no writing) from the dictionary.

      Any samples in the forum?

      It is possible to detect the name or id of the instance ?

      do you suggest to load the dictionary using a LOCK semaphore?



      Thanks

      Comment


        #4
        Originally posted by gbulle View Post
        do you suggest to load the dictionary using a LOCK semaphore?
        Yes, I'd suggest using a static constructor, too.

        Comment


          #5
          Thanks. I'll try.I;ll post the solution as soon as I have one

          Comment


            #6
            Hi,
            I write here the summary of what I did: my target was to load a dictionary and share it through optimization iterations.
            I added to the project NewtonJsoft DLL.

            private static Dictionary<DateTime, My_Json> my_json_dic;
            private Object _lock = new Object();
            .....
            protected override void OnStateChange()
            {
            .....
            else if (State == State.DataLoaded)
            {
            lock (_lock)
            {
            if (file_json == null && my_json_dic == null)
            {

            my_json_dic = new Dictionary<DateTime, My_Json>();
            file_json = new System.IO.FileStream(@"c:\temp\dax.json", System.IO.FileMode.Open, System.IO.FileAccess.Read);
            reader = new System.IO.StreamReader(file_json);
            string json;
            Print(" Loading Start.." + DateTime.Now.ToString());
            while ((json = reader.ReadLine()) != null)
            {
            My_Json my_json_o = JsonConvert.DeserializeObject<My_Json>(json);
            my_json_dic.Add(my_json_o.Start, my_json_o);
            }
            reader.Close();
            reader.Dispose();
            Print(" Loading End.." + DateTime.Now.ToString());
            }
            else if (my_json_dic != null)
            {
            Print(" Data Dictionary Tot Rec2 " + my_json_dic.Count.ToString());

            }
            else
            {
            if (my_json_dic != null)
            {
            Print(" Data Dictionary Tot Rec3 " + my_json_dic.Count.ToString());

            }
            }
            }
            }

            Comment


              #7
              Hello gbulle,

              NinjaTrader does not support having one object shared by multiple strategies.

              However, if you want to use unsupported code to achieve this, below is a link to an example of static methods and variables.
              https://ninjatrader.com/support/foru...245#post712245
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by lunardiplomacy, Today, 08:09 AM
              0 responses
              3 views
              0 likes
              Last Post lunardiplomacy  
              Started by YevhenShynkarenko, Yesterday, 06:07 PM
              1 response
              13 views
              0 likes
              Last Post NinjaTrader_PaulH  
              Started by akuntysh, 07-23-2019, 09:41 PM
              4 responses
              31 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by Goolden, Today, 05:47 AM
              2 responses
              15 views
              0 likes
              Last Post Goolden
              by Goolden
               
              Started by mestril, Today, 06:00 AM
              7 responses
              21 views
              0 likes
              Last Post mestril
              by mestril
               
              Working...
              X