Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

replace item in a List

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

    replace item in a List

    I would like to conditionally replace the value of an item in a list.

    How can I achieve that task ?

    For example :

    public Value ( int barnumber, double price)
    { barnumbe r= BarCount;
    price = Price; }

    private List<Value> xx = new List<Value>();

    then on the OnBarUpdate() method , I add values to my xx until it contain n values (barnumber,price).

    now I want to replace "price" on the i-th row of my list when Condition1 is met.
    How do I do that ?

    #2
    see this to get an idea.

    Comment


      #3
      Hello,

      Thanks for your forum post.

      Please see the following link specifically Inserting Elements sections.



      Let me know if I can be of further assistance.
      BrettNinjaTrader Product Management

      Comment


        #4
        Thank you Brett for your reply.

        If I understand well,I shoud "RemoveAt(i)" first and then use "xx.Insert(i,barcount,newprice)" (not sure of the Insert part since my List contains 2 elements on each row,could you confirm ?).

        By the way,I was wondering if there is a more proper way to do it ?

        Comment


          #5
          Hello,

          This is how I would do it.

          Let me know if I can be of further assistance.
          BrettNinjaTrader Product Management

          Comment


            #6
            It does not work since insert method only take 2 arguments (index,item).

            I cannot write xx.Insert(i,barnumber,newprice).

            Any help would be highly appreciated

            Comment


              #7
              baba123,

              If it only takes 2 parameters, then you will need to use a signature with only 2 parameters. This is a more general C# programming concepts and I would highly recommend trying to search google for some examples of how to achieve what you want with either Lists or ArrayLists.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                I just figure it out. It works fine exept in the log file I get the following error message :

                Error on calling the OnBarUpdate on bar 502(lastbar) : You are accessing an index with a value that is invalid since its out of range

                I printed the first calculation I made ie :

                if (CurrentBar < period)
                return;
                else
                {

                Cvalue.Set(SMA(Close,period)[0]);
                Print(Cvalue[0]);
                ....

                Is there something wrong with the SMA or am I doing something wrong ?

                that is when I get the error message
                Last edited by baba123; 12-06-2010, 01:12 PM.

                Comment


                  #9
                  Hello,

                  I believe you want this:

                  if (CurrentBar > period)
                  BrettNinjaTrader Product Management

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  571 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  330 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  101 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  548 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  549 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X