question

FranKDuc-4126 avatar image
0 Votes"
FranKDuc-4126 asked DanielZhang-MSFT commented

How to pass in a foreach the result from 2 different list?

Hello,

I am having a foreach statement:

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

Inside my list i have the current trading price:

 var listm = new List<double>();
 listm.Add(Close.GetValueAt(CurrentBar));


Later in the code i have a variable call nearclose. That variable return the equivalent of a new Close price.

Than i have another list:


 var listN = new List<double>();
 listN.Add(nearclose));


I want to be able to pass the result of listN into the foreach, replace listm by listN. But nearclose wont return a value until listm has been process first.

Than nearclose will return a second value that will return in listN to be pass into the foreach again, and a third value, and a fourth...

It should return in listm: 10
than listN: 10, 11, 12 ...

Is there a way to do that?

Thank you

dotnet-csharp
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Hi @FranKDuc-4126,
>>I want to be able to pass the result of listN into the foreach, replace listm by listN. But nearclose wont return a value until listm has been process first.
Could you explain in detail?
>>Than nearclose will return a second value that will return in listN to be pass into the foreach again, and a third value, and a fourth...it should return in listm: 10than listN: 10, 11, 12 ...
Please provide more code to reproduce this situation.
Best Regards,
Daniel Zhang


0 Votes 0 ·

Daniel,

In listm you have Close.GetValueAt(CurrentBar), lets say 10$.

The foreach take the item in the listm (10$) and insert it in the equation.

fibo2 = (10 - 6 )/ (8 - 6)

fibo2 = 2

fibo2 is process in the rest of the code which is too long to be copy/paste here.

At the end of the code nearclose return a number, lets say 9$.

9$ end up in listN, my new list. (if only it could return in listm but it will cause a false answer)

I want listN to pass in my foreach:

foreach (var item in listN)

But i cant do that until listm has been process first.
After that nearclose will return a new price, lets say 11$

I want to pass in the fibo2 equation multiple times the result of nearclose. Everytime you get a new nearclose you send back the value in the fibo2 equation to get a new result of nearlcose.

I am wondering which method could help me to achieve that.

Thank you

0 Votes 0 ·

For example, i tried using concat.

foreach(item in listf.Concat(listm)) {

}

Even if both list iterate throught the foreach simultaneously, listm will always need to be on top to be sure nearclose return a value. Because without close (listm) there cant be no value of nearclose.

0 Votes 0 ·

Hi @FranKDuc-4126,
According to my understanding, you can get nearclose in the first foreach and add it to listN. Then use Enumerable.Zip method in another foreach to get the sum of the two lists

 var listmAndlistN = listm.Zip(listN, (m, n) => new { item1 = m, item2= n });
 foreach (var mn in listmAndlistN)
 {
    // Console.WriteLine(mn.item1 + mn.item2);
    list.Add(mn.item1 + mn.item2);
 }

Best Regards,
Daniel Zhang


0 Votes 0 ·

Unfortunately, it returns the same as using Concat.

Using zip or concat its like having simultaneously Close price 10$ and nearclose iterating in the foreach:

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

   }

Once the first close price 10$ has been process fibo2 = (10 - 6 )/ (8 - 6) than nearclose return 9.5$ and replace the 10$ in the equation to get the next value of nearclose.

fibo2 = (9.5 - 6 )/ (8 - 6)

Then if nearclose value is 10.5$ its coming back in the fibo2 equation to get the next value of nearclose and so on. I can punch myself 9.5 in the equation and it will return the next value, but thats not how it suppose to be.

Problem, once i get the first value of nearclose 9.5$ its not returning the next value of nearclose. I am just ending up with 10 and 9.5 in the output. Why does it stop after the first iteration?

Frank

0 Votes 0 ·

Hi @FranKDuc-4126,
You need to use For instead of Foreach.
You can add a new nearclose value into the listm, and then get the nearclose value just added by the index of the collection.
Code looks like below:

 static void Main(string[] args)
 {
     var listm = new List<double>();
     listm.Add(10);
     var listN = new List<double>();
    
     for (int i=0;i<10; i++)
     {
         fibo2 = (listm[i] - lowPrice0) / (Ncma - lowPrice0);//Get the newly acquired nearclose by index
         double nearclose = fibo2 * 4.75;//In order to be equal to 9.5 you said.
         listm.Add(nearclose);
     }          
 }

Best Regards,
Daniel Zhang

0 Votes 0 ·

But is it possible to pass the two elements inside the equation:

fibo2 = (mn.item1 and mn.item2- lowPrice0) / (Ncma - lowPrice0);

at the same time?

i cant create 2 fibo equation with each item, they need to be process in the same formula.

ty

0 Votes 0 ·

Hi @FranKDuc-4126,
>>mn.item1 and mn.item2- lowPrice0
It is impossible to pass two values into an equation at the same time to calculate separately, you can only pass in two values separately and perform calculations.
Best Regards,
Daniel Zhang

0 Votes 0 ·

1 Answer

FranKDuc-4126 avatar image
0 Votes"
FranKDuc-4126 answered DanielZhang-MSFT commented

I get an error message: NinjaScript File Error Code Line Column
NEWSDBA.cs Operator '-' cannot be applied to operands of type 'void' and 'double' CS0019 159 14

 var listm = new List<double>();
             listm.Add(Close.GetValueAt(CurrentBar));
                
             var listf = new List<double>();
             listf.Add(nearclose);
                
                
              var list = new List<double>();
    
     var listmAndlistf = listm.Zip(listf, (m, n) => new { item1 = m, item2= n });
                      foreach (var mn in listmAndlistf)
                      {
                    
 fibo2 = (list.Add(mn.item1 + mn.item1) - lowPrice0) / (Ncma - lowPrice0);
                    
                 Print("num"+mn);
                 }
                    
                    
                    
 I thought it could be possible by using a combination of foreach and while loop:

while (listm.Count <= 4)
{ }

Cant figure out how to solve this one.

I just want to iterate both of my list seperatly throught the foreach. Than reiterate once more the result of nearclose into fibo2 multiple times.

The 9.5 and 10 are just examples, its the result of the variables that need to be fix.

What you dont understand is that i cant iterate listN in the foreach until listm has iterated first, otherwise nearclose get no result and listN is empty.

Than once nearclose has a result, listN has too and can be iterated thought the foreach to produce a new result.
Than you can use a for loop or while loop to reproduce a chain of iteration for listN and produce multiple results of nearclose.

ty


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Again i am not explaining myself correctly.

Is it possible to iterate two lists in the same foreach separately? zip or concat merge the list or give the priority to the first list.

What i want is the two lists process in my code of formulas each one, alone independant. I thought with parallel.foreach it could be possible but no.

0 Votes 0 ·

Hi @FranKDuc-4126,
>>Is it possible to iterate two lists in the same foreach separately? zip or concat merge the list or give the priority to the first list.
You can use Enumerable.Zip method that I said above.
And in my code, I just add the elements in the two sets, you can also get them separately(mn.item1, mn.item2).
Best Regards,
Daniel Zhang

0 Votes 0 ·