none
How to extract results from a list and use it with LinQ ? RRS feed

  • Question

  • I have a list from a list:

    List<double> alist = new List<double>() {xlist[y]};
                               
                          for(int a = 0; a < alist.Count; a++)
                                        {
                          alist[a] = (alist[a] + counter) / (someOtherInt - counter);

      alist[a]  is the result and return a series of number doubles like 1.23, 1.56, 1.65, etc.

    Fibo is a variable that return a number changing when new data entering same as  alist[a] . The result of Fibo can be for exemple 1.12.

    I would like to find the closest number from Fibo. 

    I came up with:

    List<double> list = new List<double>() { alist[a] };
    var minDistance = list.Min(no => Math.Abs(fibo- no));
    var closest = list.First(no => Math.Abs(fibo- no) == minDistance);
    {
    Print(closest);
    }

    Unfortunatly  it appears i am performing a search on a single element list.      

          Print(closest); return all elements in the list and not the closest.

    Its like the list is prisoner of the for loop. What ever i tried .Min or .Max, OrderBy it all return the entire list instead of a single number in this case the closest from fibo 1.23.

    Any solution?

    ty


    • Edited by Frankdot Tuesday, June 25, 2019 5:29 PM
    Tuesday, June 25, 2019 5:29 PM

All replies

  • Hi,

        The main issue is that you are using "alist[a]" as though it was a list while it is, in fact, a single element from that list. For example, in

    List<double> alist = new List<double>() { xlist[y] };

    you create alist with only one element (element y of xlist) .

    You want something like

    List<double> alist = new List<double>(xlist); for (int a = 0; a < alist.Count; a++) { alist[a] = (alist[a] + counter) / (someOtherInt - counter); }

    and

    List<double> list = new List<double>(alist);
                var minDistance = list.Min(no => Math.Abs(fibo - no));
                var closest = list.First(no => Math.Abs(fibo - no) == minDistance);
                {
                    Print(closest);
                }

    Ethan


    Ethan Strauss

    Tuesday, June 25, 2019 5:39 PM
  • Already tried that and its not working. It still see each element as one. Like one by one in the list.
     The result from xlist and alist are different.

    for (int i = 0; i <= barIndex; i++)
           {
          if(average < highPrice && average > lowPrice)
           {  
          int decrementBy = barminus;
                               List<int> xlist = new List<int>() {barIndex};
            
             for(int y = 0; y < xlist.Count; y++)
                {
             xlist[y] = (xlist[y] - decrementBy);
              {
          //total0          
                               List<double> zlist = new List<double>() {xlist[y]};
            
             for(int z = 0; z < zlist.Count; z++)
                {
             zlist[z] = (zlist[z] /= someOtherInt);
              
          //total
           List<double> alist = new List<double>() {xlist[y]};
            
              for(int a = 0; a < alist.Count; a++)
                {
             alist[a] = (alist[a] + counter) / (someOtherInt - counter);
              

    • Edited by Frankdot Tuesday, June 25, 2019 6:04 PM
    Tuesday, June 25, 2019 6:00 PM
  • I think I see what is happening. You are remaking your lists each time through the outer loop and so they never have more than one value. In this loop, xlist is created anew each time through the loop.

        for (int i = 0; i <= barIndex; i++)
                {
                    List<int> xlist = new List<int>() { barIndex };
                }

    to add a new item to it each time you need to create it outside the loop.

     List<int> xlist = new List<int>();
                for (int i = 0; i <= barIndex; i++)
                {
                    xlist.Add(barIndex );
                }

    will add barIndex each time through the loop. I suspect that you actually want to add i each time so that would be

    List<int> xlist = new List<int>(); for (int i = 0; i <= barIndex; i++) { xlist.Add(i); }

    Try making all your lists outside the loop and see what happens.

    Ethan


    Ethan Strauss

    Tuesday, June 25, 2019 6:23 PM
  • I get the idea but i am not sure how to implement the solution.

    How will i be able to enter that part of the code :

     xlist[y] = (xlist[y] - decrementBy);

    Can  xlist.Add(i); be xlist.Add(i) = ((xlist.Add(i) - decrementBy); ?

    last time it tried something like that it returned an error.

     int decrementBy = barminus;
                               List<int> xlist = new List<int>();
            
             for(int i = 0; i < xlist.Count; i++)   i or y part?
                {
             xlist.Add(i) = ((xlist.Add(i) - decrementBy); ?
               }

    I am not sure i follow you.

    Tuesday, June 25, 2019 7:21 PM
  • Here is a version that compiles and should be close to what you are looking for. The math is very weird since I don't know what is going on, but I am sure this'll run and perhaps even do what you want.

    Ethan

            public void blach()
            {
                int barIndex = 95;
                int highPrice = 7;
                int lowPrice = 2;
                int average = 4;
                int barminus = 1;
                int someOtherInt = 2;
                int counter = 1;
    
                List<int> xlist = new List<int>();
                List<double> zlist = new List<double>();
                List<double> alist = new List<double>();
                for (int i = 0; i <= barIndex; i++)
                {
                    if (average < highPrice && average > lowPrice)
                    {
                        int decrementBy = barminus;
                        xlist.Add(barIndex);
    
                        for (int y = 0; y < xlist.Count; y++)
                        {
                            xlist[y] = xlist[y] - decrementBy;//This Works. 
                            {
                                //total0          
                                zlist.Add(xlist[y]);
    
                                for (int z = 0; z < zlist.Count; z++)
                                {
                                    zlist[z] = (zlist[z] /= someOtherInt);
    
                                    //total
                                    alist.Add(xlist[y]);
    
                                    for (int a = 0; a < alist.Count; a++)
                                    {
                                        alist[a] = (alist[a] + counter) / (someOtherInt - counter);
                                    }
                                }
                            }
                        }
                    }
                }
            }


    Ethan Strauss

    Tuesday, June 25, 2019 7:32 PM
  • It's not working it crash the program. Ninjatrader charting plateform.
    Wednesday, June 26, 2019 12:22 PM
  • It's not working it crash the program. Ninjatrader charting plateform.

    How is Ninjatrader involved? Is it running the script somehow? If so, I have no clue how it works.

    Ethan


    Ethan Strauss

    Wednesday, June 26, 2019 3:29 PM
  • Ninjatrader is a charting program. They have a ninjascript window where you can enter your code.

    When you compile the result of the formulas, in my case, it comes out in the output window.

    The code you gave me compile but if i try to start the indicator the program crash. According to the log there is errors.

    I tried many solutions like removing }{ but it returns the same result. 

    What's bugging me is that the code work.

    List<double> list = new List<double>() {1.1,2,3,4};
         var minDistance = list.Min(no => Math.Abs(fibo- no));
         var closest = list.First(no => Math.Abs(fibo- no) == minDistance);
         {
         Print(closest);
         }

    But if i switch  {1.1,2,3,4}; for { alist[a] }; it stop working.

    Wednesday, June 26, 2019 3:54 PM
  • OK. A few things:

    • I don't know what Ninjatrader is doing. If you could run the code without that it would be much better for troubleshooting (for me at least)
    • The code I gave you compiles, but then Ninjatrader crashes when it is run. Is that correct? What errors does it give? If you ran my code exactly as is, I would not be surprised if there were divide by zero or other math errors. I didn't pay any attention to the numbers I was using
    • When you say that this works

    List<double> list = new List<double>() {1.1,2,3,4};
         var minDistance = list.Min(no => Math.Abs(fibo- no));
         var closest = list.First(no => Math.Abs(fibo- no) == minDistance);
         {
         Print(closest);
         }

    But { alist[a] }; does not, do you mean it crashes or that you get the wrong result?  If you get the wrong result it is almost certainly because alist[a] is a single value, not a list.

    Ethan



    Ethan Strauss

    Wednesday, June 26, 2019 4:04 PM
  • Ninjatrader produce chart from data of stock market and the ninjascript allow you to create indicators. Like sma, Bollinger bands, etc.

    In my case i created my own indicator to forecast the market. I am at 70% of the job done but i stumble on that issue. My indicator require programming formulas and than the return will be plot into the chart. 

    What you see in my code is some of the formulas. I dont think i can run the script without NT. For exemple, the index are bars included inside a portion of the chart. 

    The code compile than i start the indicator in the chart and NT is crashing. No i did adapt the code i only replaced:

    List<int> xlist = new List<int>();
               
    List<double> zlist = new List<double>();
               
    List<double> alist = new List<double>();

    and replaced :

    List<int>xlist = new List<int>() {barIndex};

    by 

    xlist.Add(barIndex);

    But i dont understand how .Add can help. It is use to add more elements to an existing list. We dont add anything here i just want the alist[a] to reflect all the results included.

    alist[a] returns more than one result: it can return 1.2, 1.5, 2.6, 4.75

    So if fibo = 1.0 the closest answer is 1.2 and only 1.2 comes out in the output window. Right now all result are coming out 1.2, 1.5, 2.6, 4.75.

    But i tried just with {1.1,2,3,4}; and only 1.1 came out, so the code work.Why cant the code pick up the specific only right result in alist is a mistery!

    About the errors its not conclusive:

    multiple bartypes with the same barsperiodtype were found: ignoring ninjascriptmirrorbars….

    i am out of ideas.

    ty

    Wednesday, June 26, 2019 4:24 PM
  • Hi again,

      The bottom line for me is that the error "multiple
    bartypes with the same barsperiodtype were found: ignoring
    ninjascriptmirrorbars
    " pretty clearly indicates a problem with that code interacting with NinjaScript. That seems to indicate that the portions of code we have been talking about are basically OK, but doing something Ninjatrader doesn't like. You are going to have to find out how Ninjatrader works.

    I would recommend that you start with a much simpler version of your code. Maybe see if you can you put in hard coded values rather than doing the calculations. Then add calculations one at a time to figure out exactly what part of your code is causing issue.

    I am mystified by your statement that

    "alist[a] returns more than one result: it can return 1.2, 1.5, 2.6, 4.75"

    if alist is a List<double> and a is an int then I don't see any way that alist[a] can be a more than one value. If it can, then I think Ninjatrader is doing something I am unaware of.

    I guess I would see if Ninjatrader has a forum that might be able to help?

    Ethan



    Ethan Strauss

    Wednesday, June 26, 2019 4:34 PM
  •  for(int a = 0; a < alist.Count; a++)
                {
             alist[a] = (alist[a] + counter) / (someOtherInt - counter);  

              Print(alist[a]);

    a is a Int because it represent the bars in the chart. Each bar return an Int, bar1, bar2, bar3, etc.

    We need to count all the bars in the index, that is the for loop part. Than to  the number of bars we add counter etc. but in the end divided all together it returns a double. xlist in the beginning returns a series of int (200, 300, 400) after that xlist get modified to alist, blist, clist etc. So its actually 200 + counter / (someOtherInt - counter); , 300 + counter / (someOtherInt - counter);  etc.

    That's the reason why we end up with multiple results in double in the lists. 

    But why we cant list the result of the alist to use Linq i dont know.

    there is a ninjatrader forum and i had this conversation with them:

    https://ninjatrader.com/support/forum/forum/ninjatrader-8/indicator-development/1060774-linq-and-nt

     What's causing the issue is your code, it wont accept      zlist.Add(xlist[y]);
    Wednesday, June 26, 2019 5:01 PM
  • Interesting solution but how do i translate the code to find the closest number from my variable fibo which contain a number ex(1.62)?

    Without Min.value or linQ .Min i wont be able to find my closest number.

    I tried that but dont know how to do next:

    List<double> lists = new List<double>() {alist[a]}; 
     IEnumerable<double> querylist =
      from list  in lists
     where list > fibo
    select list;
    foreach (int o in querylist)          
    {
    Print(o);
    }

    Friday, July 5, 2019 6:03 PM