File size: 1,801 Bytes
e18d63b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import datetime
from pydantic import BaseModel, Field
from typing import Dict, List, Optional
import yfinance as yf
import plotly.express as px
from workcell.integrations.types import PlotlyPlot


class Input(BaseModel):
    tickers: List[str] = Field(
        default=['AAPL','AMZN','META'], max_items=10, description="List of ticker values"
    )


def load_data(tickers):
    """Download ticker price data from ticker list.
    e.g. tickers = ['AAPL','AMZN','GOOG']
    """
    start = datetime.datetime(2022, 1, 1)
    end = datetime.datetime.now() # latest
    data = yf.download(tickers, start=start, end=end, interval='1d')
    # adjust close
    close = data['Adj Close']
    return close


def visualization(df):
    """Visualization price plot by yfinance dataframe.
    e.g. df = yf.download(***)
    """
    template = 'plotly_white'
    fig = px.line(df, x=df.index, y=df.columns.tolist(), template=template)
    fig.update_xaxes(
        rangeslider_visible=True,
        rangeselector=dict(
            buttons=list([
                dict(count=1, label="1m", step="month", stepmode="backward"),
                dict(count=6, label="6m", step="month", stepmode="backward"),
                dict(count=1, label="YTD", step="year", stepmode="todate"),
                dict(count=1, label="1y", step="year", stepmode="backward"),
                dict(step="all")
            ])
        )    
    )
    fig.update_layout(hovermode="x")    
    return fig


def stock_viewer(input: Input) -> PlotlyPlot:
    """Input ticker list, returns multiple stocks price. Data frome yfinance."""
    # Step1. load data
    dataframe = load_data(input.tickers)
    # Step2. create plot
    fig = visualization(dataframe)
    # Step3. wrapped by output
    output = PlotlyPlot(data=fig)
    return output