Covid hasn’t exactly provided many positives over past year or so, but looking on the bright side it has gifted me a fair amount of time. And filling that newfound sense of time has been difficult. Leading up to the Christmas holidays of 2020, I was heavily researching how to turbocharge my investment strategies. I’ll be honest, I am fully aware I’m not a financial advisor, but what I think I do well is extensive research and coding. I decided I’d try to code a solution for finding patterns (trading signals) within market data to understand when was the best time to buy or sell particular stock.

Just over 6 months later after obsessing over countless technical indicators, weighted thresholds, coding patterns, trading providers, minute vs. hour aggregates, back testing, stock pickers (screeners), and data modeling, my trading bot is finally ready to release into the wild. I usually try to learn while building out side hustles to ensure I make the most out of them. The basic premise of the project was to build a trading bot which would produce a positive return on investment (most of the time at least).

Trading provider (market data and executing trades)

First you need an API to get aggregated market data (preferably in real-time via a websocket) and then you need more APIs to make the trades and get updates on your current positions. Luckily, Alpaca offers both of these. However, the free account provides data that is around 15mins old so is generally only good for testing. For the most up-to-date data you’ll have to subscribe to a paid plan or find another market data provider. 

The good news is that Alpaca provides the market data (via websockets for live data and endpoints for historical) as well as API endpoints to manage orders and portfolio management (inc. current positions etc). This isn’t to say Alpaca is perfect, it isn’t, but it’s certainly one of the best providers for an easy to use API which covers most of what I needed to get up and running with my own trading bot. The missing elements, especially for a UK resident, are that in order to trade with real money (capital) on Alpaca you need to be a US resident. There are also other minimum capital requirements etc but for me the US residency requirement made things very difficult (although I live in hope that Alpaca will eventually open up to UK and European residents soon). Another issue is that for a Node.js developer there is no official (or easy) way to back test (more on this later).

Of course, there are other trading providers which would be useful to integrate into the trading bot, especially if you’re based outside the US. This was one of the reasons for making the decision to make the trading bot as provider agnostic as possible. There is certainly more work to be done in integrating more providers into the RoboBull Trading Bot project and I think the best contenders would be IG, Interactive Brokers and potentially eTorroX. It’s hard to say which one to integrate first. We’ll have to see. I’d also like to review the integration of a crypto exchange. It would be really interesting to see how the current setup and technical indicators handle the crypto market. The only issue there is picking a Crypto provider/exchange which is regulated to avoid further issues.

Technical indicators 

Once you’ve downloaded a batch of past (historical) data you need to compare it with current data to find patterns. There are many, many ways to find patterns (signals or indicators) within the data but there is also no guaranteed approach. There are certainly best practices but definitely no guarantees of success. The first place to start is of course long/short signals, then the moving averages, then more specific averages such as weighted or exponential, and then move into other signals such as Relative Strength Index, Bollinger Bands and Money Flow Index. The trading bot supports up to 11 types of technical indicators (plus bullish/bearish engulfing patterns) with more planned for the future including Extrema (similar to Williams Fractal). All the indicators are fully configurable (even standard deviations) and include weightings which can be adjusted to your own preference. Its worth mentioning that all the weightings for all you enabled indicators must add up to (at least) your thresholdToBuy and thresholdToBuy in order to be able to create an order (trade).

Weighted thresholds (and signals)

Technical signals are important but you need to apply them accordingly. In my opinion, without a weighting, an indicator has no differentiation from the next. I believe a weighting provides that difference, becoming a rating of sorts which is applied to an overall threshold. I decided that my bot would utilise technical indicators which are mapped to specified weightings. These mappings determine whether a signal (BUY/SELL) is generated. If either of the overall weightings threshold (thresholdToBuy and thresholdToBuy) are met, based on all generated signals for a specific stock, an order is created. As mentioned in the previous section above, the indicators and their weightings are fully configurable and can be edited via a file (algos.json).

The discussion around how to use signals effectively for executing trades (orders) is debatable and quite complex. It’s been discussed widely and definitely worthy of another post instead of explaining more in this post. But for now it's worth doing some more research for yourself. And if I had time I would definitely read this.

Stock screener

It turns out, picking the right stocks at the right time is one of the most important things. Sounds simple, however you could have the best trading bot in the world but if you feed it garbage stocks it’ll almost always produce negative returns. I decided to move the stock screener functionality of my trading bot into another service and then integrate (call an endpoint to receive a selection for up-to-date stocks performing well in the last 15mins) it into the bot for ease of use, especially for everyone attempting to use the bot themselves. I'll explain a little more on how my stock screener sources and calculates (ranks) the best performing stocks in another post but whether you choose to use it is completely up to you. I can't guarantee success with any of the stocks it provides, however it can say I've included at least some trending stocks from Reddit and Twitter (although I've had to blacklist some of those trending stocks as they've become too popular and no longer perform well).