简体   繁体   中英

How to Return a value into a method?

I am not sure i am using the right choice of words. I have this variable fibo2 and inside there is another variable nearclose. nearclose is coming from the result of a process of formulas. At the end of the script nearclose return a number, lets say 10. It returns 10 because fibo2 starts by calculating at the beginning with the variable Close.GetValueAt(CurrentBar):

fibo2 = (Close.GetValueAt(CurrentBar) - lowPrice0) / (Ncma - lowPrice0);

Once the first calculation is done i want to replace Close.GetValueAt(CurrentBar) by the result of nearclose, 10. I can do it manually like this by replacing Close.GetValueAt(CurrentBar) by 10:

fibo2 = (10 - lowPrice0) / (Ncma - lowPrice0);

and nearclose at the end returns a new value 11. So that the next time 11 replace 10 to produce a new value that will again replace 11. This way i can create a list of all values after processing every bars.

So i decided to replace Close.GetValueAt(CurrentBar) by nearclose:

fibo2 = (nearclose - lowPrice0) / (Ncma - lowPrice0);

The problem its creating somekind of internal loop and return 0 or the wrong value.

Edited Part:

Now this is a small example of the main idea:

    double fibo2 = 0;
    double nearclose = 8;
    double lowPrice0 = 4;
    double Ncma = 5;
    double x = 10;  
        
        
        
        
        fibo2 = (nearclose - lowPrice0) / (Ncma - lowPrice0);
        
        
        nearclose = fibo2 * x;
        
        
            var numbers = new List<double> {};
            numbers.Add(nearclose);
            
            
        foreach (var item in numbers)
            {   
                
                Print(item);
            
            }

The Print return 40 but if you replace nearclose by 40 it will returned 360.Than in the list you end up with {8, 40, 360...} You send back the new value produced by nearclose to nearclose itself in fibo2. In my code (not this sample) nearclose = 0; because the result come from formulas generated by a for loop of bars in a chart. I hope this help more.

Edited part 2020-10-27

public void WhateverTheChartingNameIs()
                {
                    
                    var barCount = 3;    
                    var results = new List<double> { nearclose };
                    
                    for(int i = 0; i < barCount; i++)
                    {
                        var result = RunCalculation(results[i]); 
                        results.Add(result);

                        Print("Result"+result);
                    }
                }
                        

                        
    private double RunCalculation(double nearclose)
            {
                
                return (nearclose - lowPrice0) / (Ncma - lowPrice0);
            }                   

It sounds like you simply want to run a calculation, record the result and then use that result as a variable in the next calculation for a given amount of executions. If so, I think you can do this:

public IList<double> CalculateResults(double startNumber, int barCount)
{    
    var results = new List<double> { startNumber };
    
    for(int i = 0; i < barCount; i++)
    {
        var result = RunCalculation(results[i]);
        results.Add(result);
    }
    
    return results;
}

private double RunCalculation(double nearClose)
{
    var lowPrice0 = //whatever this is;
    var Ncma = //whatever this is;
    return (nearClose - lowPrice0) / (Ncma - lowPrice0);
}

Update

If by your comment you mean the charting software just prints the results then rather than returning the list of results make it void and print the results instead like this:

public void WhateverTheChartingNameIs()
{
    var nearClose = 8;
    var barCount = 3;    
    var results = new List<double> { nearClose };
    
    for(int i = 0; i < barCount; i++)
    {
        var result = RunCalculation(results[i]); //same as above. If you can't do this just stick the logic from this method inside the for loop.
        results.Add(result);

        Print(result);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM