Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The introduction course of Real quantization-- the compiling idea of ② Real quantization Python Strategy

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Fourth, how to do all this on the real platform

Now that we want to realize our strategy in real quantification, what do we need to do?

First, authentic quantization uses Python to write strategies. We need to have some preliminary understanding of the Python language. Compared with C++ or Java, Python is a very easy-to-use script programming language, which is very suitable for non-computer professional users to quantify transactions.

To take a simple example, you might need to write this code if you directly use C++ to call CTP's API to issue an order delegate:

In true quantization, you only need one line of Python code:

The time saved can be used to study strategies or to do something more meaningful.

4.1platform IDE is not only the development environment but also the running environment

True quantization consists of a simple web-based Python editor, a database that provides huge amounts of data, and a powerful server for running policies. After users have written the policy code in the editor, they can directly test back, trade simulations or trade firm orders. Users do not need to switch frequently between local databases, local compilation environments, and various software on cloud servers, spend their own time and energy recording market data, and do not have to have a headache to optimize the performance of hardware such as network and memory.

The three-in-one design of backtesting, simulation and real disk makes the code reusability significantly improved. The strategy research code can be put into firm trading with only a few changes.

4.2 the idea of compiling Python Strategy in True Lattice quantization

4.2.1 event-driven design ideas

The strategy design of true quantification should follow the development idea of "event-driven". When a new "event" (such as new price data or a change in the status of the account) is pushed to the policy program, the program calls the handler corresponding to the event to perform the relevant operation.

This is slightly different from sequential programming, which we use most often. The order in which the program performs each task depends on whether it listens to the occurrence of specific events. Event-driven programs are in a loop after they are started. This loop is listening for events. The listenable events in true lattice quantization and their corresponding listening functions are as follows:

These events can be divided into the following categories:

For example:

In order to automatically log in to the firm account before the opening of trading every day, we can use the OnMarketQuotationInitialEx function to monitor the stock market to initialize this event. When the event source (that is, the exchange) sends the event (market initialization information), the listening function (OnMarketQuotationInitialEx) that listens to the event will receive the message and respond (such as logging into the firm account).

We should also note that market data needs to be subscribed (through the SubscribeQuote or SubscribeBar function). Only the price of the subscribed item is continuously pushed to the corresponding listening function (such as the OnQuote or OnBar function). Without subscribing to the market, there will be no market data to drive the corresponding listening function.

The following example shows the use of different events to drive different operations. For example, use the "start event" to drive the "login account" function, the "opening event" to drive the "subscription quotation (K-line)" function, and the "K-line update event" to drive the "calculation moving average" function.

Using the idea of event-driven design, we can connect the various parts of the program through the various "messages" that we hear and the "instructions" of the response function.

For event-driven programs, we can also divide the components into "elements" and "action functions":

Elements include:

Event source

Event listener

Event object

Operation functions include:

Monitoring action function

Send event function

Call the response function of the listening function

When we write a strategy, we should pay attention to distinguish between elements and operation functions, to know which events are needed for an operation as a starting condition, and to monitor what actions can be taken after an event occurs.

4.2.2 data subscription

Market subscriptions with different cycles:

In the event-driven section, we have introduced that strategic programs rely on subscription quotes to drive. In backtesting or firm trading, we can specify that the policy operates on the corresponding "time period" by selecting different time period parameters (for example, whether the policy is traded at the minute level or at the Tick level).

In the back test, if the "time period" selects "Tick", the historical tick market can be obtained through SubscribeQuote. " If you select "minutes" for the time period, you can obtain the historical K-line prices of these different minute periods of 1-240 minutes through the SubscribeBar function. " Time period "choose" every day "can subscribe to" day "," week "," month "," quarter "and" year "of these longer periods of historical K-line quotations through the SubscribeBar function.

In solid trading, if the "time period" selects "Tick", you can get the latest tick quotes through SubscribeQuote. " If you select "minutes" in the time period, you can get the latest K-line quotes of these different minute periods of 1-240 minutes through the SubscribeBar function. " Time period "choose" every day "can subscribe to the latest K-line quotes with longer periods such as" day "," week "," month "," quarter "and" year "through the SubscribeBar function.

When using the SubscribeBar function, note that its BarType parameter should correspond to the "time period" parameter:

For example, "time period" selects "every day", the BarType parameter of SubscribeBar function can be written as BarType.Week,BarType.Month, BarType.Day, etc. "time period" selects "minute", and the BarType parameter of SubscribeBar function can be written as BarType.Min3,BarType.Min15,BarType.Min and so on.

For the historical market of the past period of time, such as all daily K lines from December 1, 2018 to March 1, 2019, you can query it through the GetHisData function.

4.2.3 transaction related processing

The most common thing we deal with in transactions is all kinds of information related to accounts.

The information related to the account in the true quantification can be regarded as an "object". For example, the account itself can be used as an object, and its various "methods" can be called, such as login, query transaction, risk control and so on.

For example, the login method for an account:

And the query of the account transactions, positions and other information, itself can also be used as an object, with the corresponding "attributes" and "methods". For example, for a "delegate object", you can query its delegate price, quantity and other properties, as well as query whether the delegate is revocable or not.

For example, some properties of a delegate are queried by listening for a delegate state change (OnOrderChange):

4.3 how should Python be written?

For Python beginners, you can refer to the "Rookie course" to learn the basic grammar. When you have a knowledge of Python's loop control, arrays, and functions, you can already use most of the functions of true lattice quantization.

For users with some experience in Python programming, you can carefully read the "real quantization API documentation" (where the importance of the API document is equivalent to the importance of the flight manual to the pilot, 80% of your questions can be answered in the API document) and refer to tutorials devoted to the application of Python in the financial field, such as "Python for Finance-Analyze Big Financial Data". There are also many Python programming and strategy design skills related to real quantification on Wechat official account (searching for "true quantification" on Wechat) and Zhihu (on Zhihu search for "true quantification"), which can provide inspiration for you to write your own quantitative trading strategy.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report