Issue with retrieving static variable (Keeps changing frequently)


I am having some trouble getting static variables from a different function to a main dashboard function. While the variables are being referenced correctly and values are retrieved, I notice that the values keep refreshing themselves and show some dummy values at times. I'm sure there is something I am missing here but after spending quite some time on it, I havent been able to figure it out.

Requirement :
There are 5 strategies running in different charts. Each of them are storing some info into static variables.
I am trying to collate all the information in a single chart as a dashboard.

I am jotting down part of the code from one of the strategies and how I am trying to reference it into the main dashboard.

Strategy Code:

(The entire code is using loops where a few variables have been defined and generated)

// Some code here that defines the static variables
// These variables get updated every 5-15min depending on the type of strategy

//Towards the end of the loop, static variables are declared


When I run this program above and look at the variables generated (individually), all show up well without issues (No frequent changing)

Next, I use the above variables into a dashboard function

cestrike_BNSOI = StaticVarGet("BNSOICEStrike");
pestrike_BNSOI = StaticVarGet("BNSOIPEStrike");
ce_units_BNSOI = StaticVarGet("BNSOICEUnits");
pe_units_BNSOI = StaticVarGet("BNSOIPEUnits");

//Some Gfx functions
// Printing the static variable information into the GUI
GfxTextOut("CE Stk :"+StrFormat("%.0f",cestrike_BNSOI),4/100*width,39/100*height);
GfxTextOut("PE Stk :"+StrFormat("%.0f",pestrike_BNSOI),4/100*width,42/100*height);
GfxTextOut("CE Un :"+StrFormat("%.3f",ce_units_BNSOI),4/100*width,45/100*height);
GfxTextOut("PE Un:"+StrFormat("%.3f",pe_units_BNSOI),4/100*width,48/100*height);

The static variables hence declared keep flickering every few seconds.

Can you please help / guide as to how I can avoid it or help me naildown the issue further. Let me know if a video of the flickering will help or any other output that I can provide.

Thanks and Regards
SIddhant Kankani

Why are you using StaticVarSet in a BarCount loop for setting a numeric variable (not an array) ?
(It should be done only once outside the loop regardless of array or number).

and if you need a number that has value i-1, then why loop through all the bars to just set that value ?
To me it just looks like badly written code so you can share the whole snippet and members can help you.

Hi @nsm51

This is not a numeric value. Apologies should have provided more information.

Each of these are arrays that can change with every bar and hence are in a loop. The reason [i-1] is used is because the previous value needs to be stored and shared through the static variable.

The entire snippet is too large, hence posting a similar code.

Closing = Foreign("BANKNIFTY-I.NFO",Close);
ce_strike[i] = Closing[i];
// This is an over simplified version of what I am doing - for this version a loop is not required but the //idea is to show if there is an error in using the static variable. 



Also, by changing the StaticVarSet outside the loop, it doesnt make a difference:

//Tried declaring this outside the loop

The issue persists.

Another thing I notice is:

To retrieve the static variable, if I use "Nz":

cestrike_BNDSS = Nz(StaticVarGet("BNDSSCEStrike"));

The value flickers between the correct value and "0". So i'm assuming for some reason the static variable goes into a NULL value at some point of time ?


// AND
//Tried declaring this outside the loop

Both lines are not the same thing.
So it is not clear if you want to share a single value or an entire array using SV.

If the formula that is setting the variables is slow because of your code, then use local variables throughout. Only at the end, create/update the Static var so the Dashboard may access proper last updated values.

This type of declaration can also help and 6-27-1-beta link at the end.

Thanks @nsm51

Will try using the static keyword and let you know it goes.

Just out of curiosity, why are the two lines not the same thing? Aren't both referring to the previous array value of the variable.

Siddhant Kankani

[] specifically refers to an arrays index and will be a single numeric value while Ref() can shift entire array depending on whats passed to it. So you want to pass an array to Ref and expect an array to be returned.

Understood, thanks. .

So if I am looking for the n-1 value of the array, I should be looking at using



Thanks again,
Siddhant Kankani

yes right or even

 lastv = variable[ BarCount - 2 ];
// -1 for 0 based, and another -1 for 2nd last ( n-1)

So I think the fluctuation - like you pointed out - could be due to the time taken for the entire formula to run.

The formula has multiple calculations in terms of Options Greeks and hence the inherent time it takes to execute it completely.

So I'm assuming during each run the variable resets itself and is re-assigned ?
Can there be a way not to re-init and keep the last assignment till the next assignment request comes in? Hope my query is clear :slight_smile:

P.S. The fluctuation happens only when I access the StaticVariable from another chart / function. Within the same chart / function there is no fluctuation of values.

Siddhant Kankani

So thats what i wrote in earlier post.
Just use local variables throughout. Dont set/unset at the beginning. Only at the end of the formula, set the static vars so all other formulas referring to them will always access the correct last calculated value.

Thanks mate.. @nsm51
Your suggestion to move static variables out of the loop helped resolve the issue.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.