Stock Extraction

In this package, we provide functionalities to extract the stock data. and user is able to retrieve the data from 2 different engines. The module fetches the stock data from yahoo finance or investing.com.

User is able to fetch multiple stocks at once, calculate various technical indicators and create advance plots. This example is going to demonstrate the method of fetching the data.

Resouces link:

import numpy as np
import pandas as pd
from time_series_transform.stock_transform import *
from time_series_transform.plot import *
from time_series_transform.io import *
from time_series_transform import (Time_Series_Transformer,Stock_Transformer)

Stock Transformer

We can simply fetch the data by using the Stock_Transformer, and user can specify the engine they would like to use.

In the investing engine, user need to specify the country as a parameter.

Extraction Method

  1. from_stock_engine_date

  2. from_stock_engine_period

  3. from_stock_engine_intraday

investing engine doesn’t provide the intraday data

In the following, we demonstrate 3 different methods to extract the data, user is able to change the engine to either yahoo or investing.

stock_data = Stock_Transformer.from_stock_engine_date("aapl", "2020-02-01", "2020-12-10", "investing", country = "united states")
port_data = Stock_Transformer.from_stock_engine_period(["aapl", "msft", "amzn"], "1y" , "yahoo")
intra_data = Stock_Transformer.from_stock_engine_intraday("amzn", "2020-12-01", "2020-12-10", "yahoo", interval = "5m")

print("stock_data: ")
print(to_pandas(stock_data.time_series_data, None, False, None).head())
print("===============================")
print("port_data (amzn data): ")
print(to_pandas(port_data.time_series_data['amzn'], None, False, None).head())
print("===============================")
print("intra_data: ")
print(to_pandas(intra_data.time_series_data, None, False, None).head())
print("===============================")
C:UsersAllen Chianganaconda3libsite-packagespandascoreframe.py:1485: FutureWarning: Using short name for 'orient' is deprecated. Only the options: ('dict', list, 'series', 'split', 'records', 'index') will be used in a future version. Use one of the above to silence this warning.
  warnings.warn(
stock_data:
         Date   Open   High    Low  Close     Volume Currency
0  2020-02-03  76.08  78.37  75.56  77.17  173985600      USD
1  2020-02-04  78.83  79.91  78.41  79.71  136616544      USD
2  2020-02-05  80.88  81.19  79.74  80.36  118826872      USD
3  2020-02-06  80.64  81.31  80.07  81.30  105425536      USD
4  2020-02-07  80.59  80.85  79.50  80.01  117684048      USD
===============================
port_data (amzn data):
         Date         Open         High          Low        Close   Volume  0  2020-01-06  1860.000000  1903.689941  1860.000000  1902.880005  4061800
1  2020-01-07  1904.500000  1913.890015  1892.040039  1906.859985  4044900
2  2020-01-08  1898.040039  1911.000000  1886.439941  1891.969971  3508000
3  2020-01-09  1909.890015  1917.819946  1895.800049  1901.050049  3167300
4  2020-01-10  1905.369995  1906.939941  1880.000000  1883.160034  2853700

   Dividends  Stock Splits
0          0             0
1          0             0
2          0             0
3          0             0
4          0             0
===============================
intra_data:
              Datetime         Open         High          Low        Close  0  2020-11-30 16:00:00  3147.530029  3153.000000  3143.055176  3152.020020
1  2020-11-30 16:05:00  3152.274902  3154.577393  3148.370117  3153.360107
2  2020-11-30 16:10:00  3152.070068  3153.949951  3150.100098  3150.370117
3  2020-11-30 16:15:00  3150.070068  3156.979980  3148.000000  3150.929932
4  2020-11-30 16:20:00  3150.446533  3156.379883  3149.679932  3150.750000

   Volume  Dividends  Stock Splits
0       0          0             0
1   38406          0             0
2   38418          0             0
3   50079          0             0
4   26603          0             0
===============================

Technical Indicator

Stock_Transformer provides the function of get_technial_indicator. Combining with the pandas_ta module, we are able to calculate the technical indicator in a few lines of code.

import pandas_ta as ta

MyStrategy = ta.Strategy(
    name="DCSMA10",
    ta=[
        {"kind": "macd"},
        {"kind": "ema", "length": 10},
        {"kind": "bbands", "length": 20, "col_names": ("BBL", "BBM", "BBU")},
    ]
)

stock_data = stock_data.get_technial_indicator(MyStrategy)
print(stock_data.to_pandas()[25:30])
          Date   Open   High    Low  Close     Volume Currency  MACD_12_26_9  25  2020-03-10  69.28  71.61  67.34  71.33  285290080      USD     -3.682278
26  2020-03-11  69.35  70.31  67.97  68.86  256379872      USD     -3.711825
27  2020-03-12  63.98  67.50  62.00  62.06  418474080      USD     -4.235124
28  2020-03-13  66.22  69.98  63.24  69.49  370732128      USD     -4.004146
29  2020-03-16  60.49  64.77  60.00  60.55  322423456      USD     -4.490712

    MACDh_12_26_9  MACDs_12_26_9     EMA_10        BBL      BBM        BBU
25      -0.372633      -3.309645  72.193387  65.614075  75.2845  84.954925
26      -0.321744      -3.390081  71.587317  64.912182  74.7325  84.552818
27      -0.676034      -3.559090  69.855077  62.992357  73.7455  84.498643
28      -0.356045      -3.648101  69.788700  62.852085  73.1590  83.465915
29      -0.674088      -3.816623  68.108936  61.104089  72.1245  83.144911

Plot

In this example, we are going to create the candle plot.

For more advance manipulation on the plot module, please refer to the Plot Gallery example.

stock_data.plot