Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help on Arraying

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

    Help on Arraying

    Hello,

    I have a C# question, which I can find too much info about to understand it right to use it properly: arrays. As I'm no C# prodigy, I mainly learn from looking at existing code after which I try and alter it for my own use. Up till now with success, but I come to a point where I think my code could use optimization because loads of recurring lines of code are being used, while the same could probably coded more efficient and profesionally. I hope some of you might help me in the right direction.

    In my code I have the following lines for example:

    Under Variables I have:
    private IOrder enterOrderPOPH = null;
    private IOrder enterOrderPOPC = null;
    private IOrder enterOrderPOCO = null;
    private IOrder enterOrderPOCH = null;
    and so another 358 (really...)

    Later under OnBarUpdate after some trigger:
    if (enterOrderPOPC != null) {CancelOrder(enterOrderPOPC); enterOrderPOPC = null; exitOrderPOPC = null; filledOrderPOPC = 0;}
    if (enterOrderPOPH != null) {CancelOrder(enterOrderPOPH); enterOrderPOPH = null; exitOrderPOPH = null; filledOrderPOPH = 0;}
    if (enterOrderPOCO != null) {CancelOrder(enterOrderPOCO); enterOrderPOCO = null; exitOrderPOCO = null; filledOrderPOCO = 0;}
    if (enterOrderPOCH != null) {CancelOrder(enterOrderPOCH); enterOrderPOCH = null; exitOrderPOCH = null; filledOrderPOCH = 0;}
    and again 358 other variations on the above.

    There should be a more intelligent way than mine to have these 362 lines of if's checked and possibly executed without having to state all the variations literally. The only deviations between the lines are the letters like "POPC", which give each order it's unique key so I can trace them on the order updates and executions.

    When looking online and the NT support forum, I come back to arrays again and again, without really understand how to bend this into a usable form whivh suits my enormous amount of lines in the code. And these 362 line are coming back several times, like on entering, exiting, checking on fills etc.

    I hope you could get me along in tackling this RAM eating beast as it is right now.

    Kind regards,
    Francis

    #2
    Hello Francis,

    An Array could be used to help reduce the amount of lines of code that you are using inside of your strategy, but it is not supported by NinjaTrader is all since it is more C# related. Here is a quick example of this:

    Code:
    private IOrder[] entryOrder = new IOrder[362];
    
    protected override void OnStartUp()
    {
    	for(int i=0;i< entryOrder.Length; i++)
    	{
    		entryOrder[i] = null;
    	}
    }
    Here is some examples of how to use an Array that you may view.

    JCNinjaTrader Customer Service

    Comment


      #3
      Hello JC,

      Thank you for your reply. I understand NT does not support standard programming, or want to explain every possible variation of this.

      Although your example might be clear to most of the people here, it won't help me any further, as this is such an example I see everywhere, but which I just don't understand when explained more explicit to my situation. Maybe I just have to stick with my hundreds of records, as those do work, but probably not as quick as they should.

      Kind regards,
      Francis

      Comment


        #4
        Hello Francis,

        I'll try to explain it in a different way. Using an Array like that it is a different way of having multiple variables. Think of it as a numbered list, when you use the "[]" brackets you are just creating a numbered list of that object for you to use. So when you are accessing it instead of having a different name you would tell it what number on the list you are looking for.

        So an when you access "entryOrder[0]" it is like calling "enterOrderPOPH".

        "entryOrder[1]" would be like calling "enterOrderPOPC".

        This way you have a list of number you just tell it what number you are looking at.

        Note, while this is easier to declare it is harder to remember which number is referring to what especially with over 300 items. You may want to keep the "entryOrderPOPC" way so that way you know which order you are referring to.

        Let me know if you have any questions.
        JCNinjaTrader Customer Service

        Comment


          #5
          Hello JC,

          Your explanation this way does help to understand how an array can be used. So I have to make up a list first with all the 362 different items once, to which I can reffer to later using the right number from their sequence?

          You say it yourself it has not really use to do so, because I still would have to use 362 separate lines with all this numerical refferals instead of the original names. That way I also see no advantage.

          But is there no solution in which you can point me to in which a standardize line of code could replace all these unique lines for example? My way seems a bit too simple and thus amateuristic. My code constists of over 20.000 lines, mostly because of all these single lines, but used for different assignments. The code is fast, but uses a lot of memory, where I think it can be much lighter.

          Thanks again.

          Comment


            #6
            Hello Francis,

            Originally posted by Francis View Post
            So I have to make up a list first with all the 362 different items once, to which I can reffer to later using the right number from their sequence?

            Correct.

            You say it yourself it has not really use to do so, because I still would have to use 362 separate lines with all this numerical refferals instead of the original names. That way I also see no advantage.

            The advantages is that you can write a loop to set all of the variables in your array instead of manually coding it.

            But is there no solution in which you can point me to in which a standardize line of code could replace all these unique lines for example? My way seems a bit too simple and thus amateuristic. My code constists of over 20.000 lines, mostly because of all these single lines, but used for different assignments. The code is fast, but uses a lot of memory, where I think it can be much lighter.

            An Array would be a good way reduce the single lines of code, but I do not believe it would be that much more memory efficient. Is there a reason why you need 300+ IOrder objects? After an order is filled typically you want to set the IOrder to null and then you may reuse it.
            JCNinjaTrader Customer Service

            Comment


              #7
              Hello JC,

              Sorry for responding this late.

              I'm looking into speeding up the code and make it lighter. Because of all my lines of codes for which should be a proper solution possible, I was pointed on the array's. But if there propbably is less to none noticable profit by altering my current code, maybe I just leave it as it is right now, untill I fully inderstand arrays etc.

              And yes, I really use all of the 362. I have 362 different possibility for entry signals. If such a entry is filled, I keep track of it with it's specific IOrder, for the quantity of fill, if the exits has occured (partially), so alter the entry and exits prices on price changes when not filled etc. I use all of these signals at once and theoretically all can be executed (appending on the set maximum open positions). So it's quite a big code lines wise...

              Thanks again,
              Francis

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              648 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              369 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              108 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              572 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              573 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X