{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "
Function using the yfinance API to get all market data
" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Tickers" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#array with all the studied currencies (yahoo finance currency names)\n", "currencies = [\"BTC-USD\",\"ETH-USD\",\"TTE.PA\"] " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def getMetaData(s : str) -> dict :\n", " \"\"\"Get the meta information for a Ticker (currency)\n", " \n", " Args : \n", " s (str) : name of the Ticker\n", " \n", " Return : \n", " dict : return all the meta information of the Ticker, the dict is the following : \n", " - symbol : str ~> name online (yahoo finance) of the stock (is also the ticker name used by the yfinance API)\n", " - instrumentType : str ~> type of stock, can be a cryptocurrency , a stock ...\n", " - regularMarketTime int : ~> date when those informatation has been gathered in epoch/unix time \n", " - gmtoffset int : ~> number of second of offset between the stock's market timezome and the gtm timzome\n", " - timezone str : ~> name of the stock's market timezone\n", " other var a disponnible but not used here (see yfinance documentation)\n", " \"\"\" \n", " ticker = yfin.Ticker(s)\n", " ticker.history()\n", " meta = ticker.history_metadata\n", " return meta\n", " \n", "def getHistoryData(s : str) -> dict :\n", " \"\"\"Get all the information since the creation of the Ticker (currency)\n", " \n", " Args : \n", " s (str) : name of the Ticker\n", " \n", " Return : \n", " dict : return all the information of the Ticker since creation, the dict contaign the following information : {Date,Open,Hight,Low,Close,Volume}\n", " \"\"\"\n", " ticker = yfin.Ticker(s)\n", " data = ticker.history(\"max\", interval = '1d')\n", " data['Date'] = data.index\n", " data = data[[\"Date\", \"Open\", \"High\", \"Low\", \"Close\", \"Volume\"]]\n", " data.reset_index(drop=True, inplace=True)\n", " return data\n", "\n", "def getPeriodData(s : str , period : str) -> dict :\n", " \"\"\"Get the information of the Ticker for a certain period\n", " \n", " Args : \n", " s (str) : name of the Ticker\n", " period (str) : duration of the period , the information period get are ending to the current day and start a period back can take value like {1d , 5d , 1m , 1y , ...}\n", " \n", " Return : \n", " dict : return all the information of the Ticker for the period, the dict contaign the following information : {Date,Open,Hight,Low,Close,Volume}\n", " \"\"\"\n", " ticker = yfin.Ticker(s)\n", " data = ticker.history(period = period ,interval = '1d')\n", " data['Date'] = data.index\n", " data = data[[\"Date\", \"Open\", \"High\",\"Low\", \"Close\", \"Volume\"]]\n", " data.reset_index(drop=True, inplace=True)\n", " return data" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "This table contaign all the usefull metadata of each currencies fetched. It contaigns :
\n", "Usefull an necessary function for next parts
" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def cleanNameForSQL(s : str) -> str:\n", "\t\"\"\"Return a new string that dont contaign special char to use it as sql table name\n", "\n", "\tArgs:\n", "\t\ts (str): string where to remove special char \n", "\n", "\tReturns:\n", "\t\tstr: new string without special char nor spaces \n", "\t\"\"\"\n", "\tfor char in ['-','.','^',' ']:\n", "\t\ts = s.replace(char,\"\")\n", "\treturn s" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Scipt to initialize the database
" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "def setDb(currencies : list):\n", " \"\"\"Initialize the database\n", "\n", " Args:\n", " currencies (list): list of currencies to add during the db initailization\n", " \"\"\"\n", " createCurrencyMetadataTable()\n", " for currency in currencies:\n", " print(currency)\n", " insertCurrencyMetadataElement(currency)\n", " createCurrencyMarketTable(currency)\n", " fillCurrencyMarketTable(currency,period = \"1y\")\n", " createCurrencyForecastTable(currency)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BTC-USD\n", "INSERT INTO CURRENCIES_METADATA (currencyName,instrumentType,regularMarketTime,gtmOffset,timezone)\n", " VALUES (\n", " 'BTC-USD',\n", " 'CRYPTOCURRENCY',\n", " '2023-04-25 11:45:00+00:00',\n", " 0,\n", " 'UTC')\n", "ETH-USD\n", "INSERT INTO CURRENCIES_METADATA (currencyName,instrumentType,regularMarketTime,gtmOffset,timezone)\n", " VALUES (\n", " 'ETH-USD',\n", " 'CRYPTOCURRENCY',\n", " '2023-04-25 11:45:00+00:00',\n", " 0,\n", " 'UTC')\n", "TTE.PA\n", "INSERT INTO CURRENCIES_METADATA (currencyName,instrumentType,regularMarketTime,gtmOffset,timezone)\n", " VALUES (\n", " 'TTE.PA',\n", " 'EQUITY',\n", " '2023-04-25 13:32:36+02:00',\n", " 7200,\n", " 'CEST')\n" ] } ], "source": [ "#create db\n", "try:\n", " os.remove(\"mydatabase.db\") #delete database if it exixts \n", "except:\n", " pass # if it not exists just go next\n", "setDb(currencies)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "