Time Series Data Plot¶
Introduction¶
In this example, we are introducing our plot engine for the time series data and stock data. We are able to create a general plot like scatter plot, bar chart, line chart and more graphs based on the input. To be more specific, this package is also able to create the candle plot in one line of code.
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)
To extract the stock data, we can use the from_stock_engine_period and from_stock_engine_date from the Stock_Transformer class.
symbols: string of the symbol or a list of the symbols
start_date/end_date: string of the date in the format of %Y-%m-%d, eg. “2020-02-20” (used in from_stock_engine_date)
period: string of the period, eg. “1y” (used in from_stock_engine_period)
engine: string of the engine, valid input: yahoo, investing
In this example, we will create 3 types of data for demonstration.
ts_data: Time_Series_Data, which is a general time series data
stock_data: Stock data which contains data of a single stock
port_data: Portfolio data which contains a list of Stock data
data = pd.DataFrame({'time':[1,2,3,4,5,6,7,8,9,10],'x1':[1,2,3,4,5,6,7,8,9,10],'x2':[6,7,8,9,10,16,27,38,49,50]})
ts_data = Time_Series_Transformer(data, 'time')
stock_data = Stock_Transformer.from_stock_engine_date("aapl", "2020-02-01", "2020-12-10", "yahoo")
port_data = Stock_Transformer.from_stock_engine_period(["aapl", "msft", "amzn"], "1y" , "yahoo")
print("ts_data: ")
print(to_pandas(ts_data.time_series_data,None,False,None).head())
print("===============================")
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("===============================")
ts_data: time x1 x2 0 1 1 6 1 2 2 7 2 3 3 8 3 4 4 9 4 5 5 10 =============================== stock_data: Date Open High Low Close Volume 0 2020-01-31 79.545540 79.979294 76.412600 76.714989 199588400 1 2020-02-03 75.423639 77.701469 74.908095 76.504311 173985600 2 2020-02-04 78.152574 79.225810 77.736172 79.029999 136616400 3 2020-02-05 80.187502 80.494853 79.054790 79.674438 118826800 4 2020-02-06 79.952039 80.608865 79.379484 80.606384 105425600 Dividends Stock Splits 0 0.0 0.0 1 0.0 0.0 2 0.0 0.0 3 0.0 0.0 4 0.0 0.0 =============================== 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 ===============================
Time_Series_Plot¶
This package provides the plot function for the time series data. We can simply create the plot using the create_plot function, or simply call the .plot from the transformer, or just use .plot.line.
Functions¶
The Plot module has several functions to support figure with more details.
Trace data¶
add_line
remove_line
add_marker
Layout¶
update_layout
remove_subplot
User is able to add new subplot by changing the value of subplot in the add_line function
tsp = ts_data.plot(cols=['x1','x2'], title = 'sample title', type = 'scatter')
tsp
tsp.add_line(col = "x2", lineType="bar")
tsp
markerDf = ts_data.to_pandas()
markerDf['x3'] = 50-ts_data.time_series_data.data['x2']
tsp.add_marker(
x = markerDf['time'],
y = markerDf['x3'],
color = '#8815EB',
legendName = 'inverse_x2'
)
tsp
Stock Plot¶
Next, we are going to demonstrate how the candle plot works.
First, we can take a look at the general candle plot with one stock, stock_data.
Simply call the .plot from the transformer we created previously.
fig = stock_data.plot
fig
Now we can add a horizontal line of the mean of Close data throughout the period with the customized data
time_series_data = stock_data.time_series_data
horiLine = np.ones(len(time_series_data.time_index['Date'])) * np.mean(time_series_data.data['Close'])
fig.add_line(data = horiLine, lineType = 'scatter', legendName = 'mean', color = '#4358F1')
fig
Combine with the get_technial_indicator from the Stock_Transformer, we can create more advanced graph.
With the pandas_ta package, we are able to create various technical indicator. In this example, we are going to add the SMA_10, Bollinger Bands, and MACD in a different subplot.
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().head())
Date Open High Low Close Volume 0 2020-01-31 79.545540 79.979294 76.412600 76.714989 199588400 1 2020-02-03 75.423639 77.701469 74.908095 76.504311 173985600 2 2020-02-04 78.152574 79.225810 77.736172 79.029999 136616400 3 2020-02-05 80.187502 80.494853 79.054790 79.674438 118826800 4 2020-02-06 79.952039 80.608865 79.379484 80.606384 105425600 Dividends Stock Splits MACD_12_26_9 MACDh_12_26_9 MACDs_12_26_9 0 0.0 0.0 NaN NaN NaN 1 0.0 0.0 NaN NaN NaN 2 0.0 0.0 NaN NaN NaN 3 0.0 0.0 NaN NaN NaN 4 0.0 0.0 NaN NaN NaN EMA_10 BBL BBM BBU 0 NaN NaN NaN NaN 1 NaN NaN NaN NaN 2 NaN NaN NaN NaN 3 NaN NaN NaN NaN 4 NaN NaN NaN NaN
# Bollinger Band
fig.add_line(col = 'BBL', lineType = 'scatter', color = '#F2E64C')
fig.add_line(col = 'BBM', lineType = 'scatter', color = '#F2E64C')
fig.add_line(col = 'BBU', lineType = 'scatter', color = '#F2E64C')
# EMA_10
fig.add_line(col = 'EMA_10', lineType = 'scatter')
# MACD in a different subplot
fig.add_line(col = 'MACD_12_26_9', lineType = 'scatter', subplot = 'y3')
fig.add_line(col = 'MACDh_12_26_9', lineType = 'scatter', subplot = 'y3')
fig.add_line(col = 'MACDs_12_26_9', lineType = 'scatter', subplot = 'y3')
fig.show()
Next, we can move on to the Portfolio plot, which contains multiple layers of plots.
port_fig = port_data.plot
port_fig