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 Help

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

    Array Help

    I've always found ways around using arrays in the past by just using multiple variables, even knowing how useful they would be, so I've been attempting to learn the code. A few questions have come from it...

    1) I think I want my arrays to have a specific size. So I've been creating them like so:

    Code:
    #region Variables
    private int loCount = 0;
    private DataSeries swingLow;
    #endregion
    
    protected override void Initialize()
            {
                 double[] swingLow = new double[loCount];  // Collect all the swingLows
            }
    
    protected override void OnBarUpdate()
            {
                 if (some conditions are satisfied, add one to swing loCount number, shift all previous values to the right and create a new, latest swingLow point at [0])
                 {
                     loCount = loCount +1;
                
                     for (int i = loCount; i > 0; i--)
                     {
                        swingLow[i] = swingLow[i-1];
                     }
                 }
             }
    
                    swingLow[0] = Low[2];
    Question is.... a) did I create my array using the right syntax or could it be simpler even, b) does the way I've initialized my array - using loCount variable for the size - allow it to be expanded/contracted depending on what happens to loCount variable in the code, and c) when array is contracted, from say 9 to 8, I first do the opposite of above by making the value at place [0] = [1], from 0 to 8, and then i say loCount = loCount-1, hoping that it's the last value that will be eliminated. Make sense? Is that what happens?


    2) I created another array, I wanted it to contain boolean values, true or false.
    //Variables
    private bool oneTime = true;
    private DataSeries tradeOK;
    //Initialize
    bool[] tradeOK = new bool[oTot];
    //OnBarUpdate
    Code:
                while (oneTime == true)  // Want it to assign all the values in this array initially as true....
                {
                    for (int i = 0; i < oTot; i++)
                    {
                        tradeOK[i] = true;
                    }                
                    oneTime = false;
                }    
    
    // then later, i run a loop trying to find the first entry that has a tradeOK[z] = true value
    int z = 0;
                        while (z < oTot)
                        {
                            if (tradeOK[z] = true)
                            {
    ....
    Question is it keeps giving me a cannot implicitly convert type 'bool' to 'double' CS0029 Error. Why? I thought I created a DataSeries for boolean values, and then tried to assign it boolean values, what's wrong?

    Hope everything is clear enough - thanks in advance for the help!

    Steve

    #2
    Steve,

    I would suggest using a dataseries here instead, as they don't need to have this resetting you are doing. They behave similar to an array, but will automatically manage for space requirements as long as MaximumBarsLookback is set to the 256 option, i.e. 256 elements is the most they will store.



    You can also use a BoolSeries for the booleans. True and false are not 0 and 1 in C#, so this is probably where you are running into an issue.



    Please let me know if I may assist further.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      1) So, I thought about using dataseries, but wouldn't I then have a set containing the same number of elements as bars in a chart? I just wanted a list of values important to me, the most recent being 0, then 1, 2, etc., and when I was done using the value I wanted, I want it gone, I want to erase it. I can't think of a way to do this using a Dataseries?

      Only thing I thought of was to create an array of bar numbers to reference later. But then, I'm right back at the same place....

      2) Well, I'm not trying to convert them to zeros or ones, I was just trying to input the word 'true' or 'false' for a given [x] placeholder in the array of size oTot (being the total allowable number of orders = 3), so the array should look like

      tradeOK[x] = [ , , ].

      I'm using the position in the array, 0, 1 or 2, to carry into the trade's ID, as well as for the stop/target/trigger values being generated. Keeps everything individual/separated for each trade.

      Moving forward, then I want the array to look like this

      tradeOK = [true, true, true]. // I'd love it if I could just initiate the array to look like this in the first place, how to do?

      Then, after the first trade is executed, will look like

      tradeOK = [false, true, true].

      Then, I'm only allowing 3 simultaneous trades to be placed, at which time it would be

      tradeOK = [false, false, false].

      And if by chance the 2nd trade's stop/target/triggers are hit, then will be

      tradeOK = [false, true, false].

      So now the 1 slot is open for a new trade, it's own [1] ID, and its own stop/target/trigger values.

      Why won't it accept my bool 'true/false' inputs into the array?

      Thanks for the feedback!

      Comment


        #4
        steevescott,

        You need to create an array designed to hold the boolean values.

        bool[] array = new bool[3];
        array[0] = true;
        array[1] = true;
        array[2] = true;

        As far as your second question, it will hold up to 256 elements from the historical bars. It removes data that is older than this. The main advantage of using dataseries is that they have built in functionality that a simple array does not have and they update as your bars update fairly efficiently.

        Adding to the data series is as simple as myDataseries.Set(number)

        There are also plenty of ways to check other items, like if it contains a value.

        DataSeries.ContainsValue(int barsAgo)

        You can still use the dataseries like an array MyDataseries[X]

        Please let me know if I may assist further.

        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Thanks, I used DataSeries to take care of one issue, but I still think I need an array to hold all the stops/targets/triggers. The array won't change size ever. I defined the DataSeries variable, then Initialized it, set initial values, then updated the values in the code. But I receive an error from output window. I used Print to determine where it happens.

          Code:
          //Variables
          private DataSeries swingLow;
          
          //Initialize
          double[] longStop = new double[oTot]; //oTot never changes
          for (int i=0; i < oTot; i++)
          {
             longStop[i] = 0;
          }
          
          //OnBarUpdate
          if ( some conditions are met )
          {
          longStop[oCount] = swingLowTrig - stop * TickSize;
          }
          However, in the output window, upon running the strategy, it returns this:
          **NT** Error on calling 'OnBarUpdate' method for strategy 'aeroPacific009/14de82aba0634cf2b8d64b0679e11f72': Object reference not set to an instance of an object.
          Do you have enough information? How do I correct?

          I know it's the longStop[oCount]...line causing the problem since Output window Prints before it but not after.

          Thanks

          Thanks
          Last edited by stevescott05; 05-09-2012, 12:52 AM.

          Comment


            #6
            Hello,

            You probably need to add the following in your Initialize()

            swingLow = new DataSeries(this);
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              This is my Initialize section

              Code:
              swingLow = new DataSeries(this, MaximumBarsLookBack.Infinite);  // Collect all the swingLows            
                          
                          double[] longStop = new double[oTot];
                          double[] longTrig = new double[oTot];
                          double[] longTarget1 = new double[oTot];
                          double[] longTarget2 = new double[oTot];
                          double[] loPrice = new double[oTot];
                          
                          for (int i=0; i < oTot; i++)
                          {
                              longStop[i] = 0;
                              longTrig[i] = 0;
                              longTarget1[i] = 0;
                              longTarget2[i] = 0;
                              loPrice[i] = 999999;
                          }
              Then, separately trying to make it print longStop[0] or longStop[oCount] (same thing), it will result in not wanting to make the printout either.

              Comment


                #8
                steverscott,

                Please try using the following.

                try
                {

                //code I want to try

                }
                catch(Exception e)
                {
                Print(e.ToString());
                }

                This will print out any exceptions as messages in the Tools > output window that you can use for debugging. Many times it includes which line of your code is causing the error. You just need to put all code inside the Try part.
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  It's telling me the same exception, however, it continues to run the code... giving me a P/L in the Strategy Testing window.

                  I thought maybe:

                  Code:
                  MaximumBarsLookBack.Infinite
                  was causing an issue, or the first lines of the OnBarUpdate:

                  Code:
                  if (CurrentBar < sma2)     // sma2 is the longer period sma
                  return;
                  But, i don't see anything pointing to those things.


                  Does anything else stand out as a problem in your opinion?

                  Comment


                    #10
                    steve,

                    Does it identify which object isn't being instantiated in the exception print? What is occurring is that one object isn't being set to something and is probably null. You could also compare your arrays / dataseries to null and if they are null print some message.

                    if(test == null)
                    {
                    Print("Test is NULL!");
                    }
                    Adam P.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks for the idea. Turns out I hadn't declared the array correctly. Was seeing it as a DataSeries, now I can treat it like the array I wanted.

                      It was:

                      Code:
                      //Variables
                      private DataSeries longStop;
                      
                      //Initialize
                      double[] longStop = new double[oTot];
                      Should have been:
                      Code:
                      //Variables
                      private double[] longStop;
                      
                      //Initialize
                      longStop = new double[oTot];
                      Now, the remaining bug to fix is this repeating message in Output Window:
                      System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
                      Parameter name: index
                      at System.Collections.ArrayList.get_Item(Int32 index)
                      at NinjaTrader.Data.DataSeries.ContainsValue(Int32 barsAgo)
                      at NinjaTrader.Strategy.aeroPacific009.OnBarUpdate()
                      Again, would appreciate any advice you have... in the meantime, thanks much for your help thus far, Adam!

                      Comment


                        #12
                        The error simply means you are trying to reference and arrays value that does noot exist yet. Most likely it is one of your own arrays.

                        Please check into the code you are using to access you arrays as this is what will need to be changed.

                        I suggest commenting our one array access line at a time and running. As this will allow us to isolate the line of code that is causing the issues.

                        -Brett

                        Comment


                          #13
                          Two strategies using the same array members

                          #region addStocks
                          Add("XOM", PeriodType.Minute, 390);
                          Add("CVX", PeriodType.Minute, 390);
                          Add("GE", PeriodType.Minute, 390);
                          Add("PG", PeriodType.Minute, 390);
                          Add("T", PeriodType.Minute, 390);
                          Add("JNJ", PeriodType.Minute, 390);
                          Add("PFE", PeriodType.Minute, 390);
                          Add("WFC", PeriodType.Minute,390);
                          Add("PM", PeriodType.Minute,390);
                          Add("KO", PeriodType.Minute, 390);
                          Add("MRK", PeriodType.Minute, 390);


                          The above code is embedded in two separate strategies ... upon enabling the first strategy the array gets loaded.... fine. When the second strategy is enabled you no longer see the list of stocks being loaded - Sometimes strategy 2 does not fire.

                          Sometimes if I disable and then re-enable Strategy 2 the orders get processed and start working.

                          Is there a possible problem in two strategies calling and using the same instrument array?
                          Regards and many thanks.

                          F Everington
                          Last edited by elliot5; 09-27-2015, 06:42 AM.

                          Comment


                            #14
                            Hello everington_f,

                            Thank you for writing in. There are a number of potential issues with submitting orders to the same instrument from two different strategies simultaneously, but I am not positive if this is what you are doing. You should not have any issue with both strategies loading both instrument lists.

                            Could you please check the strategies tab of the control center when you enable these strategies and let me know if when the issue occurs, one of the strategies is yellow instead of green?

                            Thank you in advance.
                            Michael M.NinjaTrader Quality Assurance

                            Comment


                              #15
                              No yellow involved. What are the issues with submitting orders to the same instrument from two different strategies? Regards

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by RubenCazorla, Today, 09:07 AM
                              1 response
                              5 views
                              0 likes
                              Last Post RubenCazorla  
                              Started by Irukandji, Today, 09:34 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by TraderBCL, Today, 04:38 AM
                              3 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              11 responses
                              1,423 views
                              0 likes
                              Last Post jculp
                              by jculp
                               
                              Started by BarzTrading, Today, 07:25 AM
                              2 responses
                              29 views
                              1 like
                              Last Post BarzTrading  
                              Working...
                              X