Skip to main content

Routines for Power Users

Short video on what power users can do with routines

Input Variables

When utilizing basic routines capabilities, users had to create separate routines for each specific ticker, such as my_due_diligence_AAPL.openbb or my_due_diligence_TSLA.openbb. This approach was suboptimal, considering that we had control over reading these scripts and they were meant to be used within our ecosystem.

To address this limitation, we introduced the concept of arguments, inspired by the Perl language. These arguments are variables referenced within the .openbb script as $ARGV or $ARGV[0], $ARGV[1], and so on. They are provided in the terminal when running exe by adding the --input flag, followed by the variables separated by commas.

For instance, if a routine file called script_with_input.openbb had the following format:

image

And we run it in the terminal with exe —file script_with_input.openbb —input MSFT, what would be run would be stocks/load MSFT --start 2015-01-01/ta/ema -l 20,50,100,200 and so you could use the same routine for multiple tickers - making it a more powerful automated workflow.

For instance, the example below shows how you can run the same script for MSFT but also TSLA ticker.

And we run it in the terminal with exe —file script_with_input.openbb —input MSFT, what would be run would be stocks/load MSFT --start 2015-01-01/ta/ema -l 20,50,100,200 and so you could use the same routine for multiple tickers - making it a more powerful automated workflow.

For instance, the example below shows how you can run the same script for MSFT but also TSLA ticker.

image

Example

Let's look into the following routine (the file can be downloaded here):

# This script requires you to use arguments. This can be done with the following:
# exe --file routines_template_with_inputs.openbb -i TSLA,AAPL,MSFT

# Go to the stocks menu
stocks

# Load a ticker, given the argument used. E.g. -i TSLA
load $ARGV[0]

# Enter the Technical Analysis (ta) menu
ta

# Show the fibonacci retracement levels
fib

# Enter the comparison analysis (ca) menu
../ca

# Set two extra tickers based on the arguments used. E.g. -i TSLA,AAPL,MSFT
add $ARGV[1],$ARGV[2]

# Plot the historical prices
historical

# Return to home
home

This script includes $ARGV[0], $ARGV[1] and $ARGV[2]. This means that the script requires you to submit three arguments. In this case, they refer to stock tickers. Therefore, like the script also says, you can include these arguments with -i or --input followed by three tickers (e.g. /exe routines_template_with_inputs.openbb -i TSLA,AAPL,MSFT). Resulting in the following,

OpenBB Script with Input

Note: Make sure you saved this script in the ~/OpenBBUserData/routines/ folder else you are not able to execute it.

Set Variables

In addition to enabling users to run scripts with external variables using the keyword ARGV, we also support the use of internal variables within the script. These variables are defined by starting with the $ character.

image

Which has the following output:

image

Note that the variable can have a single element or can be constituted by an array where elements are separated using a comma “,”.

Variables Example

Example of the script below:

# Set date variable
$DATE = 2022-01-01

# Set list of tickers to iterate
$TICKERS = AAPL,MSFT

# dive into stocks
stocks

# candle chart for first ticker
load $TICKERS[0] --start $DATE/candle

# candle chart for second ticker
load $TICKERS[1] --start $DATE[0]/candle

Note that a variable can be declared as a single argument $DATE = 2022-01-01 but it can also be declared as a list $TICKERS = AAPL,MSFT.

When declared as a list, the user needs to use the indexing to access the element of interest, i.e. if interested in MSFT then $TICKERS[1] should be used.

When a single element is defined, then the user can access it through the variable name or indexing the first position equally, i.e. $DATE = $DATE[0].

Note that slicing is also possible, and the same convention as python is utilized. If the user has defined inputs AAPL,MSFT,TSLA,NVDA,GOOG then by selecting $ARGV[1:3] the tickers MSFT,TSLA are selected.

Relative Time Keyword Variables

In addition to the powerful variables discussed earlier, OpenBB also supports the usage of relative keywords, particularly for working with dates. These relative keywords provide flexibility when specifying dates about the current day. There are four types of relative keywords:

  1. AGO: Denotes a time in the past relative to the present day. Valid examples include $365DAYSAGO, $12MONTHSAGO, $1YEARSAGO.

  2. FROMNOW: Denotes a time in the future relative to the present day. Valid examples include $365DAYSFROMNOW, $12MONTHSFROMNOW, $1YEARSFROMNOW.

  3. LAST: Refers to the last specific day of the week or month that has occurred. Valid examples include $LASTMONDAY, $LASTJUNE.

  4. NEXT: Refers to the next specific day of the week or month that will occur. Valid examples include $NEXTFRIDAY, $NEXTNOVEMBER.

The result will be a date with the conventional date associated with OpenBB, i.e. YYYY-MM-DD.

Relative Time Example

By picking on the previous example, we can add to the load --start argument the keyword $18MONTHSAGO.

image

This will result in the following output:

image

Foreach Loop

Finally, what scripting language would this be if there were no loops? For this, we were inspired by MatLab. The loops in OpenBB utilize the foreach and end convention, allowing for iteration through a list of variables or arguments to execute a sequence of commands.

To create a foreach loop, you need to follow these steps:

  1. Create the loop header using the syntax: foreach $$VAR in X where X represents either an argument or a list of variables. It's worth noting that you can choose alternative names for the $$VAR variable, as long as the $$ convention is maintained.

  2. Insert the commands you wish to repeat on the subsequent lines.

  3. Conclude the loop with the keyword end.

Loop Examples

# Iterates through ARGV elements from position 1 onwards
foreach $$VAR in $ARGV[1:]
load $$VAR --start $DATES[0] --end $DATES[1]/dps/psi/..
end
# Loops through all $ARGV variables
FOREACH $$SOMETHING in $ARGV
load $$SOMETHING --start $DATE[0]/ins/stats/..
end
# Iterates through ARGV elements in position 1,2
foreach $$VAR in $ARGV[1:3]
load $$VAR --start 2022-01-01
ba
regions
..
END
# Loops through PLTR and BB
foreach $$X in PLTR,BB
load $$X --start $LASTJANUARY
candle
END