0%

【How 2】 Vol. 2. How to build an automated stock trading script

Quick words

This is the Vol. 2 of the 【How 2】 column.
It has been 3 weeks since my previous post. As my MACD strategy automated trading script is almost completed, I would like to share this article to document my current research methodology and workflow that I used to capture the new idea from the internet. Hopefully, this can help anyone who’s doing the same as I am doing now, and I can review this in the future to improve my workflow.

Automated trading script project detail and setup introduction

I’ve been learning algo-trading and building my own algo-trading framework (both workflow-wise and coding-wise) for the past 4-5 months. While researching and backtesting the MACD strategy that I posted here, I finally gain the confidence to proceed to code and build the automated trading script. In these 3 weeks or so, I’ve tried my best to organize the thoughts and produce a workflow that I think it’s reasonable and efficient that can apply to beginners like me (it’s an occupational habit for a project manager to produce a workflow for everything … :)). There are several stages that I have put together so far:

1. Tones of readings

This is the part that probably consumes the most time. First I’ve been spending time reading articles in the forum, quant platform, and also blogs to gain exotic thoughts that are potential enough to be transformed into code. There are several places that I checked out to find out new thoughts:

  • analyzingalpha.com
    • It’s a blog I like a lot which talks about quant trading and data analysis. Even though it published a new article every two to four weeks, the content usually gives me new thoughts that I can put into my quant strategy.
  • tradingsim.com
    • It’s a blog that talks about trading strategy, especially technical analysis. The MACD strategy is originated from here.
  • medium
    • It’s a very well-known media platform that touches everything, from politics to economic or from mental health care to physical training. Even though it doesn’t illustrate the algo-trading strategy in a specific way, it does have tones of materials to read and reflect upon.
  • Quantopian forum (It’s going to be shut down on Nov, 14th, 2020)
    • It’s probably one of the most well-known quant platforms in recent years. Quantopian backtest platform covers testing data for people to access during backtesting, a full set of web-based backtesting tools, and also a thriving community for professional and non-professional quants to discuss drafted strategy and pick each other’s brain. And it’s is free! What else you can ask for more? Sadly, at the end of last week, Quantopian announced the news that it’s going to close down everything by Nov 14th, 2020. There’s no specific reason stated why so, but it is what it is.
  • QuantConnect forum (It’s an alternative after Quantopian is closing)
    • It’s an alternative 3rd party quant platform that I’ve found. It seems good and provides tools for Quantopian users to import their code into their platform. I’m still walking through all tools provided. One thing that I need to vent it out is that their UI/UX in the boot camp and projects is a bit difficult for former Quantopian users like me to comprehend. Still exploring at the moment.

2. Research and backtesting on 3rd party platform

This is the most crucial part of all the stages. You need to transform the ideas into code. Quantopian and QuantConnect are the 3rd party platforms that help you to backtest and adjust the params in your algo. In these platforms, you can backtest your algo strategy against the historic pricing data provided, and the backtest will give you a simulated return over the time frame you specified.

In my experience, the backtest framework is consist of several parts:

  • A stock universe screener that contains selected stocks, such as high-quality stocks that have high ROE or highly leveraged stocks that have high debt to equity ratio.
  • Trading signal generation module that processes data periodically in order to generate trading signals.
  • Trading execution module that picks the right timing to place buy/sell orders based on the signals generated from the above module.
  • Risk management module (or a portfolio weight distributor) that helps manage the risk in various ways: allocating your cash to your target stock by market value, cutting losses if the stock plummeting than expected, … etc..

Also in this stage, you will accumulate abundant pseudocode and code snippets in Jupyter notebook as part of your research. These snippets can be reused while you’re building your automated script later and can save more time.

3. Analyze the backtest result and optimize

Here are a few major indexes that you need to pay attention to while reviewing your algo results:

  • Returns: The final return of the algorithm at the end of the backtest date
  • Alpha: Excess market-neutral return that is generated by this strategy
  • Beta: Represents an individual stock’s returns against those of the market as a whole
  • Sharpe: How much returns can we get if we take one more unit of risk
  • Drawdown: Maximum lost in a continuous period

Therefore, you’re trying to look for a strategy that could give you 1. high return, 2. high alpha, 3. high Sharpe ratio, 4. low beta (not necessary), 5. small drawdown.

Other than these indexes, one thing you might also need to watch out as well: win rate. There’s no formal way of calculating the win rate as far as I know. It basically indicates how effective your strategy compares to the benchmark, usually an index ETF like SPY.

Scenario 1 Scenario 2

In the above example, two scenarios have similar total returns and Sharpe ratios. However, if you look into the diagram, that scenario 1 tends to outperform the benchmark constantly, meaning the portfolio in scenario 1 better hedge the downward risk than the portfolio in scenario 2. This is more of an objective method to compare among portfolios and to find a better strategy that hedges market risk.

4. Write a blog to review the result

As a former senior project manager, one thing that I learned from experience is that documentation and records are the most valuable assets in a project. Therefore I’m writing blogs as a means to preserve the documentation. There’s no technical detail in the blog, and they are left in the code and Github, but the background stories and reasoning of decision making can be preserved better in this way. This is not a necessary step in the research workflow.

5. QA and dry run

By saying QA or dry run your algo, it means that you have to release your algo into the market and let it run without the actual trading, namely “paper trading”. Unfortunately, Quantopian doesn’t provide this service as a tool. There are alternatives that you can try out:

  • Alpaca is famous for paper trading, and you can build your script on your local machine and connect to Alpaca to perform paper trading. If you want to dry run your algo without committing to any specific platform, this is definitely your choice.
  • QuantConnect provides a service for paper trading. However, you need to first construct your strategy on their platform using functions and APIs the platform provided.

Alpaca dashboard

6. Automated trading script. It’s the real deal!

Ok, we’ve been messing around with all the tools/services provided to build our trading strategy that seems profitable. Now it’s time to get your hands dirty and to make something yourself from scratch.

When building your own automated trading script, there are two major topics that you have to addressed beforehand:

  1. Does your broker provide external API provisioned for you to call in your script?
    • TD Ameritrade does have APIs provided and a developer website. However, I’ve been told that their APIs are not user-friendly enough and the documentation is poorly written. Thankfully, there’s an open-source library tda-api that really helps me out here. This library is still being maintained actively and it does have a small group of contributors helping to improve this library. So I do recommend you look into this library if your broker is TD Ameritrade.
    • Another broker is Futuniuniu. It’s a Hong Kong-based broker that allows you to buy/sell stocks traded in the US, HK, and China. Their API document is quite detailed and clear.
  2. Where are you going to get your historic data from?
    • There are tones of free and paid 3rd-party data providers out there such as Quandl, yfinance, IEX Cloud, …. I’m using Tiingo as the price is more approachable for me as a beginner ($99/year for stock pricing and another $99/year for all fundamental data).
    • PS. Of course, as a professional skinflint, I have tried yfinance for a week but finally quit. yfinance is essentially a scraper that scrapes the data from Yahoo Finance! in realtime. However, I’m requesting fundamental data for over 2000 stocks. Once I got ~2000+ API successful responses, then my IP got blacklisted by Yahoo Finance!. So if you’re not requesting thousands of API calls in a short period of time, then yfinance is still probably your best choice.

After you are able to resolve these two major topics, then you can move on to building your automated trading script.

I’m choosing python over C, as pandas is still one of the essential libraries to relieve me from managing the data analyzing and transforming. Here’s my setup for my project:

  • APScheduler: To run scheduled jobs such as buy stocks and sell stocks in a specific time in cron format.
  • multiprocessing: To spawn numerous threads in order to fetch stocks data simultaneously, reducing the total time spent on fetching data.
  • pandas: To batch-process the data fetched.
  • datetime, pytz: To control datetime and timezone.
  • requests: To manage API calls.

The code was too complex and huge so I won’t post it here. You can comment below if you are interested in a specific part of the code, and I can show you how I do it.

Final words

These are pretty much what I’ve been doing in these three weeks. At the moment, I’m dry-running my script in the live environment and see if there are any bugs that I can capture before I put this script into practice. I’m also going to open another column to present the results of this automated trading script that I built. Fingers crossed.

Enjoy reading? Some donations would motivate me to produce more quality content