Login Page - Create Account

Support Board


Date/Time: Sun, 27 Oct 2024 08:30:56 +0000



[Programming Help] - trying to get started programming, problem with simple exampe (own SMA)

View Count: 320

[2023-09-11 23:42:30]
User138457 - Posts: 38
I tried to write my own SMA as an exercise (I know it exists as a funtion).
Can anyone tell me what is going wrong with

  double sma;
  int BarIndexSize = sc.Input[0].GetInt();
  sma=0.;

  
  for ( int BarIndex = sc.Index; BarIndex>sc.Index-BarIndexSize; BarIndex--)
  {
    sma=sma+(sc.Close[BarIndex]);
        
  }
sma=sma/(BarIndexSize*1.f)

?

It always return 0. is sth off with the indices? I skipped all the definitions here for brewety, but obv.
happy to provide is necessary.

TIA
[2023-09-18 23:53:11]
ForgivingComputers.com - Posts: 949
Your loop starts off meeting the condition so it exits. Try adding this before the loop:
if(sc.Index < BarIndexSize)
return;

[2023-09-21 16:55:25]
User138457 - Posts: 38
Not helping either, curretnly the code reads
double sma;
  int BarIndexSize = sc.Input[0].GetInt();
  
  sma=0.;
  if(sc.Index < BarIndexSize)

return;
  //for (int BarIndex = sc.UpdateStartIndex-BarIndexSize+1; BarIndex < sc.ArraySize; BarIndex++)
  for ( int BarIndex = sc.ArraySize-BarIndexSize-1; BarIndex<sc.ArraySize-1; BarIndex++)
  {
    msg.Format("BarIndex = %d",BarIndex);
    sc.AddMessageToLog(msg,1);  
    
    sma=sc.Close[BarIndex];
    msg.Format("SMA = %d",sma);
    sc.AddMessageToLog(msg,1);    
  }
  //sma=sma/(1.*BarIndexSize);
  msg.Format("SMA = %d",sma);
  sc.AddMessageToLog(msg,1);    

Apparently I am gravely misunderstanding sc.ArraySize and sc.Index,
the (rather short) documentation did not enlight me either. Can anyone enlight me, please?
[2023-09-21 17:27:00]
ForgivingComputers.com - Posts: 949
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;
  
}

[2023-09-21 17:50:13]
User138457 - Posts: 38
wow, thanks for the detailed answer. I'll try that once I'm back at my Sierra Chart PC.

I think I was puzzled because with alerts you count backwards from the current with negative numbers, not with positive and I was for some reason assuming it would be the same with programming.
Date Time Of Last Edit: 2023-09-21 17:54:38
[2023-09-21 17:59:42]
ForgivingComputers.com - Posts: 949
You are welcome.

For extra credit enter 0 for the Bars setting.

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

Login

Login Page - Create Account