Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to use struct in strategy

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

    How to use struct in strategy

    I would like to use struct to hold several data with each bar. How to make the declaration to use with BarArray? I usded the following code, but reports error. Thanks!

    public struct StructTrand
    {
    public Series<double> series1;
    public Series<int> series2;
    }

    private StructTrend seriesTrend;

    else if (State == State.DataLoaded)
    {
    seriesTrend = new Series<StructTrend>(BarsArray[1]);
    }


    #2
    What error does it report?

    Please attach a screenshot.

    Comment


      #3
      The following is the error message.
      Cannot implicitly convert type 'NinjaTrader.NinjaScript.Series<NinjaTrader.NinjaS cript.Strategies.ATM22.StructTrend>' to 'NinjaTrader.NinjaScript.Strategies.ATM22.StructTr end'

      Comment


        #4
        There is no space in the "StructTrend" at the last of the sentence.

        Comment


          #5
          Hello atrader,

          You have a spelling error in your sample
          Your stuct is named StructTrand but you are trying to use it as StructTrend


          Code:
          public struct [B]StructTrand[/B]
          {
          public Series<double> series1;
          public Series<int> series2;
          }
          
          private [COLOR=#c0392b][B]StructTrend [/B][/COLOR]seriesTrend;
          
          else if (State == State.DataLoaded)
          {
          seriesTrend = new Series<[COLOR=#c0392b][B]StructTrend[/B][/COLOR]>(BarsArray[1]);
          }

          You also need to define the private variable as a series not a struct:

          Code:
          private [B]Series<StructTrand>[/B] seriesTrend;

          Comment


            #6
            Hi Jesse,

            The struct name is a typo here, but it was correct when compiling. So the data type can only be a simple data type not a struct in Series, right?

            It is ok to use
            private Series<int> seriesTrend;

            but not ok to use, right?
            private Series<StructTrend> seriesTrend;

            Thank you!

            Comment


              #7
              Hello atrader,

              If you made a typo on just the forum that won't help us to assist you, in the future please just copy paste so we can provide accurate help. The code you provided doesn't make sense and won't actually compile due to the spelling error and also the variable error. The answer is still the same here, you need to use the correct type in all places its used and then define the variable like the help guide describes. https://ninjatrader.com/support/help...ightsub=series

              It is valid to use private Series<StructTrend> seriesTrend;
              but you need to make sure StructTrend exists, your sample does not have StructTrend it has StructTrand which is a different type. The name has to match exactly.

              The code I posted highlights the problems in your original sample, you need to fix the spelling error and you also need to change the variable you made to match that type.

              The error you are reporting is specifically because you are trying to assign a series to a different type variable:

              This is a private variable for 1 struct object: private StructTrand seriesTrend;

              This is a private variable for a series of struct objects: private Series<StructTrand> seriesTrend;

              You would need to make sure the private variable is a series of your struct if you are trying to assign a series to that variable.

              Keep in mind that if you use a more complex object that needs instantiated you also need to create a new object and assign it to the series for every OnBarUpdate call.
              That would look like the following:
              seriesTrend[0] = new StructTrand();



              Comment


                #8
                Hi Jesse,

                Sorry for the typo. The error message I posted here is after I corrected the typo (I forgot to correct it here since I copied and pasted it before I made the correction). And the error message points to the line in the else if (State == State.DataLoaded) section as below
                seriesTrend = new Series<StructTrend>(BarsArray[1]);

                It looks differenct with your seriesTrend[0] = new StructTrand(); in the OnBarUpdatae section and yours looks like for each individual variable insinde the strcture, right? I am not quite familar with the grammers here. Would you provide more detailed instruction?

                Thank you so much!

                Comment


                  #9
                  Hello atrader,

                  The error message you posted was also answered in post 5, you used the wrong type of variable and that is the reason you got that error.

                  seriesTrend the private variable needs to be of type Series<StructTrend> , you have it as StructTrend only and not a series. I highlighted this in post 5 and showed the correct code. Here it is again, this is how you define a series as a variable:

                  private Series<StructTrend> seriesTrend;


                  The other comment I made is that you need to create an object for each series index. Similar to how you would assign a value to a Series<double>. Because you are using Objects now you need to instantiate it like I had shown in post 7, here's that code again:

                  seriesTrend[0] = new StructTrand();

                  You would need to create a new instance of your struct, assign whatever values you wanted to the struct and then assign that to the series.

                  Comment


                    #10
                    Hi Jesse, finally got what you mean. Thank you so much again! Have a nice day and weekend!

                    Comment


                      #11
                      Hi Jesse,

                      It all worked when I added the code seriesTrend[0] = new StructTrand(); as you mentioned in your last post. But when I tried to assign a value to the int variable inside the struct by using
                      seriesTrend[0].series2 = 1;
                      it report the following errors:
                      Cannot modify the return value of 'NinjaTrader.NinjaScript.Series<NinjaTrader.NinjaS cript.Strategies.ATM22.StructTrand>.this[int]' because it is not a variable
                      Cannot implicitly convert type 'int' to 'NinjaTrader.NinjaScript.Series<int>'

                      To make it simple, I deleted the double series1 inside the struct. Also, you said "You would need to create a new instance of your struct, assign whatever values you wanted to the struct and then assign that to the series." Is the "seriesTrend[0] = new StructTrand();" for creating the new instance of struct? Is the "seriesTrend[0].series2 = 1;" for assgning value to the individual value to the struct? If not, would you provide some sample code? Also, how to assgn that to the series?

                      Thank you!

                      Comment


                        #12
                        Hello atrader,

                        For a struct that would be expected, you would have to make a constructor and initialize the properties through the constructor when using a struct. It sounds like you probably want to use a Class instead of a struct if you want to be able to set the variables like you are showing.

                        There are a few samples on the following page, I have extracted a few parts to demonstrate the difference: https://www.educba.com/c-sharp-struct-vs-class/
                        Code:
                        public struct Demo
                        {
                            public int x, y;
                            //parameterized constructor
                           public Demo(int x, int y)
                           {
                              this.x = x;
                              this.y = y;
                           }
                        }
                        
                        Demo myDemo = new Demo(1,2);
                        This struct now always has the values 1 and 2 for its properties. They cannot be changed later.

                        Code:
                        public class Demo
                        {
                            public int x {get;set;}
                            public int y {get;set;}
                        }
                        
                        Demo myDemo = new Demo();
                        myDemo.x = 1;
                        myDemo.y = 2;
                        This is a class which has public properties. You can set their value whenever you would like. You still have to define a new object for each bar just like a struct when using it with a series but this lets you set the values later in OnBarUpdate.

                        Any object can be used with a Series<T> T being generic for the type you want to use. A struct is good for holding constant values that don't need to change, a class is used for variable values that may need to change or be updated. A struct needs defined when you create it, a class only needs values defined when you want to define a value for its property.
                        Last edited by NinjaTrader_Jesse; 08-08-2022, 02:02 PM.

                        Comment


                          #13
                          Hi Jesse, finally got it worked. Thank you so much for all your information!

                          Comment


                            #14
                            Originally posted by bltdavid View Post
                            What error does it report?

                            Please attach a screenshot.
                            Hello bltdavid!

                            I'd like to be in contact with you. I need you code an NT8 indicator based on your (https://ninjatraderecosystem.com/use...on-coloring-2/) indicator!

                            Comment


                              #15
                              Originally posted by TRADER_F View Post

                              Hello bltdavid!

                              I'd like to be in contact with you. I need you code an NT8 indicator based on your (https://ninjatraderecosystem.com/use...on-coloring-2/) indicator!
                              I've sent you a private message with my email address.

                              Do know how to check your forum Inbox for such messages?
                              It should be there ...

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              56 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              132 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              73 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              45 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              49 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X