FrankDuc
FrankDuc

Reputation: 27

How to test more than one variable in an if statement?

I am trying to test more than one variable in an if statement.

double firstPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[15][0];
double secondPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[14][0];

In fact their will be more than one variable: thirdPlot fourthPlot …

if(firstPlot < highPrice && firstPlot > lowPrice)

Is it possible to test each variable in one shot inside the if statement without having to repeat the if statement the same number of times as the number of variables? Like creating one variable that will gather all Plot and be tested all at the same time separately inside the if statement?

Thank you

Upvotes: 1

Views: 184

Answers (2)

vc 74
vc 74

Reputation: 38179

Looks like Values is a jagged array from which you want to extract the first element of each underlying array, so the following should work:

using System.Collections.Generic;
using System.Linq;
...
double[][] plots = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values;

IEnumerable<double> firstValues = plots.Select(a => a[0]);

bool allValuesInRange = firstValues.All(v => v < highPrice && v > lowPrice);

EDIT after your comment:

NinjaTrader.NinjaScript.Series<double>[] series = 
    RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values;

IEnumerable<double> firstValues = series.Select(s => s[0]);

foreach (double firstValue in firstValues)
{
    Console.WriteLine(firstValue);
}

Upvotes: 1

John von No Man
John von No Man

Reputation: 3030

Can you iterate through your array instead? E.g., something like this:

    private bool CheckValues(/*params here*/)
    {
        var RMMAVals = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5);
        for (int k = 0;  k<RMMAVals.Length; k++)
        {
            if (RMMAVals[k][0] >= highPrice || RMMAVals[k][0] <= lowPrice)
                return false;
        }
        return true;
    }

Upvotes: 0

Related Questions