diff --git a/brent_predictor.ipynb b/brent_predictor.ipynb new file mode 100644 index 0000000..37d14da --- /dev/null +++ b/brent_predictor.ipynb @@ -0,0 +1,816 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyN71igwPAy2MDioDzo5fpta", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "source": [ + "from google.colab import drive\n", + "drive.mount('/content/drive')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6kDDYN6n7q5q", + "outputId": "31d91381-08c7-4b1b-b826-b20590e05774" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "import sys\n", + "sys.path.append('/content/drive/MyDrive/my_packages')\n", + "import api_alphavantage as av" + ], + "metadata": { + "id": "l-VV33HX_7zw" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "UyqYP65d0og3" + }, + "outputs": [], + "source": [ + "import requests\n", + "import json\n", + "import pandas as pd\n", + "\n", + "def alphavantage_get( symbol,dfrom, dto,interval,api_key):\n", + " \n", + " '''retrieves time series of prices for function in range dfrom to dto at interval with api_key'''\n", + " \n", + " interval = interval\n", + " dfrom=dfrom\n", + " dto=dto\n", + " api_key=api_key\n", + " \n", + " url = f'https://www.alphavantage.co/query?function={symbol}&interval={interval}&apikey={api_key}&outputsize=full'\n", + " \n", + " r = requests.get(url)\n", + " data = r.json()\n", + " \n", + " df = pd.DataFrame(data)\n", + " \n", + " df['date'] = df['data'].apply(lambda x: x['date'])\n", + " df['value'] = df['data'].apply(lambda x: x['value'])\n", + "\n", + " df.drop('data', axis=1, inplace=True)\n", + " df['date'] = pd.to_datetime(df['date'])\n", + " df.set_index('date', inplace=True)\n", + "\n", + " # filter data to specified time period\n", + " df = df[(df.index >= dfrom) & (df.index <= dto)]\n", + "\n", + " #error message suggested that there is a string '.' ; ->locate and remove string\n", + " df = df[df['value'] != '.']\n", + " \n", + " df.value = df['value'].astype(float)\n", + " \n", + " return df\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "source": [ + "def moving_avs(dataframe):\n", + " \n", + " df=dataframe\n", + " \n", + " # make sure type is correct\n", + " df['value'] = df.value.astype(float)\n", + " \n", + " # calculate weekly moving average\n", + " weekly_ma = df['value'].rolling('7D').mean()\n", + " df['weekly_ma'] = weekly_ma\n", + "\n", + " # calculate quaterly moving average\n", + " yearly_ma = df['value'].rolling('365D').mean()\n", + " df['yearly_ma'] = yearly_ma\n", + " \n", + " return df" + ], + "metadata": { + "id": "R03eX0NTB65e" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#plot dataframe\n", + "import plotly.express as px\n", + "import plotly.graph_objs as go\n", + "import pandas as pd\n", + "\n", + "\n", + "def plotter(dataframe,symbol, moving=0,amount='', footnote=''):\n", + " \n", + " df=dataframe\n", + " \n", + " # plot the data\n", + " \n", + " \n", + " \n", + " if moving == 1:\n", + "\n", + " # create a new DataFrame with the moving averages\n", + "\n", + " df_ma = pd.concat([df['value'], df.weekly_ma, df.yearly_ma], axis=1)\n", + "\n", + " # Create the plot\n", + " fig = go.Figure()\n", + " fig.add_trace(go.Scatter(x=df_ma.index, y=df_ma['weekly_ma'], name='Weekly Moving Average', opacity=0.3))\n", + " fig.add_trace(go.Scatter(x=df_ma.index, y=df_ma['yearly_ma'], name='Yearly Moving Average'))\n", + "\n", + " # title and axis labels\n", + " fig.update_layout(title=symbol, xaxis_title='Date', yaxis_title=f'in {amount} (USD)', \n", + " legend_title='Moving Average', plot_bgcolor=' #cce6ff',paper_bgcolor=' #e6f2ff')\n", + "\n", + " # footnote\n", + " fig.add_annotation(xref='paper', yref='paper', x=-0.1, y=-0.17, text=footnote, showarrow=False, font=dict(\n", + " family='Arial',\n", + " size=9,\n", + " \n", + " ))\n", + "\n", + " \n", + " fig.show()\n", + " \n", + " else: \n", + "\n", + " fig = go.Figure() \n", + " fig.add_trace(go.Scatter(x=dataframe.index, y=dataframe.value, name='Global GDP'))\n", + " fig.update_layout(title=symbol, xaxis_title='Date', yaxis_title=f'in {amount} (USD)', \n", + " legend_title='symbol', plot_bgcolor=' #cce6ff',paper_bgcolor=' #e6f2ff')\n", + " fig.add_annotation(xref='paper', yref='paper', x=-0.1, y=-0.17, text=footnote, showarrow=False, font=dict(\n", + " family='Arial',\n", + " size=9,\n", + " \n", + " ))\n", + " fig.show()\n" + ], + "metadata": { + "id": "FBM5IVlG4rCI" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#alphavantage_get entries\n", + "#call alphavantage_get(symbol,dfrom,dto, intervel, api_key)\n", + "\n", + "#api key\n", + "api_key = av\n", + "\n", + "#symbol\n", + "symbols=['BRENT','ALL_COMMODITIES']\n", + "\n", + "# daily interval\n", + "interval = 'daily' \n", + "\n", + "#time period\n", + "start_date = '2005-01-01'\n", + "end_date = '2015-12-31'\n" + ], + "metadata": { + "id": "FZauzajf0xh6" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# get all_commodidities from alphavantage\n", + "comod = alphavantage_get(symbols[1], start_date, end_date, interval, api_key)\n", + "comod = moving_avs(comod)\n", + "amount=''\n", + "footnote='Source: International Monetary Fund (IMF Terms of Use), Global Price Index of All Commodities, retrieved from FRED, Federal Reserve Bank of St. Louiss; data accessed through AlphaVantage API.'\n", + "plotter(comod,'GLOBAL COMMODITIES',1,amount,footnote)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 542 + }, + "id": "YxTxPgmR4u35", + "outputId": "cbd6e1b1-57c8-46e8-8567-277dd3ff90b4" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "
\n", + "
\n", + "\n", + "" + ] + }, + "metadata": {} + } + ] + }, + { + "cell_type": "code", + "source": [ + "#get brent data from alphavantage and plot\n", + "brent = alphavantage_get(symbols[0], start_date, end_date, interval, api_key)\n", + "brent = moving_avs(brent)\n", + "amount=''\n", + "footnote='Source: U.S. Energy Information Administration, Crude Oil Prices: Brent - Europe, retrieved from FRED, Federal Reserve Bank of St. Louis; Alpha Vantage API, accessed on 2023-04-05.'\n", + "plotter(brent,'BRENT-Europe',1,amount, footnote)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 542 + }, + "id": "5A_CCSw8Cdcm", + "outputId": "4cae729e-db94-4f73-afc5-1319be41f987" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "
\n", + "
\n", + "\n", + "" + ] + }, + "metadata": {} + } + ] + }, + { + "cell_type": "code", + "source": [ + "!mkdir /content/drive/MyDrive/datasets" + ], + "metadata": { + "id": "u_oq0FhOD94P" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "from google.colab import files\n", + "import os\n", + "\n", + "# Create directory if it doesn't exist\n", + "if not os.path.exists('/content/drive/MyDrive/datasets'):\n", + " os.mkdir('/content/drive/MyDrive/datasets')\n", + "\n", + "# Upload file\n", + "uploaded = files.upload()\n", + "\n", + "# Get filename and content of uploaded file\n", + "filename, content = next(iter(uploaded.items()))\n", + "\n", + "# Write content to file\n", + "with open('/content/drive/MyDrive/datasets/' + filename, 'wb') as f:\n", + " f.write(content)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + }, + "id": "d-MEoPH6Ci10", + "outputId": "ec294482-0d0b-4d0d-c14a-0a72e36579d6" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": [ + "" + ], + "text/html": [ + "\n", + " \n", + " \n", + " Upload widget is only available when the cell has been executed in the\n", + " current browser session. Please rerun this cell to enable.\n", + " \n", + " " + ] + }, + "metadata": {} + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Saving gdp_wb_2005_2015.csv to gdp_wb_2005_2015 (6).csv\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "import numpy as np\n", + "\n", + "ggdp = pd.read_csv('/content/drive/MyDrive/datasets/gdp_wb_2005_2015.csv')\n", + "\n", + "#Transform and format global gdp\n", + "\n", + "ggdp['date']=ggdp['Year']\n", + "ggdp=ggdp.drop(columns=['Year'])\n", + "ggdp.set_index('date', inplace=True)\n", + "\n", + "ggdp['value']=np.divide(ggdp['\\tGDP (current US$)'],1e11)\n", + "ggdp = ggdp.drop(columns=['\\tGDP (current US$)'])\n", + "\n", + "ggdp.head(1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 112 + }, + "id": "pYglyq59GWv3", + "outputId": "0bfc6332-87d0-4ba4-a0e0-c358f6583e6f" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " value\n", + "date \n", + "2005 48.136604" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
value
date
200548.136604
\n", + "
\n", + " \n", + " \n", + " \n", + "\n", + " \n", + "
\n", + "
\n", + " " + ] + }, + "metadata": {}, + "execution_count": 41 + } + ] + }, + { + "cell_type": "code", + "source": [ + "amount='Trillion'\n", + "footnote='Source: World Bank'\n", + "plotter(ggdp, 'Global GDP',0,amount, footnote )" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 542 + }, + "id": "1YWlvI4pHyIz", + "outputId": "deb904ec-0a08-4e28-e116-f6cff80c2f1e" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "
\n", + "
\n", + "\n", + "" + ] + }, + "metadata": {} + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# Neuer Abschnitt" + ], + "metadata": { + "id": "kWXxFpjdYoMz" + } + } + ] +} \ No newline at end of file