|
| 1 | +--- |
| 2 | +title: Stock Report |
| 3 | +date: today |
| 4 | +date-format: long |
| 5 | +published-title: produced |
| 6 | +jupyter: python3 |
| 7 | +format: email |
| 8 | +attachments: |
| 9 | + - data.csv |
| 10 | +email-preview: true |
| 11 | +--- |
| 12 | + |
| 13 | +## Report for TSLA |
| 14 | + |
| 15 | +```{python} |
| 16 | +#| echo: true |
| 17 | +
|
| 18 | +import pandas as pd |
| 19 | +import yfinance as yf |
| 20 | +import datetime |
| 21 | +
|
| 22 | +tsla = yf.Ticker("TSLA") |
| 23 | +hist = tsla.history("5y") |
| 24 | +
|
| 25 | +hist['Change'] = hist['Close'] - hist['Open'] |
| 26 | +
|
| 27 | +latest = hist.last_valid_index() |
| 28 | +days_90 = latest - pd.to_timedelta("90d") |
| 29 | +
|
| 30 | +prices_1d = hist[latest:] |
| 31 | +prices_90d = hist[days_90:] |
| 32 | +prices_rolling = hist.asfreq('D').rolling(window=52*7, min_periods=1) |
| 33 | +
|
| 34 | +data=[ |
| 35 | + [ |
| 36 | + prices_1d['High'].values[0], |
| 37 | + prices_1d['Low'].values[0], |
| 38 | + prices_1d['Volume'].values[0].round(), |
| 39 | + ], |
| 40 | + [ |
| 41 | + prices_rolling.max()[latest:]['High'].values[0], |
| 42 | + prices_rolling.min()[latest:]['Low'].values[0], |
| 43 | + prices_rolling.mean()[latest:]['Volume'].values[0].round(), |
| 44 | + ] |
| 45 | +] |
| 46 | +
|
| 47 | +df = pd.DataFrame( |
| 48 | + data, |
| 49 | + columns=['High', 'Low', 'Avg Volume'], |
| 50 | + index=['Most Recent Trading Day', '52-Week']) |
| 51 | +df |
| 52 | +``` |
| 53 | + |
| 54 | +### Price History |
| 55 | + |
| 56 | +```{python} |
| 57 | +#| echo: false |
| 58 | +#| label: fig-history |
| 59 | +#| fig-cap: "TSLA price history" |
| 60 | +
|
| 61 | +_ = hist['Close'].plot(grid=True) |
| 62 | +``` |
| 63 | + |
| 64 | +## Raw Data |
| 65 | + |
| 66 | +```{python} |
| 67 | +#| echo: false |
| 68 | +
|
| 69 | +hist |
| 70 | +``` |
| 71 | + |
| 72 | +## Legacy Information |
| 73 | + |
| 74 | +This report also produces a CSV file containing recent price data, which may |
| 75 | +be used in other analysis. |
| 76 | + |
| 77 | +```{python} |
| 78 | +#| echo: false |
| 79 | +#| include: false |
| 80 | +
|
| 81 | +prices_90d.to_csv("data.csv") |
| 82 | +``` |
| 83 | + |
| 84 | +[Link to CSV](data.csv) |
| 85 | + |
| 86 | +## Email |
| 87 | + |
| 88 | +This report also produces an email that is sent to key stakeholders with summary |
| 89 | +information. |
| 90 | + |
| 91 | +::: {.email} |
| 92 | + |
| 93 | +::: {.subject} |
| 94 | +```{python} |
| 95 | +#| echo: false |
| 96 | +#| output: asis |
| 97 | +
|
| 98 | +closes = prices_90d['Close'] |
| 99 | +change = closes[-2] - closes[-1] |
| 100 | +abschange = abs(change) |
| 101 | +updown = "up" |
| 102 | +if change < 0: |
| 103 | + updown = "down" |
| 104 | +print(f"TSLA is {updown} today by ${abschange:.2f}") |
| 105 | +``` |
| 106 | +::: |
| 107 | + |
| 108 | +Hello Team, |
| 109 | + |
| 110 | +```{python} |
| 111 | +#| echo: false |
| 112 | +#| output: asis |
| 113 | +
|
| 114 | +print(f"Here are the latest stock prices for **TSLA** as of {datetime.date.today()}:") |
| 115 | +``` |
| 116 | + |
| 117 | +```{python} |
| 118 | +#| echo: false |
| 119 | +
|
| 120 | +prices_90d.head(5)[['Open', 'Close', 'Change', 'Volume']] |
| 121 | +``` |
| 122 | + |
| 123 | +The historical trend is shown below: |
| 124 | + |
| 125 | +```{python} |
| 126 | +#| echo: false |
| 127 | +
|
| 128 | +_ = prices_90d['Close'].plot(grid=True) |
| 129 | +``` |
| 130 | + |
| 131 | +Let me know if you have any questions. |
| 132 | + |
| 133 | +Best, |
| 134 | + |
| 135 | +Team Lead |
| 136 | + |
| 137 | +::: |
0 commit comments