locked
How to merge values once for loop has been indexed? RRS feed

  • Question

  • Hello,

    I indexed a for loop retuning multiple values. I want to create one list of the overall values but indexing of for loop prevent to do so.

    Any help?

     double fibos = 0;
    				
    for(int barIndex = ChartBars.GetBarIdxByX(chartControl, cursorPointX); barIndex <= blueIndex; barIndex++)
    	{
    					
    double closePrice = Bars.GetClose(barIndex);
    double open = Bars.GetOpen(barIndex);
    					
    double co = closePrice - open;
    			
    int bco = 0;
    					
    if (co < 0)
    	{
    	bco = barIndex;
    					
    					
    					
    double sum21 = 0;
    double sum22 = 0;
    double cmaMu = 0;
    	
    //for loop is indexed to bco to create multiple bars and multiple cmaMu					
    					
    for(int barIndex0 = bco; barIndex0 <= ChartBars.ToIndex; barIndex0++)
    	{
    					
    double closePrice0 = Bars.GetClose(barIndex0);
    double volumes = Bars.GetVolume(barIndex0);	
    					
    double clovol = closePrice0 * volumes;	
    					
    					
    	sum21 += clovol;
    	sum22 += volumes;
    				
    cmaMu = sum21 / sum22;
    
    					
    	}		
    					
    					
    				
    //once you got the fibo values i search for highest value in the list of fibos.
    				
    				
    fibos = ((Close.GetValueAt(CurrentBar) - lowPrice)) / (cmaMu - lowPrice);
    				
    				
    
    											
    									Print(fibos);		
    				
    				
    double highPrice = Int32.MinValue;
    				
    List<double> uplist = new List<double>() {fibos};
    					
    double highFound = highPrice;
    
    foreach (double i in uplist)
    
              {
    				            
     if (highFound < i)
    	  {
    				
     highFound = i;
    				  
    Print("HF"+highFound);
                }	
    								
    	}		
    										
    								
    }
    					
    }
    Here's what it returns. Gives the highest value for each return when i want the highest for all of them. Answer LF should return one time 1.3569
    
    1,35691166545776
    LF1,35691166545776
    1,35640617698412
    LF1,35640617698412
    1,35600885360173
    LF1,35600885360173
    1,35493753331619
    LF1,35493753331619
    1,35449137356122
    LF1,35449137356122
    1,35374867728372
    LF1,35374867728372
    1,35202512085795
    LF1,35202512085795
    1,35140258312983
    LF1,35140258312983
    1,34999264039604
    LF1,34999264039604
    1,34944907240576
    LF1,34944907240576
    
    Thank you
    				

    Monday, September 14, 2020 5:24 PM

Answers

  • Thank you for your reply but the issu has been resolve.

    If you resolve the issue then please mark the reply that solve this as an answer so others will know what resolved this.

    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

    • Marked as answer by Frankdot Friday, September 18, 2020 11:40 AM
    Thursday, September 17, 2020 9:13 PM

All replies

  • Hello,

    The first thing you need to do is clean up the code block posted as currently its so unformatted it's difficult to follow.


    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

    Monday, September 14, 2020 6:23 PM
  • I dont know what you expect. Any example to copy?
    Monday, September 14, 2020 6:27 PM
  • I dont know what you expect. Any example to copy?

    Just write the code in Visual Studio or Visual Studio Code and turn on SMART or BLOCK Formatting in the preferences.  Then copy and paste once the IDE has automatically indented appropriately.

    The code you posted seriously looks like you just made it up in Notepad and aren't aware that indentation is part of readability standards in every programming/scripting language, and actually part of functional/compiler standards in several.


    Before you can learn anything new you have to learn that there's stuff you don't know.

    Monday, September 14, 2020 6:36 PM
  • I dont have Visual studio i use insert code bloc

    sorry

    Monday, September 14, 2020 6:45 PM
  • I dont have Visual studio i use insert code bloc

    sorry

    "Insert Code Block" is part of the MSDN Website interface, so what you just said makes about as much sense as shutting down every small business in the USA for months on end in response to a new strain of the common cold.

    What IDE or compiler are you using in the first place?  If none, how or why do you think there's a problem to post on MSDN about in the first place?


    Before you can learn anything new you have to learn that there's stuff you don't know.

    Monday, September 14, 2020 7:09 PM
  • Its ninjatrader charting software script.

    I think my question was clear. LF returns highest price per value and  what i want, it to return highest price for the entire list of values.

    Ty

    Monday, September 14, 2020 7:17 PM
  • Hi Frankdot,
    >>Answer LF should return one time 1.3569
    Based on your code, what is "LF"? 
    And do you want to keep four decimal places?
    If so, you can use Math.Round method.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 16, 2020 7:11 AM
  • Hi Daniel,

    Lets take a smaller approach. Its not about decimals.

    for(int barIndex0 = bco; barIndex0 <= ChartBars.ToIndex; barIndex0++)
    	{
    					
    double closePrice0 = Bars.GetClose(barIndex0);
    	
    	}	
    
    // imagine bco = 1,2,3 the loop will test 1 than 2 than 3 till ChartBars.ToIndex. So if ChartBars.ToIndex = 10
    it will return 1,2,3,4,5,6,7,8,9,10 than 2,3,4,5,6,7,8,9,10 etc. 
    // it will return 3 series of price for example:
     10,12,15
    11, 16, 17
    11, 17, 19
    
    //now if i try to get highest price it will return 3 values: 
    // 15 
    // 17
    // 19
    
    // all i want is to return 19, the highest of the 3 series of values.
    
    

    Thank you

    Wednesday, September 16, 2020 12:09 PM
  • Its ninjatrader charting software script.

    I think my question was clear. LF returns highest price per value and  what i want, it to return highest price for the entire list of values.

    Ty

    Since this is third party use their forums

    https://ninjatrader.com/support/forum/forum


    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

    Wednesday, September 16, 2020 12:12 PM
  • No they wont provide help because its c# related.

    BarsGetClose or ChartBarsToIndex are proprietary method from the software but its not the issu. Its all C# if you dont know the answer dont blame the software. 

    I thought by using list or array, aggreagate i could solve this but there is some method that should allow me to find the answer and its using c# method.

    Indexing a for loop lead to multiple level of values and there must be a method to gather all values togheter.

    Ty

    Wednesday, September 16, 2020 12:59 PM
  • No they wont provide help because its c# related.

    BarsGetClose or ChartBarsToIndex are proprietary method from the software but its not the issu. Its all C# if you dont know the answer dont blame the software. 

    I thought by using list or array, aggreagate i could solve this but there is some method that should allow me to find the answer and its using c# method.

    Indexing a for loop lead to multiple level of values and there must be a method to gather all values togheter.

    Ty

    Well you should had mentioned that you tried their forums.

    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

    Wednesday, September 16, 2020 1:05 PM
  • // imagine bco = 1,2,3 the loop will test 1 than 2 than 3 till ChartBars.ToIndex. So if ChartBars.ToIndex = 10
    it will return 1,2,3,4,5,6,7,8,9,10 than 2,3,4,5,6,7,8,9,10 etc. 
    // it will return 3 series of price for example:
     10,12,15
    11, 16, 17
    11, 17, 19
    
    //now if i try to get highest price it will return 3 values: 
    // 15 
    // 17
    // 19
    
    // all i want is to return 19, the highest of the 3 series of values.
    


    Since you already have code that gets the highest value in each set, store
    those highest values as you get them into a List. Then use the Max method
    to get the highest value from that list.

    e.g. -

    List<double> ld = new List<double>();
    ld.Add(15);            
    ld.Add(19);
    ld.Add(17);
    
    Console.WriteLine("Highest value in list = {0}", ld.Max());
    

    - Wayne

    Wednesday, September 16, 2020 2:22 PM
  • Wayne,

    Thats the plan but in this context how do i get each highest prices? 

     double fibos = 0;
    				
    for(int barIndex = ChartBars.GetBarIdxByX(chartControl, cursorPointX); barIndex <= blueIndex; barIndex++)
    	{
    					
    double closePrice = Bars.GetClose(barIndex);
    double open = Bars.GetOpen(barIndex);
    					
    double co = closePrice - open;
    			
    int bco = 0;
    					
    if (co < 0)
    	{
    	bco = barIndex;
    					
    					
    					
    double sum21 = 0;
    double sum22 = 0;
    double cmaMu = 0;
    	
    //for loop is indexed to bco to create multiple bars and multiple cmaMu					
    					
    for(int barIndex0 = bco; barIndex0 <= ChartBars.ToIndex; barIndex0++)
    	{
    					
    double closePrice0 = Bars.GetClose(barIndex0);
    double volumes = Bars.GetVolume(barIndex0);	
    					
    double clovol = closePrice0 * volumes;	
    					
    					
    	sum21 += clovol;
    	sum22 += volumes;
    				
    cmaMu = sum21 / sum22;
    
    					
    	}		

    We must first extract each bco before use .Add? How do i get to store than into multiple Add?

    TY

    Wednesday, September 16, 2020 2:43 PM
  • Hi Frankdot,
    >>Here's what it returns. Gives the highest value for each return when i want the highest for all of them. Answer LF should return one time 1.3569
    Thank you for your smaller approach. I checked your original code and find that If you put the “Print("HF"+highFound)” statement outside of the foreach loop, you will get the highest for all of them.
    Please correct me if I understand it wrong.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 17, 2020 7:06 AM
  • Thank you for your reply but the issu has been resolve.

    Thursday, September 17, 2020 2:06 PM
  • Thank you for your reply but the issu has been resolve.

    If you resolve the issue then please mark the reply that solve this as an answer so others will know what resolved this.

    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

    • Marked as answer by Frankdot Friday, September 18, 2020 11:40 AM
    Thursday, September 17, 2020 9:13 PM