vnpy source reading and learning: preparation

vnpy source reading and learning

target

  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

Preparation

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 vn.py (I. initial test of environment configuration and backtesting)

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

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

Source interpretation of vn.py (IV. main engine code analysis - initialization function)

vn.py source code interpretation (v. main engine code analysis - CTP module)

vn.py source code interpretation (VI. main engine code analysis - policy module)

Source interpretation of vn.py (VII. Back testing code analysis)

Source code interpretation of vn.py (VIII. Code analysis of back test results calculation)

vn.py source code interpretation (IX. policy code analysis)

Source code interpretation of vn.py (X. parameter optimization)

Among them, the vn.py 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


+examples


+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


+examples


+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/run.py. 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 run.py, 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 vnpy.app.option_master 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
    main_engine.add_gateway(FemasGateway)
    
    main_engine.add_gateway(DeribitGateway)

    #This looks like the main engine configuration
    main_engine.add_app(OptionMasterApp)

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

    qapp.exec()


if __name__ == "__main__":
    #Start of entrance
    main()

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 vnpy.app.cta_strategy import CtaStrategyApp
# from vnpy.app.csv_loader import CsvLoaderApp
# from vnpy.app.algo_trading import AlgoTradingApp
# from vnpy.app.cta_backtester import CtaBacktesterApp
# from vnpy.app.data_recorder import DataRecorderApp
# from vnpy.app.risk_manager import RiskManagerApp
# from vnpy.app.script_trader import ScriptTraderApp
# from vnpy.app.rpc_service import RpcServiceApp
# from vnpy.app.spread_trading import SpreadTradingApp
# from vnpy.app.portfolio_manager 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