CS460: Machine Learning

CS460 : An Aside

Creating an AI bot to trade FOREX in the Stock Market

8 September 2021

Submitted as part of course requirements for CS460: Machine Learning, Fall 2021
Offered by the School of Computer Sciences,
National Institute of Science Education and Research,
Bhubaneswar.
Course Instructor: Dr. Subhankar Mishra
Faculty Mentor: Dr. Amarendra Das

Relevant Papers




Gambling: Armed with Statistics



THE KELLY CRITERION

For simplicity, assume that the exchange rate can take only wo values after some period of time, iven its value today, under the random walk hypothesis, with probabliies, $$P(r=\mu+\sigma)=\frac{1}{2}=P(r=\mu-\sigma)$$ with equal probablities rhat the exchange rate, \(r\) would appreciate or deprecate. Here, \(\mathbf{E}(r)=\mu\) is the avearge expectation value of the exchange rate while \(\sigma>0\) is the volatility in the exchange rate. If \(c_0\) is the initial available capital and \(f\) is the leverage hat is being traded with then, the final capital after \(n\) trading periods is $$\begin{align} c_n(f)&=c_0(1+f\cdot r)\\ &=c_0 \prod_{i=1}^{n}(1+f\cdot r_i) \end{align}$$ Maximising the returns is equivalent to maximising the average rate of growth per trading period or $$G(f)=\mathbf{E}\left(\log\frac{c_n(f)}{c_0}\right)$$ If there are \(n\) trading periods in the considered interval of time, then for each trading period \(\mu^\prime=\frac{\mu}{n}\) and (\sigma^\prime=\frac{\sigma}{\sqrt{n}}\) $$P(r=\mu^\prime+\sigma^\prime)=\frac{1}{2}=P(r=\mu^\prime-\sigma^\prime)$$ Under this condition the growth function is $$\begin{align} G_n(f)&=\mathbf{E}\left(\log\frac{c_n(f)}{c_0}\right)\\ &=\mathbf{E}\left(\log\frac{c_0 \prod_{i=1}^{n}(1+f\cdot r_i)}{c_0}\right)\\ &=\mathbf{E}\left(\log\left(\prod_{i=1}^{n}(1+f\cdot r_i)\right)\right)\\ &=\mathbf{E}\left(\sum_{i=1}^{n}\log(1+f\cdot r_i)\right)\\ &=\frac{1}{2} \sum_{i=1}^{n} \left[\log(1+f(\mu^\prime+\sigma^\prime))+\log(1+f(\mu^\prime-\sigma^\prime))\right]\\ &=\frac{1}{2} \sum_{i=1}^{n} \left[\log\left(1+f\left(\frac{\mu}{n}+\frac{\sigma}{\sqrt{n}}\right)\right)+\log\left(1+f\left(\frac{\mu}{n}-\frac{\sigma}{\sqrt{n}}\right)\right)\right]\\ &=\frac{1}{2} \sum_{i=1}^{n} \left[\log\left(\left(1+\frac{f\mu}{n}\right)+\frac{f\sigma}{\sqrt{n}}\right)\left(\left(1+\frac{f\mu}{n}\right)-\frac{f\sigma}{\sqrt{n}}\right)\right]\\ &=\frac{n}{2} \log\left[\left(1+\frac{f\mu}{n}\right)^2-\left(\frac{f\sigma}{\sqrt{n}}\right)^2\right]\\ &=\frac{n}{2} \log\left[1+2\frac{f\mu}{n}+\frac{f^2\mu^2}{n^2}-\frac{f^2\sigma^2}{n}\right]\\ &=\frac{n}{2}\,\left[\frac{2f\mu}{n}+\frac{f^2\mu^2}{n^2}-\frac{f^2\sigma^2}{n}\right]\\ &=f\mu+\frac{f^2\mu^2}{2n}-\frac{f^2\sigma^2}{2}\\ \end{align}$$ where the approximation \(log(1+x)\approx x\) was used. For a sufficiently large \(n\) the second term \(\frac{f^2\mu^2}{2n}\rightarrow 0\), and the second term can be dropped and $$G_n(f)=f\mu-\frac{f^2\sigma^2}{2}$$ To maximise \(G_n(f)\) with respect o \(f\), the first derivative is taken and set to \(0\). $$\begin{align} G^\prime_n(f)&=\mu-f\sigma^2\\ or,\,\,\,0&=\mu-f\sigma^2\\ or,\,\,\,f&=\frac{\mu}{\sigma^2} \end{align}$$ The statisical properties of the exchange rate determine \(f\) to be the optimal fraction to be invested. Using the Kelly criterion, he leverage is higher when the average returns are higher and the exchange rate is less volatile.