CoolFace
Apppublic

humanist96/FinGPT_Forecaster

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
prepare_data.ipynb1546 linesDownload Raw Back to root
1{2 "cells": [3  {4   "cell_type": "code",5   "execution_count": 30,6   "id": "3c4d096e",7   "metadata": {},8   "outputs": [],9   "source": [10    "import os\n",11    "import re\n",12    "import csv\n",13    "import math\n",14    "import time\n",15    "import json\n",16    "import random\n",17    "import finnhub\n",18    "import datasets\n",19    "import pandas as pd\n",20    "import yfinance as yf\n",21    "from datetime import datetime\n",22    "from collections import defaultdict\n",23    "from datasets import Dataset\n",24    "from openai import OpenAI"25   ]26  },27  {28   "cell_type": "code",29   "execution_count": 31,30   "id": "ace9fdb4",31   "metadata": {},32   "outputs": [],33   "source": [34    "START_DATE = \"2022-12-31\"\n",35    "END_DATE = \"2023-05-31\"\n",36    "\n",37    "DATA_DIR = f\"./{START_DATE}_{END_DATE}\"\n",38    "os.makedirs(DATA_DIR, exist_ok=True)\n",39    "\n",40    "finnhub_client = finnhub.Client(api_key=\"your finnhub key\")\n",41    "\n",42    "client = OpenAI(api_key = 'your openai key')"43   ]44  },45  {46   "cell_type": "markdown",47   "id": "2fce2503",48   "metadata": {},49   "source": [50    "# Raw Financial Data Acquisition"51   ]52  },53  {54   "cell_type": "code",55   "execution_count": 43,56   "id": "c6564114",57   "metadata": {},58   "outputs": [],59   "source": [60    "def bin_mapping(ret):\n",61    "    \n",62    "    up_down = 'U' if ret >= 0 else 'D'\n",63    "    integer = math.ceil(abs(100 * ret))\n",64    "    \n",65    "    return up_down + (str(integer) if integer <= 5 else '5+')\n",66    "\n",67    "\n",68    "def get_returns(stock_symbol):\n",69    "    \n",70    "    # Download historical stock data\n",71    "    stock_data = yf.download(stock_symbol, start=START_DATE, end=END_DATE)\n",72    "    \n",73    "    weekly_data = stock_data['Adj Close'].resample('W').ffill()\n",74    "    weekly_returns = weekly_data.pct_change()[1:]\n",75    "    weekly_start_prices = weekly_data[:-1]\n",76    "    weekly_end_prices = weekly_data[1:]\n",77    "\n",78    "    weekly_data = pd.DataFrame({\n",79    "        'Start Date': weekly_start_prices.index,\n",80    "        'Start Price': weekly_start_prices.values,\n",81    "        'End Date': weekly_end_prices.index,\n",82    "        'End Price': weekly_end_prices.values,\n",83    "        'Weekly Returns': weekly_returns.values\n",84    "    })\n",85    "    \n",86    "    weekly_data['Bin Label'] = weekly_data['Weekly Returns'].map(bin_mapping)\n",87    "\n",88    "    return weekly_data\n",89    "\n",90    "\n",91    "def get_news(symbol, data):\n",92    "    \n",93    "    news_list = []\n",94    "    \n",95    "    for end_date, row in data.iterrows():\n",96    "        start_date = row['Start Date'].strftime('%Y-%m-%d')\n",97    "        end_date = row['End Date'].strftime('%Y-%m-%d')\n",98    "        print(symbol, ': ', start_date, ' - ', end_date)\n",99    "        time.sleep(1) # control qpm\n",100    "        weekly_news = finnhub_client.company_news(symbol, _from=start_date, to=end_date)\n",101    "        weekly_news = [\n",102    "            {\n",103    "                \"date\": datetime.fromtimestamp(n['datetime']).strftime('%Y%m%d%H%M%S'),\n",104    "                \"headline\": n['headline'],\n",105    "                \"summary\": n['summary'],\n",106    "            } for n in weekly_news\n",107    "        ]\n",108    "        weekly_news.sort(key=lambda x: x['date'])\n",109    "        news_list.append(json.dumps(weekly_news))\n",110    "    \n",111    "    data['News'] = news_list\n",112    "    \n",113    "    return data\n",114    "\n",115    "\n",116    "def get_basics(symbol, data, always=False):\n",117    "    \n",118    "    basic_financials = finnhub_client.company_basic_financials(symbol, 'all')\n",119    "    \n",120    "    final_basics, basic_list, basic_dict = [], [], defaultdict(dict)\n",121    "    \n",122    "    for metric, value_list in basic_financials['series']['quarterly'].items():\n",123    "        for value in value_list:\n",124    "            basic_dict[value['period']].update({metric: value['v']})\n",125    "\n",126    "    for k, v in basic_dict.items():\n",127    "        v.update({'period': k})\n",128    "        basic_list.append(v)\n",129    "        \n",130    "    basic_list.sort(key=lambda x: x['period'])\n",131    "            \n",132    "    for i, row in data.iterrows():\n",133    "        \n",134    "        start_date = row['End Date'].strftime('%Y-%m-%d')\n",135    "        last_start_date = START_DATE if i < 2 else data.loc[i-2, 'Start Date'].strftime('%Y-%m-%d')\n",136    "        \n",137    "        used_basic = {}\n",138    "        for basic in basic_list[::-1]:\n",139    "            if (always and basic['period'] < start_date) or (last_start_date <= basic['period'] < start_date):\n",140    "                used_basic = basic\n",141    "                break\n",142    "        final_basics.append(json.dumps(used_basic))\n",143    "        \n",144    "    data['Basics'] = final_basics\n",145    "    \n",146    "    return data\n",147    "    \n",148    "\n",149    "def prepare_data_for_company(symbol, with_basics=True):\n",150    "    \n",151    "    data = get_returns(symbol)\n",152    "    data = get_news(symbol, data)\n",153    "    \n",154    "    if with_basics:\n",155    "        data = get_basics(symbol, data)\n",156    "        data.to_csv(f\"{DATA_DIR}/{symbol}_{START_DATE}_{END_DATE}.csv\")\n",157    "    else:\n",158    "        data['Basics'] = [json.dumps({})] * len(data)\n",159    "        data.to_csv(f\"{DATA_DIR}/{symbol}_{START_DATE}_{END_DATE}_nobasics.csv\")\n",160    "    \n",161    "    return data\n"162   ]163  },164  {165   "cell_type": "code",166   "execution_count": 59,167   "id": "caf02ab7",168   "metadata": {},169   "outputs": [],170   "source": [171    "DOW_30 = [\n",172    "    \"AXP\", \"AMGN\", \"AAPL\", \"BA\", \"CAT\", \"CSCO\", \"CVX\", \"GS\", \"HD\", \"HON\",\n",173    "    \"IBM\", \"INTC\", \"JNJ\", \"KO\", \"JPM\", \"MCD\", \"MMM\", \"MRK\", \"MSFT\", \"NKE\",\n",174    "    \"PG\", \"TRV\", \"UNH\", \"CRM\", \"VZ\", \"V\", \"WBA\", \"WMT\", \"DIS\", \"DOW\"\n",175    "]\n",176    "\n",177    "# prepare_data_for_company(\"DOW\", False)"178   ]179  },180  {181   "cell_type": "code",182   "execution_count": 81,183   "id": "43d65960",184   "metadata": {185    "scrolled": true186   },187   "outputs": [188    {189     "name": "stdout",190     "output_type": "stream",191     "text": [192      "[*********************100%%**********************]  1 of 1 completed\n",193      "AXP :  2023-01-08  -  2023-01-15\n",194      "AXP :  2023-01-15  -  2023-01-22\n",195      "AXP :  2023-01-22  -  2023-01-29\n",196      "AXP :  2023-01-29  -  2023-02-05\n",197      "AXP :  2023-02-05  -  2023-02-12\n",198      "AXP :  2023-02-12  -  2023-02-19\n",199      "AXP :  2023-02-19  -  2023-02-26\n",200      "AXP :  2023-02-26  -  2023-03-05\n",201      "AXP :  2023-03-05  -  2023-03-12\n",202      "AXP :  2023-03-12  -  2023-03-19\n",203      "AXP :  2023-03-19  -  2023-03-26\n",204      "AXP :  2023-03-26  -  2023-04-02\n",205      "AXP :  2023-04-02  -  2023-04-09\n",206      "AXP :  2023-04-09  -  2023-04-16\n",207      "AXP :  2023-04-16  -  2023-04-23\n",208      "AXP :  2023-04-23  -  2023-04-30\n",209      "AXP :  2023-04-30  -  2023-05-07\n",210      "AXP :  2023-05-07  -  2023-05-14\n",211      "AXP :  2023-05-14  -  2023-05-21\n",212      "AXP :  2023-05-21  -  2023-05-28\n",213      "AXP :  2023-05-28  -  2023-06-04\n",214      "[*********************100%%**********************]  1 of 1 completed\n",215      "AMGN :  2023-01-08  -  2023-01-15\n",216      "AMGN :  2023-01-15  -  2023-01-22\n",217      "AMGN :  2023-01-22  -  2023-01-29\n",218      "AMGN :  2023-01-29  -  2023-02-05\n",219      "AMGN :  2023-02-05  -  2023-02-12\n",220      "AMGN :  2023-02-12  -  2023-02-19\n",221      "AMGN :  2023-02-19  -  2023-02-26\n",222      "AMGN :  2023-02-26  -  2023-03-05\n",223      "AMGN :  2023-03-05  -  2023-03-12\n",224      "AMGN :  2023-03-12  -  2023-03-19\n",225      "AMGN :  2023-03-19  -  2023-03-26\n",226      "AMGN :  2023-03-26  -  2023-04-02\n",227      "AMGN :  2023-04-02  -  2023-04-09\n",228      "AMGN :  2023-04-09  -  2023-04-16\n",229      "AMGN :  2023-04-16  -  2023-04-23\n",230      "AMGN :  2023-04-23  -  2023-04-30\n",231      "AMGN :  2023-04-30  -  2023-05-07\n",232      "AMGN :  2023-05-07  -  2023-05-14\n",233      "AMGN :  2023-05-14  -  2023-05-21\n",234      "AMGN :  2023-05-21  -  2023-05-28\n",235      "AMGN :  2023-05-28  -  2023-06-04\n",236      "[*********************100%%**********************]  1 of 1 completed\n",237      "AAPL :  2023-01-08  -  2023-01-15\n",238      "AAPL :  2023-01-15  -  2023-01-22\n",239      "AAPL :  2023-01-22  -  2023-01-29\n",240      "AAPL :  2023-01-29  -  2023-02-05\n",241      "AAPL :  2023-02-05  -  2023-02-12\n",242      "AAPL :  2023-02-12  -  2023-02-19\n",243      "AAPL :  2023-02-19  -  2023-02-26\n",244      "AAPL :  2023-02-26  -  2023-03-05\n",245      "AAPL :  2023-03-05  -  2023-03-12\n",246      "AAPL :  2023-03-12  -  2023-03-19\n",247      "AAPL :  2023-03-19  -  2023-03-26\n",248      "AAPL :  2023-03-26  -  2023-04-02\n",249      "AAPL :  2023-04-02  -  2023-04-09\n",250      "AAPL :  2023-04-09  -  2023-04-16\n",251      "AAPL :  2023-04-16  -  2023-04-23\n",252      "AAPL :  2023-04-23  -  2023-04-30\n",253      "AAPL :  2023-04-30  -  2023-05-07\n",254      "AAPL :  2023-05-07  -  2023-05-14\n",255      "AAPL :  2023-05-14  -  2023-05-21\n",256      "AAPL :  2023-05-21  -  2023-05-28\n",257      "AAPL :  2023-05-28  -  2023-06-04\n",258      "[*********************100%%**********************]  1 of 1 completed\n",259      "BA :  2023-01-08  -  2023-01-15\n",260      "BA :  2023-01-15  -  2023-01-22\n",261      "BA :  2023-01-22  -  2023-01-29\n",262      "BA :  2023-01-29  -  2023-02-05\n",263      "BA :  2023-02-05  -  2023-02-12\n",264      "BA :  2023-02-12  -  2023-02-19\n",265      "BA :  2023-02-19  -  2023-02-26\n",266      "BA :  2023-02-26  -  2023-03-05\n",267      "BA :  2023-03-05  -  2023-03-12\n",268      "BA :  2023-03-12  -  2023-03-19\n",269      "BA :  2023-03-19  -  2023-03-26\n",270      "BA :  2023-03-26  -  2023-04-02\n",271      "BA :  2023-04-02  -  2023-04-09\n",272      "BA :  2023-04-09  -  2023-04-16\n",273      "BA :  2023-04-16  -  2023-04-23\n",274      "BA :  2023-04-23  -  2023-04-30\n",275      "BA :  2023-04-30  -  2023-05-07\n",276      "BA :  2023-05-07  -  2023-05-14\n",277      "BA :  2023-05-14  -  2023-05-21\n",278      "BA :  2023-05-21  -  2023-05-28\n",279      "BA :  2023-05-28  -  2023-06-04\n",280      "[*********************100%%**********************]  1 of 1 completed\n",281      "CAT :  2023-01-08  -  2023-01-15\n",282      "CAT :  2023-01-15  -  2023-01-22\n",283      "CAT :  2023-01-22  -  2023-01-29\n",284      "CAT :  2023-01-29  -  2023-02-05\n",285      "CAT :  2023-02-05  -  2023-02-12\n",286      "CAT :  2023-02-12  -  2023-02-19\n",287      "CAT :  2023-02-19  -  2023-02-26\n",288      "CAT :  2023-02-26  -  2023-03-05\n",289      "CAT :  2023-03-05  -  2023-03-12\n",290      "CAT :  2023-03-12  -  2023-03-19\n",291      "CAT :  2023-03-19  -  2023-03-26\n",292      "CAT :  2023-03-26  -  2023-04-02\n",293      "CAT :  2023-04-02  -  2023-04-09\n",294      "CAT :  2023-04-09  -  2023-04-16\n",295      "CAT :  2023-04-16  -  2023-04-23\n",296      "CAT :  2023-04-23  -  2023-04-30\n",297      "CAT :  2023-04-30  -  2023-05-07\n",298      "CAT :  2023-05-07  -  2023-05-14\n",299      "CAT :  2023-05-14  -  2023-05-21\n",300      "CAT :  2023-05-21  -  2023-05-28\n",301      "CAT :  2023-05-28  -  2023-06-04\n",302      "[*********************100%%**********************]  1 of 1 completed\n",303      "CSCO :  2023-01-08  -  2023-01-15\n",304      "CSCO :  2023-01-15  -  2023-01-22\n",305      "CSCO :  2023-01-22  -  2023-01-29\n",306      "CSCO :  2023-01-29  -  2023-02-05\n",307      "CSCO :  2023-02-05  -  2023-02-12\n",308      "CSCO :  2023-02-12  -  2023-02-19\n",309      "CSCO :  2023-02-19  -  2023-02-26\n",310      "CSCO :  2023-02-26  -  2023-03-05\n",311      "CSCO :  2023-03-05  -  2023-03-12\n",312      "CSCO :  2023-03-12  -  2023-03-19\n",313      "CSCO :  2023-03-19  -  2023-03-26\n",314      "CSCO :  2023-03-26  -  2023-04-02\n",315      "CSCO :  2023-04-02  -  2023-04-09\n",316      "CSCO :  2023-04-09  -  2023-04-16\n",317      "CSCO :  2023-04-16  -  2023-04-23\n",318      "CSCO :  2023-04-23  -  2023-04-30\n",319      "CSCO :  2023-04-30  -  2023-05-07\n",320      "CSCO :  2023-05-07  -  2023-05-14\n",321      "CSCO :  2023-05-14  -  2023-05-21\n",322      "CSCO :  2023-05-21  -  2023-05-28\n",323      "CSCO :  2023-05-28  -  2023-06-04\n",324      "[*********************100%%**********************]  1 of 1 completed\n",325      "CVX :  2023-01-08  -  2023-01-15\n",326      "CVX :  2023-01-15  -  2023-01-22\n",327      "CVX :  2023-01-22  -  2023-01-29\n",328      "CVX :  2023-01-29  -  2023-02-05\n",329      "CVX :  2023-02-05  -  2023-02-12\n",330      "CVX :  2023-02-12  -  2023-02-19\n",331      "CVX :  2023-02-19  -  2023-02-26\n",332      "CVX :  2023-02-26  -  2023-03-05\n",333      "CVX :  2023-03-05  -  2023-03-12\n",334      "CVX :  2023-03-12  -  2023-03-19\n",335      "CVX :  2023-03-19  -  2023-03-26\n",336      "CVX :  2023-03-26  -  2023-04-02\n",337      "CVX :  2023-04-02  -  2023-04-09\n",338      "CVX :  2023-04-09  -  2023-04-16\n",339      "CVX :  2023-04-16  -  2023-04-23\n",340      "CVX :  2023-04-23  -  2023-04-30\n",341      "CVX :  2023-04-30  -  2023-05-07\n",342      "CVX :  2023-05-07  -  2023-05-14\n",343      "CVX :  2023-05-14  -  2023-05-21\n",344      "CVX :  2023-05-21  -  2023-05-28\n",345      "CVX :  2023-05-28  -  2023-06-04\n",346      "[*********************100%%**********************]  1 of 1 completed\n",347      "GS :  2023-01-08  -  2023-01-15\n",348      "GS :  2023-01-15  -  2023-01-22\n",349      "GS :  2023-01-22  -  2023-01-29\n",350      "GS :  2023-01-29  -  2023-02-05\n",351      "GS :  2023-02-05  -  2023-02-12\n",352      "GS :  2023-02-12  -  2023-02-19\n",353      "GS :  2023-02-19  -  2023-02-26\n",354      "GS :  2023-02-26  -  2023-03-05\n",355      "GS :  2023-03-05  -  2023-03-12\n",356      "GS :  2023-03-12  -  2023-03-19\n",357      "GS :  2023-03-19  -  2023-03-26\n",358      "GS :  2023-03-26  -  2023-04-02\n",359      "GS :  2023-04-02  -  2023-04-09\n",360      "GS :  2023-04-09  -  2023-04-16\n",361      "GS :  2023-04-16  -  2023-04-23\n",362      "GS :  2023-04-23  -  2023-04-30\n",363      "GS :  2023-04-30  -  2023-05-07\n",364      "GS :  2023-05-07  -  2023-05-14\n",365      "GS :  2023-05-14  -  2023-05-21\n",366      "GS :  2023-05-21  -  2023-05-28\n",367      "GS :  2023-05-28  -  2023-06-04\n",368      "[*********************100%%**********************]  1 of 1 completed\n",369      "HD :  2023-01-08  -  2023-01-15\n",370      "HD :  2023-01-15  -  2023-01-22\n",371      "HD :  2023-01-22  -  2023-01-29\n",372      "HD :  2023-01-29  -  2023-02-05\n",373      "HD :  2023-02-05  -  2023-02-12\n",374      "HD :  2023-02-12  -  2023-02-19\n",375      "HD :  2023-02-19  -  2023-02-26\n",376      "HD :  2023-02-26  -  2023-03-05\n",377      "HD :  2023-03-05  -  2023-03-12\n",378      "HD :  2023-03-12  -  2023-03-19\n",379      "HD :  2023-03-19  -  2023-03-26\n",380      "HD :  2023-03-26  -  2023-04-02\n",381      "HD :  2023-04-02  -  2023-04-09\n",382      "HD :  2023-04-09  -  2023-04-16\n",383      "HD :  2023-04-16  -  2023-04-23\n",384      "HD :  2023-04-23  -  2023-04-30\n",385      "HD :  2023-04-30  -  2023-05-07\n",386      "HD :  2023-05-07  -  2023-05-14\n",387      "HD :  2023-05-14  -  2023-05-21\n",388      "HD :  2023-05-21  -  2023-05-28\n",389      "HD :  2023-05-28  -  2023-06-04\n",390      "[*********************100%%**********************]  1 of 1 completed\n",391      "HON :  2023-01-08  -  2023-01-15\n",392      "HON :  2023-01-15  -  2023-01-22\n",393      "HON :  2023-01-22  -  2023-01-29\n",394      "HON :  2023-01-29  -  2023-02-05\n",395      "HON :  2023-02-05  -  2023-02-12\n",396      "HON :  2023-02-12  -  2023-02-19\n",397      "HON :  2023-02-19  -  2023-02-26\n",398      "HON :  2023-02-26  -  2023-03-05\n",399      "HON :  2023-03-05  -  2023-03-12\n",400      "HON :  2023-03-12  -  2023-03-19\n",401      "HON :  2023-03-19  -  2023-03-26\n",402      "HON :  2023-03-26  -  2023-04-02\n",403      "HON :  2023-04-02  -  2023-04-09\n",404      "HON :  2023-04-09  -  2023-04-16\n",405      "HON :  2023-04-16  -  2023-04-23\n",406      "HON :  2023-04-23  -  2023-04-30\n",407      "HON :  2023-04-30  -  2023-05-07\n",408      "HON :  2023-05-07  -  2023-05-14\n",409      "HON :  2023-05-14  -  2023-05-21\n",410      "HON :  2023-05-21  -  2023-05-28\n",411      "HON :  2023-05-28  -  2023-06-04\n",412      "[*********************100%%**********************]  1 of 1 completed\n",413      "IBM :  2023-01-08  -  2023-01-15\n",414      "IBM :  2023-01-15  -  2023-01-22\n",415      "IBM :  2023-01-22  -  2023-01-29\n",416      "IBM :  2023-01-29  -  2023-02-05\n",417      "IBM :  2023-02-05  -  2023-02-12\n",418      "IBM :  2023-02-12  -  2023-02-19\n",419      "IBM :  2023-02-19  -  2023-02-26\n",420      "IBM :  2023-02-26  -  2023-03-05\n",421      "IBM :  2023-03-05  -  2023-03-12\n",422      "IBM :  2023-03-12  -  2023-03-19\n",423      "IBM :  2023-03-19  -  2023-03-26\n",424      "IBM :  2023-03-26  -  2023-04-02\n",425      "IBM :  2023-04-02  -  2023-04-09\n",426      "IBM :  2023-04-09  -  2023-04-16\n",427      "IBM :  2023-04-16  -  2023-04-23\n",428      "IBM :  2023-04-23  -  2023-04-30\n"429     ]430    },431    {432     "name": "stdout",433     "output_type": "stream",434     "text": [435      "IBM :  2023-04-30  -  2023-05-07\n",436      "IBM :  2023-05-07  -  2023-05-14\n",437      "IBM :  2023-05-14  -  2023-05-21\n",438      "IBM :  2023-05-21  -  2023-05-28\n",439      "IBM :  2023-05-28  -  2023-06-04\n",440      "[*********************100%%**********************]  1 of 1 completed\n",441      "INTC :  2023-01-08  -  2023-01-15\n",442      "INTC :  2023-01-15  -  2023-01-22\n",443      "INTC :  2023-01-22  -  2023-01-29\n",444      "INTC :  2023-01-29  -  2023-02-05\n",445      "INTC :  2023-02-05  -  2023-02-12\n",446      "INTC :  2023-02-12  -  2023-02-19\n",447      "INTC :  2023-02-19  -  2023-02-26\n",448      "INTC :  2023-02-26  -  2023-03-05\n",449      "INTC :  2023-03-05  -  2023-03-12\n",450      "INTC :  2023-03-12  -  2023-03-19\n",451      "INTC :  2023-03-19  -  2023-03-26\n",452      "INTC :  2023-03-26  -  2023-04-02\n",453      "INTC :  2023-04-02  -  2023-04-09\n",454      "INTC :  2023-04-09  -  2023-04-16\n",455      "INTC :  2023-04-16  -  2023-04-23\n",456      "INTC :  2023-04-23  -  2023-04-30\n",457      "INTC :  2023-04-30  -  2023-05-07\n",458      "INTC :  2023-05-07  -  2023-05-14\n",459      "INTC :  2023-05-14  -  2023-05-21\n",460      "INTC :  2023-05-21  -  2023-05-28\n",461      "INTC :  2023-05-28  -  2023-06-04\n",462      "[*********************100%%**********************]  1 of 1 completed\n",463      "JNJ :  2023-01-08  -  2023-01-15\n",464      "JNJ :  2023-01-15  -  2023-01-22\n",465      "JNJ :  2023-01-22  -  2023-01-29\n",466      "JNJ :  2023-01-29  -  2023-02-05\n",467      "JNJ :  2023-02-05  -  2023-02-12\n",468      "JNJ :  2023-02-12  -  2023-02-19\n",469      "JNJ :  2023-02-19  -  2023-02-26\n",470      "JNJ :  2023-02-26  -  2023-03-05\n",471      "JNJ :  2023-03-05  -  2023-03-12\n",472      "JNJ :  2023-03-12  -  2023-03-19\n",473      "JNJ :  2023-03-19  -  2023-03-26\n",474      "JNJ :  2023-03-26  -  2023-04-02\n",475      "JNJ :  2023-04-02  -  2023-04-09\n",476      "JNJ :  2023-04-09  -  2023-04-16\n",477      "JNJ :  2023-04-16  -  2023-04-23\n",478      "JNJ :  2023-04-23  -  2023-04-30\n",479      "JNJ :  2023-04-30  -  2023-05-07\n",480      "JNJ :  2023-05-07  -  2023-05-14\n",481      "JNJ :  2023-05-14  -  2023-05-21\n",482      "JNJ :  2023-05-21  -  2023-05-28\n",483      "JNJ :  2023-05-28  -  2023-06-04\n",484      "[*********************100%%**********************]  1 of 1 completed\n",485      "KO :  2023-01-08  -  2023-01-15\n",486      "KO :  2023-01-15  -  2023-01-22\n",487      "KO :  2023-01-22  -  2023-01-29\n",488      "KO :  2023-01-29  -  2023-02-05\n",489      "KO :  2023-02-05  -  2023-02-12\n",490      "KO :  2023-02-12  -  2023-02-19\n",491      "KO :  2023-02-19  -  2023-02-26\n",492      "KO :  2023-02-26  -  2023-03-05\n",493      "KO :  2023-03-05  -  2023-03-12\n",494      "KO :  2023-03-12  -  2023-03-19\n",495      "KO :  2023-03-19  -  2023-03-26\n",496      "KO :  2023-03-26  -  2023-04-02\n",497      "KO :  2023-04-02  -  2023-04-09\n",498      "KO :  2023-04-09  -  2023-04-16\n",499      "KO :  2023-04-16  -  2023-04-23\n",500      "KO :  2023-04-23  -  2023-04-30\n",501      "KO :  2023-04-30  -  2023-05-07\n",502      "KO :  2023-05-07  -  2023-05-14\n",503      "KO :  2023-05-14  -  2023-05-21\n",504      "KO :  2023-05-21  -  2023-05-28\n",505      "KO :  2023-05-28  -  2023-06-04\n",506      "[*********************100%%**********************]  1 of 1 completed\n",507      "JPM :  2023-01-08  -  2023-01-15\n",508      "JPM :  2023-01-15  -  2023-01-22\n",509      "JPM :  2023-01-22  -  2023-01-29\n",510      "JPM :  2023-01-29  -  2023-02-05\n",511      "JPM :  2023-02-05  -  2023-02-12\n",512      "JPM :  2023-02-12  -  2023-02-19\n",513      "JPM :  2023-02-19  -  2023-02-26\n",514      "JPM :  2023-02-26  -  2023-03-05\n",515      "JPM :  2023-03-05  -  2023-03-12\n",516      "JPM :  2023-03-12  -  2023-03-19\n",517      "JPM :  2023-03-19  -  2023-03-26\n",518      "JPM :  2023-03-26  -  2023-04-02\n",519      "JPM :  2023-04-02  -  2023-04-09\n",520      "JPM :  2023-04-09  -  2023-04-16\n",521      "JPM :  2023-04-16  -  2023-04-23\n",522      "JPM :  2023-04-23  -  2023-04-30\n",523      "JPM :  2023-04-30  -  2023-05-07\n",524      "JPM :  2023-05-07  -  2023-05-14\n",525      "JPM :  2023-05-14  -  2023-05-21\n",526      "JPM :  2023-05-21  -  2023-05-28\n",527      "JPM :  2023-05-28  -  2023-06-04\n",528      "[*********************100%%**********************]  1 of 1 completed\n",529      "MCD :  2023-01-08  -  2023-01-15\n",530      "MCD :  2023-01-15  -  2023-01-22\n",531      "MCD :  2023-01-22  -  2023-01-29\n",532      "MCD :  2023-01-29  -  2023-02-05\n",533      "MCD :  2023-02-05  -  2023-02-12\n",534      "MCD :  2023-02-12  -  2023-02-19\n",535      "MCD :  2023-02-19  -  2023-02-26\n",536      "MCD :  2023-02-26  -  2023-03-05\n",537      "MCD :  2023-03-05  -  2023-03-12\n",538      "MCD :  2023-03-12  -  2023-03-19\n",539      "MCD :  2023-03-19  -  2023-03-26\n",540      "MCD :  2023-03-26  -  2023-04-02\n",541      "MCD :  2023-04-02  -  2023-04-09\n",542      "MCD :  2023-04-09  -  2023-04-16\n",543      "MCD :  2023-04-16  -  2023-04-23\n",544      "MCD :  2023-04-23  -  2023-04-30\n",545      "MCD :  2023-04-30  -  2023-05-07\n",546      "MCD :  2023-05-07  -  2023-05-14\n",547      "MCD :  2023-05-14  -  2023-05-21\n",548      "MCD :  2023-05-21  -  2023-05-28\n",549      "MCD :  2023-05-28  -  2023-06-04\n",550      "[*********************100%%**********************]  1 of 1 completed\n",551      "MMM :  2023-01-08  -  2023-01-15\n",552      "MMM :  2023-01-15  -  2023-01-22\n",553      "MMM :  2023-01-22  -  2023-01-29\n",554      "MMM :  2023-01-29  -  2023-02-05\n",555      "MMM :  2023-02-05  -  2023-02-12\n",556      "MMM :  2023-02-12  -  2023-02-19\n",557      "MMM :  2023-02-19  -  2023-02-26\n",558      "MMM :  2023-02-26  -  2023-03-05\n",559      "MMM :  2023-03-05  -  2023-03-12\n",560      "MMM :  2023-03-12  -  2023-03-19\n",561      "MMM :  2023-03-19  -  2023-03-26\n",562      "MMM :  2023-03-26  -  2023-04-02\n",563      "MMM :  2023-04-02  -  2023-04-09\n",564      "MMM :  2023-04-09  -  2023-04-16\n",565      "MMM :  2023-04-16  -  2023-04-23\n",566      "MMM :  2023-04-23  -  2023-04-30\n",567      "MMM :  2023-04-30  -  2023-05-07\n",568      "MMM :  2023-05-07  -  2023-05-14\n",569      "MMM :  2023-05-14  -  2023-05-21\n",570      "MMM :  2023-05-21  -  2023-05-28\n",571      "MMM :  2023-05-28  -  2023-06-04\n",572      "[*********************100%%**********************]  1 of 1 completed\n",573      "MRK :  2023-01-08  -  2023-01-15\n",574      "MRK :  2023-01-15  -  2023-01-22\n",575      "MRK :  2023-01-22  -  2023-01-29\n",576      "MRK :  2023-01-29  -  2023-02-05\n",577      "MRK :  2023-02-05  -  2023-02-12\n",578      "MRK :  2023-02-12  -  2023-02-19\n",579      "MRK :  2023-02-19  -  2023-02-26\n",580      "MRK :  2023-02-26  -  2023-03-05\n",581      "MRK :  2023-03-05  -  2023-03-12\n",582      "MRK :  2023-03-12  -  2023-03-19\n",583      "MRK :  2023-03-19  -  2023-03-26\n",584      "MRK :  2023-03-26  -  2023-04-02\n",585      "MRK :  2023-04-02  -  2023-04-09\n",586      "MRK :  2023-04-09  -  2023-04-16\n",587      "MRK :  2023-04-16  -  2023-04-23\n",588      "MRK :  2023-04-23  -  2023-04-30\n",589      "MRK :  2023-04-30  -  2023-05-07\n",590      "MRK :  2023-05-07  -  2023-05-14\n",591      "MRK :  2023-05-14  -  2023-05-21\n",592      "MRK :  2023-05-21  -  2023-05-28\n",593      "MRK :  2023-05-28  -  2023-06-04\n",594      "[*********************100%%**********************]  1 of 1 completed\n",595      "MSFT :  2023-01-08  -  2023-01-15\n",596      "MSFT :  2023-01-15  -  2023-01-22\n",597      "MSFT :  2023-01-22  -  2023-01-29\n",598      "MSFT :  2023-01-29  -  2023-02-05\n",599      "MSFT :  2023-02-05  -  2023-02-12\n",600      "MSFT :  2023-02-12  -  2023-02-19\n",601      "MSFT :  2023-02-19  -  2023-02-26\n",602      "MSFT :  2023-02-26  -  2023-03-05\n",603      "MSFT :  2023-03-05  -  2023-03-12\n",604      "MSFT :  2023-03-12  -  2023-03-19\n",605      "MSFT :  2023-03-19  -  2023-03-26\n",606      "MSFT :  2023-03-26  -  2023-04-02\n",607      "MSFT :  2023-04-02  -  2023-04-09\n",608      "MSFT :  2023-04-09  -  2023-04-16\n",609      "MSFT :  2023-04-16  -  2023-04-23\n",610      "MSFT :  2023-04-23  -  2023-04-30\n",611      "MSFT :  2023-04-30  -  2023-05-07\n",612      "MSFT :  2023-05-07  -  2023-05-14\n",613      "MSFT :  2023-05-14  -  2023-05-21\n",614      "MSFT :  2023-05-21  -  2023-05-28\n",615      "MSFT :  2023-05-28  -  2023-06-04\n",616      "[*********************100%%**********************]  1 of 1 completed\n",617      "NKE :  2023-01-08  -  2023-01-15\n",618      "NKE :  2023-01-15  -  2023-01-22\n",619      "NKE :  2023-01-22  -  2023-01-29\n",620      "NKE :  2023-01-29  -  2023-02-05\n",621      "NKE :  2023-02-05  -  2023-02-12\n",622      "NKE :  2023-02-12  -  2023-02-19\n",623      "NKE :  2023-02-19  -  2023-02-26\n",624      "NKE :  2023-02-26  -  2023-03-05\n",625      "NKE :  2023-03-05  -  2023-03-12\n",626      "NKE :  2023-03-12  -  2023-03-19\n",627      "NKE :  2023-03-19  -  2023-03-26\n",628      "NKE :  2023-03-26  -  2023-04-02\n",629      "NKE :  2023-04-02  -  2023-04-09\n",630      "NKE :  2023-04-09  -  2023-04-16\n",631      "NKE :  2023-04-16  -  2023-04-23\n",632      "NKE :  2023-04-23  -  2023-04-30\n",633      "NKE :  2023-04-30  -  2023-05-07\n",634      "NKE :  2023-05-07  -  2023-05-14\n",635      "NKE :  2023-05-14  -  2023-05-21\n",636      "NKE :  2023-05-21  -  2023-05-28\n",637      "NKE :  2023-05-28  -  2023-06-04\n",638      "[*********************100%%**********************]  1 of 1 completed\n",639      "PG :  2023-01-08  -  2023-01-15\n",640      "PG :  2023-01-15  -  2023-01-22\n",641      "PG :  2023-01-22  -  2023-01-29\n",642      "PG :  2023-01-29  -  2023-02-05\n",643      "PG :  2023-02-05  -  2023-02-12\n",644      "PG :  2023-02-12  -  2023-02-19\n",645      "PG :  2023-02-19  -  2023-02-26\n",646      "PG :  2023-02-26  -  2023-03-05\n",647      "PG :  2023-03-05  -  2023-03-12\n",648      "PG :  2023-03-12  -  2023-03-19\n",649      "PG :  2023-03-19  -  2023-03-26\n",650      "PG :  2023-03-26  -  2023-04-02\n",651      "PG :  2023-04-02  -  2023-04-09\n",652      "PG :  2023-04-09  -  2023-04-16\n",653      "PG :  2023-04-16  -  2023-04-23\n",654      "PG :  2023-04-23  -  2023-04-30\n",655      "PG :  2023-04-30  -  2023-05-07\n",656      "PG :  2023-05-07  -  2023-05-14\n",657      "PG :  2023-05-14  -  2023-05-21\n",658      "PG :  2023-05-21  -  2023-05-28\n",659      "PG :  2023-05-28  -  2023-06-04\n",660      "[*********************100%%**********************]  1 of 1 completed\n",661      "TRV :  2023-01-08  -  2023-01-15\n",662      "TRV :  2023-01-15  -  2023-01-22\n",663      "TRV :  2023-01-22  -  2023-01-29\n",664      "TRV :  2023-01-29  -  2023-02-05\n",665      "TRV :  2023-02-05  -  2023-02-12\n",666      "TRV :  2023-02-12  -  2023-02-19\n",667      "TRV :  2023-02-19  -  2023-02-26\n",668      "TRV :  2023-02-26  -  2023-03-05\n",669      "TRV :  2023-03-05  -  2023-03-12\n",670      "TRV :  2023-03-12  -  2023-03-19\n",671      "TRV :  2023-03-19  -  2023-03-26\n"672     ]673    },674    {675     "name": "stdout",676     "output_type": "stream",677     "text": [678      "TRV :  2023-03-26  -  2023-04-02\n",679      "TRV :  2023-04-02  -  2023-04-09\n",680      "TRV :  2023-04-09  -  2023-04-16\n",681      "TRV :  2023-04-16  -  2023-04-23\n",682      "TRV :  2023-04-23  -  2023-04-30\n",683      "TRV :  2023-04-30  -  2023-05-07\n",684      "TRV :  2023-05-07  -  2023-05-14\n",685      "TRV :  2023-05-14  -  2023-05-21\n",686      "TRV :  2023-05-21  -  2023-05-28\n",687      "TRV :  2023-05-28  -  2023-06-04\n",688      "[*********************100%%**********************]  1 of 1 completed\n",689      "UNH :  2023-01-08  -  2023-01-15\n",690      "UNH :  2023-01-15  -  2023-01-22\n",691      "UNH :  2023-01-22  -  2023-01-29\n",692      "UNH :  2023-01-29  -  2023-02-05\n",693      "UNH :  2023-02-05  -  2023-02-12\n",694      "UNH :  2023-02-12  -  2023-02-19\n",695      "UNH :  2023-02-19  -  2023-02-26\n",696      "UNH :  2023-02-26  -  2023-03-05\n",697      "UNH :  2023-03-05  -  2023-03-12\n",698      "UNH :  2023-03-12  -  2023-03-19\n",699      "UNH :  2023-03-19  -  2023-03-26\n",700      "UNH :  2023-03-26  -  2023-04-02\n",701      "UNH :  2023-04-02  -  2023-04-09\n",702      "UNH :  2023-04-09  -  2023-04-16\n",703      "UNH :  2023-04-16  -  2023-04-23\n",704      "UNH :  2023-04-23  -  2023-04-30\n",705      "UNH :  2023-04-30  -  2023-05-07\n",706      "UNH :  2023-05-07  -  2023-05-14\n",707      "UNH :  2023-05-14  -  2023-05-21\n",708      "UNH :  2023-05-21  -  2023-05-28\n",709      "UNH :  2023-05-28  -  2023-06-04\n",710      "[*********************100%%**********************]  1 of 1 completed\n",711      "CRM :  2023-01-08  -  2023-01-15\n",712      "CRM :  2023-01-15  -  2023-01-22\n",713      "CRM :  2023-01-22  -  2023-01-29\n",714      "CRM :  2023-01-29  -  2023-02-05\n",715      "CRM :  2023-02-05  -  2023-02-12\n",716      "CRM :  2023-02-12  -  2023-02-19\n",717      "CRM :  2023-02-19  -  2023-02-26\n",718      "CRM :  2023-02-26  -  2023-03-05\n",719      "CRM :  2023-03-05  -  2023-03-12\n",720      "CRM :  2023-03-12  -  2023-03-19\n",721      "CRM :  2023-03-19  -  2023-03-26\n",722      "CRM :  2023-03-26  -  2023-04-02\n",723      "CRM :  2023-04-02  -  2023-04-09\n",724      "CRM :  2023-04-09  -  2023-04-16\n",725      "CRM :  2023-04-16  -  2023-04-23\n",726      "CRM :  2023-04-23  -  2023-04-30\n",727      "CRM :  2023-04-30  -  2023-05-07\n",728      "CRM :  2023-05-07  -  2023-05-14\n",729      "CRM :  2023-05-14  -  2023-05-21\n",730      "CRM :  2023-05-21  -  2023-05-28\n",731      "CRM :  2023-05-28  -  2023-06-04\n",732      "[*********************100%%**********************]  1 of 1 completed\n",733      "VZ :  2023-01-08  -  2023-01-15\n",734      "VZ :  2023-01-15  -  2023-01-22\n",735      "VZ :  2023-01-22  -  2023-01-29\n",736      "VZ :  2023-01-29  -  2023-02-05\n",737      "VZ :  2023-02-05  -  2023-02-12\n",738      "VZ :  2023-02-12  -  2023-02-19\n",739      "VZ :  2023-02-19  -  2023-02-26\n",740      "VZ :  2023-02-26  -  2023-03-05\n",741      "VZ :  2023-03-05  -  2023-03-12\n",742      "VZ :  2023-03-12  -  2023-03-19\n",743      "VZ :  2023-03-19  -  2023-03-26\n",744      "VZ :  2023-03-26  -  2023-04-02\n",745      "VZ :  2023-04-02  -  2023-04-09\n",746      "VZ :  2023-04-09  -  2023-04-16\n",747      "VZ :  2023-04-16  -  2023-04-23\n",748      "VZ :  2023-04-23  -  2023-04-30\n",749      "VZ :  2023-04-30  -  2023-05-07\n",750      "VZ :  2023-05-07  -  2023-05-14\n",751      "VZ :  2023-05-14  -  2023-05-21\n",752      "VZ :  2023-05-21  -  2023-05-28\n",753      "VZ :  2023-05-28  -  2023-06-04\n",754      "[*********************100%%**********************]  1 of 1 completed\n",755      "V :  2023-01-08  -  2023-01-15\n",756      "V :  2023-01-15  -  2023-01-22\n",757      "V :  2023-01-22  -  2023-01-29\n",758      "V :  2023-01-29  -  2023-02-05\n",759      "V :  2023-02-05  -  2023-02-12\n",760      "V :  2023-02-12  -  2023-02-19\n",761      "V :  2023-02-19  -  2023-02-26\n",762      "V :  2023-02-26  -  2023-03-05\n",763      "V :  2023-03-05  -  2023-03-12\n",764      "V :  2023-03-12  -  2023-03-19\n",765      "V :  2023-03-19  -  2023-03-26\n",766      "V :  2023-03-26  -  2023-04-02\n",767      "V :  2023-04-02  -  2023-04-09\n",768      "V :  2023-04-09  -  2023-04-16\n",769      "V :  2023-04-16  -  2023-04-23\n",770      "V :  2023-04-23  -  2023-04-30\n",771      "V :  2023-04-30  -  2023-05-07\n",772      "V :  2023-05-07  -  2023-05-14\n",773      "V :  2023-05-14  -  2023-05-21\n",774      "V :  2023-05-21  -  2023-05-28\n",775      "V :  2023-05-28  -  2023-06-04\n",776      "[*********************100%%**********************]  1 of 1 completed\n",777      "WBA :  2023-01-08  -  2023-01-15\n",778      "WBA :  2023-01-15  -  2023-01-22\n",779      "WBA :  2023-01-22  -  2023-01-29\n",780      "WBA :  2023-01-29  -  2023-02-05\n",781      "WBA :  2023-02-05  -  2023-02-12\n",782      "WBA :  2023-02-12  -  2023-02-19\n",783      "WBA :  2023-02-19  -  2023-02-26\n",784      "WBA :  2023-02-26  -  2023-03-05\n",785      "WBA :  2023-03-05  -  2023-03-12\n",786      "WBA :  2023-03-12  -  2023-03-19\n",787      "WBA :  2023-03-19  -  2023-03-26\n",788      "WBA :  2023-03-26  -  2023-04-02\n",789      "WBA :  2023-04-02  -  2023-04-09\n",790      "WBA :  2023-04-09  -  2023-04-16\n",791      "WBA :  2023-04-16  -  2023-04-23\n",792      "WBA :  2023-04-23  -  2023-04-30\n",793      "WBA :  2023-04-30  -  2023-05-07\n",794      "WBA :  2023-05-07  -  2023-05-14\n",795      "WBA :  2023-05-14  -  2023-05-21\n",796      "WBA :  2023-05-21  -  2023-05-28\n",797      "WBA :  2023-05-28  -  2023-06-04\n",798      "[*********************100%%**********************]  1 of 1 completed\n",799      "WMT :  2023-01-08  -  2023-01-15\n",800      "WMT :  2023-01-15  -  2023-01-22\n",801      "WMT :  2023-01-22  -  2023-01-29\n",802      "WMT :  2023-01-29  -  2023-02-05\n",803      "WMT :  2023-02-05  -  2023-02-12\n",804      "WMT :  2023-02-12  -  2023-02-19\n",805      "WMT :  2023-02-19  -  2023-02-26\n",806      "WMT :  2023-02-26  -  2023-03-05\n",807      "WMT :  2023-03-05  -  2023-03-12\n",808      "WMT :  2023-03-12  -  2023-03-19\n",809      "WMT :  2023-03-19  -  2023-03-26\n",810      "WMT :  2023-03-26  -  2023-04-02\n",811      "WMT :  2023-04-02  -  2023-04-09\n",812      "WMT :  2023-04-09  -  2023-04-16\n",813      "WMT :  2023-04-16  -  2023-04-23\n",814      "WMT :  2023-04-23  -  2023-04-30\n",815      "WMT :  2023-04-30  -  2023-05-07\n",816      "WMT :  2023-05-07  -  2023-05-14\n",817      "WMT :  2023-05-14  -  2023-05-21\n",818      "WMT :  2023-05-21  -  2023-05-28\n",819      "WMT :  2023-05-28  -  2023-06-04\n",820      "[*********************100%%**********************]  1 of 1 completed\n",821      "DIS :  2023-01-08  -  2023-01-15\n",822      "DIS :  2023-01-15  -  2023-01-22\n",823      "DIS :  2023-01-22  -  2023-01-29\n",824      "DIS :  2023-01-29  -  2023-02-05\n",825      "DIS :  2023-02-05  -  2023-02-12\n",826      "DIS :  2023-02-12  -  2023-02-19\n",827      "DIS :  2023-02-19  -  2023-02-26\n",828      "DIS :  2023-02-26  -  2023-03-05\n",829      "DIS :  2023-03-05  -  2023-03-12\n",830      "DIS :  2023-03-12  -  2023-03-19\n",831      "DIS :  2023-03-19  -  2023-03-26\n",832      "DIS :  2023-03-26  -  2023-04-02\n",833      "DIS :  2023-04-02  -  2023-04-09\n",834      "DIS :  2023-04-09  -  2023-04-16\n",835      "DIS :  2023-04-16  -  2023-04-23\n",836      "DIS :  2023-04-23  -  2023-04-30\n",837      "DIS :  2023-04-30  -  2023-05-07\n",838      "DIS :  2023-05-07  -  2023-05-14\n",839      "DIS :  2023-05-14  -  2023-05-21\n",840      "DIS :  2023-05-21  -  2023-05-28\n",841      "DIS :  2023-05-28  -  2023-06-04\n",842      "[*********************100%%**********************]  1 of 1 completed\n",843      "DOW :  2023-01-08  -  2023-01-15\n",844      "DOW :  2023-01-15  -  2023-01-22\n",845      "DOW :  2023-01-22  -  2023-01-29\n",846      "DOW :  2023-01-29  -  2023-02-05\n",847      "DOW :  2023-02-05  -  2023-02-12\n",848      "DOW :  2023-02-12  -  2023-02-19\n",849      "DOW :  2023-02-19  -  2023-02-26\n",850      "DOW :  2023-02-26  -  2023-03-05\n",851      "DOW :  2023-03-05  -  2023-03-12\n",852      "DOW :  2023-03-12  -  2023-03-19\n",853      "DOW :  2023-03-19  -  2023-03-26\n",854      "DOW :  2023-03-26  -  2023-04-02\n",855      "DOW :  2023-04-02  -  2023-04-09\n",856      "DOW :  2023-04-09  -  2023-04-16\n",857      "DOW :  2023-04-16  -  2023-04-23\n",858      "DOW :  2023-04-23  -  2023-04-30\n",859      "DOW :  2023-04-30  -  2023-05-07\n",860      "DOW :  2023-05-07  -  2023-05-14\n",861      "DOW :  2023-05-14  -  2023-05-21\n",862      "DOW :  2023-05-21  -  2023-05-28\n",863      "DOW :  2023-05-28  -  2023-06-04\n"864     ]865    }866   ],867   "source": [868    "for symbol in DOW_30:\n",869    "    prepare_data_for_company(symbol)\n",870    "#     prepare_data_for_company(symbol, False)"871   ]872  },873  {874   "cell_type": "markdown",875   "id": "af655d8b",876   "metadata": {},877   "source": [878    "# Generate Prompt from Financial Data"879   ]880  },881  {882   "cell_type": "code",883   "execution_count": 65,884   "id": "5a53c0ae",885   "metadata": {886    "scrolled": true887   },888   "outputs": [],889   "source": [890    "def get_company_prompt(symbol):\n",891    "    \n",892    "    profile = finnhub_client.company_profile2(symbol=symbol)\n",893    "\n",894    "    company_template = \"[Company Introduction]:\\n\\n{name} is a leading entity in the {finnhubIndustry} sector. Incorporated and publicly traded since {ipo}, the company has established its reputation as one of the key players in the market. As of today, {name} has a market capitalization of {marketCapitalization:.2f} in {currency}, with {shareOutstanding:.2f} shares outstanding.\" \\\n",895    "        \"\\n\\n{name} operates primarily in the {country}, trading under the ticker {ticker} on the {exchange}. As a dominant force in the {finnhubIndustry} space, the company continues to innovate and drive progress within the industry.\"\n",896    "\n",897    "    formatted_str = company_template.format(**profile)\n",898    "    \n",899    "    return formatted_str\n",900    "\n",901    "\n",902    "def get_prompt_by_row(symbol, row):\n",903    "\n",904    "    start_date = row['Start Date'] if isinstance(row['Start Date'], str) else row['Start Date'].strftime('%Y-%m-%d')\n",905    "    end_date = row['End Date'] if isinstance(row['End Date'], str) else row['End Date'].strftime('%Y-%m-%d')\n",906    "    term = 'increased' if row['End Price'] > row['Start Price'] else 'decreased'\n",907    "    head = \"From {} to {}, {}'s stock price {} from {:.2f} to {:.2f}. Company news during this period are listed below:\\n\\n\".format(\n",908    "        start_date, end_date, symbol, term, row['Start Price'], row['End Price'])\n",909    "    \n",910    "    news = json.loads(row[\"News\"])\n",911    "    news = [\"[Headline]: {}\\n[Summary]: {}\\n\".format(\n",912    "        n['headline'], n['summary']) for n in news if n['date'][:8] <= end_date.replace('-', '') and \\\n",913    "        not n['summary'].startswith(\"Looking for stock market analysis and research with proves results?\")]\n",914    "\n",915    "    basics = json.loads(row['Basics'])\n",916    "    if basics:\n",917    "        basics = \"Some recent basic financials of {}, reported at {}, are presented below:\\n\\n[Basic Financials]:\\n\\n\".format(\n",918    "            symbol, basics['period']) + \"\\n\".join(f\"{k}: {v}\" for k, v in basics.items() if k != 'period')\n",919    "    else:\n",920    "        basics = \"[Basic Financials]:\\n\\nNo basic financial reported.\"\n",921    "    \n",922    "    return head, news, basics\n",923    "\n",924    "\n",925    "def sample_news(news, k=5):\n",926    "    \n",927    "    return [news[i] for i in sorted(random.sample(range(len(news)), k))]\n",928    "\n",929    "\n",930    "def map_bin_label(bin_lb):\n",931    "    \n",932    "    lb = bin_lb.replace('U', 'up by ')\n",933    "    lb = lb.replace('D', 'down by ')\n",934    "    lb = lb.replace('1', '0-1%')\n",935    "    lb = lb.replace('2', '1-2%')\n",936    "    lb = lb.replace('3', '2-3%')\n",937    "    lb = lb.replace('4', '3-4%')\n",938    "    if lb.endswith('+'):\n",939    "        lb = lb.replace('5+', 'more than 5%')\n",940    "#         lb = lb.replace('5+', '5+%')\n",941    "    else:\n",942    "        lb = lb.replace('5', '4-5%')\n",943    "    \n",944    "    return lb\n",945    "\n",946    "\n",947    "def get_all_prompts(symbol, min_past_weeks=1, max_past_weeks=3, with_basics=True):\n",948    "\n",949    "    \n",950    "    if with_basics:\n",951    "        df = pd.read_csv(f'{DATA_DIR}/{symbol}_{START_DATE}_{END_DATE}.csv')\n",952    "    else:\n",953    "        df = pd.read_csv(f'{DATA_DIR}/{symbol}_{START_DATE}_{END_DATE}_nobasics.csv')\n",954    "    \n",955    "    company_prompt = get_company_prompt(symbol)\n",956    "\n",957    "    prev_rows = []\n",958    "    all_prompts = []\n",959    "\n",960    "    for row_idx, row in df.iterrows():\n",961    "\n",962    "        prompt = \"\"\n",963    "        if len(prev_rows) >= min_past_weeks:\n",964    "            idx = min(random.choice(range(min_past_weeks, max_past_weeks+1)), len(prev_rows))\n",965    "            for i in range(-idx, 0):\n",966    "                # Add Price Movement (Head)\n",967    "                prompt += \"\\n\" + prev_rows[i][0]\n",968    "                # Add News of previous weeks\n",969    "                sampled_news = sample_news(\n",970    "                    prev_rows[i][1],\n",971    "                    min(5, len(prev_rows[i][1]))\n",972    "                )\n",973    "                if sampled_news:\n",974    "                    prompt += \"\\n\".join(sampled_news)\n",975    "                else:\n",976    "                    prompt += \"No relative news reported.\"\n",977    "\n",978    "        head, news, basics = get_prompt_by_row(symbol, row)\n",979    "\n",980    "        prev_rows.append((head, news, basics))\n",981    "        if len(prev_rows) > max_past_weeks:\n",982    "            prev_rows.pop(0)  \n",983    "\n",984    "        if not prompt:\n",985    "            continue\n",986    "\n",987    "        prediction = map_bin_label(row['Bin Label'])\n",988    "        \n",989    "        prompt = company_prompt + '\\n' + prompt + '\\n' + basics\n",990    "        prompt += f\"\\n\\nBased on all the information before {row['Start Date']}, let's first analyze the positive developments and potential concerns for {symbol}. Come up with 2-4 most important factors respectively and keep them concise. Most factors should be inferred from company related news. \" \\\n",991    "            f\"Then let's assume your prediction for next week ({row['Start Date']} to {row['End Date']}) is {prediction}. Provide a summary analysis to support your prediction. The prediction result need to be inferred from your analysis at the end, and thus not appearing as a foundational factor of your analysis.\"\n",992    "\n",993    "        all_prompts.append(prompt.strip())\n",994    "    \n",995    "    return all_prompts"996   ]997  },998  {999   "cell_type": "code",1000   "execution_count": null,1001   "id": "92208b72",1002   "metadata": {},1003   "outputs": [],1004   "source": [1005    "B_INST, E_INST = \"[INST]\", \"[/INST]\"\n",1006    "B_SYS, E_SYS = \"<<SYS>>\\n\", \"\\n<</SYS>>\\n\\n\"\n",1007    "\n",1008    "\n",1009    "SYSTEM_PROMPT = \"You are a seasoned stock market analyst. Your task is to list the positive developments and potential concerns for companies based on relevant news and basic financials from the past weeks, then provide an analysis and prediction for the companies' stock price movement for the upcoming week. \" \\\n",1010    "    \"Your answer format should be as follows:\\n\\n[Positive Developments]:\\n1. ...\\n\\n[Potential Concerns]:\\n1. ...\\n\\n[Prediction & Analysis]:\\n...\\n\"\n",1011    "\n",1012    "print(SYSTEM_PROMPT)\n",1013    "\n",1014    "# prompts = get_all_prompts(\"AAPL\", 1, 3)\n",1015    "# prompts = get_all_prompts(\"MSFT\", 1, 3, False)\n",1016    "prompts = get_all_prompts(\"TRV\", 1, 4)\n",1017    "\n",1018    "print(prompts[0])\n"1019   ]1020  },1021  {1022   "cell_type": "markdown",1023   "id": "2b010a45",1024   "metadata": {},1025   "source": [1026    "# Request to GPT-4 for Financial Analysis"1027   ]1028  },1029  {1030   "cell_type": "code",1031   "execution_count": 86,1032   "id": "3e355117",1033   "metadata": {},1034   "outputs": [],1035   "source": [1036    "def append_to_csv(filename, input_data, output_data):\n",1037    "    \n",1038    "    with open(filename, mode='a', newline='') as file:\n",1039    "        writer = csv.writer(file)\n",1040    "        writer.writerow([input_data, output_data])\n",1041    "\n",1042    "        \n",1043    "def initialize_csv(filename):\n",1044    "    \n",1045    "    with open(filename, mode='w', newline='') as file:\n",1046    "        writer = csv.writer(file)\n",1047    "        writer.writerow([\"prompt\", \"answer\"])\n",1048    "\n",1049    "\n",1050    "def query_gpt4(symbol_list, min_past_weeks=1, max_past_weeks=3, with_basics=True):\n",1051    "\n",1052    "    for symbol in symbol_list:\n",1053    "        \n",1054    "        csv_file = f'{DATA_DIR}/{symbol}_{START_DATE}_{END_DATE}_gpt-4.csv' if with_basics else \\\n",1055    "                   f'{DATA_DIR}/{symbol}_{START_DATE}_{END_DATE}_nobasics_gpt-4.csv'\n",1056    "        \n",1057    "        if not os.path.exists(csv_file):\n",1058    "            initialize_csv(csv_file)\n",1059    "            pre_done = 0\n",1060    "        else:\n",1061    "            df = pd.read_csv(csv_file)\n",1062    "            pre_done = len(df)\n",1063    "\n",1064    "        prompts = get_all_prompts(symbol, min_past_weeks, max_past_weeks, with_basics)\n",1065    "\n",1066    "        for i, prompt in enumerate(prompts):\n",1067    "            \n",1068    "            if i < pre_done:\n",1069    "                continue\n",1070    "\n",1071    "            print(f\"{symbol} - {i}\")\n",1072    "            \n",1073    "            cnt = 0\n",1074    "            while cnt < 5:\n",1075    "                try:\n",1076    "                    completion = client.chat.completions.create(\n",1077    "                        model=\"gpt-4\",\n",1078    "                        messages=[\n",1079    "                            {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",1080    "                            {\"role\": \"user\", \"content\": prompt}\n",1081    "                          ]\n",1082    "                    )\n",1083    "                    break    \n",1084    "                except Exception:\n",1085    "                    cnt += 1\n",1086    "                    print(f'retry cnt {cnt}')\n",1087    "            \n",1088    "            answer = completion.choices[0].message.content if cnt < 5 else \"\"\n",1089    "            append_to_csv(csv_file, prompt, answer)\n",1090    "      "1091   ]1092  },1093  {1094   "cell_type": "code",1095   "execution_count": 121,1096   "id": "a9ff6ff3",1097   "metadata": {1098    "scrolled": true1099   },1100   "outputs": [1101    {1102     "name": "stdout",1103     "output_type": "stream",1104     "text": [1105      "WBA - 12\n",1106      "WBA - 13\n",1107      "WBA - 14\n",1108      "WBA - 15\n",1109      "WBA - 16\n",1110      "WBA - 17\n",1111      "WBA - 18\n",1112      "WBA - 19\n"1113     ]1114    }1115   ],1116   "source": [1117    "# query_gpt4(DOW_30, 1, 3)\n",1118    "query_gpt4(DOW_30, 1, 4)\n",1119    "# query_gpt4(['WBA'], 1, 4)"1120   ]1121  },1122  {1123   "cell_type": "markdown",1124   "id": "238ba9f0",1125   "metadata": {},1126   "source": [1127    "# Transform into Llama2 Training Format"1128   ]1129  },1130  {1131   "cell_type": "code",1132   "execution_count": 93,1133   "id": "d2627f5a",1134   "metadata": {},1135   "outputs": [],1136   "source": [1137    "def gpt4_to_llama(symbol, with_basics=True):\n",1138    "    \n",1139    "    csv_file = f'{DATA_DIR}/{symbol}_{START_DATE}_{END_DATE}_gpt-4.csv' if with_basics else \\\n",1140    "                   f'{DATA_DIR}/{symbol}_{START_DATE}_{END_DATE}_nobasics_gpt-4.csv'\n",1141    "    \n",1142    "    df = pd.read_csv(csv_file)\n",1143    "    \n",1144    "    prompts, answers, periods, labels = [], [], [], []\n",1145    "    \n",1146    "    for i, row in df.iterrows():\n",1147    "        \n",1148    "        prompt, answer = row['prompt'], row['answer']\n",1149    "        \n",1150    "        res = re.search(r\"Then let's assume your prediction for next week \\((.*)\\) is ((:?up|down) by .*%).\", prompt)\n",1151    "        \n",1152    "        period, label = res.group(1), res.group(2)\n",1153    "#         label = label.replace('more than 5', '5+')\n",1154    "        \n",1155    "        prompt = re.sub(\n",1156    "            r\"Then let's assume your prediction for next week \\((.*)\\) is (up|down) by ((:?.*)%). Provide a summary analysis to support your prediction. The prediction result need to be inferred from your analysis at the end, and thus not appearing as a foundational factor of your analysis.\", \n",1157    "            f\"Then make your prediction of the {symbol} stock price movement for next week ({period}). Provide a summary analysis to support your prediction.\",\n",1158    "            prompt\n",1159    "        )\n",1160    "        try:\n",1161    "            answer = re.sub(\n",1162    "                r\"\\[Prediction & Analysis\\]:\\s*\",\n",1163    "                f\"[Prediction & Analysis]:\\nPrediction: {label.capitalize()}\\nAnalysis: \",\n",1164    "                answer\n",1165    "            )\n",1166    "        except Exception:\n",1167    "            print(symbol, i)\n",1168    "            print(label)\n",1169    "            print(answer)\n",1170    "            continue\n",1171    "            \n",1172    "        new_system_prompt = SYSTEM_PROMPT.replace(':\\n...', '\\nPrediction: ...\\nAnalysis: ...')\n",1173    "#         new_system_prompt = SYSTEM_PROMPT.replace(':\\n...', '\\nPrediction: {Up|Down} by {1-2|2-3|3-4|4-5|5+}%\\nAnalysis: ...')\n",1174    "        \n",1175    "        prompt = B_INST + B_SYS + new_system_prompt + E_SYS + prompt + E_INST\n",1176    "        \n",1177    "        prompts.append(prompt)\n",1178    "        answers.append(answer)\n",1179    "        periods.append(period)\n",1180    "        labels.append(label)\n",1181    "        \n",1182    "    return {\n",1183    "        \"prompt\": prompts,\n",1184    "        \"answer\": answers,\n",1185    "        \"period\": periods,\n",1186    "        \"label\": labels,\n",1187    "    }\n",1188    "\n",1189    "\n",1190    "def create_dataset(symbol_list, train_ratio=0.8, with_basics=True):\n",1191    "\n",1192    "    train_dataset_list = []\n",1193    "    test_dataset_list = []\n",1194    "\n",1195    "    for symbol in symbol_list:\n",1196    "\n",1197    "        data_dict = gpt4_to_llama(symbol, with_basics)\n",1198    "#         print(data_dict['prompt'][-1])\n",1199    "#         print(data_dict['answer'][-1])\n",1200    "        symbols = [symbol] * len(data_dict['label'])\n",

Showing the first 1,200 of 1546 lines. Download the file for the rest.