- How calculation works?
- The formula editor
- User defined functions
The purpose of the Macrobond formula language is to make calculations on series. The series are typically time series, and the result of a formula is a time series. By using formulas, you can combine arithmetic operations with built in functions to create powerful calculations. You can find a complete list of built-in functions here.
Formulas can be utilized in both the Series list and Formula analysis. The method of writing formulas is identical between the two except for how references to series are written. The instructions below are targeted at making calculations in the Series list. Thus, when referencing to a series, the name of the series will be used and not an alias. To learn more about aliases and the differences between using the Series list and the Formula analysis for calculations, click here.
You can use the name of a series from the database in formulas. A very simple example is:
Which references to the Macrobond series called 'sek'.
Examples of numerical constants are:
123 -123 123.456 123.456e-3
An example of a simple formula is the following:
which calculates the cross rate between the Swedish and the Norwegian krona.
The time series sek and nok are treated as vectors of values. In a vector, each value can either be a number or a null value.
Before any calculations are made, all series in the formulas are converted to the same frequency. The series also need to be the same length. This is achieved by adding null values to the beginning of the vectors until they are of equal length.
The division operator / takes two series and produces a new series by dividing the corresponding values in the two series. For most calculations, the result is null when any of the values is null.
This can be illustrated using a fictional example:
The operators, such as +, - and * handles null in the same way.
In addition to the arithmetic operators there are also functions. A function can take zero or more parameters. Parameters are written within parentheses and separated with commas.
An example of a formula using a function is the following:
This will calculate the sum of all values in the series. With the same fictional values, the result would look like this:
Another example is the following formula:
The result is a new series where each value is multiplied by 100. There is no function multiplying a series with a constant. Instead, the formula interpreter will create a series where each value is 100 and then multiply these series:
In most cases you do not need to know about the conversion of constants to series, but it can help you to understand what happens with a formula like this:
If you have this formula together with the series 'sek', the result would look like this:
You get the value 600 because the constant 100 is converted to a series with six values of 100 and then this series is summed.
There are functions in Macrobond with the same name that takes different number of parameters. Many of these functions takes a parameter that specifies the length of a window. With them you can create rolling series.
|Sum(sek)||This is the sum of all values in the series.|
|Sum(sek, 3)||This is the sum of three observations including the current observation and the two previous ones.|
If there are not enough data points to fill the window, the values will be null. This is depicted in the table below.
Another useful example of a function that takes a window length is Mean(series, window). The result of this function is also known as the moving average.
Each position in the series has an ordinal number called an observation number. You can get a series of the ordinal numbers with the function Counter().
If it is a time series, each ordinal number is associated with a date.
Please note that you should never make any assumption about what the ordinal numbers are. Thus, you should not use an expression like At(sek, 0) to get the first value of a series. The ordinal number of the first observation can be any number. Instead, you should use FirstValid(sek), which returns the ordinal number of the first observation that is not null.
There are a number of functions related to dates and ordinal numbers. A useful function is Date(year, month, day) that returns the ordinal number for a specific date.
With the function At(series, observation) you can get the value at a specific observation number.
|Observation number||sek||At(sek, Date(1971,1,8))|
The logical values in a series are represented by 1 for True and 0 for False.
In the following example the operator > (greater than) is used to compare a series with a lagged series. The result will be True (1) when the value has increased since the last observation.
|Observation number||sek||Lag(sek, 1)||sek > Lag(sek, 1)|
The function If is commonly used with logical expressions. The function takes three parameters. The first parameter is interpreted as a logical expression that determines if the result should be the value of the second or the third parameter.
The following example replaces any null values (missing values) with the highest value of the last ten observations
If (IsNull(sek),Hi(sek, 10), sek)
You can write comments in formulas by typing two slashes ' // '. Everything after the slashes to the end of the line, will be regarded as a comment and not part of the formula expression.
All operators work on constants and series. Here are the operators sorted in order of precedence. Parentheses can be used to further control the order of evaluation.
|>=||Greater than or equal||3|
|<=||Less than or equal||3|
The Formula editor helps you write formulas. In the Series list, you can bring up the Formula editor by clicking on the 'fx' button to the right of an expression, or by pressing Ctrl+E on the keyboard.
Some of the useful features of the editor are:
- You can double click on a function in the list to the right to insert a function.
- You can filter the list of functions by typing a text in the box above the list of functions. Only functions that contain the text in the function name or description will be included in the list.
- The tooltip of a function in the list or in an expression will show details about the function:
- The tooltip of a series will show the title of the series:
- When the text cursor is just before a left parenthesis or directly after a right parenthesis , a blue shade on the background will show the area between the matching parentheses:
- When the text cursor is within or next to an identifier, all instances of that identifier in the expression are shaded:
- The editor will check the syntax as you write and show a red squiggle if there is an error. You can see the error text at the bottom of the editor or as a tooltip:
- Numerical constants are dark red. Series, parameters and variables are blue. Comments are green.
- The series used in the document will be displayed at the end of list of functions. The tooltip will tell you the title of the series. You can double click to enter them into the formula.
- Auto-suggestions for series name. When you start typing a series name in the Formula editor, suggestions will be shown if the text you type matches the start of a series in the document or any of the series you have marked as favorite. Use the mouse or press the down arrow to walk the list. The tooltip will give you the series title. Press 'Enter' to select.
User defined functions are a way to structure formula expressions and to reuse commonly used expressions. It allows you to both reduce the amount of formulas you write and save the functions for future use. For more information see: User defined formulas