Login Page - Create Account

Support Board


Date/Time: Fri, 29 Nov 2024 03:46:32 +0000



[Programming Help] - C++, ACSIL functions question

View Count: 812

[2023-03-11 13:24:38]
BenjFlame - Posts: 324
Hi, here is the skeleton of a basic ACSIL study.


#include "sierrachart.h"

SCDLLName("Custom Studies")

SCSFExport scsf_tradeLogger(SCStudyInterfaceRef sc)
{

if (sc.SetDefaults)
{
}


int variableA = 1;


int myFunction() // Definition. Wants to access variableA
{
int newvar = variableA + 2;
return newvar;
}

myFunction(); // call


}

Is there any way for myfunction() definition to access variableA without passing it as parameter? Or is it an obligation to pass to the function each and every variables it is going to use? C++ Lambda, could this be a solution?
I'm confused because I come from another language where you can access freely any variable in the scope just higher.
Date Time Of Last Edit: 2023-03-11 14:36:48
[2023-03-11 16:43:21]
ondafringe - Posts: 286
From what I understand: (never speak in absolutes!) lol

I believe it has to be passed.
C++ Lambda: Don't know

Plus, your skeleton is not correct, you won't get a clean compile.

Your function has to be defined above the SCSFExport line. If you want it defined below that very last curly brace, then you have to first declare the function above the SCSFExport line.

You also need a variable to receive the return value from your function.
Date Time Of Last Edit: 2023-03-11 16:44:27
[2023-03-11 18:15:47]
BenjFlame - Posts: 324
Your function has to be defined above the SCSFExport line.

Yes, I noticed that. What's the rule that governs this? Again, I'm confused as I come from other languages.
Is this C++ rule?

And you are right about the variable for holding the return value of the function.
[2023-03-11 19:25:53]
ForgivingComputers.com - Posts: 960
Your function has to be defined above the SCSFExport line.

What's the rule that governs this?

The rule is you cannot use a function before it has been declared. Just like variables.
[2023-03-11 19:39:35]
BenjFlame - Posts: 324
Indeed, it works when I put the definition code outside of SCSFExport.
But I was thinking that I could declare function in SCSFExport which apparently, I can't. That is the rule I was curious about. Again, coming from other language it's no problem at all to declare a function in a function so I'm a bit surprised.

Also, is there any way to give that function outside SCSFExport scsf_tradeLogger, access to the variables inside it, but without passing each of them as argument?
Date Time Of Last Edit: 2023-03-11 19:40:15
[2023-03-11 20:05:26]
Ticks - Posts: 183
There is also lots of example code in the "ACS_Source" folder within the Sierra Chart installation folder.
That is what I resorted to when I first came to SC and had to convert my TradeStation indicators from EasyLanguage to ACSIL.
[2023-03-11 20:26:16]
ondafringe - Posts: 286
After thinking about it, you could declare variableA and newvar as globals (above the SCSFExport line).

Then define your function as void, as per the rules I posted earlier.

I'm thinking you *should* then be able to manipulate the global variables within the function itself, without passing anything, and still have both variables accessible in the main part of your study.
Date Time Of Last Edit: 2023-03-11 23:03:58
[2023-03-11 21:35:28]
ForgivingComputers.com - Posts: 960
Also, is there any way to give that function outside SCSFExport scsf_tradeLogger, access to the variables inside it, but without passing each of them as argument?

When you pass the (SCStudyInterfaceRef sc) parameter, you get access to all of the sc.Input[x], sc.Subgraph[y], and sc.GetPersistent*(z) Variables, among other things.
[2023-03-12 09:11:36]
BenjFlame - Posts: 324
After thinking about it, you could declare variableA and newvar as globals (above the SCSFExport line).

That could be a way of doing it thanks.

When you pass the (SCStudyInterfaceRef sc) parameter, you get access to all of the sc.Input[x], sc.Subgraph[y], and sc.GetPersistent*(z) Variables, among other things.

Very interesting. Is there anyway within the function to use the SCSubgraphRef Subgraph_Ref_adx alias instead of the sc.Subgraph[y] nomenclature?
[2023-03-12 11:53:09]
User431178 - Posts: 544
Is there anyway within the function to use the SCSubgraphRef Subgraph_Ref_adx alias instead of the sc.Subgraph[y] nomenclature?

Pass it in as a parameter


void SomeFunc(SCSubgraphRef Subgraph_Ref_adx)
{
....some code
}

or declare within the function


void SomeFunc(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_Ref_adx = sc.Subgraph[?];

....some code
}

One way or other you need to get the variable(s) you wish to use into the scope of the function.

Here are some links to information that might help.

C++ Functions
ACSIL Programming Concepts: Passing ACSIL Interface Members Structure To Secondary Function

https://en.cppreference.com/w/cpp/language/scope
https://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm
https://www.geeksforgeeks.org/scope-of-variables-in-c/
Date Time Of Last Edit: 2023-03-12 11:55:02
[2023-03-12 17:38:15]
BenjFlame - Posts: 324
Thanks all. It's clear now.
First, function in function is not allowed in C++. Period.

And you gave solutions to get the desired result.

There is also the c++ lambda. I can have it inside of another function, and it has access to all its variables:


auto lambda = [&]() { // Reference link to all variables
};

lambda();

It compiles, but I haven't tried it in a live environment yet.
Is there any reason why I should not use a lambda such as this one in SierraChart study context? In terms of memory usage and such?
Date Time Of Last Edit: 2023-03-12 17:47:06

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

Login

Login Page - Create Account