Sometimes it is important to restrict the execution of your strategy, analysis technique, or TradingApp to just one instance. One way of achieving this is by leveraging the functionality found in the EasyLangauge GlobalDictionary. The GlobalDictionary is an EasyLanguage object that can be accessed from any analysis technique while the TradeStation desktop platform is running. All you need to do is to create a GlobalDictionary with a name and specify that you want to share it. From this point forward, any analysis technique that creates a shared dictionary with the same name will have access to all of the values that are stored within. So how does this help?
Let’s say that you only want the first instance of a strategy to calculate. By creating a GlobalDictionary, you can let other instances know that your strategy is already calculating, thus restricting them from executing their code. For example:
Let’s create a dictionary in the AnalysisTechnique_Initialize event:
Let’s skip the uniqueID assignment and jump into the creation of the dictionary (I’ll talk about the need for that later). In this method we’ve created a GlobalDictionary object named “SAMPLE_GDC_STRATEGY_RESTRICT”. This object was created as a shared object across any windows type (by specifying true in the first parameter) so that we can access it from any analysis technique in any window in the platform.
If we are the first to create the dictionary (by checking the number of Keys created) we can assume that we should allow this strategy to calculate (by setting the canCalculate flag to TRUE). If not, we can display a dialog in the window that is attempting to host this strategy and let the user know that he already has an instance running.
All you need to do to ensure that the strategy is not calculating is to wrap your logic with a conditional statement that checks the canCalculate flag before calculating.
Now, create two charts and apply the strategy to both; it should look like this:
Back to the uniqueID…generating a uniqueID allows your strategy to keep track of how many strategy instances are open at any given time. You will want to make sure to manage this number by removing each unique key when the strategy is uninitialized. Removing the uniqueID when the strategy is uninitialized will reduce the number of keys in the dictionary and bring the count back to zero, which will allow the next new instance to calculate properly.
Visit https://github.com/frankts/tradestation-easylanguage-gdc-strategy-restrict to download the source and try it out in the TradeStation Desktop Platform.