none
Cant break loop because of bolean? RRS feed

  • Question

  • Hello,

    I try to break a loop when foundIndex = barIndex but i get an error message saying cant covert int into bool.

    Any idea what i am doing wrong?

    Thank you


    • Edited by Frankdot Tuesday, July 23, 2019 7:33 PM
    Tuesday, July 23, 2019 1:08 PM

Answers

All replies

  • Is the method a void or does it return a value? I'm asking because you did not post the entire method and the code posted appears error-free.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 23, 2019 1:17 PM
    Moderator
  • foundIndex return a number of bars, actually when foundIndex = barIndex the condition return true, so the loop should break.



    • Edited by Frankdot Tuesday, July 23, 2019 7:34 PM
    Tuesday, July 23, 2019 1:33 PM
  • You're using assignment, not equality

    `foundIndex = barIndex` assigns the value of `barIndex` to `foundIndex` and because this is such a difficult thing to see in C++ (and yet it still works) C# treats it as an error. Try equality instead.

    if (foundIndex == barIndex)

    However I would argue that you don't need the if statement. The conditional check in the for loop can handle this and that is exactly what it is there for.

    //While barIndex is less than ChartBars.ToIndex AND not foundIndex
    for ( int barIndex = i; barIndex <= ChartBars.ToIndex && foundIndex != barIndex; barIndex++)


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 23, 2019 1:57 PM
    Moderator
  • I thought of that so i tried :


         

    But its not breaking at all. Imagine it like a snake eating its own tail. By replacing Period by i in barIndex = i i am testing every period from 1 to all bars in the chart. When the number of period (i) = the same number of period in foundIndex it stops.

    The code is longer but the number of period tested as an impact on foundIndex. Right now the break is not stopping the increment done and the cma return the value of all the bars in the chart when it should stop when foundIndex == barIndex.

    The Period represented by i increase 1, 2, 3 etc till it reach the last bar. foundIndex stop before the end because other conditions such as volume trigger the break.  if (sumOfVolumes4 >= sumvolfibo)

    But for foundIndex = barIndex it wont, i am puzzle.



    • Edited by Frankdot Tuesday, July 23, 2019 7:34 PM
    Tuesday, July 23, 2019 2:11 PM
  • But the fix (minus the == true) resolves your compiler error so that is a start.

    Put a breakpoint on that if statement. barIndex is the only value changing and it is going from i to whatever the highest value is. I suspect your `foundIndex` is outside that range. Since it is `barIndex` will never be equal to it.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 23, 2019 2:15 PM
    Moderator
  • I agree with CoolDadTx, set a break-point or the alternate is (and know the breakpoint method is better but some developer never care to learn how to debug)

     for(int barIndex = i; barIndex <= ChartBars.ToIndex; barIndex++)
        {
          {
    		 Console.WriteLine($"{foundIndex} - {barIndex}";
             if ((foundIndex == barIndex))
         
              break;
          }
    Now you can inspect values in the IDE Output window.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 23, 2019 2:20 PM
    Moderator
  • If i print foundIndex and barIndex, the first return what it is suppose to return 583 but for barIndex there is no break it return from 1 to 1575 which is the number of total bars without breaking when it cross 583.

    Tuesday, July 23, 2019 2:30 PM
  • It's not going to mess up an equality. Therefore either this code isn't executing, the code you posted doesn't line up with what code is actually executing or you have code further down that is mucking with either foundIndex or barIndex and you didn't post that.

    This is guaranteed to behave correctly, without exception provided foundIndex is between i and the upper bounds of your check AND barIndex is only being incremented once each time through the loop. 

    for (int i = 0; i <= totalBarIndex; i++)
    {    
        for (int barIndex = i; barIndex <= ChartBars.ToIndex; barIndex++)
        {
           if (foundIndex == barIndex)    
              break;
    
           double closePrice = Bars.GetClose(barIndex);
           double volumes = Bars.GetVolume(barIndex); 
         
           double clovol = closePrice * volumes;      
         
           sum1 += clovol;
           sum2 += volumes;
       };
    };
        

    To help narrow this down please use a breakpoint. Set a breakpoint on the if statement. Does it hit it? F5 to continue running. Does it hit it next time through the loop and has the value been incremented by 1? If so then the loop is behaving properly (likely) but we can check for the very specific case you mention - conditional breakpoints.

    On the breakpoint you have set on that line right click the breakpoint and select Conditions. In the Conditional Expression section specify `barIndex >= foundIndex`. Now run the code. The breakpoint will get hit when this condition is true. Now evaluate both values. This is the first time in the loop that condition is being met. At this point you should be able to see if the values are out of sync. Furthermore you can now step (F10 or F11) to step through your logic to verify it is executing exactly the way you want.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 23, 2019 2:46 PM
    Moderator
  • for(int barIndex = i; barIndex <= ChartBars.ToIndex && barIndex < foundIndex; barIndex++)

    will return 0 to foundIndex but its running in cercle and mess up the calculation. it keep printing 1,2, till 583 and than start again at 0, 1 , 2  etc

    but if (foundIndex == barIndex)   
             
    break;

    wont work.

    Tuesday, July 23, 2019 3:32 PM
  • The for loop behaves  correctly for me as does the if statement. Note that in your example code you expect it to run again. Outside of this for loop you posted is another for loop `for (int i = 0; i <= totalBarIndex; i++)` 

    So your inner for loop will execute once for each of the totalBarIndex values. Because that is where `i` comes from each time through the loop your `barIndex` will be slightly larger. Let's approach this using consistent values so we can frame the conversation.

    static void Main ( string[] args )
    {
        var totalBarIndex = 10;
    
        var toIndex = 10;
        var foundIndex = 5;
    
        for (int i = 0; i <= totalBarIndex; i++)
            for (int barIndex = i; barIndex <= toIndex && barIndex < foundIndex; barIndex++)
            {
                Console.WriteLine($"{i} - {barIndex}");
            };
    }

    Note: Indice are zero-based so I suspect your outer loop should be using < and not <=.

    If you run this code the inner loop runs 5 + 4 + 3 + 2 + 1 = 15 times. You'll get this output.

    0 - 0
    0 - 1
    0 - 2
    0 - 3
    0 - 4
    1 - 1
    1 - 2
    1 - 3 
    1 - 4
    2 - 2
    2 - 3
    2 - 4
    3 - 3
    3 - 4
    4 - 4
    
    Since the inner loop is not relying on any of the outer loop values other than where to start you are basically repeating the same loop logic over and over again against the same "bars". 



    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 23, 2019 3:51 PM
    Moderator
  • I decided to replace i by foundIndex 

    for(int barIndex = foundIndex; barIndex <= ChartBars.ToIndex; barIndex++)

    it looks like its doing the job all  my numbers are in  line. one problem solve thank you

    I got another question, i try to find the lowest low with a for loop. If i try this solution to get the highest high it work but if i switch for low it fails. Any idea why? With Bars.GetHigh it return the highest high on the chart.

    for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
    {
    if (Bars.GetLow(barIndex) < lowPrice)
    {
    lowPrice = Bars.GetLow(barIndex);
    index = barIndex;
    Print(index);
    }
    }

    Tuesday, July 23, 2019 4:41 PM
  • Personally I'd use LINQ but I suspect you made the classic init mistake for calculating low.

    var lowPrice = 0;
    
    for (...)
    {
       if (< lowPrice)
    }
    Because `lowPrice` is initialized to a low value it is never higher than your actual lowest value. Initialize `lowPrice` to a very large value like `Int32.MaxValue`.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 23, 2019 4:56 PM
    Moderator
  • Thanks for the Int32.MaxValue you were right. Its working.

    • Marked as answer by Frankdot Tuesday, July 23, 2019 5:21 PM
    Tuesday, July 23, 2019 5:21 PM
  • Thanks for the Int32.MaxValue you were right. Its working.

    Hello,

    I would recommend to also mark CoolDadTx as an answer also as this is where you solution came from.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 23, 2019 8:06 PM
    Moderator