CTA strategies, or Commodity Trading Advisors (CTA), generally require at least 60% of assets to be invested in the futures market. In other words, if a private fund managing futures strategies has 100 million yuan available for investment, at least 60 million yuan must be allocated as futures margin. CTA strategies have always been an important component in the field of quantitative investment, offering more stable returns and lower risks compared to stock quantitative strategies. It is precisely the hedging nature and high frequency of CTA strategies that smooth out all market fluctuations, meaning that as long as you hedge well, you can largely ignore the overall market.
Recently, I organized the development tools for CTA strategies. There are programmable trading software available in the market, such as TB or MC, but on one hand, these commercial software packages are quite expensive, and on the other hand, the so-called EasyLanguage for programming is actually very niche, making the development of complex functions quite troublesome. After some comparisons, I ultimately chose VN.PY as the development platform for CTA strategies. This article introduces the basics of VN.PY to help everyone understand VN.PY and get started quickly, rather than focusing on strategy development (which could fill a book...).
1. Installing VN.PY#
CTA strategies based on VN.PY mainly rely on VNStation, which can be downloaded from the official website for the latest version. The current latest version is 2.5.1. The default installation directory is C:\vnstudio. Here are some points to note:
- VNStation comes with a Python package, so there is no need to install Python locally beforehand. For the development IDE, such as VSCode, it is recommended to use the built-in Python package as the compiler. It is not advisable to have other Python environments locally; it is best to use VNStation exclusively.
- The source code for vnpy can be found in C:\vnstudio\Lib\site-packages\vnpy, but this source code differs somewhat from the one on GitHub. The vnpy directory should be consistent, but the GitHub source code includes other directories, such as examples, which contain many reference source codes not available in the installation package.
2. Starting VNStation#
- Create a strategy code directory
Create the directory C:\Users[username or Administrator]\strategies, where the completed strategy code can be placed. - Start VNStation
Launch VNStation, and the page will appear as follows:
Select 【VN Trader Pro】 to start the following page:
On the left side, choose the underlying interface as 【CTP】 or 【CTP Test】. On the right side, select the upper-level applications based on our needs, generally choosing 【CTA Automatic Trading Module】 and 【CTA Backtesting Research Module】 more often. Ensure that the running directory matches the directory created in step 1. Click to start. Additional explanations are as follows:
- CTP and CTP Test
Currently, the Shanghai Futures Exchange supports API access for penetrating supervision, primarily developed in C++. Since VN.PY has already handled this complex task for us at the underlying level, we do not need to worry about this part. However, connecting to penetrating supervision still requires an account, both a test account (i.e., CTP Test) and a live account (i.e., CTP). We use the test account to verify the correctness of the investment strategy and the live account for actual trading. The application for this account is mainly done through the futures company you are associated with. The application steps can refer to the article “After reading this, you will completely master the futures penetrating CTP API”.
If you only want to conduct strategy research temporarily without live trading, there is another way to obtain a CTP trading account, which is through Simnow's simulated live/test account. If you plan to use a Simnow account, please pay attention to the following points:
- The underlying interface should be selected as CTP, not CTP Test!!
- SimNow provides two environments, where the first simulated live environment can only be used during trading hours (9:00-15:00), and the second simulated testing environment can only be used during non-trading hours (all other times); using the first environment during non-trading hours will result in no output information in the log area at the bottom left of the main interface. If you cannot connect during the corresponding time period, it is recommended to log back into VnTrader to try again.
- The first environment is divided into three groups of servers; according to vn.py's official recommendation, you can only connect to the second and third groups of servers (versions that support penetrating verification), and not the first group.
- Due to operational maintenance reasons, SimNow servers may sometimes undergo downtime for maintenance (especially frequent after 2019), at which point you can only wait until the next trading day to try again.
- CTA Automatic Trading Module and CTA Backtesting Research Module
As the names suggest, the former is for implementing CTA strategies, while the latter is for backtesting CTA strategies. If neither is selected, the subsequent startup page can only perform basic buy and sell operations based on the page and cannot use the CTA strategy trading functionality.
- CTP or CTP Test Connection
Enter the main page of VNTrader, as shown below:
Click on the top left 【System】 - 【Connect to CTP (or Connect to CTP Test)】 to configure CTP (or CTP Test):
Regarding the configuration of CTP or CTP Test, generally, futures companies will provide relevant documentation. If it is based on Simnow's CTP configuration, you can refer to the official website introduction or the article “Domestic Futures CTP Configuration Tutorial”. The log window page after a successful connection to Simnow is as follows:
After successfully connecting to the futures company, it may not be as detailed as Simnow; everything is subject to the standards set by the futures company.
After connecting, you can confirm whether the data in the 【Position】 and 【Funds】 windows is displayed correctly.
You can also test whether market data can be correctly retrieved by entering the contract code in the 【Trading】 window - 【Code】 section, pressing Enter, and checking if the market window displays correctly. Note that the contract code must be entered correctly; the naming rules for contracts differ among the four domestic futures exchanges, and vn.py uses the official naming convention, which is case-sensitive. For example, TF2003, rb2003, and so on.
3. CTA Backtesting and CTA Strategies#
- Global Configuration of VNTrader
Before using 【CTA Strategy】 or 【CTA Backtesting】, we need to prepare the data. In strategies, we may need to calculate technical indicators based on historical data, and in backtesting, we may use historical data to validate strategies.
VNTrader's data preparation can be done in two ways: using RQData data or building a local database service. The former is more convenient and is recommended; the specific usage method is as follows:
- Open the 【Configuration】 page
- Configure the following two items on the 【Global Configuration】 page
Note: The rqdata.username and rqdata.password here are not the same as the username and password on the RQData official website!!
rqdata.username is fixed as: license
rqdata.password is: (after applying for rqdata, the official website will provide a long letter License)
After configuration, start 【Function】 - 【CTA Backtesting】 and confirm whether RQData is connected normally in the log window, as shown below:
If it cannot connect normally and checks are correct, you can try to open the VN Station prompt window and enter pip install rqdatac -U to update and see if it resolves the issue.
Additionally, RQData provides continuous contract data in various types, such as 88, 888, 99, etc. Taking stock index futures contracts as an example, the differences are as follows:
- IF88: Simply concatenates the volume and price data of each main month of the IF stock index futures without any smoothing, resulting in gaps in data during the main contract rollover; therefore, this data is only suitable for intraday CTA strategy backtesting (no positions at close);
- IF888: Based on IF88 data, records the price difference amount when the main contract rolls over and adds or subtracts this price difference from all previous historical data for smoothing; therefore, this data is suitable for overnight CTA strategy backtesting (positions at close);
- IF99: Data weighted by cumulative open interest from all tradable varieties, resulting in index data; since it cannot be traded in reality (the exchange does not provide index contracts), this data is not recommended for backtesting but can be used for certain data model research.
- CTA Backtesting
VN.PY has pre-developed some classic strategies for investors to backtest and trade, such as the ArtRsi strategy and DualThrust strategy, which beginners can start with by using or modifying these strategies.
Start 【Function】 - 【CTA Backtesting】, select a strategy, input the local code (formatted as 【Commodity Code.Commodity Exchange】, e.g., TF2003.CFFEX) and other backtesting conditions, and click 【Start Backtesting】. The backtesting page will display as follows:
Various backtesting indicators are displayed in the middle, while the graphical pages for 【Account Net Value】, 【Net Value Backtesting】, 【Daily Profit and Loss】, and 【Profit and Loss Distribution】 are displayed on the right side.
Note:
- (Backtesting) trading does not necessarily start from the start date, as VN.PY requires a certain number of trading days (e.g., 10 trading days) of data for initialization, during which there will be no trading data. At this point, you can confirm through the backtesting indicator's 【First Trading Day】. This part of the program can refer to the self.load_bar(10) in the strategy's on_init function.
- The backtesting page cannot specifically set the commission for closing positions (as the commission for closing positions and overnight positions differs significantly, affecting profit results).
- If the backtesting indicator bar in the middle does not display correctly, you can try
i) reducing font.size in the global configuration
ii) setting line 184 of C:\vnstudio\Lib\site-packages\vnpy\app\cta_backtester\ui\widget.py to a smaller value, such as 600.
In the 【CTA Backtesting】 page, you can also perform parameter optimization. For example, if a strategy involves comparing moving average entanglements, you need to explore how to set the window periods for the fast and slow moving averages to achieve the best return or Sharpe ratio. Click 【Parameter Optimization】 to iterate through the target value, fast moving average window value, and slow moving average window value for comparison. After the comparison, you can click 【Optimization Results】 to view.
- CTA Strategies
Start 【Function】 - 【CTA Strategy】 and confirm that the log window shows the following information:
Select a strategy and click 【Add Strategy】,
strategy_name: represents the strategy name, which can be any string, such as demo1
vt_symbol: represents the trading code, formatted as 【Commodity Code.Commodity Exchange】, e.g., TF2003.CFFEX
Others should be set according to the needs of your strategy.
You can see that some variables, such as atr_value, atr_ma, etc., are set to 0, which is mainly because they have not yet been calculated through historical data. Click 【Initialize】 to complete the strategy initialization and assign values to the aforementioned required variables.
After initialization, you can click 【Start】 to launch the strategy, at which point quantitative trading will begin according to the strategy.
At this point, even if you close the 【CTA Strategy】 window, the execution of the strategy will not stop.
- Strategy Debugging
Method 1: CMD Debugging
If you want to debug the strategy, you need to start VN Station through the VN Studio Prompt. Enter the command line: python -m vnstation. As shown in the figure below:
In VN Trader, select the CTA Backtesting module, choose the strategy you want to debug, and click to start backtesting. The corresponding debug output (which needs to be set according to personal needs, generally using the print() function) will be displayed in CMD. The output shown in CMD is what the Python interpreter outputs when running the strategy file. This achieves strategy debugging.
Method 2: Jupyter Notebook Debugging
Launch VN Station through the desktop icon, and in the pop-up graphical interface, select Jupyter Notebook.
Set the directory where the strategy to be debugged is located as the running directory for Jupyter Notebook, i.e., C:\Users[username or Administrator]\strategies.
In Jupyter Notebook, import the strategy to be debugged,
#%%
from vnpy.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting
from vnpy.app.cta_strategy.strategies.atr_rsi_strategy import (
AtrRsiStrategy,
)
from datetime import datetime
Then modify vt_symbol, interval, start, end, rate, slippage, size, pricetick, and capital according to the trading variety you want to backtest, the start time of the backtest, the end time of the backtest, trading commission, slippage, price tick, and backtesting capital. Finally, set the strategy to be debugged with engine.add_strategy(the strategy to be debugged, {}).
#%%
engine = BacktestingEngine()
engine.set_parameters(
vt_symbol="IF88.CFFEX",
interval="1m",
start=datetime(2019, 1, 1),
end=datetime(2019, 4, 30),
rate=0.3/10000,
slippage=0.2,
size=300,
pricetick=0.2,
capital=1_000_000,
)
engine.add_strategy(AtrRsiStrategy, {})
Run the code, and if an error occurs, you can access the variables in the strategy through engine.strategy. to troubleshoot the error. You can also use the print() function in the strategy code. More detailed demo code can be found in vnpy-master\examples\cta_backtesting\backtesting_demo.ipynb.
4. Related Learning Materials for VN.PY#
This article introduced a quick start to VN.PY, which is clearly just the tip of the iceberg for learning the vast and profound VN.PY. The following learning materials can help everyone gain a deeper understanding of VN.PY.
-
VN.PY WeChat Official Account Video Tutorial (Paid)
Tutorial Name: “All Practical Advanced Strategies - CTA Strategies.” This tutorial is very suitable as a primary learning resource as it operates the computer screen while teaching, gradually breaking down the content from easy to difficult. Depending on your positioning (investor, strategy developer, platform developer), you can selectively watch different chapters (this selection needs to be judged by yourself). -
VN.PY Official Website Project Documentation
https://www.vnpy.com/docs/cn/index.html
This project documentation is concise and more suitable for reference; it may be a bit difficult for beginners, so it can be viewed in conjunction with the videos for easier understanding. -
VN.PY Zhihu
https://zhuanlan.zhihu.com/vn-py
This is the Zhihu channel of the VN.PY author, which is continuously updated and can serve as ongoing learning material. -
VN.PY Snail Blog
http://www.snailtoday.com/archives/category/qihuo/vnpy
This is a learning blog for VN.PY by a prominent figure, with many articles that complement the official VN.PY documentation, covering many principles of VN.PY source code and being highly practical, more suitable for platform developers. -
“Python Quantitative Trading” (by Zhang Yangfei)
This book is written based on VN.PY for quantitative trading, with the advantage of providing a very detailed introduction to the principles of VN.PY. Additionally, it offers detailed financial explanations and descriptions of some classic trading strategies pre-developed by VN.PY, filling in the gaps of the official documentation (which focuses on Python development). However, the downside is that VN.PY updates its versions quickly, and much of what is introduced in the book may not align closely with the latest version.