0

I am getting an error code in the result. code below. I am basically trying to get a data set from SingleColumn method and use it in SMA method. But I am getting results deos not exist in current context.

static public void SMA()
{
    double[] closePrice = results.ToArray(); 

Below you can see the SingleColumn and part of SMA code.

#region Single Column
//static public double results;
static public void SingleColumn(IEnumerable<string> strs, int highNum)
{
    #region spilt data to columns
    Console.WriteLine("Single Column Query:");
    var columnQuery = from line in strs 
                      let elements = line.Split(',')
                      select Convert.ToDouble(elements[highNum]); 
    var results = columnQuery.ToList();
    double[] closePrice = results.ToArray();
    #endregion

    #region max, min, avg
    double average = results.Average();
    double max = results.Max();
    double min = results.Min();
    Console.WriteLine("High: {0}: Low: {1}: Average: {2:0.00}", max, min, average);
    #endregion
}

    #region Strategy Code SMA
static public void SMA()
{
    double[] closePrice = results.ToArray(); 
    int TOTAL_PERIODS = closePrice.Length;
    double[] output = new double[TOTAL_PERIODS];
    int begin;
    int length;

    for (int i = 0; i < closePrice.Length-TOTAL_PERIODS; i++) //had to change from -1 to -TOTAL_PERIODS
    {
        closePrice[i] = (double)i;
    }

    TicTacTec.TA.Library.Core.RetCode retCode = Core.Sma(0, closePrice.Length-1, closePrice, PERIODS_AVERAGE, out begin, out length, output);

3 Answers 3

2

You have some options:

  1. have SingleColumn return results and add that as a parameter to SMA
  2. make results a field of the class so it is shared.

Option 1. is cleaner since it forces callers to call SingleColumn first (or come up with a list on their own)

    static public double[] SingleColumn(IEnumerable<string> strs, int highNum)
    {
        ...
        return closePrice;
    }

        #region Strategy Code SMA
    static public void SMA(double[] closePrice)
    {
        int TOTAL_PERIODS = closePrice.Length;
        double[] output = new double[TOTAL_PERIODS];
        ...
    }   

Note that I changed your output/input from result to closePrice since it was just converting it to a List and back. It's cleaner just to leave it as a double[]. You can also clean up the code a bit by just using ToArray instead of using ToList and then ToArray.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your help. Seems to work and I can compile. This might be a stupid question, but how do I actually reach the SMA code and it? can you provide me with a quick example.
What do you mean "reach" They're static methods, so you call them with the class name in front: {classname}.SMA().
@D Stanley when trying to call SMA from the GetHigh method, I am getting "No overload for method 'SMA' takes 0 arguments. I called it by writing SMA();
I got it :) Program.SMA(SingleColumn(lines, high + 1)); Thanks for to all who assisted me int his matter!
0

You should use static variable.

private static IEnumerable<double> result;

and then in method SingleColumn asign columnQuery.ToList() to this variable

Comments

0

Each variable in C# is exists within a scope which is defined by curly braces. In your case the variable result is within the scope SingleCloumn.

public static void SingleColumn(IEnumerable<string> strs, int highNum)
{

}

To use result in another scope, you can make "result" as a global variable. As I can see you have commented out

//static public double results; 

first un-comment it and remove var from

var results = columnQuery.ToList();

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.