Login Page - Create Account

Support Board


Date/Time: Fri, 29 Nov 2024 08:44:10 +0000



[Programming Help] - ACSIL value not being assigned

View Count: 317

[2023-02-28 19:44:01]
User538264 - Posts: 26
Hello,
In the given code, I am trying to get the values of highLevel and lowLevel.
The code is working fine except when it is initialized.
When initially applied (or Recalculated), if the last crossover condition was CROSS_FROM_BOTTOM then only the value of highLevel is correct and lowLevel is coming out as a garbage value. And if the last crossover condition was CROSS_FROM_TOP then only lowLevel value is correct and highLevel is a garbage value.
If I keep the study and an opposite crossover also occurs then both values become correct.

How can I rectify this.
Any help is much appreciated.
Thanks.

SCSFExport scsf_aza_MACDLevelsStudy(SCStudyInterfaceRef sc)
{
  int sgNums = 0;
  int inputNums = 0;
  int persInts = 0;
  int persFloats = 0;

  SCSubgraphRef Subgraph_MACDLine = sc.Subgraph[sgNums++];
  SCSubgraphRef Subgraph_SignalLine = sc.Subgraph[sgNums++];
  SCSubgraphRef Subgraph_MACDData = sc.Subgraph[sgNums++];
  SCSubgraphRef Subgraph_Histogram = sc.Subgraph[sgNums++];

  SCSubgraphRef Subgraph_MACDCross = sc.Subgraph[sgNums];

  SCFloatArrayRef Array_CrossUp = sc.Subgraph[sgNums].Arrays[0];
  SCFloatArrayRef Array_CrossDn = sc.Subgraph[sgNums].Arrays[1];
  SCFloatArrayRef Array_HighLevel = sc.Subgraph[sgNums].Arrays[2];
  SCFloatArrayRef Array_LowLevel = sc.Subgraph[sgNums].Arrays[3];

  SCInputRef i_InputData = sc.Input[inputNums++];
  SCInputRef i_FastLen = sc.Input[inputNums++];
  SCInputRef i_SlowLen = sc.Input[inputNums++];
  SCInputRef i_MACDLen = sc.Input[inputNums++];
  SCInputRef i_MAType = sc.Input[inputNums++];



  if (sc.SetDefaults)
  {
    // Set the configuration and defaults

    sc.GraphName = "MACD Levels Study";
    sc.GraphRegion = 1;
    sc.AutoLoop = 1;

    Subgraph_SignalLine.Name = "Signal Line";
    Subgraph_SignalLine.PrimaryColor = RGB(0, 255, 0);
    Subgraph_SignalLine.DrawStyle = DRAWSTYLE_LINE;
    Subgraph_SignalLine.LineWidth = 1;

    Subgraph_MACDLine.Name = "MACD Line";
    Subgraph_MACDLine.PrimaryColor = RGB(250, 0, 0);
    Subgraph_MACDLine.DrawStyle = DRAWSTYLE_LINE;
    Subgraph_MACDLine.LineWidth = 1;

    Subgraph_Histogram.Name = "Histogram";
    Subgraph_Histogram.PrimaryColor = RGB(255, 255, 0);
    Subgraph_Histogram.DrawStyle = DRAWSTYLE_BAR;
    Subgraph_Histogram.LineWidth = 2;
    Subgraph_Histogram.AutoColoring = AUTOCOLOR_POSNEG;

    i_InputData.Name = "Input Data";
    i_InputData.SetInputDataIndex(SC_LAST);

    i_FastLen.Name = "Fast Moving Average Length";
    i_FastLen.SetInt(12);
    i_FastLen.SetIntLimits(1, MAX_STUDY_LENGTH);

    i_SlowLen.Name = "Slow Moving Average Length";
    i_SlowLen.SetInt(26);
    i_SlowLen.SetIntLimits(1, MAX_STUDY_LENGTH);

    i_MACDLen.Name = "MACD Moving Average Length";
    i_MACDLen.SetInt(9);
    i_MACDLen.SetIntLimits(1, MAX_STUDY_LENGTH);

    i_MAType.Name = "Moving Average Type";
    i_MAType.SetMovAvgType(MOVAVGTYPE_EXPONENTIAL);



    return;
  }


  if (sc.IsFullRecalculation && sc.Index == 0)
    sc.ClearAllPersistentData();

  // Do data processing  
  sc.MACD(sc.BaseDataIn[i_InputData.GetInputDataIndex()], Subgraph_MACDData, sc.Index, i_FastLen.GetInt(), i_SlowLen.GetInt(), i_MACDLen.GetInt(), i_MAType.GetInt());

  Subgraph_MACDLine[sc.Index] = Subgraph_MACDData.Data[sc.Index];
  Subgraph_SignalLine[sc.Index] = Subgraph_MACDData.Arrays[2][sc.Index];
  Subgraph_Histogram[sc.Index] = Subgraph_MACDLine[sc.Index] - Subgraph_SignalLine[sc.Index];

  int barsSinceCrossUp = 0;
  int barsSinceCrossDn = 0;

  float swingHigh;
  float swingLow;
  float& highLevel = sc.GetPersistentFloat(persFloats++);
  float& lowLevel = sc.GetPersistentFloat(persFloats++);

  int swingHighIndex;
  int swingLowIndex;
  int& highIndex = sc.GetPersistentInt(persInts++);
  int& lowIndex = sc.GetPersistentInt(persInts++);


  if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED) //Defining and plotting swing levels
  {
    if (sc.CrossOver(Subgraph_SignalLine, Subgraph_MACDLine) == CROSS_FROM_BOTTOM)
    {
      Array_CrossDn[sc.Index] = 1;
      swingHigh = sc.GetHighest(sc.High, sc.Index - barsSinceCross(Array_CrossDn, 1) + 1, barsSinceCross(Array_CrossUp, 1) - barsSinceCross(Array_CrossDn, 1));
      swingHighIndex = sc.GetIndexOfHighestValue(sc.High, sc.Index - barsSinceCross(Array_CrossDn, 1) + 1, barsSinceCross(Array_CrossUp, 1) - barsSinceCross(Array_CrossDn, 1));
      highLevel = swingHigh;
      highIndex = swingHighIndex;
    }
    else if (sc.CrossOver(Subgraph_SignalLine, Subgraph_MACDLine) == CROSS_FROM_TOP)
    {
      Array_CrossUp[sc.Index] = 1;
      swingLow = sc.GetLowest(sc.Low, sc.Index - barsSinceCross(Array_CrossUp, 1) + 1, barsSinceCross(Array_CrossDn, 1) - barsSinceCross(Array_CrossUp, 1));
      swingLowIndex = sc.GetIndexOfLowestValue(sc.Low, sc.Index - barsSinceCross(Array_CrossUp, 1) + 1, barsSinceCross(Array_CrossDn, 1) - barsSinceCross(Array_CrossUp, 1));
      lowLevel = swingLow;
      lowIndex = swingLowIndex;
    }
    else
    {
      Array_CrossDn[sc.Index] = 0;
      Array_CrossUp[sc.Index] = 0;
    }



    SCString msg;
    msg.Format("high = %f, low = %f", highLevel, lowLevel);
    sc.AddMessageToLog(msg, 1);


    
  }
  else
  {
    return;
  }



}

[2023-02-28 19:48:48]
User538264 - Posts: 26
I forgot to include the function barsSinceCross(). Here is the code for that function.


int barsSinceCross(SCConstFloatArrayRef arr, int occurence) {
  int bars = 1;
  int reps = 1;

  for (int i = arr.GetArraySize() - 1; i > 0; i--)
  {
    if (arr[i] == 0.0)
    {
      bars++;
    }
    else if (reps < occurence)
    {
      reps++;
      bars++;
    }
    else
    {
      break;
    }
  }
  return bars;
}

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account