STK Analyzer Basic Macros Example

« Go Back

Information

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

Introduction

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. You can apply the principles in this example to any macro. These macros are written in VBScript, but you can use many different scripting languages. You can put macros into Analyzer by clicking the Macro button in the main Analyzer window.
User-added image

The macros

1. Init macro

In the Init macro, you need to create the counter variable that will keep track of the run number since the last save. This will be a variable that is visible as a parameter in the trade study you choose. To create variables that will show up as parameters in Analyzer, you must define the variable in the commented-out header of the Init macro. Here is the input required:

‘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, provide the following input:

app.setValue"Model.STK.unsavedRuns",0

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

This is all that you 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, provide the following input:

‘active=true

For the header, this is all that you need to do. You do not need to create any new variables within Analyzer, since you did that 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. Enter the following script in the onPostRun() sub, being sure to change the path of your scenario within the "if" statement:
 
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
app.setValue"Model.STK.unsavedRuns",i
 
' 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

               app.setValue"Model.STK.unsavedRuns",0
               i = 0
               stk.ExecuteCommand(command)
              
end if


The main functionality of this script is to keep count of the run that Analyzer is commencing. It does this by assigning the "unsavedRuns" variable’s value (this is the variable that you 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). If so, the script then executes the commands in the "if" statement that tell STK to save the scenario.

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 need to do with the End macro.

Conclusion

This example covers all of the steps needed to set up basic macros that directly monitor the run number and actively save STK scenarios. You can use this example to prevent memory overflow. By including this macro every time you run a trade study in Analyzer, you know the macro will save trigger a save of the STK scenario after the selected number of runs. This is just one of the many applications of macros that you can use to extend the capabilities of STK Analyzer!
 
TitleSTK Analyzer Basic Macros Example
URL NameSTK-Analyzer-Basic-Macros-Example

Related Files