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

LinQ and NT

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

    LinQ and NT

    Hello,

    I found some code that is suppose to work with LinQ and i modified it. It suppose to find the closest value in a list.
    fibo = 1.23 and zlist has a bunch of numbers in it. I want to find the closest number to 1.23 in the zlist.


    List<double> list = new List<double>() {zlist[z]};
    double number = fibo;

    double closest = list.Aggregate((x,w) => Math.Abs(x-number) < Math.Abs(w-number) ? x : w);

    Print(closest);

    Its not returning the closest number, it just returning the entire list. Does it have to do with LinQ?

    #2
    Hello frankduc, thanks for your note.

    This seems to be a problem with the initial list as this test works for me:

    Code:
    List<double> list = new List<double>() {1,2,3,4,5};
    double number = 2;
    double closest = list.Aggregate((x,w) => Math.Abs(x-number) < Math.Abs(w-number) ? x : w);
    Print(closest);
    Helping with Linq statement or any code debugging would be outside of the scope of support I could provide, but it does seem you are not performing this Linq statement on a correct data structure. Other members of the forum community might be willing to help out with this, so this post will remain open for discussion.

    Kind regards.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      I'm trying several solution but everytime NT comes with an error message.
      It looks like it doesn't accept
      OrderBy, First., MinBy
      varordered =aList.OrderBy(x =>Math.Abs(x -fibo));
      varclosest =ordered.First();
      varclosest =list.MinBy(n =>Math.Abs(fibo-n));

      Any reason why? it looks like NT does not accept LinQ library.

      Only

      if (Math.Abs(fibo - clist[c]) < 0.03) comes with a close answer.
      Last edited by frankduc; 06-18-2019, 05:49 AM.

      Comment


        #4
        Hi frankduc, thanks for your reply.

        If you could post a reduced version of the script I would be happy to take a look on my PC. Please make sure you include only the code needed to recreate the problem.

        I look forward to hearing from you.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hello Chris,

          From the start there's 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);


          The alist[a] return all the results from the formula. I try to find the closest value from fibo = 1.63. see in attachement
          I ask to stackoverflow:

          They came with this:

          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);

          they also came with:
          varordered =list.OrderBy(x =>Math.Abs(x -fibo));

          var closest =ordered.First();




          As you can see in the attachement its only returning all the values from alist. But data pour in an order from smallest to greatest number. Is it like a ++?
          I mean should the last number be the closest to fibo or only 1,6734693877551 should show up in the output window?

          This is more a c# question sorry. But i am wondering, once i get the closest value from fibo will it show up alone, on top or at the bottom?

          The constant flow of data in the OW is fooling me. Also do i need to list the list?

          ty
          Attached Files
          Last edited by frankduc; 06-18-2019, 12:07 PM.

          Comment


            #6
            Hi frankduc, thanks for your reply.

            The Linq statement will only return one value. If your script runs oneachtick or onprice change, that would cause the resultant value to be printed many times in one bar (or this is the calculation from all the historical bar on the chart) Remember that OnBarUpdate will run for each historical bar on the chart. It seems like your linq statement is working fine from what I can see.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              As you can see in the file it return all the results and not just the closest from fibo. I dont get it. I dont understand either why its returning every tick instead of every bar. I dont know what's wrong in the script. Its not
              OnBarUpdate its OnRender. Does Onrender is suppose to return on tick?

              Seriously what's wrong with this:


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

              for(int a = 0; a < alist.Count; a++)
              {
              alist[a] = (alist[a] + counter) / (someOtherInt - counter);
              {
              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);
              }
              }
              Last edited by frankduc; 06-19-2019, 06:32 AM.

              Comment


                #8
                Hello frankduc, Thanks for your reply.

                OnRender is not locked to the calculation mode. That method will run many times throughout the lifetime of the script. Adding a print to the OnRender method and moving around the chart bars would prove this. I am unable to debug any such code, but I see things that stick out like adding only one element of a list to another list, this code you have will make a new list with only one number in it:

                List<double> list = new List<double>() { alist[a] };

                So it appears you are performing a search on a single element list.

                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Its easy when
                  List<double> list = new List<double>() {1,2,3,4 etc};

                  But when its List<double> list = new List<double>() { alist[a] };

                  Obviously you have to single out each number in the list to analyze them one by one to see if they fit the condition of being = to fibo.
                  Probably something close to that:


                  List<double> list = new List<double>() {alist[a]};

                  for(int t = 0; t < list.Count; t++)
                  {
                  var minDistance = list.Min(no => Math.Abs(fibo- no));
                  var closest = list.First(no => Math.Abs(fibo- no) == minDistance);

                  {
                  Print(closest);
                  }
                  }

                  Thanks anyway
                  Last edited by frankduc; 06-19-2019, 11:04 AM.

                  Comment


                    #10
                    Its been 3 weeks i waste my time researching this and there must something between LinQ and NT that is going wrong. Anyone has tried use LinQ with Nt?

                    How come this code return the entire list and not the Max number:

                    for(int barIndex = ChartBars.GetBarIdxByX(chartControl, cursorPointX); barIndex <= ChartBars.ToIndex; barIndex++)
                    {


                    double highPrice = Bars.GetHigh(barIndex);
                    List<double> QQlist = new List<double>() {highPrice};

                    double max = QQlist.Max();

                    Print(max);

                    }


                    But this will return the right answer which is 8:
                    for(int barIndex = ChartBars.GetBarIdxByX(chartControl, cursorPointX); barIndex <= ChartBars.ToIndex; barIndex++)
                    {


                    double highPrice = Bars.GetHigh(barIndex);
                    List<double> QQlist = new List<double>() {1.4,2.5,3.6,8};
                    double max = QQlist.Max();
                    Print(max);
                    }


                    I dont get it! Nowhere it says IEnumerable Max method cant return dynamic feed from variables.

                    Comment


                      #11
                      Hi frankduc, thanks for your follow up.

                      This post on stack overflow provides some solutions:
                      I have List List&lt;MyType&gt;, my type contains Age and RandomID Now I want to find the maximum age from this list. What is the simplest and most efficient way?


                      I'm not sure why that is not working for you. When I use it this works:

                      List<double> QQlist = new List<double>() {8};
                      Print(QQlist.Max());

                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Of course the second exemple works, it will return 8 if you input manually all numbers in the collection.
                        List<double> QQlist = new List<double>() {1.4,2.5,3.6,8};

                        But it wont work if you replace by
                        {highPrice};

                        List<double> QQlist = new List<double>() {highPrice};

                        double max = QQlist.Max();

                        You tried the wrong one.
                        I dont know why it does that. I want to know if its NT or i am using the wrong classes system collection generic? I doubt it i tried everyting even Query.

                        Comment


                          #13
                          Hi frankduc, thanks for your reply.

                          Why not use HighestBar()? I'm not quite clear on what it is you need to be done in the code. I have mentioned before that this line is just creating a new list with only 1 element:

                          List<double> QQlist = new List<double>() {highPrice};

                          So technically, if you call Max on it, you will get the entire list (one number)

                          Chris L.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Chris,

                            The entire list equal one number? Even if there is a list of highs for each bars? So even if i get 2025.5, 2025.75, 2026.25 from the list and they all represent the high of each bars the List sees it as one number? Cause the list return more than one number it can return 60 like 200 depending the number of bars.
                            I need to be able to apply linQ to my calculation.

                            How do i made the List to see all the numbers inside? Like if it was
                            {
                            2025.5, 2025.75, 2026.25
                            };

                            This is what i try to do for a while. Get the closest number from fibo.

                            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);
                            }

                            I'm still stuck on that. But why the list represent one number when the output window show a hundred? What method or group method will allow me to test every number in the list if its equal or close to fibo?

                            TY

                            Comment


                              #15
                              Hi frankduc, thanks for your reply.

                              I made a script that does something similar, it is a little more in the context of NinjaScript.

                              I also found this post on stack overflow: https://stackoverflow.com/questions/...ue-in-an-array

                              It looks like the second solution would work for you (I could not find any dll to reference for MoreLinq, it seems that library has a "MinBy" action)

                              It also looks like a user wrote their own method to do this:

                              int[] array = new int[5]{5,7,8,15,20}; int TargetNumber = 13; For a target number, I want to find the closest number in an array. For example, when the target number is 13, the closest number to ...
                              Attached Files
                              Chris L.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by cmtjoancolmenero, Yesterday, 03:58 PM
                              8 responses
                              31 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by helpwanted, Today, 03:06 AM
                              2 responses
                              21 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by DayTradingDEMON, Today, 09:28 AM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by navyguy06, Today, 09:28 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post navyguy06  
                              Started by rjbtrade1, 11-30-2023, 04:38 PM
                              2 responses
                              77 views
                              0 likes
                              Last Post DavidHP
                              by DavidHP
                               
                              Working...
                              X