locked
How to process values in a list by step? RRS feed

  • Question

  • Hello,

    I want to process values in a list by order in a while statement. It need to listm.Add(previous2) First and than switch to listm.Add(previous). Right now Close.GetValueAt has a value, lets say 10 but nearclose has no value. Once Close.GetValueAt has been process first nearclose will get a value. How can i do that?

    var listm = new List<double>();
    
    listm.Add(Close.GetValueAt(CurrentBar));
    listm.Add(nearclose);
    			
    while (listm.Count <= 4)
            {	
    
    // nearclose cant return a value until Close.GetValueAt
     has been process first. That is why previous2 must be 
    process before previous.
    
    var previous = listm[listm.Count-1];
    
    listm.Add(previous);
    
    
    var previous2 = listm[listm.Count-2];
    
    listm.Add(previous2);
    					
    
       	}
    

    Thank you

    Friday, November 27, 2020 2:10 PM

All replies

  • Your description does not make sense.  How do you think nearclose is going to "get a value"?  And why do you say "previous2" must be processed before "previous", when your code does exactly the opposite?

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Sunday, November 29, 2020 7:30 AM
  • Hello,

    Its part of a larger code. 

    In listm we have Close and nearclose.

    Following the actual code there is this:

    foreach (var item in listm)
    {
    fibo2 = (item - lowPrice0) / (Ncma - lowPrice0);
            }

    The last value in the listm is process in the rest of the code to generate a value for nearclose. If i put in listm nearclose at the end it will return 0 and the While loop will send back 10,0,0,0,0.

    Thats why i need to process Close first. This way the Foreach will process Close and produce a value for nearclose. Than nearclose will return a value, for example 5. The result will show from the whileloop 10, 5, 7, 8,2. nearclose need to be process in the foreach after Close has been process first.

    I thought with Switch, case, Goto something could be done to revolve this. But i am not sure how.

    Thank you

    Monday, November 30, 2020 12:45 PM
  • Greetings Frankdot.

    I really don't understand what you are trying to do, but there appears to be one obvious mistake. When you call listm.Add, the value of listm.Count will go up. This means previous and previous2 will be the same value.

    // Suppose Count = 2 here.
    var previous = listm[listm.Count-1]; // Count - 1 = 1
    
    // This will increase Count to 3.
    listm.Add(previous);
    
    
    var previous2 = listm[listm.Count-2]; // Count - 2 = 1, so we get the same value as before.
    
    listm.Add(previous2);

    You might be trying to do something like this.

    var previous = listm[listm.Count-1];
    
    var previous2 = listm[listm.Count-2];
    
    
    listm.Add(previous);
    listm.Add(previous2);

    Monday, November 30, 2020 10:28 PM
  • Hello Ante,

    The idea is to give an order in the list. 

    In fibo2 = (item - lowPrice0) / (Ncma - lowPrice0);, item is the last value in listm, that means nearclose for the moment. 

    But if i replace item by nearclose, nearclose will return 0. Meaning that having nearclose at the end of the list will return 0. What i want is nearclose to return the proper value so that the while loop reuse nearclose in the formula to return a list of values. 

    To do that Close must be process first. Than nearclose will have a value to be calculated. The new value of nearclose is added at the end of the list and will be process in fibo2.

    Tuesday, December 1, 2020 12:50 PM
  • Hi Frankdot,
    Based on your code and description, you keep adding the nearclose value to listm in your while statement.
    So what is the correct value you want Nearclose to return
    Ante Meridian has provided a detailed explanation and solution, have you tried it?
    And what problem did you encounter?
    Best Regards,
    Daniel Zhang


    "Visual c#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post.

    Wednesday, December 2, 2020 7:45 AM
  • Hello Daniel,

    Hello Ante,

    Lets put aside the While loop.

    We have 2 variables Close and nearclose. Imagine there is only Close in listm. 

    foreach (var item in listm)
           {
    fibo2 = (item - lowPrice0) / (Ncma - lowPrice0);
            }

    fibo2 is the entry point of the calculation of my algorithm. item takes the value of Close in the listm. fibo2 is processed in the rest of the algorithm and return a value for nearclose. We could simply replace item by Close to get a value for nearclose.

    The While loop is there to repeat the calculation of the algorithm. If i replace item by nearclose, nearclose will return 0. 

    Once we get the first value of nearclose i want to send back the value in fibo2 to get a second different value.

    So if Close = 5, nearclose return 8, than nearclose (8) takes the place of item once more and return another value in the listm and so on.

    The problem with the While loop as you noticed is that :

    var previous2 = listm[listm.Count-2];

    will return the first value Close. (depending which one come first)

    But that is irrelevant for now because first we must find a way to process Close and than take nearclose value and return it in item.

    Close must enter in fibo2 first and than once you get nearclose value, you switch it for nearclose and use while loop to get a certain numbers of values.

    I tried using Select  var fibo2list = listm.Select( x => (x - lowPrice0) / (Ncma - lowPrice0) ). ToList(); the result produce  2 values of nearclose but only one is showing up in the listm of course the one that return 0,   Thanks for the help.



    • Edited by Frankdot Wednesday, December 2, 2020 7:11 PM
    Wednesday, December 2, 2020 1:25 PM
  • Is it something like this that you're after?

    float item = listm(0); // Assuming there is one value in listm to start with.
    someNumber = 4;
    
    while(listm.Count < someNumber)
    {
       fibo2 = (item - lowPrice()/(Ncma - lowPrice());
       listm.Add(fibo2);
       item = fibo2;
    }

    Wednesday, December 2, 2020 11:01 PM