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

On managing different types of array

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

    On managing different types of array

    I would like some explanation on manipulating different types of array.Let's take an example :

    Assume I have 3 arrays : int[] array1 = new int{0,0,0}
    int[] array2 = new int{0,0,0}
    double[] array3 = new double{0,0,0}


    I want to do a simple operation, something like "array3=array1/array2"

    When I use the following snippet :

    for (i=0;i<array1.length;i++) {array3[i]=array1[i]/array2[i];}

    ...It always returns 0 values. My best guess is that I must convert array1/array2 into a double before assigning it to array3 but I am not sure of that.

    Using the "AssignTo" method might be more straightforward but I am not very familiar with it. Could you give me the best method to do these kind of operations with different types of arrays ?

    #2
    Hello,

    Thanks for the note.

    This is more of a C# general question then a NinjaScript question. I have not dealt too much with arrays myself however the only issue I see here unless I'm missing this section of code is that all arrays currently hold 0. 0/0 would yield exception or 0.

    Would also recommend adding Print statments to print the variables state to see whats going on.

    Print();

    Let me know if I can be of further assistance.

    Comment


      #3
      to be more precise I filled array1 and array2 with non-zero values but when I try to do array3=array1/array2 and Print(array3[0]) for example it always returns 0. Why ?

      It seems like dividing integer value by another integer value returns zero if the result is not an integer. Am I clear ?
      Last edited by baba123; 08-12-2011, 12:52 PM.

      Comment


        #4
        Hello,

        Not sure on this one sorry and no real pointers I could provide either. I would expect it to work not sure why it would not work. Might suggest using a resource such asa MSDN or a c# code tutorial site to see if your missing something on use of your array. it looks good from here. If I was facing the problem this would be my first step, after I added Prints() to make sure that Array[1] and Array[2] held values.

        Let me know if I can be of further assistance.

        Comment


          #5
          Seems like I am doomed :-(

          I already searched the MSDN database but did not find anything. I wonder if converting my int arrays into double arrays at the beginning of the code would do the trick.
          I will try it now but it will be my last chance...

          Comment


            #6
            baba123, I just noticed something here. Your arrays don't match data types. Please try casting the result into a double before setting it.
            Code:
            array3[i] = (double) array2[i]/ (double) array1[i]
            // or
            array3[i] = double.Parse(array2[i]/array1[i]);
            EDIT: You just noticed this too. Hopefully it works. If not, try putting the math stuff into a try catch block and see if it spits out a more useful error.
            AustinNinjaTrader Customer Service

            Comment


              #7
              I already tried this but it did not work. Now I am trying to rebuild the code using only double arrays but I have a problem or rather an interrogation :

              When I write :

              if (CurrentBar<1)
              return;
              else
              {
              double compte = (double) Math.Sign(Close[0]-Close[1]);
              Value1.Set(compte);
              Print(Value1[0]);
              }


              in the output window I only get integer values (-1,0,1). Basically I try to put sum from a to b (Value1[0]) in those array1 and array2 defined earlier if some conditions are met. a and b are dependant of those conditions too.
              At first I defined them as integer arrays since they only contained sum of 0,1,-1 values (integer values).

              But somewhere in the script i need to divide array1 by array2 and that is when things get nasty. The results of this operation is clearly a double but I only get 0 in the output window no matter what I try. Clearly frustrating !

              I will define array1 and array2 as double array and see if this works but even converting Math.Sign values into double stil returns integer value I am afraid I might run into another problem....

              Comment


                #8
                baba123, the Math.Sign() function is working as expected. It only returns values of -1, 0, or 1, indicating negative, zero, and positive values.

                If the values you're working with are directly related to bar vales, perhaps you should be working with a DataSeries instead, which is basically an array, but tied to bars.

                Here is a sample that shows how to store calculations in a DataSeries - http://www.ninjatrader.com/support/f...ead.php?t=7299.
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  I finally get it to work. The solution was to convert all integer variables into double ones in order to work with only 1 type of array.

                  Thank you for your help.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by geotrades1, Today, 10:02 AM
                  1 response
                  4 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by ender_wiggum, Today, 09:50 AM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by rajendrasubedi2023, Today, 09:50 AM
                  1 response
                  12 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by bmartz, Today, 09:30 AM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_Erick  
                  Started by geddyisodin, Today, 05:20 AM
                  3 responses
                  26 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Working...
                  X