{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[*********************100%***********************]  1 of 1 completed\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 101960 characters]\n",
      "text/plain": [
       "<Figure size 1512x1008 with 6 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import yfinance as yf\n",
    "from tqdm import tqdm\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def stockreturn(prices,shift=1):\n",
    "    stock_returns = (prices / prices.shift(shift)) - 1\n",
    "    stock_returns =  stock_returns\n",
    "    stock_returns.columns = [f'{shift} days Return']\n",
    "    return stock_returns*100\n",
    "\n",
    "\n",
    "ticker = \"SAF.PA\"\n",
    "period = \"5y\"\n",
    "\n",
    "ohlc = yf.download(ticker, period=period)\n",
    "prices = ohlc[\"Adj Close\"].to_frame()\n",
    "\n",
    "fig, axs = plt.subplots(2, 3, figsize=(3*7, 2*7))\n",
    "for ax, shift in zip(axs.flat, [1, 5, 20, 50, 100, 250]):\n",
    "    try:\n",
    "        x = stockreturn(prices, shift=shift)\n",
    "        sns.histplot(x, kde=True, ax = ax, bins=30) #binwidth=10)\n",
    "        ax.axvline(np.nanmean(x.values), color=\"red\")\n",
    "        ax.set_xlabel(\"% return\")\n",
    "        ax.axvline(np.nanmean(x.values) - np.nanstd(x.values), color=\"green\")\n",
    "        ax.axvline(np.nanmean(x.values) + np.nanstd(x.values), color=\"green\")\n",
    "    except ValueError:\n",
    "        pass\n",
    "plt.show()\n",
    "prices[\"SMA_10\"] = prices[\"Adj Close\"].rolling(window=10).mean()\n",
    "prices[\"SMA_20\"] = prices[\"Adj Close\"].rolling(window=20).mean()\n",
    "prices[\"SMA_50\"] = prices[\"Adj Close\"].rolling(window=50).mean()\n",
    "prices[\"SMA_100\"] = prices[\"Adj Close\"].rolling(window=100).mean()\n",
    "prices[\"SMA_250\"] = prices[\"Adj Close\"].rolling(window=250).mean()\n",
    "prices['EMA_50'] = prices[\"Adj Close\"].ewm(span=50,adjust=False).mean()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 172216 characters]\n",
      "text/plain": [
       "<Figure size 1512x1008 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(3*7, 2*7))\n",
    "plt.plot(prices.index, prices[\"Adj Close\"], label=\"Price\")\n",
    "#plt.plot(prices.index, prices[\"SMA_10\"], label=\"SMA_10\")\n",
    "plt.plot(prices.index, prices[\"SMA_20\"], label=\"SMA_20\")\n",
    "plt.plot(prices.index, prices[\"SMA_50\"], label=\"SMA_50\")\n",
    "plt.plot(prices.index, prices[\"EMA_50\"], label=\"EMA_50\")\n",
    "#plt.plot(prices.index, prices[\"SMA_100\"], label=\"SMA_100\")\n",
    "#plt.plot(prices.index, prices[\"SMA_250\"])\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 59232 characters]\n",
      "text/plain": [
       "<Figure size 1512x504 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(3*7, 7))\n",
    "#plt.plot(prices.index, prices[\"SMA_10\"])\n",
    "plt.plot(prices.index, prices[\"SMA_20\"], label=\"SMA_20\")\n",
    "plt.plot(prices.index, prices[\"SMA_50\"], label=\"SMA_50\")\n",
    "#plt.plot(prices.index, prices[\"SMA_100\"])\n",
    "#plt.plot(prices.index, prices[\"SMA_250\"])\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
