<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Wesley Warbington on Medium]]></title>
        <description><![CDATA[Stories by Wesley Warbington on Medium]]></description>
        <link>https://medium.com/@wesleywarbington_22315?source=rss-4eb544b55644------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gziETt7amBoHLl_ykoJjaQ.jpeg</url>
            <title>Stories by Wesley Warbington on Medium</title>
            <link>https://medium.com/@wesleywarbington_22315?source=rss-4eb544b55644------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 08:19:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wesleywarbington_22315/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AI + Stock Trading]]></title>
            <link>https://medium.com/@wesleywarbington_22315/ai-stock-trading-d71955621834?source=rss-4eb544b55644------2</link>
            <guid isPermaLink="false">https://medium.com/p/d71955621834</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[technical-analysis]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Wesley Warbington]]></dc:creator>
            <pubDate>Wed, 01 Feb 2023 23:01:17 GMT</pubDate>
            <atom:updated>2023-02-01T23:09:11.720Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently finished a masters course at Georgia Tech titled “machine learning for trading”, a class which merged computer science with finance. With this course as a baseline, I expanded upon the ideas there developed and created a few interesting trading strategies… one tuned by me, and two tuned by separate computer models (decision tree &amp; reinforcement learning). I constrained myself and the models to technical analysis, disregarding any information related to fundamental analysis. In short, only historical stock price data was used and any indicators secondarily created are based off historical stock price (**adjusted close price).</p><p>The first step was to decide upon which indicators to use to inform the decision of whether to enter a long or short position. Bollinger bands percentage, relative strength index, and an exponential moving average cross were chosen as the three technical indicators that all three strategies used. Secondly, the models were limited to holding a maximum of 1000 shares either short or long. The strategies are stock symbol agnostic, and data has been provided in my <a href="https://github.com/wesleywarbington/tradingStrategies">GitHub</a> to be able to play around with additional symbols and parameters. For the purpose of this report and further analysis, the following parameters were used:</p><pre><strong><em>Symbol</em></strong> = &quot;JPM&quot; (JPMorgan Chase &amp; Co)<br><strong><em>Training Period</em></strong> = January 1, 2008 --&gt; December 31, 2009<br><strong><em>Testing Period</em></strong> = January 1, 2010 --&gt; December 31, 2011<br><strong><em>Starting Portfolio Value</em></strong> = $100,000</pre><p>Let’s first begin with the manual strategy tuned by me. The goal of the trading strategy was to build a model that increases portfolio value over time. Again, I limited myself to using three technical indicators to decide when to long, short or do nothing. It was a lot of trial and error for this first strategy, tuning the thresholds that decide how to act. The logic looks at each indicator value and assigns a zero, positive one, or negative one based off the value. Then all three values (one for each indicator) are added together. If the total is greater than or equal to two, then enter a long position, if the total is less than or equal to two, then enter a short position, and if neither of these cases occur, then do nothing for that day. This logic requires a strong agreeance among the indicators without any strong disagreeance. What I mean is, at least two indicators have to signal a long position and the other indicator cannot be signaling a sell. This would bring the total down to 1. The same logic applies in the opposite direction for a short position.</p><p>The first machine learning strategy built utilized a decision tree algorithm. More specifically, a bagging technique was used on a “random” decision tree. The tree is described as “random” because it randomly chooses one of the indicators which decides the fork in the tree on each of the recursive builds. Randomly one of the indicators is chosen each iteration (until the last leaf node and the tree is complete) and then the tree forks at the median of the values for the indicator over the time range. The base case which is signified by a leaf node is when there are ten or less rows (days) left to split on. To query the built tree, we start at the root node and move down until we reach a leaf node. We go left if our current value is less than or equal to the indicator value of the node and right if the current value is greater. The visualization below shows a simplified example of a tree:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sz1U_bz28FX8BVFaog_IHw.jpeg" /><figcaption>Example Decision Tree</figcaption></figure><p>In the above figure, the blue circles are nodes of the tree and the triangles at the bottom are leaf nodes which can be understood as decisions on whether to enter a long or short position. Let’s step through an example with fake data and the above decision tree. We’ll say that for today, the Bollinger Bands percentage (bbp) is calculated to be 11, the relative strength index (rsi) is 4, and the exponential moving average cross (ema cross) is 7. We start at the top node, which says bbp will be the “deciding indicator”. Since our current bbp value of 11 is greater than 10 we move to the right side of the tree. Our current rsi value of 4 is less than 6 so we move to the left of the subtree. Lastly, our current bbp value of 11 is less than 15 so we move left. Now we have reached a leaf node and our final decision is to enter a long position on this day. As mentioned above, the model uses a bagging technique which basically means that the tree is built multiple times and then all of them are combined into one single model which is more robust and less prone to overfitting (hopefully). For the decision tree learner, which pulls the tree classes together, the model uses a bag size of 30. How was the “Y” data represented by the leaf nodes in the tree generated though because this is not an objective metric that we can pull from yahoo? The “Y” data was found by looking at the price five days ahead of the current day and then deciding to short or long based on a set threshold. For example, if the price of JP Morgan stock is trading at twenty dollars higher five days from now, then buying today would be proffitable so we should enter a long position.</p><p>The second machine learning strategy used was reinforcement learning. A Q-learning algorithm was used which tells an environment its current state and action it wants to take. A environment has to be defined that can take in those two things and return a reward and a next state. The value of the indicators make up the “state”. All three indicators were combined into one value from 0–1000 (1000 possible states) in the following way: discretize each respective indicator into ten buckets; multiply bbp, ema_cross, and rsi by one hundred, ten, and one respectively; then sum together. There are three possible actions those being enter a short position, long position or do nothing. Lastly, the reward is calculated multiplying the previous day’s holding (number of shares of stock) by the daily return. The reinforcement learner trains for 200 epochs or until the model converges (whichever comes first).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*zCj7b1f4yD7TohOCTqMrSQ.png" /></figure><p>The above figure shows the results of four different strategies when used on the training data (data the models have seen and trained on). The y axis is normalized but can be interpreted in US dollars. For example, all strategies begin with $100,000 but the green line ends at a portfolio value around $350,000. The reinforcement learning strategy performs the best followed by the decision tree strategy and then the manual strategy (where I set the parameters on when to long or short). The purple line is a baseline for context. It represents buying 1000 shares on the first day and just holding. Below, the next figure illustrates performance on the test data (data the models have not seen when being built).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*qpo8WPRvYhD2248V-Ezcxg.png" /></figure><p>Now our hopes and dreams of getting rich quick start to dissipate. But, it is important to note that the two machine learning models were trained locally and the entire code which includes training, testing, plotting and writing of statistics runs in under one minute on my Macbook pro. Obviously the performance here is not great but it is to be expected that performance is worse for the out of sample data (test) compared to the in sample data (train). Nevertheless we can see that the models have indeed built up an intuition on when to buy or sell. The models just overfit to the data they trained on for a certain period and are biased to that small set of information. The only information available to make a decision on was price data of one specific stock for two years and the price was only made available once a day. You can imagine that by increasing the time period and number of stocks made available to train on, the strategies could become more robust and have better performance on unseen data.</p><p>An important piece not yet discussed is the issue of “impact”. Impact here is referring to the influence that an entity has on stock price when it buys or sells. For example, you cannot buy $2,000,000 dollars worth of a stock with a market cap of $100,000,000 at the same price. There is a spread of bids and asks (people who want to buy and sell and at what price) and it would most likely not be the case that there are $2,000,000 worth of stock to be bought at one price. If sellers see that someone is buying up more and more stock, then they will raise the price in which they are willing to sell. In short, if you buy you can push the price up and if you sell you can push the price down. I wanted to capture this and see how it affected the machine learning models so for simplicity, impact is a hard coded number that is multiplied by the number of shares traded and then subtracted from the overall portfolio value. Below, the figure shows the number of trades executed as the impact fee increases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*J-Uf9w8FsV-9h9WMDfLKxQ.png" /></figure><p>This is really cool. Both models have figured out that as the cost of executing a trade increases, the more sure they need to be that this is going to be a profitable position to be in. When there is no impact fee, even gaining one dollar is better than nothing but if there is an impact fee, then that fee could potentially be greater than the profit you could make from buying and selling at a higher price. One interesting thing to note is that reinforcement learner executes a significant amount more trades than the decision tree strategy.</p><p>I was especially excited when I saw the final visualizations and the performance of the reinforcement learner. It has an interesting architecture which feels a lot like how we as humans learn and seems to generalize to many different areas. My final thoughts specifically related to reinforcement learning are as follows:</p><p>If there is a stable environment that can provide a next state and reward, given a current state and action, then reinforcement learning algorithms can find quite optimal ways to maximize reward. It seems that the bottleneck is time taken to interact with the environment. If it is constrained to a physical process, then the time to train to an acceptable performance can take a long time. Alternatively, if an environment can be defined in software, then the simulated environment can drastically speed up the training process.</p><p>Related code can be found on my <a href="https://github.com/wesleywarbington/tradingStrategies">GitHub</a>. CSV files of many different stocks have been uploaded there, as well as more thorough descriptions of the individual files. There are also additional charts and a .txt file with statistics on the separate strategies such as cumulative and daily returns.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d71955621834" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>