vnpy source reading and learning: preparation

vnpy source reading and learning


  1. By reading vnpy, learn some design ideas and concepts of quantitative trading system.
  2. Learn some skills and paradigms of python project development by reading vnpy
  3. Through the design of vnpy, we can use python to reproduce a small and simple quantitative trading system
  4. See if you can use java or. net to make a similar quantitative trading framework


Before reading all the source code, let's look up some of the work done by predecessors. For example, the source reading, evaluation and working principle of vnpy. Avoid detours. I found the document. Make a record for future use.

VNPY architecture design document

Source code interpretation of (I. initial test of environment configuration and backtesting)

Source code interpretation of (II. Code analysis of real deal)

Source code interpretation of (III. code analysis of event driven engine)

Source interpretation of (IV. main engine code analysis - initialization function) source code interpretation (v. main engine code analysis - CTP module) source code interpretation (VI. main engine code analysis - policy module)

Source interpretation of (VII. Back testing code analysis)

Source code interpretation of (VIII. Code analysis of back test results calculation) source code interpretation (IX. policy code analysis)

Source code interpretation of (X. parameter optimization)

Among them, the source code interpretation part has a more detailed explanation, but it lacks a systematic overview, and the interpretation is not systematic. When we learn the specific modules, let's combine them. Read now VNPY architecture design document Have a deep understanding of the overall structure of vnpy.

directory structure

First, we list the source directory structure to sort out the source directory structure, so that we can be familiar with the project on a macro level.

+circleci a configuration file of yml, shelved

+github some of the agreed md files of github, shelved

+ci shelved

+docs file folder, read it first to avoid mining pit


+Test placed the unit test file and shelved it

+Vnpy is the key point of vnpy, which is all class library files

After sorting out the above directory structure, we will focus on the following directories

+Doc file folder, read it first to avoid mining pit


+Vnpy is the key point of vnpy, which is all class library files

The order of research is to read the documents first, understand and be familiar with vnpy, and then start vnpy through the code in examples. With the running of the program, we have a deep understanding of vnpy.

Follow the entrance

Through reading the directory structure and documents, we can see that there are many kinds of codes calling vnpy or some modules of vnpy in examples. What we need to do is to find these codes, and sort out the venation of vnpy with these codes.

The first thing we find is the file / examples/vn_trader/ From the name, we can guess that it is the entry program to start vntrade, find a thread head, and then sort out the whole clue.

In, we have a rough look at a large number of codes. After cleaning up the annotation codes, we have reached the following codes:

from vnpy.event import EventEngine
from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import MainWindow, create_qapp
from vnpy.gateway.femas import FemasGateway
from vnpy.gateway.deribit import DeribitGateway
from import OptionMasterApp

def main():
    #Create pyqt interface
    qapp = create_qapp()

    #Event engine
    event_engine = EventEngine()

    #Main engine
    main_engine = MainEngine(event_engine)

    #Add gateway engine, which should be Pegasus data by name

    #This looks like the main engine configuration

    #Start the main window and configure the event engine and the main engine
    main_window = MainWindow(main_engine, event_engine)


if __name__ == "__main__":
    #Start of entrance

The above code arrangement:

Create pyqt interface =, create event engine =, create main engine =, configure various gateway engines =, configure configuration items =, create main form =, start.

Look at the code we annotated

# flake8: noqa
from vnpy.event import EventEngine

from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import MainWindow, create_qapp

# from vnpy.gateway.binance import BinanceGateway
# from vnpy.gateway.bitmex import BitmexGateway
#... Omit part of gateway
# from vnpy.gateway.bybit import BybitGateway
# from import CtaStrategyApp
# from import CsvLoaderApp
# from import AlgoTradingApp
# from import CtaBacktesterApp
# from import DataRecorderApp
# from import RiskManagerApp
# from import ScriptTraderApp
# from import RpcServiceApp
# from import SpreadTradingApp
# from import PortfolioManagerApp

In main method

    # main_engine.add_gateway(BinanceGateway)
    # main_engine.add_gateway(GateiosGateway)
    # main_engine.add_gateway(BybitGateway)
    # main_engine.add_app(CtaStrategyApp)
    # main_engine.add_app(CtaBacktesterApp)
    # main_engine.add_app(CsvLoaderApp)
    # main_engine.add_app(AlgoTradingApp)
    # main_engine.add_app(DataRecorderApp)
    # main_engine.add_app(RiskManagerApp)
    # main_engine.add_app(ScriptTraderApp)
    # main_engine.add_app(RpcServiceApp)
    # main_engine.add_app(SpreadTradingApp)
    # main_engine.add_app(PortfolioManagerApp)

Through the commented code, we can basically see some clues. The implementation idea of vnpy should be to create a main engine, and then focus gateway, app and event engine on the main engine. Then through the form and event engine interaction for data processing. Our source code reading can be basically divided into two lines to learn:

  1. pyqt creating and generating forms
  2. Engine mechanism of vnpy

I don't know pyqt5. So next, I'll find a simple tutorial, about pyqt5, and then I'll look back at the implementation code of vnpy's form. Let's put all kinds of engines on. Then use pyqt5 to make several forms or duplicate vnpy's forms as the first stage of the current work.

Tags: Python github Java

Posted on Sun, 12 Jan 2020 23:47:32 -0500 by bigger