STK Analyzer Basic Macros Example


QuestionHow do I use STK Analyzer Macros to set up an automatic save operation?


This article provides an example of how to use STK Analyzer Macros by showing you how to set up an automatic save operation that executes after a specified number of runs. The principles that are used in this example can be applied to any macro. These macros are written in VBScript but many different scripting languages can be used.

The Macros

1. Init Macro

In the Init macro, you need to create the counter variable that will be keeping track of the run number since the last save; this will be a variable that is visible as a parameter in the trade study that is chosen. To create variables that will show up as parameters in Analyzer, the variable must be defined in the commented-out header of the Init macro. Input:

‘variable: unsavedRuns double input
' active=true

This creates a variable called, "unsavedRuns", that is an input variable of the type, double. The second line tells Analyzer that this macro is going to be used. You now need to script within the actual onInit() sub to access and change this variable. Within the sub, input:


This sets the new value of the variable to 0, which you will then iterate to increase as the runs commence.

This is all that you will need to do with the Init macro; you have specified all of the variables that you will manipulate in the other macros.

2. PostRun Macro

Since, in this example, you want to manipulate STK after the runs, you will use the PostRun macro.

User-added image

As you did with the Init macro, input:


For the header, this is all that you need to do. You do not need to create any new variables within Analyzer since that was done in the Init macro.

User-added image

The rest of the script is the code that keeps track of the run number and sends a command to STK to save when the number reaches the specified value. Input the following script in the onPostRun() sub:
dim stk
dim scen

' Change this value to change number of iterations Analyzer goes through before saving
runSaveVal = 5
'gets run number and sets values for next iteration
i = app.getValue("Model.STK.unsavedRuns")
i = i + 1
' checks run number to save after i number of runs since last saved
if i = runSaveVal then
               command = "Save / * "  & Chr(34) & "YourScenarioPath" & Chr(34)
               set stk = app.getComponent("Model.STK").userData

               i = 0
end if

The main functionality of this script is to keep count of the run that is being commenced. It does this by assigning the "unsavedRuns" variable’s value (this is the variable that was created in the Init macro) and assigning it to the variable, "i". The "i" variable then increases its amount by 1 and pushes back to the Analyzer variable. The "if" statement then evaluates whether the run number matches the limit (in this case, 5) and, if so, the script then executes the commands in the "if" statement, which are used to save STK!

3. End Macro

The last thing to do is to set up a clean-up function for when Analyzer shuts down.

User-added image

In the End macro, write the following code in the header of the macro:

' active=true
' preserveVariables = false

This is all that you will need to do with the End macro.


This example covers all of the steps needed to set up basic macros that directly monitor the run number and actively save STK, which can be used to prevent memory overflow. This is just one of the many applications of macros that can be used to extend the capabilities of STK Analyzer!
Resource File 1 
Resource File 2