{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "```yaml\n", "operator: local.JupyterOperator\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This could very well be abstracted into some sort of StockHistoryOperator, with ticker(s), period, and interval parameterized, but here we will do the work in a notebook." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import yfinance as yf" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "stock = yf.Ticker('SPY')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "history = stock.history(period='5y', interval='1d').reset_index()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "history.columns = history.columns.str.strip().str.lower().str.replace(' ', '_').str.replace('(', '').str.replace(')', '')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "history = history.sort_values('date', ascending = False).reset_index(drop=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take final table and save to the database." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from airflow.hooks.postgres_hook import PostgresHook\n", "\n", "con = PostgresHook('postgres_default').get_sqlalchemy_engine()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "history.to_sql('stock_data',\n", " con,\n", " schema='views',\n", " if_exists=\"replace\",\n", " index=False)" ] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }