Login Page - Create Account

Support Board


Date/Time: Thu, 25 Apr 2024 20:17:25 +0000



[Programming Help] - Referencing to Persistent Variable by name NOT working *URGENT*

View Count: 2358

[2016-02-22 09:26:18]
User972044 - Posts: 154
Hello there,

I am having a lot of trouble working with Persistent variables. I have read all the documentation regarding the method of creating persistent variables and setting and getting values from the persistent variables via sc.SetPersistentInt() and sc.GetPersistentInt() and also int& variablename = -> sc.PersistVars->Integers[0] using intergers for examples if I use a specific variable name via "int& variable_name". The problem is I want to give a default value to a persistent value the FIRST time around ONLY and for the subsequent times, I want the persistent variable to be updated by other values as dictated by my program. So how do I do that or is there anyway to do that? Right now, either I am not being allowed to give a default value to the persistent value to begin with if I use the statement "int& variable_name = sc.PersistVars -> Integers[0];" or "int& variable_name = sc.GetPersistentInt(0);", whenever I reference variable_name, I will get a random number that is set by Sierra Chart which is not useful at all to me. But if I set a default value for the persistent variable via "sc.SetPersistentInt(0,0);" for example to set the default value to 0, every single time when the study runs, it keeps resetting the variable value to 0 and every time when I reference it by the variable_name after int&, the value is not accurate. It doesn't work if I put the sc.SetPersistentInt in the sc.SetDefaults block, it just gets ignored. I use Autoloop = 1 so maybe I need to use some kind of index to determine when my study is fist loaded onto the chart? Which index value should I use and how do I know or get that index value?

To illustrate: Right now, when I write my codes as follows:

int& variable_name = sc.GetPersistentInt(0);

sc.SetPersistentInt(0,0);

both of these above statements outside of the sc.SetDefaults() block and then

if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{

  variable_name += 1;

}

I only get the result for the variable_name remaining at the value of 1 when it's supposed to incremented with the closing of each bar and the reason why it's not being incremented is because it gets reset back to 0 by the sc.SetPersistentInt(0,0) statement every time when the program runs with the update of each bar. And I don't want that. I only want sc.SetPersistentInt() statement run the FIRST time when the program is loaded onto the chart and that's it. Each bar after, it the variable_name should be updated by the increment statement. How do I do that please?

Thank you for your help in advance.
Date Time Of Last Edit: 2016-02-22 09:41:42
[2016-02-22 10:00:31]
Sierra Chart Engineering - Posts: 104368

int& variable_name = sc.GetPersistentInt(1);

if(sc.Index==0)
variable_name=0;

if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{

variable_name += 1;

}

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: 2016-02-22 10:00:57
[2016-02-22 10:06:37]
User972044 - Posts: 154
I already tried if(sc.Index = 0){} several times with SetPersistentInt() or setting the variable_name = 0; Neither methods work. It just gives me a different random number, 87514 this time instead of 0. I want to set the variable_name to 0 the first time around and it just does NOT. When I don't use the SetPersistentInt() to set the number and just the GetPersistentInt(), it gives a random number 127XXX and when I use the if(sc.Index == 0) condition, it just gives me a random number starting 87XXX instead of setting the variable_name to a value that I want.

There is something seriously wrong with Persistent variable pointers it seems either that or the index value is not being aligned.

Thanks
Date Time Of Last Edit: 2016-02-22 10:23:43
[2016-02-22 10:58:29]
User972044 - Posts: 154
And furthermore, your documentation about another method of using persistent variables via the sc.PersistVars->... function in http://www.sierrachart.com/index.php?page=doc/doc_ACSIL_Members_Variables_And_Arrays.html

even states that "Initial Value: 0 (for all the persistent variables in the structure)" and "All persistent variables are set to zero values when an instance of the study is added to the chart or when a chartbook is loaded and an instance of the study is contained on a chart in the chartbook." So if (sc.Index == 0) is not even needed. And that's even more INACCURATE.

I used int& MyCounter = sc.PersistVars->Integers[0]; EXACTLY as how it is stated in the documentation but when I run the program, this is the value that I got from the first time when the study was run:

Chart: GBPUSD [M] 1 Min #1 | Study: Custom DLL Study | MyCounter Value is 175113 | 2016-02-22 05:47:59

When my code is MyCounter += 1; within the if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED){} block. So according to my code, the value of MyCounter should be 1 the first time when the study is run and NOT 175113.

And even though the documentation states that "Initial Value: 0 (for all the persistent variables in the structure)" and "All persistent variables are set to zero values when an instance of the study is added to the chart or when a chartbook is loaded and an instance of the study is contained on a chart in the chartbook.", the documentation still provided the resetting persistent variable example at (if sc.Index == 0){ sc.PersistVars -> Integers[0] = 0; } I used that example and I received the same random value as previously when I used the SetPersistentInt() method or variable_name = 0; statement as follows, 87XXX:

Chart: GBPUSD [M] 1 Min #1 | Study: Custom DLL Study | MyCounter Value is 87573 | 2016-02-22 06:03:58

So WHY would there be two different explanations regarding how the initial value of persistent variables to be set and WHY is neither of the pointer reference working?

Please advise and help.

Thank you.
Date Time Of Last Edit: 2016-02-22 11:08:47
[2016-02-22 11:20:11]
User972044 - Posts: 154
And it does NOT even work when I don't use the variable_name like MyCounter to reference the persistent variables. When I used sc.PersistVars->Integers[0] to directly manipulate the persistent variables, it gave me the same gibberish random number starting at 175XXX and it was NEVER initially set to 0.

So it is not even a reference pointer issue. Somehow the persistent variable is just NOT set to zero as stated in the documentation. And when you try to set to 0 in if (sc.Index == 0){} block it doesn't even work. Please explain and rectify if necessary.


Thank you.
Date Time Of Last Edit: 2016-02-22 11:24:03
[2016-02-22 17:04:53]
Sierra Chart Engineering - Posts: 104368
We do not provide programming help.

Refer to our policy here:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

Use step-by-step debugging so you can see what your study function is doing:
https://www.sierrachart.com/index.php?page=doc/doc_DebuggingDLLs.html

There is definitely nothing wrong with persistent variables. The results that you see are based upon what your study function is doing and how ACSIL works.

This page is now marked as a User Discussion.
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
[2016-02-22 17:07:15]
Sierra Chart Engineering - Posts: 104368

When my code is MyCounter += 1; within the if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED){} block. So according to my code, the value of MyCounter should be 1 the first time when the study is run and NOT 175113.

This is because the study is using automatic looping:
https://www.sierrachart.com/index.php?page=doc/doc_ACS_ArraysAndLooping.html
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
[2016-02-23 00:07:08]
User972044 - Posts: 154
Ok can you explain WHY would setting auto looping make a difference in setting the initial value for persistent variable? Your documentation on auto loop does not explain this and I actually read this when I first started ASCII programming; it just discusses how manual looping is but it doesn't show how it would impact persistent variable value setting or any other aspects of how the study is run. It would help if you can show what's the difference in value of sc.Index and sc.ArraySize and sc.UpdateStartIndex and sc.BaseData[] when autoloop is set 1 and 0 for example since it actually does make a big impact on how the study is run.

At least it should be stated in the documentation for Persist Variables in both places either by SetPersistent...() or PersistVars->... methods that autoloop must be 0 for initial value of persistent variables to be set and the passage that "Initial Value: 0 (for all the persistent variables in the structure)" and "All persistent variables are set to zero values when an instance of the study is added to the chart or when a chartbook is loaded and an instance of the study is contained on a chart in the chartbook." should be completely erased from the documentation for PersistVars->. It is totally misleading and not correct.

It was extremely frustrating having spent several hours trying to make something work when at the end the reason was it wasn't properly documented in the first place. And I was a little offended that Sierra Chart Support to just dismiss me for not debugging my code after all my efforts. From the several posts that I have posted before it was obvious I went through a series of tests and debugging efforts on my own to try to make it work. I understand you are busy and I appreciate your efforts in helping me but I really prefer to receive correct answers to my issue. I don't mind waiting as long as it is not TOO long.

Thank you for your help.
Date Time Of Last Edit: 2016-02-23 01:55:41
[2016-02-23 03:26:35]
Sierra Chart Engineering - Posts: 104368
Ok can you explain WHY would setting auto looping make a difference in setting the initial value for persistent variable? Your documentation on auto loop does not explain this and I actually read this when I first started ASCII programming;
The documentation does not need to explain specifically this because this is something already clear with what you are doing and the explanations already provided.

It would help if you can show what's the difference in value of sc.Index and sc.ArraySize and sc.UpdateStartIndex and sc.BaseData[] when autoloop is set 1 and 0 for example since it actually does make a big impact on how the study is run.
This is documented here:
https://www.sierrachart.com/index.php?page=doc/doc_ACS_ArraysAndLooping.html

https://www.sierrachart.com/index.php?page=doc/doc_ACSIL_Members_Variables_And_Arrays.html#scIndex
The other three variables mentioned in the quote above are documented on this same page.


the passage that "Initial Value: 0 (for all the persistent variables in the structure)" and "All persistent variables are set to zero values when an instance of the study is added to the chart or when a chartbook is loaded and an instance of the study is contained on a chart in the chartbook." should be completely erased from the documentation for PersistVars->. It is totally misleading and not correct.
What you are saying here is not correct. They are initialized to zero under those conditions.


It was extremely frustrating having spent several hours trying to make something work when at the end the reason was it wasn't properly documented in the first place. And for Sierra Support to just dismiss me not debugging my code after all my efforts, I find that a bit offensive.

Programming is not easy. And step-by-step debugging and analysis of the code is absolutely essential. The documentation is correct and quite thorough. You need to determine why you are facing the issue that you are. Why is it that you are getting the result that you are from your persistent variable? This is what you need to determine.

We have not given you any incorrect answers. And if something we said is not correct and we are not aware of that and we doubt that, it would only be because of the lack of understanding of what you are doing. This is one of many reasons why we do not provide programming help and why you should not rely upon us for programming help.
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: 2016-02-23 03:27:31
[2016-02-23 03:33:55]
Sierra Chart Engineering - Posts: 104368
Regarding our answer in post #7, we are not trying to imply that you should not be using automatic looping.

You should be but you just need to understand how it works and why your incrementing of the variable is getting run so many times initially.
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
[2016-02-23 14:35:10]
User972044 - Posts: 154
You misunderstood what I was trying to say. What I am saying is you need to specify in the documentation for persistent variables that it is ESSENTIAL for autoloop to be set to 0 for 1) initial value of all persistent variables to start off as 0 and 2) persistent value to be incremented to an initial value of an user's choice only ONCE in the if (sc.Index = 0){} block and not over and over and over again. Of course one can still have autoloop be equal to 1 and have persistent variables work properly which is what I am still doing now once I found out everything but like I said for everything that you described in the documentation in persistent variables to work, you need to specify that autoloop needs to be set to 0. This is what confused me and made me have the run-around for several hours for nothing and I am sure this will be equally confusing and cause difficulties for other users who choose to use persistent variables in their codes. This is just my suggestion.

Programming is not easy; I understand that but it shouldn't be because of something that was not documented. From my many posts that described the issues, it's clear that I already went through several hours of analysis and debugging of my codes and nothing worked. I did EVERYTHING according to the documentation, I had set the initial value in "if (sc.Index == ){}" block and I constructed the persistent variables exactly the same way as documented but it was really because the documentation, like I pointed out, failed to specify that autoloop needs to be set to 0 for what I want to do to work, none of what I did worked. So this is WHY I don't consider your answer in Post #6 of telling me to go debug my codes more to be correct answers to my question which was NOT seeking for programming help; I REALLY thought there was something genuinely wrong with Sierra Charts. In the future, I and I think majority of your users would prefer to receive answers like in Post #7 above which was very helpful and finally resolved my issue. We don't mind waiting if it's going to take you some time to arrive at answers like in Post #7 as long as it's not TOO long.

And at the same time if you had specified what you stated in post #7 in the documentation for persistent variables to begin with, you wouldn't even have seen this forum post from me in the first place. Your documentation is detailed and thorough but it is NOT infallible and my suggestion to include the specification that autoloop = 0 in the persistent variable documentation is like I said before, for your future users and customers and in essence for Sierra Charts, NOT for me. I already went through the ordeal and understood everything.

Thank you for your help.
Date Time Of Last Edit: 2016-02-23 15:21:11
[2016-02-23 18:12:15]
Sierra Chart Engineering - Posts: 104368
for persistent variables that it is ESSENTIAL for autoloop to be set to 0

This is not true, and the documentation for persistent variables would not discuss the effect of auto looping on the code that a user writes. It cannot possibly foresee or understand what a user is doing with persistent variables. How automatic looping works, is documented here:
https://www.sierrachart.com/index.php?page=doc/doc_ACS_ArraysAndLooping.html
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: 2016-02-23 18:12:44
[2016-02-23 18:49:19]
User972044 - Posts: 154
You again misunderstood what I am saying. I am NOT asking you to discuss what the effect of autoloop is on the code that a user writes. You wouldn't even know what kind of code an user would write so WHY would I ask you to write the effect of autoloop on codes that you won't know? The documentation for autolooping is fine but I just feel that if it can be clarified further in terms of how it should be set for persistent variables, it would be better.

What I AM suggesting is for you to clarify what YOU yourself wrote in the documentation for Persistent Variables will ONLY work if autoloop is = 0. For Persistent Variables to have initial value of 0 AND initial value to be set ONCE and ONCE only by user, the autoloop needs to be 0. You and I both know I am correct. You just simply do NOT wish for whatever reason that I cannot and do not wish to fathom to clarify autoloop needs to be 0 for initial value of persist variables to be 0 and persist variable values to be reset successfully ONCE in your documentation, that's YOUR choice. Like I said, I am only suggesting this because I believe this will be useful and beneficial for other users and Sierra Charts, that's all. The choice is yours.

Thank you for your help.
[2016-02-23 20:07:46]
Sierra Chart Engineering - Posts: 104368
For Persistent Variables to have initial value of 0 AND initial value to be set ONCE and ONCE only by user, the autoloop needs to be 0.

This is not correct.

The initial value is always zero in all conditions but it can be changed by your code.

Using manual looping will not cause a user set initial value to be set once. It can be set many times.


When sc.Index equals 0, that is a logical place to reset them back to a default value and should be done whether automatic or manual looping is being done.
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
[2016-02-23 22:18:26]
User972044 - Posts: 154
No this is NOT correct. When autoloop is set to 1, whatever value for persistent variables that you reset in if (sc.Index == 0) block does NOT get reset AND the initial value for persistent variables does NOT start at 0. I just proved it to you in my post Post #4 that when I first ran my program, the initial value for my persistent variable MyCounter which was set up EXACTLY as in your documentation was NOT zero; it was 175XXX as shown in the log which I included in my post. And furthermore, when I tried to initialize them to zero in if(sc.Index == 0){} block, it DID not get reset to zero either, it was set to another number 87XXX as shown in the log which I included in my post Post #4. And this is WHY I was very puzzled why it didn't work.

Did you even read my posts? I would ask you please go back to all my posts that I posted after your post Post #2 and read ALL of them. You will see those are all of my debugging efforts and showing you how the program was not behaving as you described in your documentations. It was only in Post #7 which was posted by you which pointed out to me finally that autoloop needs to be set to 0 for what the documentation described to work and that finally resolved the issue. If you don't believe me, go test it myself. First set autoloop = 1 to see what's the initial value of the persistent variables and whether resetting their values in the if (sc.Index == 0) block works and then set autoloop = 0 to see if it works. And you will see what I am talking about and whoever posted Post #7 was talking about and why I was suggesting your documentation for Persistent Variables would benefit from further clarification.

It's so ironic that you accuse me of not debugging or analyzing my codes and yet you are not even aware how the codes behave in your own platform and you haven't even tested them yourself.

Thanks for your help.
Date Time Of Last Edit: 2016-02-23 22:21:21
[2016-02-23 22:33:59]
Sierra Chart Engineering - Posts: 104368
This is not correct:

When autoloop is set to 1, whatever value for persistent variables that you reset in if (sc.Index == 0) block does NOT get reset AND the initial value for persistent variables does NOT start at 0.


And we did confirm that the initial value of persistent variables is 0 under the conditions described in the documentation. It would be very unlikely there would be a problem with that. And there is not.

There still is a lack of understanding of automatic looping.

Once again we refer you to the documentation here:
https://www.sierrachart.com/index.php?page=doc/doc_ACS_ArraysAndLooping.html#autolooping
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
[2016-02-23 22:36:46]
Sierra Chart Engineering - Posts: 104368
Be sure to refer to the prior post.

Regarding:
when I tried to initialize them to zero in if(sc.Index == 0){} block, it DID not get reset to zero either, it was set to another number 87XXX as shown in the log which I included in my post Post #4.
This is because there are about 87000 bars in the chart at the time you got this value.
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: 2016-02-23 22:38:06
[2016-02-23 22:53:03]
User972044 - Posts: 154
Ok I did further testing, the initial value now starts at 0 and the value does get reset when it's placed in the if (sc.Index == 0){} block even when autoloop = 1. I just did it with a chart with a smaller amount of bars and I do see it now but somehow when I had a chart with high number of bars the day before yesterday which is what I need to load my trading program on, I actually need to use autoloop = 0 otherwise the initial value is incremented to a ridiculously high number by the time the code is loaded.

Anyway thanks for your help.
Date Time Of Last Edit: 2016-02-23 23:38:33
[2016-02-23 23:21:54]
Sierra Chart Engineering - Posts: 104368
The persistent variables are held in a STL map which is part of a Study Graph object which is instantiated when a study is added to the chart or a Chartbook is opened which contains a study. Each study is a separate Study Graph object.

According to the C++ standard, arithmetic types like integers and floats are initialized to 0. This has been confirmed according to the standard and actual testing. This is something you can verify yourself when you check the initial value of the persistent variable with step-by-step debugging .

With this understanding explain where the 87XXX value comes from?

So you have the ability to determine exactly where this value comes from. It would be worth your while to make that determination. There is no reason to let us know. Because it is rather clear to us.
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: 2016-02-23 23:25:46
[2016-02-23 23:44:57]
User972044 - Posts: 154
Refer to my previous post. I already verified this. When autoloop = 1, I couldn't see the initial value of zero for persistent variable nor the resetting of their value in the sc.Index == 0 block because my chart contained too many bars. For charts with large number of bars, it is more efficient to have autoloop set to 0. It's just funny this autoloop setting never affected me this much for the indicators that I wrote. All my custom indicator studies ran fine when autoloop = 1. Only in my auto trading programs that autoloop setting makes a difference.

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

Login

Login Page - Create Account