Login Page - Create Account

Support Board


Date/Time: Wed, 15 Jan 2025 18:56:39 +0000



Post From: Iterate through different Study input variables

[2017-07-11 14:34:01]
User540518 - Posts: 6
Hi Sierra Chart support,

Is it possible to iterate through different ZigZag Reversal Percent input variables? rather than manually changing the ReversalPercent SCInputRef variable?

Below is my modified code where I've replaced ReversalPercent with values I'm getting from a predefined array. Basically, I want to iterate through different ZigZag subgraphs with specific ReversalPercentArray values..

I think the issue is related to manual looping....my study is only drawing the first ReversalPercentArray[] value and its not iterating through the rest of the array elements. Is there a way to force a full recalculation (I'm trying to do it by setting Index=0)? or is there some other issue I'm not seeing?

Thanks for your help,

Mike



SCSFExport scsf_TestZigZag(SCStudyInterfaceRef sc)
{
  SCSubgraphRef ZigZagLine = sc.Subgraph[0];
  SCSubgraphRef ZZPeakTypeDisplay = sc.Subgraph[1];

  SCFloatArrayRef ZigZagPeakType = ZigZagLine.Arrays[0]; //ZigZagPeakType : +1=high peak, -1=low peak, 0=not peak, calculation array (.Array) set by the internal ResettableZigZag() function
  SCFloatArrayRef ZigZagPeakIndex = ZigZagLine.Arrays[1]; //ZigZagPeakIndex: index of current peak, -1=no peak yet, calculation array (.Array) set by the internal ResettableZigZag() function
  SCFloatArrayRef ZigZagReset = ZigZagLine.Arrays[2];    //ZigZagReset: 0=no reset, 1=reset, calculation array (.Array) set by the internal ResettableZigZag() function
  SCFloatArrayRef ZigZagTrend = ZigZagLine.Arrays[3];    //ZigZagTrend: 0=none, 1=confirmed up, -1=confirmed down, calculation array (.Array) set by the internal ResettableZigZag() function
  
  SCInputRef InputDataHigh = sc.Input[0];
  SCInputRef InputDataLow = sc.Input[1];
  SCInputRef CalcOnBarClose = sc.Input[2];
  //SCInputRef ReversalPercent = sc.Input[3];
  SCInputRef LabelsOffset = sc.Input[4];

  double ReversalPercent = 0.0;
  double ReversalPercentValues[14] = {0.009, 0.008, 0.007, 0.006, 0.005, 0.004, 0.003, 0.002, 0.001, 0.0009, 0.0008, 0.0007, 0.0006, 0.0005};  //equivalent to the original code (0.1f * 0.01f)
  
  if (sc.SetDefaults)                    //Set the configuration variables and defaults
  {
    sc.GraphName = "TestZigZag";
    
    sc.FreeDLL = 1;                    //During development set this flag to 1, so the DLL can be rebuilt without restarting Sierra Chart. When development is completed, set it to 0 to improve performance.
    sc.AutoLoop = 0;                  //Automatic looping is disabled, manually loop thru our arrays
    sc.GraphRegion = 0;                  //Draws onto the main chart

    ZigZagLine.Name = "ZigZag";
    ZigZagLine.DrawStyle = DRAWSTYLE_LINE;
    ZigZagLine.LineStyle = LINESTYLE_DOT;
    ZigZagLine.PrimaryColor = RGB(202, 202, 0);
    ZigZagLine.SecondaryColorUsed = 1;
    ZigZagLine.SecondaryColor = RGB(255, 0, 0);
    ZigZagLine.LineWidth = 3;
    ZigZagLine.DrawZeros = false;

    ZZPeakTypeDisplay.Name ="ZigZag PeakType";      //Set a label so the PeakType value shows up in our Chart Values window
    ZZPeakTypeDisplay.DrawStyle = DRAWSTYLE_IGNORE;    //Dont draw this on the chart, we only want it in the Chart Values Window

    unsigned short DisplayOrder = 1;

    InputDataHigh.Name = "Input Data for High";
    InputDataHigh.SetInputDataIndex(SC_HIGH);
    InputDataHigh.DisplayOrder = DisplayOrder++;

    InputDataLow.Name = "Input Data for Low";
    InputDataLow.SetInputDataIndex(SC_LOW);
    InputDataLow.DisplayOrder = DisplayOrder++;

    CalcOnBarClose.Name = "Calculate New Values On Bar Close";
    CalcOnBarClose.SetYesNo(1);              //0 is no, 1 is yes
    CalcOnBarClose.DisplayOrder = DisplayOrder++;

    //ReversalPercent.Name = "Reversal % for Calculation Mode 1";
    //ReversalPercent.SetFloat(.3f);
    //ReversalPercent.DisplayOrder = DisplayOrder++;

    LabelsOffset.Name = "Text Label Offset";
    LabelsOffset.SetFloat(1.0f);
    LabelsOffset.DisplayOrder = DisplayOrder++;

    return;
  }
  
  int &LastCalcIndex = sc.GetPersistentInt(1);      //Remember what our current Index is, keeps this information persistent between loops. Used to make manual looping more efficient.
  int &ZigZagStartIndex = sc.GetPersistentInt(2);
  int Index = 0;

  if (sc.UpdateStartIndex == 0)              //If we are on the first bar, set our initial index values so we know where to start when iterating through our Subgraphs
  {                            //for the first time
    LastCalcIndex = -1;
    ZigZagStartIndex = 0;
    ZigZagReset[0] = 1.0f;
  }

  SCDateTime NextSessionStart = 0;            //sc.UpdateStartIndex is mainly used to keep our manual looping efficient. Gets the next trading day's session start time
  if (sc.UpdateStartIndex > 0)
    NextSessionStart = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.UpdateStartIndex - 1]) + 1 * DAYS;

  sc.EarliestUpdateSubgraphDataArrayIndex = sc.UpdateStartIndex;  //makes our study compatible with overlay and spreadsheet study, informs those studies of when our index changes
  
  
  //Iterate through different ReversalPercent values
  for (int i=0; i<14; i++)
  {
    ReversalPercent = ReversalPercentValues[i];      //an example of the REversalPrecent array element access with predefined values
    
    //print out ReversalPrecent values
    //SCString MsgLog;
    //MsgLog.Format("Reversal Percent is: %f", ReversalPercent);
    //sc.AddMessageToLog(MsgLog, 0); //non popup message

    //Populate the ZigZag Array, using Standard Manual Looping loop  
    for (Index = sc.UpdateStartIndex; Index < sc.ArraySize; ++Index)
    {
      int CalcIndex = Index;

      if (CalcOnBarClose.GetYesNo())          //Forces recalculation on new bar
      {
        CalcIndex--;
        if (CalcIndex <= LastCalcIndex)        //if the newest bar is the same bar or older, skip
          continue;
        LastCalcIndex = CalcIndex;          //otherwise, remember our newest bar that we are going to start our calculation on
      }                        

      //Calculate and set the ZigZag Subgraph and its calculation arrays
      sc.ZigZag(sc.BaseData[InputDataHigh.GetInputDataIndex()],
        sc.BaseData[InputDataLow.GetInputDataIndex()],
        ZigZagLine,                  //Populate our subgraph and calculation arrays, including ZigZagPeakType
        CalcIndex,
        //ReversalPercent.GetFloat() * 0.01f, //replace with ReversalPercentValues[i];
        ReversalPercent,  
        ZigZagStartIndex);
    }//end of manual loop

//Draw the version of the zigzag we want, based on the different ReversalPercentValues[]
//drawing code not included for brevity

//force a full recalculation
Index=0;
  }//end of iterating through ReversalPercent values array
}