{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Stock Price Data" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "import os\n", "import pandas as pd\n", "\n", "stock_prices = {}\n", "\n", "for fn in os.listdir(\"prices\"):\n", " # Get the name of the file without extension \"aapl.csv\" -> \"aapl\"\n", " name = fn.split(\".\")[0]\n", " stock_prices[name] = pd.read_csv(os.path.join(\"prices\", fn))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We chose a dictionary where the keys are the stock symbols and the values are DataFrames with the from the corresponding CSV file.\n", "\n", "Let's display the data stored for the `aapl` stock symbol:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datecloseopenhighlowvolume
02007-01-0383.80000286.28999986.57999981.899999309579900
12007-01-0485.65999884.05000185.94999883.820003211815100
22007-01-0585.04999785.77000086.19999784.400002208685400
32007-01-0885.47000085.95999886.52999885.280003199276700
42007-01-0992.57000386.45000392.97999985.150000837324600
\n", "
" ], "text/plain": [ " date close open high low volume\n", "0 2007-01-03 83.800002 86.289999 86.579999 81.899999 309579900\n", "1 2007-01-04 85.659998 84.050001 85.949998 83.820003 211815100\n", "2 2007-01-05 85.049997 85.770000 86.199997 84.400002 208685400\n", "3 2007-01-08 85.470000 85.959998 86.529998 85.280003 199276700\n", "4 2007-01-09 92.570003 86.450003 92.979999 85.150000 837324600" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stock_prices[\"aapl\"].head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Computing Aggregates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Computing Average Closing Prices " ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "avg_closing_prices = {}\n", "\n", "for stock_sym in stock_prices:\n", " avg_closing_prices[stock_sym] = stock_prices[stock_sym][\"close\"].mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Displaying the average closing prices" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eqix 165.3847721150579\n", "club 7.270509651737427\n", "bmrc 39.35481079459455\n", "cald 8.608965250965264\n", "cybe 9.964861003860992\n", "bbry 43.67659082355207\n", "chscp 29.07304635598456\n", "essa 12.126070440047481\n", "cprx 1.976200772200771\n", "arrs 17.10461388532818\n", "ctic 1.4943663119691135\n", "adrd 22.51748262046331\n", "arna 4.915745173745166\n", "ffic 16.593648647876414\n", "ca 25.746281860231644\n", "alot 10.28669884208494\n", "csfl 11.947644780694985\n", "cern 65.04237453166031\n", "fhco 4.28845945945947\n", "dvax 6.0337528984555995\n", "exel 6.616277998455593\n", "abcb 17.990475994208477\n", "alog 64.74335521467185\n", "bncn 13.986131252895746\n", "eltk 1.5323436293436348\n", "fbiz 22.95887644826253\n", "brks 10.52473359227799\n", "cunb 15.99822393513515\n", "clrb 1.204571143629345\n", "agen 2.9998899559845587\n", "amzn 275.1340775710431\n", "eqfn 5.558436266023189\n", "evep 31.358648642471\n", "bnso 1.717254826254819\n", "asys 8.914054046332067\n", "fisi 19.938084950965262\n", "cbio 8.433602686100393\n", "flic 27.73225096177597\n", "bmrn 50.521710407335874\n", "bcbp 11.546521235135131\n", "aezs 1.739144594980703\n", "cmls 3.678938223938218\n", "apwc 3.2336409266409234\n", "cnit 3.6047451737451803\n", "arkr 20.460409264092682\n", "dave 12.284664105405401\n", "ctas 50.47888414247106\n", "cldx 9.006351276061771\n", "apog 26.00773359150577\n", "cbak 2.437895752895755\n", "efii 25.840223945945958\n", "crws 5.629305019305021\n", "finl 17.241752891505783\n", "abco 47.647057967567655\n", "emkr 4.458320463320471\n", "boch 6.063119691119678\n", "ffhl 2.192687258687256\n", "cbrl 76.63736287992297\n", "botj 9.858123591891871\n", "fcnca 200.25248278146725\n", "aame 2.7796795366795344\n", "achc 24.047795338223956\n", "cake 34.31267570270274\n", "ccbg 16.266409268725862\n", "fmbi 17.35091119613901\n", "cass 42.14163373474898\n", "arlz 7.441718146718154\n", "elgx 8.97644016370655\n", "atrs 2.0350231660231692\n", "dswl 3.529177606177609\n", "csbr 1.228244384585441\n", "adru 22.371667961776062\n", "cetv 24.057965252509586\n", "astc 1.4152123552123521\n", "arry 5.1828996138996075\n", "ewbc 26.819362960231615\n", "atro 31.862567476834023\n", "anik 20.774474920463295\n", "agys 10.303613901544395\n", "ffiv 86.294579173745\n", "dxyn 6.331316601930499\n", "cdor 2.605772193822393\n", "avdl 10.103034740154433\n", "arwr 4.130016216216213\n", "fbss 15.228308892278005\n", "clmt 23.327073368339757\n", "afsi 26.699826589189257\n", "cmtl 30.96300771621617\n", "cmcsa 35.90450579227791\n", "denn 5.761945945945953\n", "ccmp 39.67996139150581\n", "depo 8.988274123938211\n", "drys 13.498539550193074\n", "cur 1.907691699604743\n", "cban 8.23264092277992\n", "emitf 12.964027813127366\n", "dwch 8.038034755212372\n", "cytr 1.9986748837837829\n", "cswc 77.75590740695002\n", "ctrn 20.54685713976835\n", "efsc 18.541934354826246\n", "cinf 42.25041697451733\n", "dmrc 22.26364027351096\n", "cbfv 18.72878765019304\n", "cyrn 2.7131410714285673\n", "esca 8.18794594324325\n", "ffkt 19.472922793050166\n", "chfn 15.708602240154498\n", "bldp 2.3273861003861005\n", "cent 11.43199613474903\n", "ceva 17.124220071042476\n", "dakt 12.215868713513515\n", "crnt 6.269598454826258\n", "axdx 8.656428568339782\n", "cntf 2.6595637119691156\n", "exas 9.390011581081083\n", "admp 1.7122164397683428\n", "bwen 5.326498072200769\n", "cytx 3.3293219922779875\n", "cece 9.062675674517372\n", "conn 21.148482605791525\n", "arci 3.1327799227799207\n", "bbgi 5.33829729729731\n", "expo 46.09936678262553\n", "cgnx 32.55762165714287\n", "cwst 6.658471042471043\n", "fccy 10.67951246486487\n", "chci 1.4581224154440184\n", "cplp 9.927482215019769\n", "eeft 35.11525484749034\n", "cpsi 44.44345173899618\n", "aaww 44.331602290347405\n", "adra 27.3514517397683\n", "belfa 21.04013901081089\n", "bdge 24.120351324324314\n", "arii 31.491413133590704\n", "aiq 10.171544398841688\n", "esnd 40.79829342664082\n", "acta 11.32055983706564\n", "allt 9.18001930270271\n", "crtn 2.1850579150579117\n", "cmco 20.10901159999999\n", "expe 53.78315830308872\n", "asur 3.8731236637065614\n", "adre 39.14505407104248\n", "aaxn 11.863907341698843\n", "dltr 57.418077247490366\n", "dmlp 22.283281861003864\n", "byfc 3.4977644787644735\n", "eslt 58.57627412471036\n", "dwsn 6.194910959459459\n", "acet 12.655212363320476\n", "dest 18.788065616216212\n", "bgfv 13.15647104671812\n", "cemi 2.4821776061776015\n", "amsf 30.34488032162161\n", "edap 3.235803088803086\n", "bbby 50.18332436486479\n", "cfnl 14.268891889189167\n", "crvl 37.443864903474925\n", "etfc 14.956660266795353\n", "cort 3.299548262548255\n", "airt 12.430108102316591\n", "cfbk 2.1748416988416936\n", "cvcy 9.671478766409258\n", "camp 9.500046333590733\n", "cohr 53.71215058262553\n", "banr 26.60423480193051\n", "bldr 6.945467184942081\n", "dvcr 7.688459461389958\n", "aapl 257.17654040231656\n", "akrx 15.387104233590746\n", "clct 14.436679601158307\n", "ccoi 23.236517377606194\n", "bdsi 4.820706564478758\n", "discb 39.652757378378595\n", "dcom 14.7273552096525\n", "eei 12.263416957915059\n", "avhi 22.406231679150594\n", "ahgp 38.20530885868731\n", "bcrx 6.095837838223926\n", "banf 49.6434980416988\n", "buse 10.92032454362932\n", "cgnt 1.5946138996139008\n", "atlo 22.101030884556003\n", "alsk 5.995567569498072\n", "blfs 0.8122763011583004\n", "arql 3.874424710424698\n", "anat 97.93825093397685\n", "cray 15.423347486486477\n", "capr 2.4732474629196006\n", "cobz 10.071579151737454\n", "atni 47.67885716216225\n", "drrx 2.3527799227799227\n", "cbsh 42.69090342934362\n", "amtd 23.49051739768336\n", "ande 41.829026980308846\n", "bybk 6.642911204633245\n", "ebix 32.53216976293444\n", "fbms 14.22483009266407\n", "cenx 18.395567551737482\n", "amswa 8.076181467181465\n", "cyccp 4.965254826254832\n", "crds 1.8903166015444017\n", "cash 32.26195366332041\n", "algt 83.70168345444011\n", "acxm 18.26306178378379\n", "bwld 89.39383399150582\n", "emcf 22.21991505482626\n", "dysl 1.8631660231660265\n", "axas 2.836629343629344\n", "adbe 51.19943628416986\n", "ffin 42.17889953474895\n", "asfi 11.159220083783804\n", "chke 19.15281466100384\n", "biib 164.53822006139012\n", "ainv 9.949749044015475\n", "evbs 8.454656358687263\n", "falc 3.609212355212365\n", "call 10.101200768339778\n", "caas 7.6334401583011715\n", "educ 5.948108107721997\n", "asna 17.81176063204633\n", "eght 5.531308880694991\n", "amnb 22.11375289034745\n", "cffn 21.416077199999965\n", "cgo 13.773633200000043\n", "centa 10.959813017140611\n", "banfp 26.415837825482622\n", "dtrm 3.723555984555983\n", "entg 9.497733591505801\n", "bmra 0.901011583011584\n", "cvly 15.41210743397683\n", "bbh 113.28309655096503\n", "fast 44.40756368957524\n", "epay 20.796501912355225\n", "acgl 63.325907376833804\n", "aste 37.29283010849429\n", "dxtr 2.749339768339764\n", "ea 37.33655212046332\n", "alny 39.17148648803088\n", "endp 37.3472664173746\n", "csq 10.269231660231666\n", "exls 26.462393820849456\n", "artx 2.0992316602316587\n", "ebtc 17.20153669227798\n", "bbsi 30.42552507722014\n", "cme 230.29466011003882\n", "alqa 1.4052982830115854\n", "cytk 4.742564193822396\n", "csco 23.628822402702724\n", "fisv 67.52742853513507\n", "eric 13.297131263706602\n", "arlp 46.94279149343634\n", "coke 80.56527417181458\n", "cacb 7.0127567953667915\n", "colb 23.599138993050214\n", "clwt 2.61713222471043\n", "cffi 32.43315446061778\n", "dspg 9.215841698069477\n", "farm 20.19316601351357\n", "abio 2.2518008000000007\n", "evlv 3.9725907335907293\n", "cznc 18.655586874131235\n", "amat 17.116648652509628\n", "algn 36.75162934864863\n", "cvti 10.437138995752889\n", "acor 27.47286873938217\n", "clbs 2.282069498069504\n", "celg 85.09483015984559\n", "ccur 4.987629343629343\n", "akam 42.818513486872554\n", "cgen 4.8826216216216265\n", "agii 40.817575337065705\n", "fll 2.4715752895752834\n", "cbli 3.6102664104247073\n", "ctg 9.576830107335903\n", "aris 2.166138996138996\n", "acad 13.82358687490347\n", "dox 37.75929727606179\n", "esbk 19.142613947490393\n", "ceco 13.657787633204645\n", "amsc 13.049243415057907\n", "afam 29.43431277065635\n", "belfb 21.68070270579156\n", "axti 3.8255212355212342\n", "bpop 17.295227834362944\n", "cool 1.547598892277985\n", "ccne 15.950173729729757\n", "cfcb 7.657006999613776\n", "extr 3.537501930501927\n", "flir 30.795725856370716\n", "aegn 19.899729709266428\n", "bde 7.226011583011578\n", "esxb 4.032393822393823\n", "csx 37.15074516833986\n", "car 24.829617774131286\n", "apdn 0.824100993822394\n", "ancx 12.260374515443988\n", "elos 11.393289570270278\n", "caty 21.829671827027056\n", "anip 17.23436011003862\n", "cpsh 1.9615839865149551\n", "atrc 11.841710419691118\n", "arcb 26.58224713436293\n", "dram 2.059262837837842\n", "dzsi 1.53823166023166\n", "asrv 2.801169884169885\n", "avnw 5.723947867953683\n", "cmct 11.270061818532817\n", "dxcm 28.520806964092642\n", "cpss 3.9003629343629274\n", "azpn 24.926833974517358\n", "fele 38.772602315830156\n", "civb 9.309308876833981\n", "cdzi 10.901432437837844\n", "cvv 8.62438610193052\n", "amag 32.49387646872589\n", "dnbf 16.71552883088808\n", "dish 39.2437143050192\n", "cspi 6.052857142857142\n", "cfnb 13.825111963706616\n", "egbn 23.785038910038622\n", "cown 5.9488223938223905\n", "edgw 5.274084942084936\n", "ctxs 58.020123949420885\n", "blkb 33.755378381853255\n", "diod 22.849976832046355\n", "avir 3.403666176833971\n", "else 4.060752895752895\n", "bofi 31.21098066177611\n", "cacc 95.4989575660232\n", "chco 39.566401530115876\n", "csgs 23.51506178725866\n", "aobc 10.594061780308893\n", "cpah 1.411618944844124\n", "brid 8.972416988030917\n", "amd 6.005552123166033\n", "cvbf 12.391718153667941\n", "artna 20.97944401119688\n", "bpopn 21.347073360231676\n", "amri 11.358054055598446\n", "cvgw 29.564660229343623\n", "chdn 72.21778764864874\n", "aplp 23.031826284169938\n", "arcc 14.933262535135157\n", "axgn 3.8202316602316624\n", "camt 2.402606177606176\n", "cycc 2.6675635610038686\n", "amgn 92.2331003965252\n", "adxs 3.166938612355214\n", "asrvp 25.09542473243236\n", "apri 1.8681738996139001\n", "bgcp 7.16218918918918\n", "bsqr 4.305370656370662\n", "cnob 13.295115791505804\n", "actg 15.997490346718152\n", "czfc 9.846169883783775\n", "casy 58.49573744092667\n", "csiq 16.0704556007722\n", "bebe 6.418196949806945\n", "cree 36.52946716216218\n", "cyan 4.164428571428576\n", "aemd 1.398042471042472\n", "cost 96.17006946409262\n", "cart 6.340498070270267\n", "cac 35.503845555598524\n", "dgii 10.49529343552125\n", "bbox 25.997579137451716\n", "brew 8.094903474517379\n", "amkr 6.955822393436287\n", "audc 4.375227799227813\n", "atvi 19.922046341312775\n", "cdti 4.0017802494208485\n", "bjri 31.854320508108007\n", "dhil 104.54806553783777\n", "ecol 28.210664105405353\n", "fcco 10.53771429034749\n", "abax 34.57868337992275\n", "biol 2.638343634749034\n", "cnmd 31.91744790231663\n", "bokf 56.16893048532805\n", "cy 14.15815303590736\n", "csii 13.97828185444016\n", "atri 228.3897761598455\n", "awre 4.083567567567558\n", "chfc 27.265100385714277\n", "dgicb 18.359567528185362\n", "adtn 23.847494206177636\n", "atec 2.7025780428571435\n", "amed 31.845227816216273\n", "bwinb 23.67302319266411\n", "aaon 23.61738606177606\n", "bcpc 41.03619678301155\n", "bstc 25.102737436293438\n", "bfin 10.907671818146719\n", "dcth 2.8911660231660314\n", "esio 11.514586870656366\n", "amwd 34.320258717374536\n", "drad 3.0539884169884184\n", "casi 1.617906349034749\n", "crox 16.67639382548266\n", "cui 3.2413753598455615\n", "fizz 19.844949835907336\n", "achn 5.941177606949804\n", "egle 8.046072912741305\n", "ctrp 45.16322006139004\n", "apps 1.8256061776061814\n", "chy 12.45603860888028\n", "dwaq 55.77838609343615\n", "adi 42.24018144826256\n", "aey 2.6871505791505896\n", "cers 4.246478764478765\n", "brkr 15.865401542471073\n", "cthr 2.464478764478758\n", "anss 62.3252007814673\n", "ezpw 15.789984552895788\n", "emci 25.215687249420863\n", "bksc 13.711621528571428\n", "core 44.285764482239365\n", "chrw 61.98583785675692\n", "ango 14.80228958725868\n", "colm 53.72719691235514\n", "bwina 23.124262511969086\n", "disca 39.23467980347492\n", "fcap 20.154212339768318\n", "alxn 97.1099267011583\n", "becn 25.59151352046335\n", "attu 4.317359073359067\n", "carv 6.872226529729721\n", "bont 10.085776056370651\n", "chi 12.60280308841697\n", "acls 3.343806946718146\n", "fbnc 16.118270270270266\n", "adsk 42.247594632818625\n", "exfo 4.9955637065637095\n", "flex 8.81742857181465\n", "brkl 10.24170270270269\n", "cigi 32.206258406177554\n", "cnbka 30.143177581081083\n", "cutr 11.777822398841714\n", "flws 5.781957528957535\n", "cvlt 39.00099612741321\n", "dxpe 38.615139020849405\n", "faro 34.87222776949802\n", "amot 9.967243240154449\n", "egt 1.329351351351346\n", "abmd 33.222420861003854\n", "creg 1.6028996138996154\n", "aeis 20.003212338996093\n", "ctib 4.690494208494198\n", "alks 28.143567575675657\n", "alco 34.68778766100379\n", "arcw 5.706739867181469\n", "cnty 4.444320463320463\n", "bmtc 23.78357530308884\n", "crai 27.154254832432418\n", "bsrr 16.140177604633163\n", "aubn 23.598679533976888\n", "atax 5.9997528957529\n", "aray 7.88737622339181\n", "eng 3.644196910810804\n", "cidm 1.9935057915057948\n", "cprt 37.01769495057904\n", "djco 110.25166789845554\n", "flxs 22.836366786486494\n", "ctws 30.461830105405415\n", "cdns 14.246918911196932\n", "calm 38.327386108108094\n", "acfc 5.596733538610015\n", "chnr 8.126077209652516\n", "ffbc 16.002316604633176\n", "bidu 193.5319112447879\n", "ctsh 57.91491119652505\n", "cmpr 51.0580579200771\n", "cvgi 8.536474901930506\n", "casm 2.4917999999999947\n", "crus 20.50549421969114\n", "bobe 37.68301545907332\n", "dorm 34.76781854324324\n", "daio 3.6515559845559764\n", "dfbg 1.4005010393822352\n", "dlhc 1.8903745173745172\n", "emms 2.2464208494208493\n", "airm 46.91825888803096\n", "ecpg 24.974409271042457\n", "elon 6.949154477992283\n", "adp 61.03234735559848\n", "aimc 20.99966024942081\n", "aal 22.074953666795338\n", "ctbi 32.6178769405405\n", "dsgx 9.768467180694982\n", "cwbc 5.892640926640933\n", "cpst 1.2069536679536692\n", "chkp 51.12600387451736\n", "dgica 14.98658300617763\n", "crmt 30.543277994594618\n", "cyrx 1.1615408884169918\n", "chmg 26.42131274517373\n", "ehth 18.973142869111996\n", "esrx 67.42808488918898\n", "casc 3.062540716602321\n", "brcd 7.668254826254833\n", "basi 2.419463320463323\n", "atlc 6.211756745173753\n", "artw 7.465903469111946\n", "atsg 6.652401541698852\n", "ebay 35.18885618648654\n", "egan 3.546579150579136\n", "dgas 24.959196903088813\n", "clfd 8.05515444362934\n", "abtl 6.233108108494209\n", "bvsn 8.850015447876446\n", "cohu 12.740305017374505\n", "ccrn 10.0547413146718\n", "crme 5.622293436293452\n", "fdef 23.233142840926636\n", "bcor 13.26049806486487\n", "bsf 8.928162200386119\n", "cnxn 14.347270266795357\n", "dynt 1.8221196911196933\n", "boom 20.597038611196922\n", "csgp 103.1035598436295\n", "arow 25.715315076833956\n", "baby 21.253150588803106\n", "csbk 11.70866774131272\n", "evol 5.7018532818532774\n", "bkmu 7.30632432432434\n", "cresy 13.095930447876428\n", "cuba 7.75155212355212\n", "asml 59.04031520656373\n", "avav 26.558296459060898\n", "cbmx 3.7302140003860997\n", "bios 5.790710422779932\n", "ardm 1.9280694980694946\n", "cnsl 18.772135127027035\n", "acnb 17.343528900386115\n", "aciw 28.27269496023162\n", "czwi 7.40648262548263\n", "feim 8.712000000000025\n", "amrb 10.411073202702676\n", "cizn 20.43169879150579\n", "abeo 2.5932200772200797\n", "crzo 34.663799243243226\n", "cris 2.4645714285714266\n", "bcli 0.9969415324324327\n", "amrn 3.9924594610038655\n", "ahpi 3.404389961389958\n", "clsn 3.008281850193048\n", "aehr 2.6085559845559865\n", "bset 13.877702711583028\n", "esgr 114.2688533061775\n", "cwco 13.618177600000015\n", "cpla 54.80040545714292\n", "exac 19.531007741698875\n", "cvco 53.365436310424684\n", "bosc 2.1880193050193038\n", "eml 17.330768325482556\n", "expd 42.86821235366803\n", "clro 6.8523706563706615\n" ] } ], "source": [ "for stock_sym in stock_prices:\n", " print(stock_sym, avg_closing_prices[stock_sym])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Minimum and maximum closing prices" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Two minimum average closing prices:\n", "(0.8122763011583004, 'blfs')\n", "(0.824100993822394, 'apdn')\n", "\n", "Two maximum average closing prices:\n", "(275.1340775710431, 'amzn')\n", "(257.17654040231656, 'aapl')\n" ] } ], "source": [ "pairs = [(avg_closing_prices[stock_sym], stock_sym) for stock_sym in stock_prices]\n", "\n", "pairs.sort()\n", "\n", "print(\"Two minimum average closing prices:\")\n", "print(pairs[0])\n", "print(pairs[1])\n", "\n", "print()\n", "\n", "print(\"Two maximum average closing prices:\")\n", "print(pairs[-1])\n", "print(pairs[-2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It appears the `amzn` and `aapl` have the highest average closing prices, while `blfs`, and `apdn` have the lowest average closing prices." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Organizing the trades per day" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to calculate a dictionary where the keys are the days and the values are list of pairs `(volume, stock_symbol)` of all trades that occurred on that day." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "trades_by_day = {}\n", "\n", "for stock_sym in stock_prices:\n", " for index, row in stock_prices[stock_sym].iterrows():\n", " day = row[\"date\"]\n", " volume = row[\"volume\"]\n", " pair = (volume, stock_sym)\n", " if day not in trades_by_day:\n", " trades_by_day[day] = []\n", " trades_by_day[day].append(pair)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Finding The Most Traded Stock Each Day" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate a dictionary there the keys are the days and the value of each day is a pair `(volume, stock_symbol)` with the most traded stock symbol on that day." ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "most_traded_by_day = {}\n", "\n", "for day in trades_by_day:\n", " trades_by_day[day].sort()\n", " most_traded_by_day[day] = trades_by_day[day][-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Verify a few of the results" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(309579900, 'aapl')\n", "(211815100, 'aapl')\n", "(208685400, 'aapl')\n", "(199276700, 'aapl')\n" ] } ], "source": [ "print(most_traded_by_day['2007-01-03'])\n", "print(most_traded_by_day['2007-01-04'])\n", "print(most_traded_by_day['2007-01-05'])\n", "print(most_traded_by_day['2007-01-08'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Searching For High Volume Days" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1533363200, '2008-01-24'),\n", " (1536176400, '2008-01-16'),\n", " (1553880500, '2007-11-08'),\n", " (1555072400, '2008-09-29'),\n", " (1559032100, '2008-02-07'),\n", " (1578877700, '2008-01-22'),\n", " (1599183500, '2008-10-08'),\n", " (1611272800, '2007-07-26'),\n", " (1770266900, '2008-10-10'),\n", " (1964583900, '2008-01-23')]" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "daily_volumes = []\n", "\n", "for day in trades_by_day:\n", " day_volume = sum([volume for volume, _ in trades_by_day[day]])\n", " daily_volumes.append((day_volume, day))\n", "\n", "daily_volumes.sort()\n", "\n", "daily_volumes[-10:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Finding Profitable Stocks" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1330.0000666666667, 'achc'),\n", " (1339.2137535980346, 'bcli'),\n", " (1525.162516251625, 'cui'),\n", " (1549.6700659868027, 'apdn'),\n", " (1707.3554472785036, 'anip'),\n", " (2230.7234281466817, 'amzn'),\n", " (2437.4365640858978, 'blfs'),\n", " (3898.6004898285596, 'arcw'),\n", " (4005.0000000000005, 'adxs'),\n", " (7483.8389225948395, 'admp')]" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "percentages = []\n", "\n", "for stock_sym in stock_prices:\n", " prices = stock_prices[stock_sym]\n", " initial = prices.loc[0, \"close\"]\n", " final = prices.loc[prices.shape[0] - 1, \"close\"]\n", " percentage = 100 * (final - initial) / initial\n", " percentages.append((percentage, stock_sym))\n", "\n", "percentages.sort()\n", "\n", "percentages[-10:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most profitable stock to buy in `2007` would have been `ADMP`, which appreciated from around `7` cents to its current price of `4.43`." ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }