Login Page - Create Account

Support Board


Date/Time: Fri, 07 Feb 2025 23:13:26 +0000



[Programming Help] - Calculate a moving average based on a custom variable

View Count: 1088

[2020-06-16 19:38:48]
MattK - Posts: 42
I'm trying to create a moving average based on a custom variable but can't make it work.

Is it not possible to do or am I just doing it wrong? Thanks


float realBody = sc.BaseData[SC_OPEN][sc.Index]-sc.BaseData[SC_CLOSE][sc.Index];
sc.MovingAverage(realBody, MABody10Subgraph, MOVAVGTYPE_SIMPLE, 10);

[2020-06-16 20:24:48]
Ackin - Posts: 1865
sc.BaseData[SC_LAST]

and next


sc.MovingAverage(realBody

realBody must be array Not single number



SCFloatArrayRef MovingAverage(SCFloatArrayRef FloatArrayIn, SCFloatArrayRef FloatArrayOut, unsigned int MovingAverageType, int Length); Auto-looping only.

Date Time Of Last Edit: 2020-06-16 20:27:21
[2020-06-17 10:32:36]
MattK - Posts: 42
Oh right, I understand why it wasn't working now.

Thanks!

And do you know how I can create an array for my variable?
[2020-06-17 10:47:07]
Ackin - Posts: 1865
try:
SCFloatArray realBody;
realBody[sc.Index] = sc.BaseData[SC_OPEN][sc.Index]-sc.BaseData[SC_LAST][sc.Index];

note
If you want to see and display the results for the user you can use instead of the new floatarray simply Subgraf SG .... (it is also an array)
Date Time Of Last Edit: 2020-06-17 10:47:27
[2020-06-17 11:15:37]
MattK - Posts: 42
Awesome! It works!

Thanks so much!
[2020-06-17 19:16:17]
MattK - Posts: 42
Actually, I spoke too fast :(

Sorry for the dumb questions but I'm still stuck.

I tried to create an array like you said, but the function returns 0. I checked with a debug message and MABody30 = 0 all the time.

Do you know what I am doing wrong?

SCFloatArray realBody;
realBody[sc.Index] = sc.BaseData[SC_OPEN][sc.Index]-sc.BaseData[SC_LAST][sc.Index];
sc.MovingAverage(realBody, MABody30Subgraph, MOVAVGTYPE_SIMPLE, 30);
float MABody30 = MABody30Subgraph[sc.Index];

Thanks!
Date Time Of Last Edit: 2020-06-17 19:25:07
[2020-06-17 19:58:47]
Ackin - Posts: 1865
"Use SG", I wrote it to you at the end of the message as another variant (see note), you sent only a part of your code, I can't know if you have a loop for filling the array and processing it.

Below you have a code that is definitely functional




SCSFExport scsf_CustomMovingAverage(SCStudyInterfaceRef sc)
{
  SCSubgraphRef Avg = sc.Subgraph[0];
  SCSubgraphRef realBody = sc.Subgraph[1];

  SCInputRef Length = sc.Input[1];

  if (sc.SetDefaults)
  {
    sc.GraphName = "Moving Average";

    sc.GraphRegion = 1;
    sc.ValueFormat = 2;
    sc.AutoLoop = 1;

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

    Length.Name = "Length";
    Length.SetInt(10);
    Length.SetIntLimits(1, MAX_STUDY_LENGTH);

    return;
  }
  sc.DataStartIndex = Length.GetInt();

  realBody[sc.Index] = sc.BaseData[SC_OPEN][sc.Index] - sc.BaseData[SC_LAST][sc.Index];
  sc.MovingAverage(realBody, Avg, MOVAVGTYPE_SIMPLE, Length.GetInt());
}

[2020-06-17 20:11:28]
MattK - Posts: 42
Got it! I didn't understand your note in the previous message at first, but now that I see the code you sent, I get it.

I changed the declaration of realBody and used SCSubgraphRef realBody = sc.Subgraph[1]; like you sent and it works.

Not sure why it works with this and not the other but I'm glad it works :)

Thank you very much for your help.

I really appreciate it.
[2020-06-17 20:36:31]
Ackin - Posts: 1865
Not sure why it works with this and not the other but I'm glad it works :)

An array is used when you fill it in one cycle. If the cycle is left, the pointer to that array is lost and a new declaration creates a new array .... for example, the content of another array or another study_array is loaded into the array ... or even what you wanted, but there would have to be a loop for filling values ..... But SG has an exact address, it's made as an array with a pointer to it ...

Simply use SG's and you won't have any problems ... when you'll improve your programming in the future, you will instead solve the performance and speed of the program and then you will to avoid autolooping and SG's.

have a nice day
Date Time Of Last Edit: 2020-06-17 20:42:25
[2020-06-17 20:54:03]
MattK - Posts: 42
It makes more sense now.

Thank you very much for your explanation.

Have an awesome day!

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

Login

Login Page - Create Account