Login Page - Create Account

Support Board


Date/Time: Tue, 14 May 2024 08:22:08 +0000



Post From: Calculation based on subgraph values

[2017-06-27 06:39:47]
Neo - Posts: 198
From the documentation you linked to I created a Moving Average study that calculates on subgraph values > 0 and ignores 0 values.

It does what I want, but has serious performance issues. I have tried to implement manual looping but can't seem to gain any performance improvement.

Any comments towards improving performance would be much appreciated.

// Test
#include "sierrachart.h"

SCDLLName("MA>Zero")

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 = "Moving Average - Zero";

    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();
  
  for (int Index = len - 1; Index < sc.ArraySize; Index++)
  {
    if (Study1Array[Index] <= 0.0f)
      continue;
  
    int count = 0;
    float summ = 0.0f;

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

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