Login Page - Create Account

Support Board


Date/Time: Tue, 14 May 2024 19:09:30 +0000



Post From: Calculation based on subgraph values

[2017-06-29 02:36:06]
Neo - Posts: 198
@Sierra Chart Engineering
-Don't waste your time trying to implement this for me, if that is what you were planning to do. Although I'd be very interested in seeing how you would do it, I now have a version with significant performance improvements. Any comments regarding further improvement would be appreciated.

// Test
#include "sierrachart.h"

SCDLLName("MaGreaterThanZerov5")

SCSFExport scsf_MovingAverageSimple(SCStudyInterfaceRef sc)
{
  SCSubgraphRef Avg = sc.Subgraph[0];

  SCInputRef InputStudy1 = sc.Input[0];
  SCInputRef Study1Subgraph = sc.Input[1];
  SCInputRef StudySubgraph1 = sc.Input[2];  
  SCInputRef Length = sc.Input[3];
  
  if (sc.SetDefaults)
  {
    sc.GraphName = "MaGreaterThanZerov5";

    sc.GraphRegion = 0;
    sc.ValueFormat = 2;

    Avg.Name = "Avg";
    Avg.DrawStyle = DRAWSTYLE_LINE;
    Avg.PrimaryColor = RGB(0, 255, 0);
    Avg.LineWidth = 1;
    Avg.DrawZeros = true;

    Length.Name = "Length";
    Length.SetInt(4);
    Length.SetIntLimits(1, MAX_STUDY_LENGTH);
    
    InputStudy1.SetStudyID(0);
  
    Study1Subgraph.SetSubgraphIndex(0);

    StudySubgraph1.Name = "Study Subgraph Reference";
    StudySubgraph1.SetStudySubgraphValues(0,0);

    sc.FreeDLL = 1;

    return;
  }
  
  SCFloatArray Study1Array;
  sc.GetStudyArrayUsingID(StudySubgraph1.GetStudyID(), StudySubgraph1.GetSubgraphIndex(), Study1Array);

  int len = Length.GetInt();
  float* temp = new float[len];

  int Index;
  for (Index = len - 1; Index < sc.ArraySize; Index++)
  {
    if (Study1Array[Index] <= 0.0f)
    {
      Avg[Index] = 0.0f;
      continue;
    }
    
    int count = 0;
    float summ = 0.0f;

    int i = Index;
    while(count < len && i >= 0)
    {
      float data = Study1Array[i];
      if (data > 0.0f)
      {
        temp[count] = data;
        summ += data;
        count ++;

      }
      i--;
    }
      
    if (count == len)
    {
      Avg[Index] = summ / count;
      break;
    }
    else
    {
      Avg[Index] = 0.0f;
    }  
  }

  for (int k = Index + 1; k < sc.ArraySize; k++)
  {
    if (Study1Array[k] <= 0.0f)
    {
      Avg[k] = 0.0f;
      continue;
    }
      
    float summ = 0.0f;
    for (int i = 0; i < len - 1; i ++)
    {
      summ += temp[i];
    }
    summ += Study1Array[k];
    Avg[k] = summ / len;

    for (int i = len - 1; i >= 1; i --)
    {
      temp[i] = temp[i - 1];
    }
    temp[0] = Study1Array[k];
  }
}

Date Time Of Last Edit: 2017-06-29 02:45:46