Symbol Change Detection - using Static Variables

[ Surely it is a trivial question and has been solved by someone, however I cannot find it.
And my coding attempts are not successful, so far ]

What is the most efficient method for AFL formula of detecting change of Symbol Ticker (from previous one) while viewing charts ? Keeping the recently viewed symbol and comparing it with the current one.

Apart form general variables - like parameters values - my master formula reads plenty of symbol-specific Static Variables (linked with Name()) from other chart panes in various time frames. In result: StaticVarCount() value exceeds hundreds or thousands. After a dozen symbol changes the program crashes.

Removing symbol-specific Static Variables while changing symbol seems to be the way.
Setting up the variable in the end of formula (after perfoming all the calculations) with "old name"
and comparing it in the begining with current symbol name is probably the solution.
How should it be coded properly ?

Windstone, I don’t understand your question. What do you mean detecting change of Symbol Ticker (from previous one) ? Changing in price or changing a Symbol? Could you tell sth more about what do you use this solution for? Maybe an example or a code?

Hi, Thanks for answering so quickly.

The AFL formula is expected to reset all symbol-specific static variables while clicking on the new symbol in the list.

At start:
[ if( StaticVarGetText( “_Old_Name” ) != VarGetText( Name() ) ) // REMOVE PREVIOUS STATIC VARIABLES
StaticVarRemove( “_Old_Name*” );
RequestTimedRefresh( 1 ); ]

    and in the end of AFL

[ VarSetText( “_Old_Name”, _Name ); ]
The above code does not work.

If it worked then eliminating previous symbol static variable would be easy using StaticVarRemove( _Name* )

Well, I still don’t know why do you need such code :wink: but this should be working for you:

CurrentSymbol = Name();
PreviousSymbol = StaticVarGetText( "PreviousSymbol" );

if( CurrentSymbol != PreviousSymbol )
    StaticVarRemove( "YourStaticVariable" + Name() + "*" );
    Say( "New symbol", 1 );

StaticVarSetText( "PreviousSymbol", Name() );

Each time a Symbol is changed, all Static Variables beginning with “YourStaticVariable” + current Name() will be removed.

Hope it helps :slight_smile:


Thank you !
Simple and elegant solution.
It started reducing the number of static variables, it flips up and down …
Amibroker support is first class, as usual !

1 Like


I did a search about static variables to find out where they are stored/written. The reason why I’m wondering is that i have a lot of problems with my static variables after i started running two Amibroker at the same time (two different databses, one for USA and one for EU - all futures data). Put short, i make a static variable when i buy/short, and programs sends a cover order if there is a static variable when cover signal is triggered. However, after i started running two instances of AB, it is a mess where variables in one AB is found, but not in the other.

I’m sure there is something obvious im’m missing, but not sure what :slight_smile:

I see it is stored in PersistVars.bin, according to AFL Function Reference. But shouldn’t both AB read static variables from the same file? Why do i get a different list of static variables in the two instances of AB?

If file is open for writing it can’t be read at the same time by other process. If you have two processes one wanting to read the file that is open for writing the read attempt will fail. If you need to run two instances run them from different working directories so they don’t interfere.

Thanks for help, that was an easy solution. While i’m here i have another question: what is the easiest way to run different groups/markets/watchlist with different sessions as i have explained in the previous post? By sessions i mean when they open and close. For example stocks are from 0930 to 1600 in the US, but in Europe it is of course different times.