Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query multiple PSE symbols with parallelization #342

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
378 changes: 378 additions & 0 deletions examples/get_pse_data_multiple_demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,378 @@
{
ajdajd marked this conversation as resolved.
Show resolved Hide resolved
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
},
"colab": {
"name": "get_pse_data_multiple_demo.ipynb",
"provenance": [],
"toc_visible": true
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "h-tZ3KhfPQW6"
},
"source": [
"*This notebook was run with Google Colab.*"
]
},
{
"cell_type": "code",
"metadata": {
"id": "FcVD1bJrL_RG"
},
"source": [
"from fastquant import get_pse_data, get_pse_data_multiple\r\n",
"from datetime import datetime"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "obCQra9qL_RL"
},
"source": [
"# Define variables"
]
},
{
"cell_type": "code",
"metadata": {
"id": "vqnwvVoAL_RL"
},
"source": [
"# Define a list of stock symbols\n",
"psei = [\n",
" \"AEV\",\n",
" \"AP\",\n",
" \"AGI\",\n",
" \"AC\",\n",
" \"ALI\",\n",
" \"BPI\",\n",
" \"BDO\",\n",
" \"BLOOM\",\n",
" \"DMC\",\n",
" \"EMP\",\n",
" \"FGEN\",\n",
" \"GLO\",\n",
" \"GTCAP\",\n",
" \"ICT\",\n",
" \"JGS\",\n",
" \"JFC\",\n",
" \"LTG\",\n",
" \"MER\",\n",
" \"MEG\",\n",
" \"MPI\",\n",
" \"MBT\",\n",
" \"TEL\",\n",
" \"PGOLD\",\n",
" \"RLC\",\n",
" \"RRHI\",\n",
" \"SMC\",\n",
" \"SECB\",\n",
" \"SM\",\n",
" \"SMPH\",\n",
" \"URC\",\n",
"]\n",
"\n",
"# Define start and stop dates\n",
"start_date = \"2021-01-01\"\n",
"end_date = datetime.utcnow().strftime(\"%Y-%m-%d\")"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "nqLGsSenL_RM"
},
"source": [
"# Method 1: Without parallelization"
]
},
{
"cell_type": "code",
"metadata": {
"id": "AOLREYbCL_RM",
"outputId": "c27f80a7-2ff2-4bd4-89f9-c5925bb3898b",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"source": [
"%%time\n",
"\n",
"for symbol in psei:\n",
" get_pse_data(symbol, start_date, end_date)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"193it [00:24, 7.72it/s]\n",
"193it [00:24, 8.54it/s]\n",
"193it [00:25, 8.02it/s]\n",
"193it [00:26, 8.21it/s]\n",
"193it [00:25, 8.83it/s]\n",
"193it [00:25, 8.30it/s]\n",
"193it [00:24, 8.73it/s]\n",
"193it [00:26, 8.22it/s]\n",
"193it [00:24, 5.93it/s]\n",
"193it [00:25, 6.94it/s]\n",
"193it [00:29, 9.29it/s]\n",
"193it [00:26, 8.26it/s]\n",
"193it [00:26, 8.53it/s]\n",
"193it [00:26, 7.34it/s]\n",
"193it [00:26, 7.40it/s]\n",
"193it [00:26, 6.06it/s]\n",
"193it [00:27, 9.16it/s]\n",
"193it [00:23, 8.38it/s]\n",
"193it [00:24, 9.32it/s]\n",
"193it [00:24, 7.80it/s]\n",
"193it [00:25, 7.72it/s]\n",
"193it [00:31, 6.21it/s]\n",
"193it [00:25, 9.26it/s]\n",
"193it [00:24, 7.66it/s]\n",
"193it [00:25, 6.85it/s]\n",
"193it [00:25, 7.69it/s]\n",
"193it [00:26, 6.93it/s]\n",
"193it [00:23, 9.43it/s]\n",
"193it [00:26, 8.46it/s]\n",
"193it [00:27, 8.13it/s]"
],
"name": "stderr"
},
{
"output_type": "stream",
"text": [
"CPU times: user 49 s, sys: 4.33 s, total: 53.3 s\n",
"Wall time: 13min 14s\n"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
"\n"
],
"name": "stderr"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3_nL-bnRL_RN"
},
"source": [
"# Method 2: With parallelization"
]
},
{
"cell_type": "code",
"metadata": {
"id": "WtCjazDsL_RN",
"outputId": "2dfbca92-ec17-411c-a15d-c64ddc5b45a4",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"source": [
"%%time\n",
"\n",
"data = get_pse_data_multiple(\n",
" psei, n_jobs=-1, start_date=start_date, end_date=end_date\n",
")"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"[Parallel(n_jobs=-1)]: Using backend LokyBackend with 2 concurrent workers.\n"
],
"name": "stderr"
},
{
"output_type": "stream",
"text": [
"CPU times: user 110 ms, sys: 44.4 ms, total: 155 ms\n",
"Wall time: 8min 34s\n"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
"[Parallel(n_jobs=-1)]: Done 30 out of 30 | elapsed: 8.6min finished\n"
],
"name": "stderr"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iDkkY5qSL_RO"
},
"source": [
"## Accessing individual stock data"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ciFsQ4J1L_RO",
"outputId": "20f4b574-697a-47c7-af2a-f89322941d3a",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 235
}
},
"source": [
"data[\"JFC\"].head()"
],
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>open</th>\n",
" <th>high</th>\n",
" <th>low</th>\n",
" <th>close</th>\n",
" <th>value</th>\n",
" <th>volume</th>\n",
" </tr>\n",
" <tr>\n",
" <th>dt</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2021-01-04</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>194.4</td>\n",
" <td>NaN</td>\n",
" <td>625440.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2021-01-05</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>194.9</td>\n",
" <td>NaN</td>\n",
" <td>513300.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2021-01-06</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>194.4</td>\n",
" <td>NaN</td>\n",
" <td>565340.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2021-01-07</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>192.6</td>\n",
" <td>NaN</td>\n",
" <td>802590.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2021-01-08</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>192.5</td>\n",
" <td>NaN</td>\n",
" <td>720470.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" open high low close value volume\n",
"dt \n",
"2021-01-04 NaN NaN NaN 194.4 NaN 625440.0\n",
"2021-01-05 NaN NaN NaN 194.9 NaN 513300.0\n",
"2021-01-06 NaN NaN NaN 194.4 NaN 565340.0\n",
"2021-01-07 NaN NaN NaN 192.6 NaN 802590.0\n",
"2021-01-08 NaN NaN NaN 192.5 NaN 720470.0"
]
},
"metadata": {
"tags": []
},
"execution_count": 5
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "9LT0bBAJPEVN"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
1 change: 1 addition & 0 deletions python/fastquant/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
get_stock_table,
# Combines get_phisix_data and get_pse_data_cache
get_pse_data,
get_pse_data_multiple,
# Gets data from PHISIX
get_phisix_data,
# Gets data from PSE Data Cache
Expand Down
Loading