Login Page - Create Account

Support Board


Date/Time: Wed, 27 Nov 2024 05:43:06 +0000



Post From: trying to get started programming, problem with simple exampe (own SMA)

[2023-09-21 17:27:00]
ForgivingComputers.com - Posts: 960
sc.Index is the current bar, sc.ArraySize is the number of bars on the chart. When recalculating the chart sc.Index starts at 0 and goes up to sc.ArraySize - 1.

Since the SMA is looking back from the current bar, the size of the array is irrelevant.

#include "sierrachart.h"
SCDLLName("Test SMA")

SCSFExport scsf_SMA(SCStudyInterfaceRef sc)
{  
if (sc.SetDefaults)
  {
    sc.GraphName = "Test SMA";
    sc.GraphRegion = 0;
    sc.AutoLoop = 1; //Automatic looping is enabled.
    
    sc.Subgraph[0].Name = "SMA";
    sc.Subgraph[0].DrawStyle = DRAWSTYLE_LINE;
    sc.Subgraph[0].PrimaryColor = RGB (0, 255, 0);
    
    sc.Input[0].Name = "Bars";
    sc.Input[0].SetInt(10);
    
    return;
  }
  
  // Section 2 - Do data processing here
  
  double sma;
  double sum;

  int Bars = sc.Input[0].GetInt();

  // We need to see at least Bars number of bars
  if (sc.Index < Bars)
    return;

  sum = 0.0;

  // Loop from current Bar back and sum close prices of each bar
  for (int BarIndex = sc.Index ; BarIndex > sc.Index - Bars; BarIndex--)
  {
    sum = sum + sc.Close[BarIndex];
  }

  // Divide by Bars to get average
  sc.Subgraph[0][sc.Index] = sma = sum/Bars;
  
}