Login Page - Create Account

Support Board


Date/Time: Thu, 25 Apr 2024 12:58:27 +0000



Question about initialization of variables

View Count: 843

[2019-03-13 15:57:05]
User257019 - Posts: 25
Hello team,

I´m learning ACSIL and doing a simple study that will draw an horizontal line in the maximum and minimums. Something like the code below.

Is it correct to initialize maximum and minimum with the "if (!maximum)"? meaning, if it´s NULL, then assign the value of currentHigh.

I am sure there is a more elegant way regarding your best practices I could use? THANKS!


SCSFExport scsf_DrawMaxAndMin(SCStudyInterfaceRef sc)
{

  if (sc.SetDefaults)
  {
    ...
  }

  int& maximum = sc.GetPersistentInt(1);
  int& minimum = sc.GetPersistentInt(2);
  
  int currentHigh = sc.BaseDataIn[SC_HIGH][sc.Index];
  int currentLow = sc.BaseDataIn[SC_LOW][sc.Index];

  if (!maximum) // Meaning... is it NULL?
    maximum = currentHigh;
  
  if (!minimum) // Meaning... is it NULL?
    minimum = currentLow;

  if (maximum < currentHigh)
    maximum = currentHigh;
  
  if (minimum > currentLow)
    minimum = currentLow;

// Drawings

}

[2019-03-13 22:11:10]
User257019 - Posts: 25
Hello team, my apologies, I just understood better persistent variables and made it work. Just in case someone else wants to know:


SCSFExport scsf_DrawMaxAndMin(SCStudyInterfaceRef sc)
{

  if (sc.SetDefaults)
  { ... }

  float& maximum = sc.GetPersistentFloat(1);
  float& minimum = sc.GetPersistentFloat(2);
  
  if (sc.IsFullRecalculation && sc.Index == 0)
  {
    sc.GetPersistentFloat(1) = 100.0;
    sc.GetPersistentFloat(2) = 100000.0;
  }

  if (sc.Index == sc.ArraySize - 1) // Only calculated for the latest bar
  {
    float currentHigh = sc.BaseDataIn[SC_HIGH][sc.Index];
    float currentLow = sc.BaseDataIn[SC_LOW][sc.Index];

    if (maximum < currentHigh)
      maximum = currentHigh;

    if (minimum > currentLow)
      minimum = currentLow;

    SCString DebugMessage;

    // Log minimum value
    DebugMessage.Format("Minimum=%f", minimum);
    sc.AddMessageToLog(DebugMessage, 0);

    // Log maximum value
    DebugMessage.Format("Maximum=%f", maximum);
    sc.AddMessageToLog(DebugMessage, 0);
  }
}

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

Login

Login Page - Create Account