Login Page - Create Account

Support Board


Date/Time: Wed, 08 May 2024 22:07:00 +0000



Sierra Chart Shared Variables

View Count: 2391

[2018-03-01 08:12:06]
User79074 - Posts: 105
Can some shared variables / arrays be created that will be accessible by any study in any chart in any chartbook?

For example

Chartbook 1, chart 1, study 1: Write to sc.SharedArray[0] = "Hello";

Now in

Chartbook 2, chart 1, study 1: Read sc.SharedArray[0] and get "Hello", and also be able to write to it

This would be a portion of memory that could be shared across any study, in any chart, in any chartbook, and would stay there even after the study is removed.

It is similar to say for example variable sc.UserName as it is shared across all studies, charts, chartbooks.
So the request is to create shared variables /arrays that can be read and written to from all studies, charts, and chartbooks.

Can this be done?
Date Time Of Last Edit: 2018-03-01 08:12:50
[2018-03-01 20:09:41]
JoseyWales - Posts: 67
In the most basic way, you could create a static struct that holds variables. Place the following code in a header file that you #include in each cpp file that needs access to the SharedData.

#ifndef SharedData_H
#define SharedData_H

#include "sierrachart.h"

enum SCStringArrayValues
{
  Value1,
  Value2,
  SCStringArraySize
};

struct MutualDataStruct
{
  int IntegerValue{};
  float FloatValue{};
  SCString SCStringValue{};

  SCString SCStringArray[SCStringArraySize]; // C style array of type SCString
  std::vector<SCString> SCStringVector; // C++ Vector style array of type SCString
};

static MutualDataStruct SharedData;

#endif SharedData_H

Of course rename the variables to something more descriptive than IntegerValue;

The curly braces are called uniform initialization a C++11 feature, which means the variables are initialized with default values when they are created. For integers and floats this means the variable will be 0, and for strings it will be blank "".

And in a study function you call whatever variable however you want:

      SharedData.IntegerValue = 3;
      SharedData.SCStringValue = "This is text";
      SharedData.SCStringArray[Value1] = "Hello";
      SharedData.SCStringVector.emplace_back("Hello Vector");

Do note that C style arrays must have their size known at compile time, which is one reason why I used an Enum value. The other being that it's wise to use a const named variable instead of a random "magic number", especially if it's being referred to from multiple cpp files. Less room for bugs that way.

You could also use a std::vector<SCString> and push_back()/emplace_back() each SCString to add it to the vector. Or maybe std::stack would better suit your needs.
http://www.cplusplus.com/reference/vector/vector/

Another thing to note is what happens when the study is recalculated? Should the vector's contents be cleared? If so, would that mess up another study that is referring to it? You could also make the struct more like a class and have it be Object-Oriented with function methods to handle the vector. Functions like SharedData.AddToSCStringArray() or SharedData.Clear() that calls the SCStringVector.clear() function and maybe assigns the other variables to 0. And if the vector is intended to be very large than you may want to declare it as a pointer so that the memory is on the heap.

std::vector<SCString>* SCStringVector = new std::vector<SCString>;

So a handful of little gotcha's depending on how complicated you want it to be. But single variables are simple and easy to work with.
Date Time Of Last Edit: 2018-03-01 20:37:31
[2018-03-01 21:53:21]
User553714 - Posts: 184
This is very helpful JW, thanks!
[2018-03-01 22:11:29]
Sierra Chart Engineering - Posts: 104368
There are these persistent variable functions:
sc.GetPersistentInt()
https://www.sierrachart.com/index.php?page=doc/ACSIL_Members_Functions.html#scGetStudyPersistentIntFromChart

But these only work within a Chartbook. Not across Chartbooks.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2018-03-01 22:12:39
[2018-03-01 23:49:06]
JoseyWales - Posts: 67
The static struct is instantiated (created in memory) when Sierra Chart loads your custom dll which happens if you click the Add Custom Study button on the Chart Studies dialog for the first time, or you load a chartbook that uses a study in the same dll. The struct has a lifetime until Sierra Chart unloads the dll when you close Sierra Chart.

Therefore it will work across chartbooks, but it will not have the same shared data with a different instance of Sierra Chart running because that would be in a different part of memory. You'd have to write/read data to a file or look into the concept of Named Pipes or even sockets if you wanted cross instance shared memory.

Keep in mind that all these methods do not store the data to disk, you can use sc.StorageBlock if you need to for that:
ACSIL Interface Members - Variables and Arrays: sc.StorageBlock
[2018-03-02 10:00:01]
User79074 - Posts: 105
JoseyWales, thank you very much for the very detailed response!! I will try the struct and see if I can get it to work.
[2018-03-02 10:33:16]
User79074 - Posts: 105
I have tested this and it works, so thanks again. The only thing though it only works when FreeDLL = 0, which makes development a bit cumbersome (having to restart Sierra to check changes after compilation). Any suggestion on how to ease this or a workaround?
[2018-03-02 11:34:37]
User79074 - Posts: 105
Actually, found a workaround. I can just copy the dll to a new file name and load that without restarting Sierra. A little extra work, but I scripted it, so its convenient. At this point I would say my concerns have been fully solved.
Date Time Of Last Edit: 2018-03-02 11:35:17

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

Login

Login Page - Create Account