summaryrefslogtreecommitdiffstats
path: root/openbb_terminal/SDK_README.md
blob: 72befab3361f5181cadfd379f227c54642f46cfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<!-- markdownlint-disable MD033 -->
# OpenBB SDK

OpenBB SDK gives you direct and programmatic access to all capabilities of the OpenBB Terminal.
You will have the necessary building blocks to create your own financial tools and applications,
whether that be a visualization dashboard or a custom report in a Jupyter Notebook.

With OpenBB SDK, you can access normalized financial data from dozens of data providers,
without having to develop your own integrations from scratch.
On top of financial data feeds, OpenBB SDK also provides you with a toolbox to perform financial analysis
on a variety of asset classes, including stocks, crypto, ETFs, funds; the economy as well as your portfolios.

OpenBB SDK is created and maintained by OpenBB team together with the contributions from hundreds of community members.
This gives us an unrivaled speed of development and the ability to maintain stable integrations with numerous third-party data providers.
Developing and maintaining an full-blown investment research infrastructure from the ground up takes a lot of time and effort.
However, it does not have to be. Take advantage of OpenBB SDK with its out-of-the-box data connectors and financial analysis toolkit.
So that you can focus on designing and building your financial reports and applications.

## SDK structure

The OpenBB SDK consists of the core package and extension toolkits.
The core package includes all necessary functionality for you to start researching or developing your dashboards and applications.

The toolkits that you can extend the OpenBB SDK with are:

- Portfolio Optimization Toolkit.
- Forecasting Toolkit.

## System and Platform Requirements

The SDK core package is expected to work in any officially supported python version 3.8 and higher (3.9 recommended).

Optimization and Forecasting toolkits installation requires specific settings on computers powered by Apple Silicon, the newer Windows ARM and Raspberry Pi.

### Minimal and Recommended System Requirements

- A computer with a modern CPU (released in the past 5 years)
- At least 8GB of RAM, 16+ recommended
- SSD drive with at least 12GB of storage space available
- Internet connection

**NOTES ON THE INTERNET CONNECTIVITY:** Installation of the SDK with all the toolkits would require downloading around 4GB of data.
Querying data does not require a lot of bandwidth but you will certainly have a more pleasant experience if you will be on a fast internet line. 4G networks provide a good enough experience so if you're traveling your personal hot-spot will do.
While it's technically possible to use a subset of the functionality in off-line mode, you will not be able to use any data that is queried from the APIs of data providers and services.

### Platform Specific Requirements

**Portfolio Optimization Toolkit and Forecasting Toolkit on Apple Silicon:** To install the Forecasting toolkit on M1/M2 macs you need to use the x86_64 version of conda  and install certain dependencies from conda-forge. Follow the [instructions in this section](https://github.com/OpenBB-finance/OpenBBTerminal/blob/main/openbb_terminal/README.md#1-install-miniconda)

**Base Linux Docker containers:** To have the package work in base linux containers like python's `slim-buster` you need to install a C anc C++ compiler that's not bundled with the distribution.
Run `sudo apt update && sudo apt install gcc cmake`

## Installation

We provide a simple installation method in order to utilize the OpenBB SDK. You must first create an environment,
which allows you to isolate the SDK from the rest of your system. It is our recommendation that you utilize a
`conda` environment because there are optional features, such as `forecast`, that utilize libraries that are
specifically sourced from `conda-forge`. Due to this, if you do not use a conda environment, you will not be
able to use some of these features. As such, the installation steps will be written under the assumption that
you are using conda.

### Steps

#### 1. **Install [Miniconda](https://docs.conda.io/en/latest/miniconda.html)**

Download the `x86_64` Miniconda for your respective system and follow along with it's installation instructions. The Miniconda architecture MUST be `x86_64` in order to use the forecasting toolkit. Follow the [instructions in this section](https://github.com/OpenBB-finance/OpenBBTerminal/blob/main/openbb_terminal/README.md#1-install-miniconda)

#### 2. **Create the virtual environment**

```bash
conda create -n obb python=3.9.6 -y
```

#### 3. **Activate the virtual environment**

```bash
conda activate obb
```

#### 4. **Install OpenBB SDK Core package**

```bash
pip install openbbterminal
```

#### 5. **(Optional) Install the Toolkits**

##### 5.1 **If you would like to use the Portfolio Optimization features**

On Apple Silicon Macs (M1/M2) install dependency from conda-forge

```bash
conda install -c conda-forge cvxpy=1.2.2 -y
```

And install the Portfolio Optimization Toolkit

```bash
pip install "openbbterminal[optimization]"
```

##### 5.2 **If you would like ML Forecasting features**

On Apple Silicon Macs (M1/M2) install dependency from conda-forge

```bash
conda install -c conda-forge lightgbm=3.3.3 -y
```

And install the Forecasting Toolkit

```bash
pip install "openbbterminal[prediction]"
```

##### 5.2 **If you would like to use both Portfolio Optimization and ML forecast features**

On Apple Silicon Macs (M1/M2) install dependencies from conda-forge

```bash
conda install -c conda-forge lightgbm=3.3.3 cvxpy=1.2.2 -y
```

And install the Both Toolkits

```bash
pip install "openbbterminal[all]"
```

Congratulations! You have successfully installed `openbbterminal` on an environment and are now able to begin using it. However, it is important to note that if you close out of your CLI you must re-activate your environment in order begin using it again.

## Setup

### 1. Import OpenBB SDK

First off, import OpenBB SDK into your python script or Jupyter Notebook with:

```python
from openbb_terminal.sdk import openbb
```

This imports all Terminal commands at once. To see all the available commands, you can press `tab` in jupyter notebook.
Another approach is to check out [OpenBB SDK Documentation](https://openbb-finance.github.io/OpenBBTerminal/sdk/), where you can explore its capabilities

### 2. Customize chart style

With OpenBB SDK, you can customize your chart style. You can switch between `dark` and `light` easily using this block of code:

```python
from openbb_terminal.sdk import TerminalStyle
theme = TerminalStyle("light", "light", "light")
```

<img width="813" alt="Screenshot 2022-10-03 at 23 56 52" src="https://user-images.githubusercontent.com/40023817/193700307-cbb12edc-0a5d-4804-9f3c-a798efd9e69d.png">

OR

```python
from openbb_terminal.sdk import TerminalStyle
theme = TerminalStyle("dark", "dark", "dark")
```

<img width="791" alt="Screenshot 2022-10-03 at 23 46 33" src="https://user-images.githubusercontent.com/40023817/193699221-e154995b-653c-40fd-8fc6-a3f8d39638db.png">

### 3. Access Documentation

Each and every command of OpenBB SDK has detailed documentation about input parameters and returned outputs. You can access them in multiple ways:

**Approach 1: Press `shift + tab`.**
This will work out of the box if you're using Jupyter Notebook. In case your IDE is VSCode, you will need to install the [Jupyter PowerToys
extension](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.vscode-jupyter-powertoys).

<img width="788" alt="Screenshot 2022-10-03 at 23 31 55" src="https://user-images.githubusercontent.com/40023817/193697567-e7143252-c560-441e-84fd-cbe38aeaf0ea.png">

**Approach 2: Type `help(command)`.**

You can also type `help(command)`, see example below, to see the command' docstring.

<img width="871" alt="Screenshot 2022-10-03 at 23 33 05" src="https://user-images.githubusercontent.com/40023817/193697676-39351008-386d-4c4c-89f2-3de7d8d4e89d.png">

**Approach 3: Use OpenBB SDK Documentation page.**

Finally, if you prefer to check documentation on a web browser, [OpenBB SDK Documentation](https://openbb-finance.github.io/OpenBBTerminal/sdk/) will be your best friend. You can browse available commands and search for any specific one that you need.

<img width="1200" alt="Screenshot 2022-10-03 at 18 41 48" src="https://user-images.githubusercontent.com/40023817/193643316-c063df03-4172-487f-ba47-ee60f36a3fef.png">

### 4. Set API Keys

You can set your external API keys through OpenBB SDK.

- Single API setup

```python
openbb.keys.fmp(key="example")

openbb.keys.reddit(
    client_id="example",
    client_secret="example",
    password="example",
    username="example",
    useragent="example")
```

![image](https://user-images.githubusercontent.com/79287829/194706829-dd720d06-9027-4da6-87f1-f39c7d2d725a.png)

- API key setup with persistence: `persist=True` means that your key will be saved and can be reused after, otherwise it will be lost when you restart the kernel.

```python
openbb.keys.fmp(key="example", persist=True)
```

![image](https://user-images.githubusercontent.com/79287829/194706848-80302ffa-6e75-4f7a-b8ce-788e083977d4.png)

- Set multiple keys from dictionary

```python
d = {
    "fed": {
        "key":"XXXXX"
        },
    "binance": {
        "key":"YYYYY",
        "secret":"example"
    },
}

openbb.keys.set_keys(d)
```

![image](https://user-images.githubusercontent.com/79287829/194706945-f1e6937f-74e2-4702-9e5e-c463287d61bd.png)

- Get info about API setup arguments

```python
openbb.keys.get_keys_info()
```

![image](https://user-images.githubusercontent.com/79287829/194706740-54bcc166-460a-410d-b34d-23e8b6c7aaf2.png)

- Get your defined keys

```python
openbb.keys.mykeys()
openbb.keys.mykeys(show=True)
```

![image](https://user-images.githubusercontent.com/79287829/194706907-239fe861-31c3-47c0-9051-7717cd026b76.png)

## Usage

Now, let's explore what OpenBB SDK can do. At a high level, you can break down OpenBB SDK's functionalities into two main buckets: (1) Data layer and (2) Toolbox layer.

### 1. Data Layer

### **Getting financial data from multiple data sources using one single SDK**

OpenBB SDK provides you access to normalized financial data from dozens of data sources, without having to built your own integration or relying on multiple third-party packages. Let's explore how we can do that.

First, you will need to load in the desired ticker. If it's not on the top of your mind, make use of our search functionality.

```python
openbb.stocks.search("apple")
```

<img width="652" alt="Screenshot 2022-10-04 at 00 00 14" src="https://user-images.githubusercontent.com/40023817/193700663-b91d57a9-4581-4f7e-a6da-764c0c9de092.png">

We want to load `Apple Inc.` listed on US exchange, so our ticker should be `AAPL`. If you want to load `Apple Inc.` from Brazilian exchange, you should load in `AAPL34.SA`.

```python
df = openbb.stocks.load("AAPL")
```

What's extremely powerful about OpenBB SDK is that you can specify the data source. Depending on the asset class, we have a list of available data sources and it's only getting bigger with contributions from our open-source community.

```python
## From YahooFinance
df_yf = openbb.stocks.load("AAPL", source='YahooFinance')

## From AlphaVantage
df_av = openbb.stocks.load("AAPL", source='AlphaVantage')

## From IEXCloud
df_iex = openbb.stocks.load("AAPL", source='IEXCloud')

## From Polygon
df_pol = openbb.stocks.load("AAPL", source='Polygon')
```

### **Easy option to switch between obtaining underlying data and charts**

Depending on your needs, you can get the outputs in form of data (e.g. `pandas dataframe`) or charts. If the latter is what you want, simple add `chart=True` as the last parameter.

### 1. Getting underlying data

```python
openbb.economy.index(indices = ['sp500', 'nyse_ny', 'russell1000'], start_date = '2010-01-01')
```

<img width="575" alt="Screenshot 2022-10-04 at 00 02 23" src="https://user-images.githubusercontent.com/40023817/193700891-f4d93440-31e3-411e-9931-3a38782f68e3.png">

You might be wondering how to find all the available indices. This type of information should be available in the docstring. Let's give it a try.

<img width="906" alt="Screenshot 2022-10-04 at 13 20 58" src="https://user-images.githubusercontent.com/40023817/193817866-b05cacee-a11b-4c44-b8c3-efb51bb9c892.png">

As mentioned in the docstring, you can access it with the following helper function.

```python
openbb.economy.available_indices()
```

<img width="1078" alt="Screenshot 2022-10-04 at 00 16 36" src="https://user-images.githubusercontent.com/40023817/193702595-ecbfc84d-3ed1-4f89-9086-e975b01c4b12.png">

### 2. Getting charts

```python
openbb.economy.index(indices = ['sp500', 'nyse_ny', 'russell1000'], start_date = '2010-01-01', chart=True)
```

<img width="741" alt="Screenshot 2022-10-04 at 00 03 57" src="https://user-images.githubusercontent.com/40023817/193701075-796ffabe-3266-4d71-9a81-3042e8ca5fc8.png">

## 2. Toolbox Layer

In addition to financial data, you can also get access to a robust and powerful toolbox to perform analysis on different asset classes and on your portfolio.

Imagine that you would like to leverage existing financial calculations from OpenBB and apply them on your own data. This can be done easily - OpenBB SDK's commands usually accept a `dataframe` as an input. Here you can load it your data, either via a `csv`, `excel` file, or connecting directly with an `API` or a `database`. The possibilities are endless.

Let's go through an example to see how we can do it in a few simple steps. Here we shall see how to use `portfolio optimization` functionalities from OpenBB SDK.

### Step 1. Loading order book

Here we will use an example orderbook for illustration purposes. You can choose to upload your own orderbook instead.

```python
order_book_path = "portfolio/allocation/60_40_Portfolio.xlsx"
tickers, categories = openbb.portfolio.po.load(excel_file = order_book_path)
```

### Step 2. Optimizing portfolio

We provide multiple portfolio optimization techniques. You can utilize basic mean-variance techniques, such as optimizing for the maximum Sharpe ratio, or minimum variance, as well as advanced optimization techniques including Hierarchical Risk Parity and Nested Clustered Optimization.

```python
## Max Sharpe optimization
weights_max_sharpe, data_returns_max_sharpe = openbb.portfolio.po.maxsharpe(tickers)

print("Max Sharpe")
weights_max_sharpe
```

<img width="734" alt="Screenshot 2022-10-04 at 13 23 45" src="https://user-images.githubusercontent.com/40023817/193818381-e3e75455-ea91-4bdd-a903-0874ac8700dc.png">

```python
## Minimum risk optimization
weights_min_risk, data_returns_min_risk = openbb.portfolio.po.minrisk(tickers)

print("Min Risk")
weights_min_risk
```

<img width="742" alt="Screenshot 2022-10-04 at 13 24 45" src="https://user-images.githubusercontent.com/40023817/193818556-89380c7c-94c3-4e5c-8848-28058c9cf056.png">

```python
## Hierarchical Risk Parity optimization

weights_hrp, data_returns_hrp = openbb.portfolio.po.hrp(tickers)

print("Hierarchical Risk Parity")
weights_hrp
```

<img width="736" alt="Screenshot 2022-10-04 at 13 34 39" src="https://user-images.githubusercontent.com/40023817/193820500-1bcde650-f517-4aed-b989-b2bd92bebbb8.png">

After having obtained the asset allocation outcomes, you can plot a correlation heatmap across tickers, as well as their individual risk contribution.

```python
openbb.portfolio.po.plot(data=data_returns_hrp,weights=weights_hrp,heat=True)
```

<img width="734" alt="Screenshot 2022-10-04 at 13 35 14" src="https://user-images.githubusercontent.com/40023817/193820624-3e6da926-aea9-4963-bd54-fd1a6df0fda3.png">

```python
openbb.portfolio.po.plot(data=data_returns_hrp,weights=weights_hrp,rc_chart=True)
```

<img width="737" alt="Screenshot 2022-10-04 at 13 36 10" src="https://user-images.githubusercontent.com/40023817/193820817-82f8727f-0