Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Some help with strategy

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

    Some help with strategy

    I used the code sample SetIndicatorValueFromAddonWindowPropertyGridExampl e_NT8.zip (4.6 KB, 429 views)

    f
    rom this thread:



    I think I have it working decently in my strategy. Only issue is when i stop the strategy it defaults back to the variable parameters saved to the strategy. Is there a way to make it store the updated variables for use when i reactivate the strategy?

    I made a short video showing problem and the code as well here:




    #2
    Hello, thanks for writing in. The Addon would need to save the last set of values as the addon is setting the values, not the indicator/strategy, this would require some changes to the Addon portion of this example to complete, as in save the last set values to your own xaml or .txt file to retrieve it at a later time. The example generates a new addon window each time it is loaded so nothing is getting saved to the workspace.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      I have worked on this and have it somewhat printing the variables to my text file so far, but still getting error as follows:
      2/10/2023 10:58:52 AM Default Unhandled exception: Type 'System.ComponentModel.ReflectPropertyDescriptor' in Assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

      I have marked all of the classes i can as serializable. Below is the code i used for the addon. Any help would be appreciated.

      private void OnOkButton_Click(object sender, RoutedEventArgs e)
      {
      if (TargetIndicator == null)
      return;

      // set the indicators class to the local class instance to save the changes
      TargetIndicator.MyExpandableClass = localPropertyClass;

      FileStream sw = new FileStream(System.IO.Path.Combine(NinjaTrader.Core .Globals.UserDataDir, @"bin\Custom\AddOns\test.txt"), FileMode.OpenOrCreate);

      BinaryFormatter formatter = new BinaryFormatter();

      formatter.Serialize(sw, TargetIndicator.MyExpandableClass);

      // trigger the indicator to show a textbox with the changed values
      //TargetIndicator.ShowValues();

      this.Close();
      }​

      Comment


        #4
        Hello, thanks for the follow up. Im not sure if this ReflectPropertyDescriptor error is coming from this code. It looks like there is something in your script that NinjaTrader is trying to save, but I am not able to speculate on what it is.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          I added it in the original script to keep it simple and same results. The text file shows the following:

          [1]Xbfd13b3b36ac4d8ca864948b7a6c07bf, Version=8.0.28.0, Culture=neutral, PublicKeyToken=null8SetIndicatorValueFromAddonWind owProperties.PropertyClass
          PropertyChanged<IntValue>k__BackingField<StringVal ue>k__BackingField
          "System.DelegateSerializationHolder[1]

          Attached Files

          Comment


            #6
            Hi, I do apologize, but this is going outside of the scope of support I am able to provide on this subject. This is more in the realm of C# programming/errors. Other members of the forum would need to volunteer to look into this issue.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              If I remove this code the error goes away:

              // this event lets the property grid know that items have changed and the grid should be regenerated
              /* public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

              // call this method to trigger the property grid to update after changes (for example in the set of a property)
              protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
              {
              if (PropertyChanged != null)
              PropertyChanged.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(pro pertyName));
              }​

              Comment


                #8
                I got this working using the following coding if anyone needs it.

                in strategy:
                using Newtonsoft.Json;
                region MY EXPANDABLE CLASS

                if (File.Exists(System.IO.Path.Combine(NinjaTrader.Co re.Globals.UserDataDir, @"bin\Custom\AddOns\test2.txt")))
                {
                string jsonpath = File.ReadAllText(System.IO.Path.Combine(NinjaTrade r.Core.Globals.UserDataDir, @"bin\Custom\AddOns\test2.txt"));
                MyExpandableClass = new SetIndicatorValueFromAddonWindowProperties2.Proper tyClass();
                MyExpandableClass = JsonConvert.DeserializeObject<SetIndicatorValueFro mAddonWindowProperties2.PropertyClass>(jsonpath);
                }
                else
                {
                MyExpandableClass = new SetIndicatorValueFromAddonWindowProperties2.Proper tyClass()
                {​....

                in addon:
                using Newtonsoft.Json;
                private void OnOkButton_Click(object sender, RoutedEventArgs e)
                {
                if (TargetIndicator == null)
                return;

                // set the indicators class to the local class instance to save the changes
                TargetIndicator.MyExpandableClass = localPropertyClass;


                string filename = (System.IO.Path.Combine(NinjaTrader.Core.Globals.U serDataDir, @"bin\Custom\AddOns\test2.txt"));
                string jsonString = JsonConvert.SerializeObject(localPropertyClass);
                File.WriteAllText(filename, jsonString);

                // trigger the indicator to show a textbox with the changed values
                //TargetIndicator.ShowValues();

                this.Close();
                }​

                Comment


                  #9
                  metical1 do you have any code that writes JSON?

                  Comment


                    #10
                    Originally posted by BartMan View Post
                    metical1 do you have any code that writes JSON?
                    I used the code i posted previously. It took me about a week to make it work as i wanted (I am not really a coder but I have been learning through this support and searching google and youtube), but if you can piece together the original code and then use what I posted it will work.

                    Here is a little video of how it does work on my project:

                    Comment


                      #11
                      Thanks Much

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by ageeholdings, Today, 07:43 AM
                      0 responses
                      7 views
                      0 likes
                      Last Post ageeholdings  
                      Started by pibrew, Today, 06:37 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post pibrew
                      by pibrew
                       
                      Started by rbeckmann05, Yesterday, 06:48 PM
                      1 response
                      14 views
                      0 likes
                      Last Post bltdavid  
                      Started by llanqui, Today, 03:53 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post llanqui
                      by llanqui
                       
                      Started by burtoninlondon, Today, 12:38 AM
                      0 responses
                      12 views
                      0 likes
                      Last Post burtoninlondon  
                      Working...
                      X