Files

24 lines
25 KiB
Markdown
Raw Permalink 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.
# 18 · How to Live Trade(实盘部署与 Paper Trading)
- **系列**:Full Algorithmic Trading Using Python
- **频道**:TradeOptionsWithMe | **本集**:How to Live Trade(实盘部署与 Paper Trading)
- **时长**:26 分 3 秒 | **原视频**:https://youtu.be/-F3ITjfelrM
- **本地视频**:[videos/18-live-trading.mp4](videos/18-live-trading.mp4)
## 🎯 本集要点(中文导读)
**把算法部署到实盘(Live Trading)**,并演示 **Paper Trading**。
- **量化实盘选 QuantConnect 托管的好处**:同地机房服务器(数据更快、成交更快)、比自建更抗断电/断网。
- **需要券商账户**:券商要支持算法交易(有 API)。QuantConnect 支持 10+ 家(Interactive Brokers、OANDA、Tradier、Binance 等);作者推荐 **Interactive Brokers**(品种全、价优,支持 100+ 市场)。
- **流程**:先充分回测 → **Paper trading(用实时数据的模拟盘)** → **小资金实盘** → 持续监控。
- ⚠️ **回测 ≠ 实盘**:滑点、成交、佣金、做空券源、各类偏差都会让实盘偏离回测。
- ⚠️ **风险**:算法依赖"数据源 + 与券商 API 的稳定连接",任一中断算法就工作异常;要监控服务器/算法状态。
- **不要"set and forget"**:尤其初期要主动盯着它是否按预期运行,但**也别频繁手动干预**。
## 📝 完整文稿(英文 · 自动转写)
> 由本地 ASR(faster-whisper)从视频音轨转录,未人工校对,供检索/精读使用。原始讲解以上方视频为准。
Welcome back to another video. In this video you will learn how to set up live algorithmic trading inside of QuantConnect by connecting your trading bot with your broker account. Since 2015, QuantConnect has hosted more than 200,000 live algorithms and over $20 billion in volume has been traded on their servers. Besides covering how to live trade with real money, I will also demonstrate how you can set up paper trading with live data in this video. If you are new to QuantConnect, I recommend first watching the first videos of my algorithmic trading course in which you will learn everything you need to know to start developing your own trading algorithms. If you do not want to host your algorithms using QuantConnect, you can still use their open source lean engine to develop and write your trading bots. However, then you would have to find your own hosting solutions. But in this video, we will focus on how to set up live trading using QuantConnect's hosting options since that is definitely the easiest option. Besides that, QuantConnect hosts the algorithms on co-located servers which means the servers receive data faster. This can lead to faster order executions and fills. In addition to that, it can better protect you against interference problems such as power outages or internet problems compared to hosting the algorithms on your own. That being said, before we head over to the QuantConnect platform and I develop a simple example bot and show you how you can deploy it to live trading, let me outline some general key points that you should be aware of when switching from backtesting to live trading. First and foremost, to be able to trade algorithm quickly, you need to have an account at a brokerage that supports algorithmic trading. This is usually the case if the broker offers an API for you to use and connect your algorithm with. As of the recording of this video, QuantConnect supports over 10 brokers including interactive brokers, Wanda, Tradia, Binance and more. However, they are continuously working on adding support for more brokers. You can check out the link in the description box to see which brokers are currently supported for which asset classes at the time you watch this. If you do not yet have a broker account with one of the supported brokers, in my opinion interactive brokers is a great choice since they are one of the biggest brokers worldwide. They offer stocks, options, futures, forex, bonds and funds on over 100 markets at competitive prices. To learn more and help out this channel, you can check out the referral link below. I will later demonstrate how to connect a broker platform using interactive brokers as an example, but this will be similar for any of the other supported platforms. Before deploying a trading algorithm to trade with real money, you will want to back test and analyze its performance thoroughly. However, here it is important to understand that back test results are not equivalent to live results under real world conditions. The reason for this is that when developing and back testing a strategy, you are basically trying to build the best possible model of reality. However, no matter how good this model is, it will still just be a model. That's why there is a difference in simulated back test and actual performance, even if they both cover the exact same time frame. Such differences can result from unrealistic filling conditions, fees, market impact, biases like overfitting or look ahead bias, differences in data, brokerage specific differences and more. The process of understanding this discrepancy is called reconciliation. And to analyze it, QuantConnect's live trading page displays the actual performance of your algorithm and a so called out-of-sample back test performance. The out-of-sample back test simply is a back test that was performed over the same period as that of the live trading performance results. Obviously, this can only be done after letting the algorithm run on live data for a while. The closer these two performance plots are to each other, the more realistic your back testing model of reality seems to be. This means that the performance resulting from back testing seems to be more indicative of actual performance. Besides looking at the chart for visual analysis, there are also different metrics you can use to measure the reconciliation. One such metric is the correlation between the returns generated by the back test and the actual returns. The closer to one this correlation is, the more correlated the returns are. Another metric that can be used to analyze the similarity of two sequences is the dynamic time warping distance or DTW in short. A DTW value close to zero means that the two sequences are more similar. For intuition, you can think of an annualized DTW value of 0.2, meaning that there is about a 20% deviation between the back test and actual performance over one year. Note that it is very hard to achieve a DTW value much lower than 20%. However, note that these two metrics are at the time of recording this not yet available for live trading. Instead, they are currently only available for alpha streams. For more details on these metrics, check out the documentation page below. Since back testing uses a theoretical model, it is always a good idea to first paper trade your algorithms on live data before deploying them with real money. QuantConnect offers multiple ways of doing this which I will demonstrate in a few minutes. Another important difference to understand between back testing and live trading lies in how your algorithm receives data. When back testing, you will usually use QuantConnect's data feed which gives you algorithm standardized data at a very specific point in time. However, when trading based on live data, you cannot always decide at what exact time you receive the data. Especially for custom or alternative data, you need to be aware that you can first use the data once it is available which might not always be at the same time. So far, we often checked if certain data was available at one specific point in time. This might work without any problems in back testing, but in reality, data might not be available at one specific point in time. Therefore, it often makes more sense to check for new data over span of time instead of at one particular time like you would in back testing. You can also use QuantConnect's data feed as the input for live trading. Alternatively, you can also use the data feed of one of the supported brokers like interactive brokers or trade a year. Just note that, to get access to real time data through IB, you will need to subscribe to one of their data feeds. Depending on where you get your data from, make sure to have a deep understanding of the data format, the related time zone and how and when your algorithm will receive this data. Last but not least, before we move on to actually deploying a live algorithm, let me outline the prerequisites for this. Besides having an algorithm that you would like to live trade, you will also need a live trading node to actually deploy your algorithm. This is the case, no matter if you want to trade with real money or just pay to trade your algorithm. Purchasing a subscription to a live trading server node will give you access to hosting your algorithm on one of QuantConnect's low latency co-located New York servers. QuantConnect currently offers four different live trading nodes that only differ in the amount of rum available. The cheapest node has half a gigabyte of RAM and costs $24 a month, while the most expensive node offers four gigabytes of RAM at a monthly cost of $96. Note that there is also a cheaper yearly subscription option. For most relatively simple algorithms, a cheaper node should be sufficient. But if you're planning on developing an algorithm with a huge universe or a memory intensive machine learning model, a more expensive node might be better suited. That being said, let me now quickly outline the strategy that we will use to demo the deployment process. Instead of writing an entirely new trading algorithm, we will actually use a slightly modified version of a strategy that we implemented in one of the previous videos. For a step-by-step walkthrough on how I created this bot, check out video 10 of my algorithmic trading course. There's a link in the description box below to that video. However, note that I made some slight code structure modifications to it, but the strategy itself basically stayed the same. But before we move on, let me still give you a quick high-level overview of the strategy that this bot follows. The algorithm follows a dynamic allocation strategy to QQQ and BND, which are two ETFs that track the top stocks on NASDAQ and the bond market respectively. Depending on the current trend of QQQQ, we either want a more aggressive allocation with 80% to QQQQ and 20% to the bond ETF or a more conservative 2080 allocation. The idea behind this is that bonds often are more stable than equities, especially during market downturns, which is why we switch to them in case of a downtrend. However, at the same time, equities have had larger historical returns, which is why we still want to capitalize on them. We identify the current trend using a 30 days simple moving average of QQQQ's price. If the average is below QQQ's price, we consider that to be an uptrend and vice versa. If the trend does not change for 30 days, we rebalance our portfolio back to the 8020 or 2080 allocation. That said, let's now head over to QQQQQ and take a quick look at this algorithm before we deploy it to live trading. The algorithm trades at an hourly resolution while the SMA is updated at a daily resolution. I didn't go for a higher resolution such as Minnadley since this would lead to a bunch of fast allocation changes while the price jumps above and below the SMA's value. In the initialised method, we set the capital for this algorithm to 100,000 US dollars. Keep in mind that this is just for backtesting purposes. When we live trade our algorithm, this number will be disregarded and the actual balance from your broker account will be used. Since I will be setting up live trading using an interactive broker's account, I will set the brokerage model for this algorithm to be an IB margin account. This will make sure the backtesting settings, including commissions, will be as realistic as possible for that account. QQQQQ also has the option for you to distinguish between backtesting and live trading mode using the live mode flag. So if you want your algorithm to treat certain scenarios differently in backtesting than an actual trading, you can do so with a simple if statement. A common use case for this would be if you are using custom data from a third party API, since in backtesting you will just use historical records while in live mode you want to perform actual API calls. That said, let me now build and backtest this algorithm so that we can get a brief first impression of this strategy's performance before I connect it with the IB account. As you can see over the time period specified in the initialised method, this algorithm seems to perform relatively well. But this isn't too surprising since QQQQ was generating good returns throughout this period. To get a good overview of when this algorithm is using which allocation, it plots QQQQ's SMA on the benchmark chart. Especially during live trading this can be a great way to determine whether the algorithm currently thinks that there is an uptrend or downtrend. Since we are using an hourly resolution, the benchmark chart will be updated with QQQ's price once an hour. The SMA value on the other hand will only be updated once a day since we do this at a daily resolution. If you want to analyze this backtest report in more detail or in general play around with this algorithm, there's a link in the description box below that allows you to clone this code. Next up, let me show you how you can set up live trading in QQQQQ. For this, I will first show how you can pay per trade directly through QQQQQQQ and thereafter how to connect your algorithm to interactive brokers. For either case, you need to click on the go live button at the top next to the backtest and optimize button. This will bring you to a new view which will look like this. Here, you first need to choose a brokerage that you want to use for their live trading. For this first demo, I will just go with QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ or for both. Notifications are a great option for you to gain insights into when your algorithm is doing something without you having to actively monitor it. Last but not least, you can choose whether you want your algorithm to automatically restart in case it runs into an error. Once you set everything up, you can click the deploy button and the algorithm will start running with real market data. Since it is still a paper trading, it won't trade any real money though, but this is nonetheless a great way to test whether your bot is set up correctly to handle live data. Once an algorithm is deployed to live trading, you can view its live performance through this page. This page has a similar structure to the backtest view with runtime statistics at the top, some charts and the ability to view orders and logs at the bottom. A new tab at the bottom is the holdings tab. This allows you to view your current positions, subscribe to new data for more securities and even place manual open and close orders for any of the subscribed securities. In case you want to stop your algorithm, you have two options. At the top, there are two buttons. The liquidate button liquids all your positions and the stop button simply stops your algorithm. Note that just clicking the stop button will leave all currently open positions open. This is especially relevant if you have an actual live broker account connected, so keep this in mind. Since you now hopefully have at least a basic understanding of what deploying an algorithm through QuantConnect looks like, let me outline how you can connect an actual interactive broker's account to UQC algorithm. For this, it is very important to understand some of the intricacies involved in connecting an IB account. So let's go over some prerequisites and important notes first. It is both possible to connect an interactive broker's paper trading account or a real account to QuantConnect. Regardless of whether you already paper traded your algorithm in QuantConnect, I would highly recommend first trying to connect your ALGO to an IB paper trading account to fully understand the process. That said, the first requirement is having an IB pro account. API trading is currently not supported through a LITE account. Secondly, it is important to understand that interactive brokers does not support more than one active session per login credentials. This is the case both for paper and live trading accounts. Here, an active connection to QuantConnect also counts as one active session. This means, while you are connected to QuantConnect, you cannot use the same login credentials to log into your IB trader workstation to make trades. If you attempt to do this, you will receive the following error. You are logged in without trading slash market data permissions, you can still manage your account but can't trade. If you would then click Continue, you would automatically sever the connection to QuantConnect and your algorithm would run into an error. So to still be able to trade from your account, besides through your algorithm, there are two possible solutions. The first would be to exclusively trade through QuantConnect's live trading page. This is possible since you can still place manual trade orders directly through QuantConnect. But an alternative solution would be to create a second user under the same name for your account. This resolves the issue since these are new login credentials for the same account which means you will be able to have another active session in that account. I'd recommend creating a second account and using this exclusively for algo trading. One more thing to be aware of is QuantConnect's compatibility with IB security measures. As of the making of this video, QuantConnect is not compatible with Interactive Brokers 2-Factor SMS authentication. So if you want to use 2-Factor authentication for better security, you will have to do so through the Interactive Brokers mobile app. Note that if you use 2-Factor authentication, you will have to re-authenticate the connected account once a week to maintain the connection to QuantConnect. With that out of the way, let me now present to you how you can connect an actual Interactive Brokers account to your QC algorithm. For this demonstration, I will simply use my IB paper trading account, but note that the process is equivalent for a real account. So once again, we click on the Go Live button to open the Live Deployment selection view. Instead of choosing QuantConnect paper trading, we select Interactive Brokers this time. Now you will have to enter your IB account username, account ID and password. Note that an Interactive Brokers paper trading account uses a different login and account ID than your actual main account. So if you want to set up the paper trading, make sure to enter your paper trading account credentials here and not your live trading account login. Also note that QuantConnect does not save your Broker login credentials. Before deploying the algorithm, you can now also select the data source that you would want your algorithm to use. If you want to use the Interactive Brokers feed as the data source, you need to have purchased a live data subscription from IB since you otherwise might get delayed data. Alternatively, you can also just use QuantConnect or both as a data source. If you choose both as a data source, the algorithm will first attempt to get the data from QuantConnect and then it requested it from IB if that was not possible. Note that you need to select IB only or both as a data source if you want to access options or index data. Lastly, you can turn on automatic restarts for your algorithm. If this is turned on, the algorithm will automatically attempt to relaunch once it runs into an error. If this happens, you will be notified by mail. However, if an algorithm fails to restart five times in a row, it will no longer attempt to redeploy. When deploying the algorithm, make sure that you are not currently logged into your IB Broker account with the same credentials anywhere else. Otherwise, the algorithm will run into an error and fail to deploy. Once the algorithm is connected with your IB account, you will see the same view as for QuantConnect paper trading. Then you can once again place manual trade orders through the QuantConnect interface. But here it is important to understand that the equity chart and performance metrics of the QuantConnect Live trading view will take many of your positions and already existing positions of your portfolio into account as well. So keep this in mind, for example, when wondering why your out-of-sample backtest performance might be deviating a lot from the show-on performance. In general, if you want to truly test the performance of your algorithm strategy, you should not interfere with it too much since you otherwise won't get representative results. If you ever want to change the code of a live running algorithm, you will have to stop and redeploy it. Since this algorithm uses an hourly resolution, let's let some time pass and then come back to see if it's actually doing what it's supposed to do. As you can see, the algorithm has now opened a position and the performance chart is no longer completely flat. We can scroll down and view our portfolio as well as any order sent out over the lifetime of this algorithm. Furthermore, we can check the logs in which we printed the moving average value as well as QQQ's most recent price. As you can see, the SMA is above the price, which means that the algorithm considers there to be a downtrend. That is why we invested 80% of the available capital into BND and only 20% into QQQ. Now, let me log into my IB account to check if we can actually see if it's connected correctly. Since I already connected my paper trading account to the algorithm, I won't be able to fully connect to the IB platform using the same credentials. However, instead, I can just use the real account credentials to log into paper trading mode. This will still allow me to view the portfolio holdings of the paper account and it won't lead to any double connection problem. Like I said earlier, if you connect your main account to QQQ, you can just create a second user for the same account to work around this problem. But since you can't create a second user for paper trading account, this is not possible here. After logging in, we can see that almost our entire portfolio is invested in equities, which is exactly what we expect. If we click on the detailed view of the invested stock portion of our portfolio, we can actually see how much is invested in which stocks. Here we can see that, just like we want it to be, about 80% is invested in BND while 20% is invested in QQQQ. Last but not least, let me go over some important aspects to be aware of when switching from backtesting to live trading and some general risks to know. First of all, when developing and testing your algorithms, make sure to put some thought into how your algorithm handles any potential existing positions. In many previous videos, we used if statements that test whether the portfolio is invested to make trading decisions. While this works great in backtesting since the portfolio will only be invested if the algorithm decides to open a position, this is not necessarily true in reality. You might already have other open positions when deploying the algo or want to place manual trade orders in certain scenarios. So always make sure to do asset specific checks instead of portfolio wide actions if that's not what you want. Another example of this would be using the liquidate method without passing a specific position. This will not only close positions that were opened by your algorithm, but it will close all open positions in your portfolio even if they existed long before you deployed your algo. To simplify things, it can be a good idea to have a completely separate account just for algo trading. Also note that your algorithm relies on data as well as a stable connection to your broker's API. If the data feed or API is down, your algorithm will not be able to function properly. This usually does not happen very often, but nonetheless you should be aware of these risks and try to add certain safeguards for situations like this. That's also why actively monitoring server uptimes and your algorithm is a very good idea. Furthermore, please remember that just because an algorithm performs well in backtesting is by no means a guarantee that it will perform great in reality. Unrealistic slippage, execution, commission settings, short share availability and biases are just a few examples that could lead to deviations in backtest and real-world results. That's why I highly recommend live paper trading your algorithm before deploying it with any real money. And even after that, start with small amounts of capital instead of rushing into it all at once. But like I mentioned earlier, try to go through the entire deployment process in paper trading mode to familiarize yourself with everything first. Also, don't make the mistake and just set and forget your algorithm. Especially in the beginning, it's a very good idea to actively monitor what it's doing to make sure that it's indeed doing what you want it to do. That being said, I hope you enjoyed this video and now understand how you can deploy your own algorithm to live trade for you. If you did, make sure to smash the like button, subscribe and turn on the notification bell. Thanks for watching.