{"id":6038,"date":"2021-02-13T08:00:35","date_gmt":"2021-02-13T02:30:35","guid":{"rendered":"https:\/\/www.upnxtblog.com\/?p=6038"},"modified":"2021-02-08T11:55:21","modified_gmt":"2021-02-08T06:25:21","slug":"how-to-create-a-fully-automated-ai-based-trading-system-with-python","status":"publish","type":"post","link":"https:\/\/www.upnxtblog.com\/index.php\/2021\/02\/13\/how-to-create-a-fully-automated-ai-based-trading-system-with-python\/","title":{"rendered":"How To Create A Fully Automated AI Based Trading System With Python"},"content":{"rendered":"<div class='booster-block booster-read-block'><\/div><p>The art of stock trading is not as easy as other people make it to be. For many who go into trading, it becomes a full-time job essentially. The time spent looking at various reports, updates, and news articles eventually takes up a good portion of one person&#8217;s day.<\/p>\n<p>All these are needed to be kept abreast of the current status of the stock market and to make predictions more accurate and informed. The allure of stock trading is also complemented by the public&#8217;s idea of stock traders. They are the young and the rich, the risk-takers who like to work hard and party even harder. Literary works like &#8220;The Wolf of Wall Street&#8221; only further supports this idea.<\/p>\n<p>And just like any other profession, burnout is a very real threat to those who partake in the activity. The stress of having invested your own or someone else&#8217;s money on something as volatile as the stock market is enough to keep one&#8217;s anxiety and stress level high. In looking for a reprieve, traders turn to distractions like gambling, partying, or even going on holidays on yachts with a <a href=\"https:\/\/www.eyespysupply.com\/complete-wireless-covert-systems.html\" target=\"_blank\" rel=\"noopener\">spy camera with audio<\/a>.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1155\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2017\/12\/06\/17-machine-learning-algorithms-that-you-should-know\/stock-photo-d-rendering-robot-learning-or-solving-problems-680929729\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/stock-photo-d-rendering-robot-learning-or-solving-problems-680929729.jpg?fit=1500%2C1101&amp;ssl=1\" data-orig-size=\"1500,1101\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"stock-photo&amp;#8211;d-rendering-robot-learning-or-solving-problems-680929729\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/stock-photo-d-rendering-robot-learning-or-solving-problems-680929729.jpg?fit=1024%2C752&amp;ssl=1\" class=\"aligncenter size-full wp-image-1155 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/stock-photo-d-rendering-robot-learning-or-solving-problems-680929729.jpg?resize=1140%2C837&#038;ssl=1\" alt=\"\" width=\"1140\" height=\"837\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/stock-photo-d-rendering-robot-learning-or-solving-problems-680929729.jpg?w=1500&amp;ssl=1 1500w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/stock-photo-d-rendering-robot-learning-or-solving-problems-680929729.jpg?resize=300%2C220&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/stock-photo-d-rendering-robot-learning-or-solving-problems-680929729.jpg?resize=768%2C564&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/stock-photo-d-rendering-robot-learning-or-solving-problems-680929729.jpg?resize=1024%2C752&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/stock-photo-d-rendering-robot-learning-or-solving-problems-680929729.jpg?resize=1100%2C807&amp;ssl=1 1100w\" data-sizes=\"auto, (max-width: 1140px) 100vw, 1140px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1140px; --smush-placeholder-aspect-ratio: 1140\/837;\" \/><\/p>\n<p>But could they all afford to shut off their trading completely?<\/p>\n<p>Not entirely. In the past, employees and assistants were more than willing to step up to the plate and take control of the trading until such time that the boss returns. Today, we have the might of powerful and interconnected computers to do most of the work without any human intervention. Yes, the stock market is one of those industries that have benefited greatly from artificial intelligence and automation. The question now is can you create an automated system yourself?<\/p>\n<p>It is possible to automate a system that you can use for trading using <a href=\"https:\/\/www.upnxtblog.com\/index.php\/2019\/07\/24\/trending-skill-python-check-out-curated-course-collection\/\">Python<\/a>. To save time, I&#8217;ve enumerated the steps we need to do before elaborating on them one by one later in the article.<\/p>\n<ol>\n<li>Gather as much real-time and granular stock price data.<\/li>\n<li>Instead of using a personal set of parameters, we&#8217;re going to be using AI and incorporating it into the system.<\/li>\n<li>The next step would be to decide on whether to sell, hold, or buy. It is here that the broker comes in to act. You can use services like Alpaca or RobinHood.<\/li>\n<li>The last thing we need to do is to implement the system somewhere and monitor its activities. You can program the system to send a notification every time the system acts to keep you updated.<\/li>\n<\/ol>\n<h2>Gathering the data<\/h2>\n<p>Getting the needed data is not going to be easy. In the past years, there were data sources like Yahoo! Finance API and Google Finance but sadly both services have been discontinued. But there are still a few alternatives available in the market today. In choosing such services, here are a couple of features to keep in mind:<\/p>\n<ul>\n<li>Free of charge\/cheap<\/li>\n<li>Real-time data<\/li>\n<li>Easy to use<\/li>\n<li>High limit rate<\/li>\n<\/ul>\n<p>Using an unofficial alternative to yahoo Finance API, <a href=\"https:\/\/pypi.org\/project\/yfinance\/\" target=\"_blank\" rel=\"noopener\">yfinance <\/a>is a simple tool for this experiment. You first have to install it after which you can access everything with the use of the Ticker object. More information is available on other methods like &#8216;dividends&#8217;, &#8216;splits&#8217;, &#8216;earnings,&#8217; and many more. These will return data in <a href=\"https:\/\/www.upnxtblog.com\/index.php\/2021\/01\/29\/9-best-edurekas-instructor-led-and-self-paced-courses\/\">&#8216;pandas&#8217;<\/a> DataFrame objects.<\/p>\n<p>At this point, we only want to get the price of the stock through time. You can use the &#8216;history&#8217; method for this step. Select the interval dates and data frequency down to a minute. And now let&#8217;s proceed to add AI.<\/p>\n<h2>Incorporating AI<\/h2>\n<p>As a caveat, this process is a very simple way to use the ARIMA model in order to forecast what the value of the next stock price will be. Imagine it as a form of a dummy model. You can certainly use other better models if you want to use this for real trading.<\/p>\n<p>You first have to divide the <a href=\"https:\/\/www.upnxtblog.com\/index.php\/2019\/07\/31\/top-10-python-libraries-for-data-science\/\">dataframe<\/a> into two: train and test. And then use the test set to validate the results of the model we just created. Then let&#8217;s use the training data model to get the forecast. Note here that the parameters on this model are fixed while in the real world you need to use cross-validation to choose the best ones.<\/p>\n<h2>Connect the broker<\/h2>\n<p>This next part will depend on which broker you&#8217;re going to be using. Two examples we&#8217;ve cited previously were <a href=\"https:\/\/alpaca.markets\/\" target=\"_blank\" rel=\"noopener\">Alpaca<\/a> and<a href=\"https:\/\/robinhood.com\/us\/en\/\" target=\"_blank\" rel=\"noopener\"> RobinHood<\/a>. Depending on the type of account you have, you might have to work around a few limitations. For example, RobinHood only allows 3 trades per 5 days if your balance is below $25,000. Alpaca, on the other hand, allows more requests but has a limit of 200 requests every minute per API key.<\/p>\n<h2>Deployment and monitoring<\/h2>\n<p>We will be deploying the system on <a href=\"https:\/\/www.upnxtblog.com\/index.php\/2017\/12\/01\/serverless-comparison-aws-lambda-vs-azure-functions-vs-google-functions\/\">AWS Lambda<\/a>. Although not the best option since Lambda doesn&#8217;t offer storage and ideally you would want to store the model that you&#8217;ve trained. However, for this example storing it at AWS Lambda will do for the meantime. Schedule Lambda to run every day to train the model even more. As for monitoring, we&#8217;ll set the model to send notifications on Telegram every time an action takes place.<\/p>\n<p><em><strong>Like this post? Don\u2019t forget to share it!<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The art of stock trading is not as easy as other people make it to be. For many who go into trading, it becomes a full-time job essentially. The time spent looking at various reports, updates, and news articles eventually takes up a good portion of one person&#8217;s day. All these are needed to be [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1155,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1,163],"tags":[356,429],"class_list":["post-6038","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-trending","category-machine-learning","tag-python","tag-trading"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/stock-photo-d-rendering-robot-learning-or-solving-problems-680929729.jpg?fit=1500%2C1101&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9fbQS-1zo","jetpack-related-posts":[{"id":6650,"url":"https:\/\/www.upnxtblog.com\/index.php\/2021\/12\/25\/crypto-day-trading-tips-you-need-to-follow\/","url_meta":{"origin":6038,"position":0},"title":"Crypto Day Trading Tips You Need To Follow","author":"Karthik","date":"December 25, 2021","format":false,"excerpt":"Crypto day trading is a relatively newer concept that is gaining fame gradually. People, across the globe, are using this investment strategy. It works by relying on a high frequency of trading using a few securities throughout the day. With the hope of making an immediate profit, the traders opt\u2026","rel":"","context":"In &quot;Blockchain&quot;","block_context":{"text":"Blockchain","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/blockchain\/"},"img":{"alt_text":"bitcoin","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":7877,"url":"https:\/\/www.upnxtblog.com\/index.php\/2024\/11\/30\/the-rise-of-crypto-trading-bots-a-new-era-in-digital-trading\/","url_meta":{"origin":6038,"position":1},"title":"The Rise Of Crypto Trading Bots: A New Era In Digital Trading","author":"Karthik","date":"November 30, 2024","format":false,"excerpt":"The cryptocurrency trading world has grown significantly in recent years, with automation playing a key role in this shift. Crypto trading bots are advanced tools that automate the trading process, making it easier for traders to navigate the volatile market. These bots execute trades based on predefined rules or complex\u2026","rel":"","context":"In &quot;Blockchain&quot;","block_context":{"text":"Blockchain","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/blockchain\/"},"img":{"alt_text":"bitcoin","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":5749,"url":"https:\/\/www.upnxtblog.com\/index.php\/2020\/09\/10\/do-you-want-to-participate-in-bitcoin-trading\/","url_meta":{"origin":6038,"position":2},"title":"Do you want to participate in bitcoin trading?","author":"Karthik","date":"September 10, 2020","format":false,"excerpt":"Do you want to participate in bitcoin trading? Then, you should learn the trading skills to earn profits before taking a plunge into the world of bitcoin trading. There are many bitcoin trading platforms that are allowing the investors and traders to carry out bitcoin trading smoothly and securely. There\u2026","rel":"","context":"In &quot;Blockchain&quot;","block_context":{"text":"Blockchain","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/blockchain\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2020\/09\/bitcon-trader.jpg?fit=1000%2C563&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2020\/09\/bitcon-trader.jpg?fit=1000%2C563&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2020\/09\/bitcon-trader.jpg?fit=1000%2C563&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2020\/09\/bitcon-trader.jpg?fit=1000%2C563&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":6652,"url":"https:\/\/www.upnxtblog.com\/index.php\/2021\/12\/24\/3-strategies-that-will-make-you-a-successful-crypto-trader\/","url_meta":{"origin":6038,"position":3},"title":"3 Strategies That Will Make You A Successful Crypto Trader","author":"Karthik","date":"December 24, 2021","format":false,"excerpt":"The knowledge available over the internet regarding cryptocurrency and trading is overwhelming. This tornado of information doesn\u2019t reflect any tangible result as a new investor is bound to get confused. Picking the required information and using it needs a lot of time, research, and in-depth analysis of the matter. Hence,\u2026","rel":"","context":"In &quot;Blockchain&quot;","block_context":{"text":"Blockchain","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/blockchain\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2021\/10\/bitcoin1.png?fit=602%2C401&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2021\/10\/bitcoin1.png?fit=602%2C401&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2021\/10\/bitcoin1.png?fit=602%2C401&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":7169,"url":"https:\/\/www.upnxtblog.com\/index.php\/2023\/03\/27\/iot-powered-decentralized-exchanges-the-future-of-digital-asset-trading\/","url_meta":{"origin":6038,"position":4},"title":"IoT-Powered Decentralized Exchanges: The Future of Digital Asset Trading","author":"Karthik","date":"March 27, 2023","format":false,"excerpt":"The world of digital asset trading is rapidly evolving, and decentralized exchanges (DEXs) are becoming more popular as a way to trade assets in a peer-to-peer manner without relying on a centralized intermediary. However, the potential of these exchanges has been limited by the lack of effective technology to power\u2026","rel":"","context":"In &quot;IOT&quot;","block_context":{"text":"IOT","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/iot\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/03\/dex.jpg?fit=780%2C440&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/03\/dex.jpg?fit=780%2C440&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/03\/dex.jpg?fit=780%2C440&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/03\/dex.jpg?fit=780%2C440&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":6647,"url":"https:\/\/www.upnxtblog.com\/index.php\/2021\/12\/22\/the-ultimate-guide-on-cryptocurrency-exchanges\/","url_meta":{"origin":6038,"position":5},"title":"The Ultimate Guide On Cryptocurrency Exchanges","author":"Karthik","date":"December 22, 2021","format":false,"excerpt":"The crypto world is oozing, which has led to obvious popularity in cryptocurrency exchanges as they basically facilitate cryptocurrency trading as well as fiat currency. It wouldn\u2019t be wrong to say that they act as an intermediary between sellers and buyers and for making money through transaction fees and commissions.\u2026","rel":"","context":"In &quot;Blockchain&quot;","block_context":{"text":"Blockchain","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/blockchain\/"},"img":{"alt_text":"bitcoin","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/bitcoin.jpg?fit=1200%2C801&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts\/6038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=6038"}],"version-history":[{"count":1,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts\/6038\/revisions"}],"predecessor-version":[{"id":6040,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts\/6038\/revisions\/6040"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/media\/1155"}],"wp:attachment":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=6038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=6038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=6038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}