Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

The array value position always keep changing

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

    The array value position always keep changing

    Hi,
    I have definied the symbol as below
    string [] symbol = new string[] { "V","AAPL","MA","IBM","MSFT"};

    Add the symbol in as MultiInstrument in State.Configure
    for (int i = 0; i < symbol.Length; ++i)
    {
    AddDataSeries(symbol[i], Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
    }

    In OnBarUpdate, I would like to calculate the Relative Strength Ratio
    MySeries[0] = new []{
    2*Closes[1][0] / Closes[1][63] +Closes[1][0] / Closes[1][126]+Closes[1][0] / Closes[1][189]+Closes[1][0] / Closes[1][252] ,
    2*Closes[2][0] / Closes[2][63] +Closes[2][0] / Closes[2][126]+Closes[2][0] / Closes[2][189]+Closes[2][0] / Closes[2][252] ,
    2*Closes[3][0] / Closes[3][63] +Closes[3][0] / Closes[3][126]+Closes[3][0] / Closes[3][189]+Closes[3][0] / Closes[3][252] ,
    2*Closes[4][0] / Closes[4][63] +Closes[4][0] / Closes[4][126]+Closes[4][0] / Closes[4][189]+Closes[4][0] / Closes[4][252] ,
    2*Closes[5][0] / Closes[5][63] +Closes[5][0] / Closes[5][126]+Closes[5][0] / Closes[5][189]+Closes[5][0] / Closes[5][252]
    };

    After Calculate the Relative Strength Ratio, I would like to sort the MySeries[0] array in every bar and hence the MyStockName.
    for (int i = 1; i < n; ++i) {
    double key = MySeries[0][i];
    string intkey = MyStockName[0][i];
    int j = i - 1;

    // Move elements of arr[0..i-1],
    // that are greater than key,
    // to one position ahead of
    // their current position
    while (j >= 0 && MySeries[0][j] < key) {
    MySeries[0][j + 1] = MySeries[0][j];
    MyStockName[0][j + 1] = MyStockName[0][j];
    j = j - 1;

    }
    MySeries[0][j + 1] = key;
    MyStockName[0][j + 1] = intkey;
    }

    But I find the symbol and MyStockName value keep changing. The most weird is the symbol array, i didn't do anything about it,just assigned the value to MyStockName, When I print symbol value, the position is kept changing.


    for (int i = 0; i < n; ++i)
    {
    // Print(MyStockName[0][i] );
    Print(symbol[i] );
    }

    Thanks.


    ----------

    The printed result:

    6/2/2021 5:00:00
    MSFT
    MA
    IBM
    AAPL
    V

    9/2/2021 5:00:00
    MA
    V
    IBM
    MSFT
    AAPL

    10/2/2021 5:00:00
    V
    AAPL
    IBM
    MA
    MSFT

    11/2/2021 5:00:00
    AAPL
    MSFT
    IBM
    V
    MA

    12/2/2021 5:00:00
    MSFT
    MA
    IBM
    AAPL
    V

    Attached Files

    #2
    Hello heiheilau2000,

    This is not supported to do and may cause unexpected and undesirable behavior (and will not work in optimizations).

    From the help guide:
    "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner."


    To clarify, using a variable, such as a list, is dynamic as the value can change.

    Regarding the print, what print is producing this output of the time and symbol? (The code you have suggested and the print do not match as there no print the time, indicating this is not the code making that output)

    If you add this to a script by itself are you able to reproduce?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your reply.
      So, can I change the variable to constant like below? however, it's still not workable.

      private static readonly string [] symbol = new string[] { "V","AAPL","MA","IBM","MSFT"};
      --------------------
      Regarding the print, I have double checked. The BarsRequiredToTrade should be > 255. otherwise there should be no print and Use the daily data for test.
      date:2015/1/1- 2021/2/12
      Thanks

      Comment


        #4
        Hello heiheilau2000,

        No, a ready only string array variable would still be a variable and would be dynamic and thus not officially supported for AddDataSeries(). Use at your own risk.

        Regarding the order of the prints, you mentioned you have double checked the print, however you have not provided it. The output in your post doesn't match the suggested prints (the output has a time the print code suggested does not) so that must be different code. What code did you double check?

        May confirm you have tested that list loop and print in a new script (to isolate the code)?

        Please provide an export of this new test script so I may see what the actual code is and the actual print output that appears from it.

        To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
        1. Click Tools -> Export -> NinjaScript...
        2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
        3. Click the 'Export' button
        4. Enter a unique name for the file in the value for 'File name:'
        5. Choose a save location -> click Save
        6. Click OK to clear the export location message
        By default your exported file will be in the following location:
        • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
        Below is a link to the help guide on Exporting NinjaScripts.
        http://ninjatrader.com/support/helpG...-us/export.htm
        Once exported, please attach the file as an attachment to your reply.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,
          Thanks for your reply.
          Attached. Please check if it is ok.
          Attached Files

          Comment


            #6
            Hello heiheilau2000,

            You may have provided the original script and not a test script with only the list, loop, and print.

            I am seeing AddDataSeries in here improperly adding series dynamically, and tons of logic in OnBarUpdate.

            Have you created a test script to test just the list and looping through the list and printing the value in the loop without any other code?
            May I have that?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi ChelseaB,

              I have simplified the code and changed to use the suggested method of AddDataSeries.

              The logic of OnBarUpdate is needed because it is the root cause to cause the symbol [] array changing every time.

              This part is calculated the Relative Strength Ratio. I have simplified the formula for easy following.
              ------------------------------
              //Relative Strength Ratio Calculation
              MySeries[0] = new []{
              2*Closes[1][0] / Closes[1][2] ,
              2*Closes[2][0] / Closes[2][2] ,
              2*Closes[3][0] / Closes[3][2] ,
              2*Closes[4][0] / Closes[4][2] ,
              2*Closes[5][0] / Closes[5][2]
              };
              int n = MySeries[0].Length;
              ----------------------

              This part is assigned the symbol[] in the MyStockNameSeries
              ----------------------
              //assign symbol to MyStockName[0]
              MyStockName[0]= symbol;
              -----------------------------------



              This part is the most important as it cause the symbol [] array changing value every time.
              What i want to do is first the sort the MySeries value and then MyStockName series followed the order MySeries together.
              I found that the problem come from these two line of codes.
              MyStockName[0][j + 1] = MyStockName[0][j];
              MyStockName[0][j + 1] = stringkey;
              if add "//", to add comment of these two line of codes, the printed symbol[] array values will not change. I have shared the two version of export file.
              One is without removing these two lines of code, another one is add comment to hide these two lines of codes, you can see the differences.
              Would you please suggest?

              ---------------------
              //sorting MySeries[0][j]
              for (int i = 1; i < n; ++i) {
              double key = MySeries[0][i];
              string stringkey = MyStockName[0][i];
              int j = i - 1;

              // Move elements of arr[0..i-1],
              // that are greater than key,
              // to one position ahead of
              // their current position
              while (j >= 0 && MySeries[0][j] < key) {
              MySeries[0][j + 1] = MySeries[0][j];
              MyStockName[0][j + 1] = MyStockName[0][j];
              j = j - 1;

              }
              MySeries[0][j + 1] = key;
              MyStockName[0][j + 1] = stringkey;
              }

              -------------------------------



              Thanks for your help.
              Attached Files

              Comment


                #8
                Hello heiheilau2000,

                You may not be understanding what I am directing.

                This script has even more code, not less..

                Are you still working on the issue where printing the values in a string array named symbol appear out of order, or are you working on a new issue?

                If not, remove all code that is not the string array, that is not the single one loop that loops through the string array.

                Remove lines 35 to 38, lines 58 to 62, lines 66 to 74, lines 80 through 130, lines 138 to 144, and lines 146 to 164.

                Once the code is removed this would show what looping through the array without modifications does.

                After the point where this is confirmed, the next steps would be to add the logic back one line at a time until the issue returns. The last added line is causing the issue..

                If you are instead asking for me to fix your logic on your behalf I will not be able to do this, however this thread will remain open for any community members that would like to do this as a convenience to you.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello ChelseaB,

                  Thanks for your help.

                  I have uploaded the simplified code according to your suggestion.
                  Attached Files

                  Comment


                    #10
                    Hello ChelseaB,

                    I have add back some code from the simplified code and find if I add this line of codes, the symbol value will be changed. Would you please suggest?
                    Sorry for asking stupid question.

                    MyStockName[0]= symbol; //assign symbol value to MyStockName[0] series
                    MyStockName[0][0]=symbol[4]; //reassign the symbol[4] value to MyStockName[0][0]
                    Attached Files

                    Comment


                      #11
                      Hello heiheilau2000,

                      To confirm, this with test script, the order of the output is no longer out of order and you are not able to reproduce, is this correct?
                      The output I am receiving on my end is attached.
                      This will confirm, that the array itself is being declared properly and that the loop is looping through properly.

                      The next steps are to start adding code back until the behavior returns. Start by adding just the declarations for private and public variables. As it is a multi-series script, add the calls for AddDataSeries() as well.

                      This shouldn't have an affect, and will rule this code out. Confirm, after adding this the behavior has not returned.

                      I'm seeing that you have changed the max index from n to 5 (instead of symbol.Count()). This is likely involved, however we will evolve toward that point.
                      Attached Files
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello ChelseaB,

                        I have add back some code from the simplified code and find if I add this line of codes, the symbol value will be changed. Would you please suggest?
                        Sorry for asking stupid question.

                        MyStockName[0]= symbol; //assign symbol value to MyStockName[0] series
                        MyStockName[0][0]=symbol[4]; //reassign the symbol[4] value to MyStockName[0][0]
                        Attached Files

                        Comment


                          #13
                          Hello heiheilau2000,

                          So in this code, you are assigning a reference to the symbol array from MyStockName[0]. MyStockName[0] now points to the symbols array.

                          Then you assign MyStockName[0][0] the value of symbol[4], which is the same as assigning symbol[0] the value of symbol[4].

                          symbol[0] is now equal to symbol[4].

                          Is this not the behavior you are wanting?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello Chelsea,

                            Yes, it is not what I want. I don't know why the symbol[] value is changed. Thanks.

                            Comment


                              #15
                              Hello heiheilau2000,

                              Thank you for your reply.

                              Chelsea is out of the office today, but I can certainly help to clarify.

                              For a reference type, the value is a reference which may be null or may be a way of navigating to an object containing the information.

                              For example, think of a variable as like a piece of paper. It could have a map to my house on it, but it couldn't have my actual house... it would have to have directions to my house. Those directions are the equivalent of a reference. In particular, two people could have different pieces of paper containing the same directions to my house - and if one person followed those directions and painted my house red, then the second person would see that change too. If they both just had separate pictures of my house on the paper, then one person coloring their paper wouldn't change the other person's paper at all.

                              In this case, symbol[] is the house, and MyStockName[0] is the directions to the house. Assigning the value of MyStockName[0][0] the value of symbol[4] is basically the equivalent of going to the house and painting it red - you're changing the underlying thing that MyStockName[0][0] points to, which is symbol[0].

                              Please let us know if we may be of further assistance to you.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              71 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              143 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              76 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              47 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              51 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X