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

Array Problem

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

    Array Problem

    I have a problem using array method.
    I would like to store values from a dataseries in 2 arrays. The code looks like this :

    private DataSeries Value1 ;
    private double[] jj ;
    private double[] vv ;

    protected override void Initialize()
    {
    Value1 = new DataSeries (this) ;
    jj = new double[period];
    vv = new double[period];
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar<1) {return;}
    Value1.Set(Math.Sign(Close[0]-Close[1]));


    if (CurrentBar>period)
    {

    for (int k =1;k==period;k++)
    {
    jj[k]=Value1[k-1];
    vv[k]=Value1[k];
    }
    }


    No problem compiling, no error in the log but when I run the indicator and try random values for jj or vv it always return 0.
    What am I doing wrong ?

    #2
    arrays start from zero.

    Comment


      #3
      The statements within the code block of a for loop will execute a series of statements as long as a specific condition remains true.

      If period != 1, then your "for loop" condition is never true, as k cannot simultaneously be equal to 1 and period initially, so your loop never get parsed.

      Comment


        #4
        I changed my for loop :

        for (int k =0;k<period;k++)
        {
        jj[k]=Value1[k];
        vv[k]=Value1[k+1];
        }

        but then I have an out of bound problem ?
        Other topic : my Value1 dataseries contains only integer but when I try to declare jj or vv as integer arrays I have an error message ('impossible to implicitely convert double to int). Why ? What should I change ?

        Comment


          #5
          Originally posted by baba123 View Post
          I changed my for loop :

          for (int k =0;k<period;k++)
          {
          jj[k]=Value1[k];
          vv[k]=Value1[k+1];
          }

          but then I have an out of bound problem ?
          At first blush, it probably comes from trying to access k+1 at the zero index. Without seeing the rest of your code, it is hard to tell.

          Other topic : my Value1 dataseries contains only integer but when I try to declare jj or vv as integer arrays I have an error message ('impossible to implicitely convert double to int). Why ? What should I change ?
          You declared Value1 as a DataSeries, not an IntegerSeries, so even if you populate it by inputting integer values, they will be stored as doubles.

          You can either cast them to int if you want to put them into an int array, or you can change the declaration of Value1 to be an IntegerSeries. If you are absolutely certain that you need only integer precision, I would do the latter.

          Comment


            #6
            I made some changes :

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



            if (CurrentBar>(2*recul))
            {

            for (int k =0 ;k < recul;k++)
            {
            int Value1a=System.Convert.ToInt32(Value1[k]);
            int Value1b=System.Convert.ToInt32(Value1[k+1]);
            jj[k]= Value1a;
            vv[k]= Value1b;
            }

            Print(jj[3]);
            }

            I still have an out of bound issue, I precise that the code is complete
            Last edited by baba123; 05-15-2011, 03:22 PM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by GussJ, 03-04-2020, 03:11 PM
            16 responses
            3,281 views
            0 likes
            Last Post Leafcutter  
            Started by WHICKED, Today, 12:45 PM
            2 responses
            19 views
            0 likes
            Last Post WHICKED
            by WHICKED
             
            Started by Tim-c, Today, 02:10 PM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by Taddypole, Today, 02:47 PM
            0 responses
            5 views
            0 likes
            Last Post Taddypole  
            Started by chbruno, 04-24-2024, 04:10 PM
            4 responses
            53 views
            0 likes
            Last Post chbruno
            by chbruno
             
            Working...
            X