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

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.

      Create a new List, add elements to it, and loop over its elements with for and foreach.


      Let me know if I can be of further assistance.

      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.

          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)

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by inanazsocial, Today, 01:15 AM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_Jason  
                  Started by rocketman7, Today, 02:12 AM
                  0 responses
                  7 views
                  0 likes
                  Last Post rocketman7  
                  Started by dustydbayer, Today, 01:59 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post dustydbayer  
                  Started by trilliantrader, 04-18-2024, 08:16 AM
                  5 responses
                  23 views
                  0 likes
                  Last Post trilliantrader  
                  Started by Davidtowleii, Today, 12:15 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post Davidtowleii  
                  Working...
                  X