0

I'm a complete beginner to C++ and this code is surely done terribly but for now I am trying to just code a simple chart indicator for SierraChart. When coding a chart indicator for sierra chart all the code for a chart indicator goes into one function and I'm stuck on this part.

I only need to run just the last direction that was crossed. What ends up happening with the code currently is that both end up running at the same time because both conditions end up being true.

I've tried an else if for the second direction and some boolean conditions inside the if blocks to try and only run the last direction and that doesn't seem to solve the issue.

How can I solve this?

int &barCount = sc.GetPersistentInt(0);
float &longCount = sc.GetPersistentFloat(0);
float &shortCount = sc.GetPersistentFloat(0);
float Highest = sc.GetHighest(sc.BaseDataIn[SC_HIGH], barCount);
float Lowest = sc.GetLowest(sc.BaseDataIn[SC_LOW], barCount);
sc.MovingAverage(sc.BaseData[Input_MA1Price.GetInputDataIndex()], sc.Subgraph[2], Input_MA1Type.GetMovAvgType(), Input_MA1Length.GetInt());
sc.MovingAverage(sc.BaseData[Input_MA2Price.GetInputDataIndex()], sc.Subgraph[3], Input_MA2Type.GetMovAvgType(), Input_MA2Length.GetInt());
int direction = sc.CrossOver(sc.Subgraph[2], sc.Subgraph[3]);

if (direction == CROSS_FROM_BOTTOM)
    {
    barCount = 0;
    longCount = 0;
    }
    if (sc.GetBarHasClosedStatus(sc.Index) == BHCS_BAR_HAS_CLOSED)
    {
    barCount++;
    barCounter[sc.Index] = barCount;
    if (sc.BaseData[SC_HIGH][sc.Index] == Highest)
        {
        longCount = 0;
        }
        if (sc.BaseData[SC_HIGH][sc.Index] > sc.BaseData[SC_HIGH][sc.Index - 1] && sc.BaseData[SC_HIGH][sc.Index] < Highest)
        {
            longCount++;
            highPrice[sc.Index] = longCount;
            if (sc.BaseData[SC_HIGH][sc.Index - 2] < sc.BaseData[SC_HIGH][sc.Index - 1] && sc.BaseData[SC_HIGH][sc.Index] < Highest)
            {
    longCount--;
        highPrice[sc.Index] = 0;
            }
        }
    }

if (direction == CROSS_FROM_TOP)
    {
        barCount = 0;
        shortCount = 0;
    }
    if (sc.GetBarHasClosedStatus(sc.Index) == BHCS_BAR_HAS_CLOSED)
    {
        barCount++;
        barCounter[sc.Index] = barCount;
        if (sc.BaseData[SC_LOW][sc.Index] == Lowest)
        {
            shortCount = 0;
        }
        if (sc.BaseData[SC_LOW][sc.Index] < sc.BaseData[SC_LOW][sc.Index - 1] && sc.BaseData[SC_LOW][sc.Index] > Lowest)
        {
            shortCount++;
            lowPrice[sc.Index] = shortCount;
            if (sc.BaseData[SC_LOW][sc.Index - 2] > sc.BaseData[SC_LOW][sc.Index - 1] && sc.BaseData[SC_LOW][sc.Index] > Lowest)
            {
                shortCount--;
                lowPrice[sc.Index] = 0;
            }
        }
    }
not2qubit
  • 14,531
  • 8
  • 95
  • 135
  • Maybe you want to use `else if`? – Fred Larson Sep 11 '20 at 17:40
  • Your indentation doesn't match the logic of the if statement (and C++ doesn't care about indentation. Try wrapping the whole indented bits after each of the major (outermost) if statements in their own braces. – L. Scott Johnson Sep 11 '20 at 17:42
  • Your indentation is inconsistent. Please decide if you want the braces aligned with what is inside the braces (as in your first `if` statement) or with what is outside the braces (as in your last `if` statement), then apply that choice to the example code. – JaMiT Sep 11 '20 at 17:49
  • If your editor can't keep the code formatted properly - get a new editor. At least run the code through an online "beautifier" or "pretty printer". When you do that, I think the issues will stand out very clearly. – 001 Sep 11 '20 at 17:52

3 Answers3

2

Your indentation doesn't match the logic of the if statement (and C++ doesn't care about indentation. Try wrapping the whole indented bits after each of the major (outermost) if statements in their own braces.:

int &barCount = sc.GetPersistentInt(0);
float &longCount = sc.GetPersistentFloat(0);
float &shortCount = sc.GetPersistentFloat(0);
float Highest = sc.GetHighest(sc.BaseDataIn[SC_HIGH], barCount);
float Lowest = sc.GetLowest(sc.BaseDataIn[SC_LOW], barCount);
sc.MovingAverage(sc.BaseData[Input_MA1Price.GetInputDataIndex()], sc.Subgraph[2], Input_MA1Type.GetMovAvgType(), Input_MA1Length.GetInt());
sc.MovingAverage(sc.BaseData[Input_MA2Price.GetInputDataIndex()], sc.Subgraph[3], Input_MA2Type.GetMovAvgType(), Input_MA2Length.GetInt());
int direction = sc.CrossOver(sc.Subgraph[2], sc.Subgraph[3]);

if (direction == CROSS_FROM_BOTTOM)
{
    {
    barCount = 0;
    longCount = 0;
    }
    if (sc.GetBarHasClosedStatus(sc.Index) == BHCS_BAR_HAS_CLOSED)
    {
    barCount++;
    barCounter[sc.Index] = barCount;
    if (sc.BaseData[SC_HIGH][sc.Index] == Highest)
        {
        longCount = 0;
        }
        if (sc.BaseData[SC_HIGH][sc.Index] > sc.BaseData[SC_HIGH][sc.Index - 1] && sc.BaseData[SC_HIGH][sc.Index] < Highest)
        {
            longCount++;
            highPrice[sc.Index] = longCount;
            if (sc.BaseData[SC_HIGH][sc.Index - 2] < sc.BaseData[SC_HIGH][sc.Index - 1] && sc.BaseData[SC_HIGH][sc.Index] < Highest)
            {
    longCount--;
        highPrice[sc.Index] = 0;
            }
        }
    }
}

if (direction == CROSS_FROM_TOP)
{
    {
        barCount = 0;
        shortCount = 0;
    }
    if (sc.GetBarHasClosedStatus(sc.Index) == BHCS_BAR_HAS_CLOSED)
    {
        barCount++;
        barCounter[sc.Index] = barCount;
        if (sc.BaseData[SC_LOW][sc.Index] == Lowest)
        {
            shortCount = 0;
        }
        if (sc.BaseData[SC_LOW][sc.Index] < sc.BaseData[SC_LOW][sc.Index - 1] && sc.BaseData[SC_LOW][sc.Index] > Lowest)
        {
            shortCount++;
            lowPrice[sc.Index] = shortCount;
            if (sc.BaseData[SC_LOW][sc.Index - 2] > sc.BaseData[SC_LOW][sc.Index - 1] && sc.BaseData[SC_LOW][sc.Index] > Lowest)
            {
                shortCount--;
                lowPrice[sc.Index] = 0;
            }
        }
    }
}
L. Scott Johnson
  • 4,213
  • 2
  • 17
  • 28
1

As an example, since I don't know which level you want to execute only one statement.

if ( condition ) expression
else if ( condition )  expression
else expresison

This is the only way to go as it won't check the other if's after passing the first one

int &barCount = sc.GetPersistentInt(0);
float &longCount = sc.GetPersistentFloat(0);
float &shortCount = sc.GetPersistentFloat(0);
float Highest = sc.GetHighest(sc.BaseDataIn[SC_HIGH], barCount);
float Lowest = sc.GetLowest(sc.BaseDataIn[SC_LOW], barCount);
sc.MovingAverage(sc.BaseData[Input_MA1Price.GetInputDataIndex()], sc.Subgraph[2], Input_MA1Type.GetMovAvgType(), Input_MA1Length.GetInt());
sc.MovingAverage(sc.BaseData[Input_MA2Price.GetInputDataIndex()], sc.Subgraph[3], Input_MA2Type.GetMovAvgType(), Input_MA2Length.GetInt());
int direction = sc.CrossOver(sc.Subgraph[2], sc.Subgraph[3]);

if (direction == CROSS_FROM_BOTTOM)
{
    barCount = 0;
    longCount = 0;
}
else if (sc.GetBarHasClosedStatus(sc.Index) == BHCS_BAR_HAS_CLOSED)
{
    barCount++;
    barCounter[sc.Index] = barCount;

    if (sc.BaseData[SC_HIGH][sc.Index] == Highest)
    {
        longCount = 0;
    }
    else if (sc.BaseData[SC_HIGH][sc.Index] > sc.BaseData[SC_HIGH][sc.Index - 1] && sc.BaseData[SC_HIGH][sc.Index] < Highest)
    {
        longCount++;
        highPrice[sc.Index] = longCount;
        if (sc.BaseData[SC_HIGH][sc.Index - 2] < sc.BaseData[SC_HIGH][sc.Index - 1] && sc.BaseData[SC_HIGH][sc.Index] < Highest)
        {
            longCount--;
            highPrice[sc.Index] = 0;
        }
    }
}
else if (direction == CROSS_FROM_TOP)
{
        barCount = 0;
        shortCount = 0;
}
else (sc.GetBarHasClosedStatus(sc.Index) == BHCS_BAR_HAS_CLOSED)
{
    barCount++;
    barCounter[sc.Index] = barCount;
    if (sc.BaseData[SC_LOW][sc.Index] == Lowest)
    {
        shortCount = 0;
    }
    else if (sc.BaseData[SC_LOW][sc.Index] < sc.BaseData[SC_LOW][sc.Index - 1] && sc.BaseData[SC_LOW][sc.Index] > Lowest)
    {
        shortCount++;
        lowPrice[sc.Index] = shortCount;
        if (sc.BaseData[SC_LOW][sc.Index - 2] > sc.BaseData[SC_LOW][sc.Index - 1] && sc.BaseData[SC_LOW][sc.Index] > Lowest)
        {
            shortCount--;
            lowPrice[sc.Index] = 0;
        }
    }
}
Jackal
  • 3,359
  • 4
  • 33
  • 78
0

You can use else if

if(a == 0)
{ }
else if(b == 0)
{ // Only if a was not zero and b is zero }
else
{ // If a was not zero and b was not zero }
Chad
  • 18,706
  • 4
  • 46
  • 63