Files
quanxiel/course/02-episode-2.md
T

27 lines
12 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 02 · How Trading Algorithms Work(算法原理与开发流程)
- **系列**:Full Algorithmic Trading Using Python
- **频道**:TradeOptionsWithMe | **本集**:How Trading Algorithms Work(算法原理与开发流程)
- **时长**:10 分 3 秒 | **原视频**:https://youtu.be/K2fnO7wTUvM
- **本地视频**:[videos/02-episode-2.mp4](videos/02-episode-2.mp4)
## 🎯 本集要点(中文导读)
本集讲**交易算法的原理**与**开发流程**(概念层面)。
- **实盘算法的组成**:① 数据输入(价格/时间为主,可能还有基本面、新闻、订单数据)→ ② 算法分析数据做决策 → ③ 通过 API 把订单传给券商。QuantConnect 代管"取数、托管、券商通信",你专注写逻辑。
- **核心:先找 Edge(ALPHA)**。好的策略靠的是"超额收益 edge",而不是先随便写个策略再硬找理由。Edge 是整件事的中心。
- **盈利 ≠ 有 edge**:要跟**基准**(如标普 500)比,而且要**风险调整后**比;长期跑赢基准才算有 edge。
- **找 alpha 的方法**:科学化地做假设检验(例:小盘股是否跑赢大盘股→用市值分组比较历史表现),并尽量找到**因果逻辑**解释。
- **资源**:Quantpedia(大量策略/论文)、QuantConnect 策略库与论坛、平台内建研究环境(后续课讲)。
- **alpha 会衰减**:越来越多人用就会失效,且策略有容量上限。
- **完整开发流程**:研究找 edge → 编码实现 → **回测** → 优化 → **Paper trading** → 小资金实盘 → 上线后适度监控但**别频繁干预**。
- ⚠️ **回测有偏误**(过拟合、未来函数/前视偏差、幸存者偏差),结果要打折看。
- **要把概念"可量化"**:例如"上升趋势"对电脑无意义,需定义成"60 日均线 > 90 日均线"。
## 📝 完整文稿(英文 · 自动转写)
> 由本地 ASR(faster-whisper)从视频音轨转录,未人工校对,供检索/精读使用。原始讲解以上方视频为准。
Welcome to the second video of my algorithmic trading video course. In this video we will take a look at how trading algorithms work on a conceptual level. Besides that, you will learn what the process of developing a trading algorithm looks like. Before we start though, I quickly want to mention that the QuantConnect team has reached out to me after seeing some of my past videos and has offered to sponsor this video series, so big thanks to QuantConnect for sponsoring this series. As I said, let's start right away by quickly breaking down how trading algorithms work. For this, it is important to understand the key components involved in a live trading algorithm. When developing trading algorithms, you are basically trying to model and quantify reality so that your algorithms can make trading decisions based on this model of reality. To do so, your algorithm requires some kind of input or connection to reality. This is what data is for. Data can come in pretty much all shapes and sizes. Most commonly, a trading algorithm will require pricing and time data, and besides that it might also need fundamental data, certain market stats, news data, order data, and more. After receiving this data, the trading algorithm will then use and analyze this data to make trading decisions. These trading decisions are then passed on to your broker. This is typically done through a so-called API, but theoretically you could also manually execute the traits that your algorithm is recommending. That would be a systematic discretionary hybrid approach. Luckily, getting data, hosting your algorithms, and communicating with your broker are all automatically taken care of by QuantConnect, which is the platform we will be using for this series. That's why you won't have to worry about this stuff too much. Nonetheless, it is very important to understand these things. Next up, let's talk about the process of developing algorithmic trading strategies. In my opinion, this process is overlooked way too often. Especially newcomers think that all algorithmic trading consists of is writing some code for a trading bot, and then you can set and forget it. In reality, this process looks quite different, so let me walk you through it. First and foremost, it is important to understand that what distinguishes a good from a bad trading strategy is an edge. The goal of an algorithmic trader is to best possibly identify such an edge and then build a strategy around this edge. Note that the edge comes first. You can't just start with some random strategy and then try to add an edge. This edge is the centerpiece of the entire strategy. It is also important to understand that just because a trading strategy achieves profitability over a certain time frame does not mean that it has an edge. On the flip side, a strategy that is not profitable can very well have an edge. This is the case because in the world of Quantitative Finance, profitability is not the main benchmark that you use to compare your strategy's performance to. Instead, you always want to compare your strategy's performance to a relevant benchmark which usually is some market index in the given sector. If for instance, your strategy focuses on trading large cap US equities, a good benchmark would be the S&P500 index. So even if your strategy manages a yearly return of 5%, it might not have an actual edge since it's being outperformed by the S&P500. Note that, besides looking at the total returns, it is also very important to look at the risk and volatility associated with these returns. Risk adjusting your returns adds a great deal of comparability. If your strategy manages to generate excess returns over its respective benchmark over a significantly long enough period, it likely has an edge. This edge or excess returns are also known as ALFA. So the first step of developing trading algorithms is to find some form of ALFA. This is usually best done by testing different hypotheses. Testing your hypothesis is best done in a scientific manner using a systematic data-driven approach. Let me give you a quick example of how you could approach finding ALFA. Let's say your hypothesis is that generally speaking, small cap stocks tend to outperform large cap stocks. To test this, you first would need to clearly define what small cap and large cap stocks are. There are different metrics to measure this, but let's say we just use market capitalization. Then you could compare the historical performance of a large number of equities to their market cap. If you can find some kind of meaningful correlation, there might be a strategy that could profit from this correlation. Besides looking at the data, it is also best to try to find some logical explanation as to why your edge exists. Try putting it into cause and effect terms. For example, one possible explanation as to why small cap stocks might generally outperform large cap stocks would be that small cap stocks have much more room to grow than their larger counterparts. If you aren't sure how to perform such research, don't worry, QuantConnect offers a build-in research environment in their platform which we will be exploring in a later lesson. But besides doing your own data-driven research and tests, you can also take advantage of the countless scientific papers that have been published about various possible factors and correlations in the markets. A great website that allows you to read about thousands of different possible strategies is called Quantpedia. It has an archive of thousands of strategies, fundamental breakdowns, relevant papers and other useful data that can be accessed free of charge. Besides Quantpedia, other useful resources are QuantConnect strategy library and forum. Note that it is important to understand that alphas can change. Just because a strategy used to work does not mean that it still does all will in the future. In fact, strategies that work very well often tend to worsen with time since more and more traders will pick up on them. World strategies only have a limited capacity until they stop working. After you have found some alphas, it is finally time to build and code a strategy around this edge. Here it is best to draw out your strategy on a conceptual level before implementing it in actual code. Learning how to implement your strategies using Python is what the main focus of this video series will be on. This implementation part is probably where you have the greatest degree of freedom. Usually, the research part should already have restricted the possibilities to some extent. But there typically are many possibilities of how you could implement a strategy and take advantage of the found edge. Depending on your programming and data science skills, you could go for a very basic implementation, something intermediate or a more complex implementation, for instance using advanced machine learning concepts. Be aware, however, that coding a trading button or anything else for that matter is not nearly as seamless as it might seem in some video tutorials. In reality, you rarely just write down your code in one go and then everything works. Instead, there is a lot of trial and error testing, bug hunting, sanity checking and reading of the documentation forums involved. After you have successfully coded your trading bot, it is time to back test it. Back testing basically allows you to analyze how this strategy would have performed if you would have traded it in the past. This is a very powerful tool since you can gain great insights into the potential of your strategy without risking any money. Nonetheless, it is very important to understand that back test results are not as expressive as real world results. Back testing results can be influenced by a number of biases and unrealistic models. Some of these include overfitting, look ahead bias, survivorship bias among others, but more than these in a later video. For now, just remember to take back testing results with a grain of salt. After thoroughly analyzing your back test results, there are a few optimization procedures that you can apply to your algorithm to potentially improve its performance and versatility. Besides back testing, it is also recommendable to paper-trad your algorithm before funding it with any real money. This is a great last check to test if everything is working properly. If all of the previous steps have yielded positive results, it is finally time to deploy your algorithm and fund it with actual money. Here it is also best to start slowly. After deploying your algorithm, you should not just set and forget it. Instead, you should somewhat actively monitor it and double check if it's doing what it's supposed to do. However, what you should not do is constantly interfere and override the trades from your algorithm. If it's doing something it's not supposed to do, then that's a different story. But otherwise, you can't expect to get any representative results if you aren't giving your algorithm the chance to actually trade according to its programmed strategy. I hope that this walkthrough gave you some insight into what the process of developing trading algorithms looks like. Summed up, you should understand that one of the most important aspects of creating trading algorithms is first researching and finding an edge. Besides that, algorithmic trading is all about modeling and quantifying reality in the most efficient way. For this, it is important to be very concrete and concise. Everything needs to be clearly defined so that your algorithm can understand it. For example, buying case of an uptrend is not something that can be understood by a computer. The problem here is that an uptrend can be interpreted in many different ways. One possible simple definition of an uptrend would be that the 60-day moving average is above the 90-day moving average. Last but not least, you should always remember that coding your own bots takes a lot of trial and error, experimenting and looking at the documentation or forum posts for help. So please don't get frustrated when you can't just code your bots in one go. That said, I hope you enjoyed this video and learned something new. In the next video lesson, we will head over to the QuantConnect platform and start looking at some of QuantConnect's key concepts so that we can start writing our first trading bot very soon. If you are enjoying this free algorithmic trading video series, make sure to subscribe, turn on the notification bell and smash the like button. Thanks for watching.