{"id":1804,"date":"2024-10-14T12:15:28","date_gmt":"2024-10-14T12:15:28","guid":{"rendered":"http:\/\/3.10.207.114\/?p=1804"},"modified":"2025-02-07T12:02:16","modified_gmt":"2025-02-07T12:02:16","slug":"apiopenai-finance-python-project","status":"publish","type":"post","link":"https:\/\/tech-musing.com\/2024\/10\/14\/apiopenai-finance-python-project\/","title":{"rendered":"API,OPENAI &amp; Finance Python Project"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/18.132.103.31\/wp-content\/uploads\/2024\/10\/image-1.png\" alt=\"\" class=\"wp-image-1806\" srcset=\"https:\/\/tech-musing.com\/wp-content\/uploads\/2024\/10\/image-1.png 1024w, https:\/\/tech-musing.com\/wp-content\/uploads\/2024\/10\/image-1-300x300.png 300w, https:\/\/tech-musing.com\/wp-content\/uploads\/2024\/10\/image-1-150x150.png 150w, https:\/\/tech-musing.com\/wp-content\/uploads\/2024\/10\/image-1-768x768.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-aioseo-table-of-contents\"><ul><li><a href=\"#aioseo-high-level\">High level<\/a><\/li><li><a href=\"#aioseo-setup-steps\">Setup steps<\/a><\/li><li><a href=\"#aioseo-high-level-aim\">High Level Aim<\/a><\/li><li><a href=\"#aioseo-what-it-does\">What it does<\/a><\/li><li><a href=\"#aioseo-considerations\">Considerations and Ideas<\/a><\/li><li><a href=\"#aioseo-files\">Files<\/a><\/li><li><a href=\"#aioseo-typical-workflow\">Typical workflow<\/a><\/li><li><a href=\"#aioseo-interface\">Interface<\/a><\/li><li><a href=\"#aioseo-basic-run-10-funds-default-settings-in-db-config\">Basic run with no Funds found<\/a><\/li><li><a href=\"#aioseo-bigger-run-100-funds-set-in-db-config\">Basic run with Funds found<\/a><\/li><\/ul><\/div>\n\n\n<h1 class=\"wp-block-heading\" id=\"aioseo-high-level\">High level<\/h1>\n\n\n<p>So combining a few of my interests: <br>1) Python<\/p>\n\n\n\n<p>2) Finance<\/p>\n\n\n\n<p>3) AI<\/p>\n\n\n\n<p>I have uploaded the script to <\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/herepete\/Fidelity\">https:\/\/github.com\/herepete\/Fidelity<\/a> <\/p>\n\n\n<h1 class=\"wp-block-heading\" id=\"aioseo-setup-steps\">Setup steps<\/h1>\n\n\n<p>From a AWS Linux Vm<\/p>\n\n\n\n<p>yum install git<br>yum install pip<br>pip3.9 install bs4 pandas<br>pip3.9 install openai==0.28<\/p>\n\n\n\n<p>create openai account > https:\/\/platform.openai.com\/docs\/overview, create an API key , add openai key to .bashrc for example <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export OPENAI_API_KEY=\"bla\"<\/code><\/pre>\n\n\n\n<p><br>make sure its visible when running<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> env | grep OPEN<\/code><\/pre>\n\n\n\n<p>run test api script.py &#8211; fix issues<br>run option 1 on menu within main.py -fix issues<\/p>\n\n\n\n<p>run option 2<\/p>\n\n\n<h1 class=\"wp-block-heading\" id=\"aioseo-high-level-aim\">High Level Aim<\/h1>\n\n\n<p>A script to find a list of income funds i am interested in.<\/p>\n\n\n\n<p>Then pass that list to openai for some feedback.<\/p>\n\n\n<h1 class=\"wp-block-heading\" id=\"aioseo-what-it-does\">What it does<\/h1>\n\n\n<p>Based on the config in config.py<\/p>\n\n\n\n<p>connect to the Fidelity OpenAi and with the minimal of calls check against criteria namely:<\/p>\n\n\n\n<p>is it an Income fund?<\/p>\n\n\n\n<p>does it have a yield over 4%<\/p>\n\n\n\n<p>is the cost  under 1%<\/p>\n\n\n\n<p>As it produced a return of over 3 in the last 1,3 &amp; 5 years<\/p>\n\n\n\n<p>If the Api fails for any reason or the criteria is not met reject the fund straight away to reduce the number of API Calls needed.<\/p>\n\n\n\n<p>If all the tests are passed some extra info is grabbed Last Years Yield, and Overall Morning Star Rating.<\/p>\n\n\n\n<p>At the end you should have a list of funds which have passed all off the test and  then that information is all passed to openai to get some feedback.<\/p>\n\n\n<h1 class=\"wp-block-heading\" id=\"aioseo-considerations\">Considerations and Ideas<\/h1>\n\n\n<p>I am the first to admit this script needs tweaking and improvement but my aim was to get a first version published rather than having a perfect solution.<\/p>\n\n\n\n<p>I though about multi-threading or multi-processing to speed everything up, but i don&#8217;t want to hammer the API.<\/p>\n\n\n\n<p>I could do more with the AI i.e langchain but my aim was to get version 1 working.<\/p>\n\n\n\n<p>I did wonder about getting the results written to a db like sqllite but thats another option for version2<\/p>\n\n\n\n<p>In the output i could use colour and ask the Openai for more information.<\/p>\n\n\n\n<p>You need quite a wide screen to display the ouput as designed.<\/p>\n\n\n\n<p>Maybe play with different openai models to check output.<\/p>\n\n\n\n<p>Maybe open a chat type windows from the command line to allow you to ask questions about the funds.<\/p>\n\n\n\n<p>Play with Openai instructions to get better answers, i haven&#8217;t done any fine tunning of the commands.<\/p>\n\n\n<h1 class=\"wp-block-heading\" id=\"aioseo-files\">Files<\/h1>\n\n\n<p>config.py &#8211; basic config, set testing to 0 if you want to unleash the power! &amp; set any criteria you wish on the other fields<\/p>\n\n\n\n<p>main.py &#8211; main script to run<\/p>\n\n\n\n<p>test_openai.py &#8211; a basic test to check openai is being called, can be run on its own or from the menu in main.py<\/p>\n\n\n\n<p>failer_test_fund.txt &#8211; is a log of what fund errored and why, this is only generated when option 2 from main.py is run. We use the funds here and mark them as Previously checked so we don&#8217;t unnecessarily test them again. <\/p>\n\n\n\n<p>fidelity_funds.csv &#8211; this is your master list of funds to consider about 3,000 ish. This file is only generated when option 1 from main.py is run. The first check in option 2 of main.py is looking through all funds names and removing anything with the word &#8220;inc&#8221; in.<\/p>\n\n\n<h1 class=\"wp-block-heading\" id=\"aioseo-typical-workflow\">Typical workflow<\/h1>\n\n\n<p>setup enviorement<\/p>\n\n\n\n<p>download git scripts<\/p>\n\n\n\n<p>run main.py choose option 1 to create your all files lookup<\/p>\n\n\n\n<p>run main.py chose option 2 &#8211; 10 funds are checked<\/p>\n\n\n\n<p>edit config.py and change testing to 0<\/p>\n\n\n\n<p>run main.py choose option 2 &#8211; sit back and watch and review results<\/p>\n\n\n<h1 class=\"wp-block-heading\" id=\"aioseo-interface\">Interface<\/h1>\n\n\n<p>It&#8217;s all Command line but i have tried to make it visualize as nice as possible.<\/p>\n\n\n\n<p>The page should refresh:<\/p>\n\n\n\n<p>Income stocks found should be about 1117 ish &#8211; is all income funds found in fidelity_funds.csv<\/p>\n\n\n\n<p>Previously checked &#8211; funds found in failer_test_fund.txt, which is failed funds from any previous runs.<\/p>\n\n\n\n<p>This Round checking &#8211; how many funds to check based on Db.config values<\/p>\n\n\n\n<p>This round rejected &#8211; how many funds failed<\/p>\n\n\n\n<p>This round suitable funds found &#8211; how many funds passed<\/p>\n\n\n\n<p>Verbose stuff &#8211; a log of the 10 most recent things than happened<br><\/p>\n\n\n<h1 class=\"wp-block-heading\" id=\"aioseo-basic-run-10-funds-default-settings-in-db-config\">Basic run with no Funds found<\/h1>\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"286\" src=\"https:\/\/18.132.103.31\/wp-content\/uploads\/2024\/10\/image-1024x286.png\" alt=\"\" class=\"wp-image-1805\" style=\"width:1079px;height:auto\" srcset=\"https:\/\/tech-musing.com\/wp-content\/uploads\/2024\/10\/image-1024x286.png 1024w, https:\/\/tech-musing.com\/wp-content\/uploads\/2024\/10\/image-300x84.png 300w, https:\/\/tech-musing.com\/wp-content\/uploads\/2024\/10\/image-768x214.png 768w, https:\/\/tech-musing.com\/wp-content\/uploads\/2024\/10\/image.png 1257w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n<h1 class=\"wp-block-heading\" id=\"aioseo-bigger-run-100-funds-set-in-db-config\">Basic run with Funds found<\/h1>\n\n\n<p>note anything below the line &#8220;**Analysis:**&#8221; is aI generated.<\/p>\n\n\n\n<p>A Screenshot didn&#8217;t look as good here  so i have copied and pasted the output as a code line<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Income Stocks Found           Previously checked            This round Checking           This Round Rejected           This Round Suitable Funds found\n\n1117                          537                           20                            18                            2\n\nVerbose Stuff...\nStarting work on - Rathbone M-A Strategic Growth Port S Inc\nYield check failure\nStarting work on - Rathbone M-A Strategic Income Port S Inc\nYield check failure\nStarting work on - Rathbone M-A Total Return Port S Inc\nYield check failure\nStarting work on - Schroder High Yield Opportunities Z Inc\nSuitable Fund Found=Schroder High Yield Opportunities Z Inc\nStarting work on - Schroder Strategic Credit L Inc\nSuitable Fund Found=Schroder Strategic Credit L Inc\n\nHere are the raw results and the AI feedback on those funds...\nBased on the provided data, here is a comparison of the two income funds:\n\n| Fund Name                           | ISIN          | Fee (%) | Yield (%) | Frequency     | Y1_Annualized | Y3_Annualized | Y5_Annualized | Last Year's Yield | Morning Star Rating |\n|------------------------------------|-------------------------------|--------|----------|----------------|----------------|----------------|----------------|------------------|---------------------\n| Schroder High Yield Opportunities Z Inc | GB00B5143284 | 0.72   | 7.71      | Monthly        | 15             | 3             | 5                | 7.82                 | 5                   |\n| Schroder Strategic Credit L Inc         | GB00B11DP098 | 0.67   | 6.33      | Semi-Annually | 12             | 3             | 4                | 5.90                 | 5                   |\n\n**Analysis:**\n\n**Schroder High Yield Opportunities Z Inc:**\n- **Pros:**\n  - Higher yield (7.71%)\n  - Monthly frequency of payouts\n  - Higher Y1_Annualized return (15%)\n  - Higher Last Year's Yield (7.82%)\n  - Morning Star Rating of 5\n\n- **Cons:**\n  - Slightly higher fee compared to the other fund (0.72%)\n  - Lower Y3_Annualized and Y5_Annualized returns\n\n**Schroder Strategic Credit L Inc:**\n- **Pros:**\n  - Lower fee (0.67%)\n  - Lower fee than the other fund\n  - Consistent Morning Star Rating of 5\n\n- **Cons:**\n  - Lower yield (6.33%)\n  - Semi-annual frequency of payouts\n  - Lower Y1_Annualized and Last Year's Yield\n  - Lower Y3_Annualized and Y5_Annualized returns compared to the other fund\n\n**Additional Research:**\n- Further research into the credit quality of the underlying assets might provide insights into the risk profile of these funds.\n- Comparison of the fund performance against relevant benchmark indices.\n\nIn summary, the Schroder High Yield Opportunities Z Inc fund offers a higher yield and better short-term performance, but at a slightly higher fee. On the other hand, the Schroder Strategic Credit L Inc fund has a lower fee and consistent Morning Star Rating, but with lower yield and overall returns. Investors may consider their investment goals, risk tolerance, and liquidity preferences when choosing between these two funds.\nAI Anslysis completed<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>High level So combining a few of my interests: 1) Python 2) Finance 3) AI I have uploaded the script to https:\/\/github.com\/herepete\/Fidelity Setup steps From a AWS Linux Vm yum install gityum install pippip3.9 install bs4 pandaspip3.9 install openai==0.28 create openai account > https:\/\/platform.openai.com\/docs\/overview, create an API key , add openai key to .bashrc for [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,36],"tags":[],"class_list":["post-1804","post","type-post","status-publish","format-standard","hentry","category-ai","category-my-projects"],"_links":{"self":[{"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/posts\/1804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/comments?post=1804"}],"version-history":[{"count":1,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/posts\/1804\/revisions"}],"predecessor-version":[{"id":1856,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/posts\/1804\/revisions\/1856"}],"wp:attachment":[{"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/media?parent=1804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/categories?post=1804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/tags?post=1804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}