0

我是 C++ 的完全初学者,这段代码肯定做得很糟糕,但现在我试图为SierraChart编写一个简单的图表指标。在为山脉图表编写图表指标时,图表指标的所有代码都进入一个函数,我被困在这部分。

我只需要跑过去的最后一个方向。当前代码最终发生的情况是两者最终同时运行,因为两个条件最终都为真。

我已经为第二个方向尝试了 else if,并且在 if 块中尝试了一些布尔条件来尝试只运行最后一个方向,但这似乎并不能解决问题。

我该如何解决这个问题?

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;
            }
        }
    }
4

3 回答 3

2

您的缩进与 if 语句的逻辑不匹配(并且 C++ 不关心缩进。尝试在每个主要(最外层)if 语句之后将整个缩进位包装在它们自己的大括号中。:

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;
            }
        }
    }
}
于 2020-09-11T17:44:08.663 回答
1

例如,由于我不知道您只想执行一个语句的级别。

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

这是唯一的方法,因为在通过第一个之后它不会检查另一个 if

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;
        }
    }
}
于 2020-09-11T17:49:03.243 回答
0

您可以使用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 }
于 2020-09-11T17:43:06.467 回答