For many non-professional traders and investors who wish to manage an investment portfolio for the purpose of personal finance, it is difficult, time-consuming, and unreasonable to do so alone. With digital technology becoming increasingly widespread, many brokerage accounts have realeased application programming interfaces (APIs) through which clients may manage a portfolio using software stored on their local device. Additionally, while most APIs used to be limited to large, institutional investors, individual clients can now utilize some APIs without a minimum net worth or investment requirement. This is an obvious alternative to managing a portfolio by hand. Rather than needing to manually look at charts, news, and statistics and then place trades while many investors are busy with work or school, a program can collect data in real-time, make decisions based on that data, and execute trades without constant intervention from a human. This is exactly the purpose of this program, which currently utilizes relatively simple algorithms and will eventually use machine learning to accurately predict price changes and act upon them for any security.
This program utilizes the Interactive Brokers Trader Workstation API to automate trades and the Twelve Data API to receive realtime stock data. In this use case, Twelve Data is advantageous to receiving market data via Interactive Brokers due to lower costs and simplified programming.
The first algorithm is called "PointAndFigure" and has the same functionality as a point and figure chart. In this setup, support and resistance are calculated using the Average True Range (ATR) and the program will create a buy signal if the price of the security surpasses resistance or a sell signal if the price of the security falls through support.
The second algorithm is called "RSIandMACD" and utlizes both the Relative Strength Index (RSI) and Moving Average Convergence Divergence (MACD) technical indicators to generate buy or sell signals. For now, a buy or sell signal triggered by either indicator will execute a trade. Both of these indicators are momentum indicators used to quantify the strength of a current price trend. For the RSI, a value over 70 indicates that a stock may be overbought and a value under 30 indicates that a stock be oversold. In both these scenarios, a price correction is likely, although the significance of this correction can vary widely. MACD uses two moving averages: one shorter-term moving average (12 day in this scenario) and one longer-term moving average (26 day in this scenario). A shorter-term average rising above the longer-term average indicates that the security's price has upward momentum and vice versa. In the case of both of these indicators, false signals are relatively uncommon but can become an issue. To regulate this, I will be developing other algorithms that use different technical indicators or a diffferent statistical method, such as machine learning to work alongside these and hopefully minimize incorrect decisions.
Through backtesting, it is estimated that these algorithms return around 2% more than the S&P 500 when tested using historical data from the past 14 years. In order to make more significant gains, algorithms will need to take dozens or more parameters into account. Programming a function from scratch to accomplish this would be extremely time consuming. Machine learning algorithms could be a promising solution and are currently being experimented with.
Machine learning (ML) algorithms are currently be tested along-side these rule-based algorithms, to test whether ML may yield greater accuracy and fine tuning. Currently, the algorithms seek to classify the current market conditions as a "buy", "sell", or "hold" signal based on technical indicators, but economic factors and news will also eventually be implemented. Classification is currently being tested using logistic regression and K-nearest-neighbors algorithms. Logistic regression uses a logistic model to find the probability that a given data point belongs to a certain classification based on its features. In K-nearest-neighbors the program "remembers" the features of each training data point and its associated class and then assigns testing data points to classes based on the training data point with most similar features. Finally, neural nets are currently being experimented with, which are designed to roughly replicate the function of the human brain.
Most of the code in the machine learning tests is dedicated to data processing. In order for a machine learning algorithm to actually be useful, the feature data has to be in a format that can be correlated with the label data. So far, this includes finding the most recent areas of support and resistance and calculating the distance from the current price to those levels, as well as identifying if the stock is in an overall up or down trend.