[VeightNa]V3.0 configure MYSQL database

eat melon

Today, I saw on Zhihu that there were some festivals between the bloggers of the two websites vnpy.cn and vnpy.com. I moved my little bench and eat melons!
Link

Link

I don't care what the connection between them is, as long as I can learn knowledge is enough.

It is reasonable to see that there is an article written by Quantitative Lin, and I suggest readers to read it—— It is not recommended to use a database, but to read and write directly through csv files

So why do I choose to configure MYSQL? The reason is that the entire framework of VeightNa's data collection, data storage, strategy, and backtesting is relatively complete, and it is more convenient to use if it is connected to the currency exchange. For entry-level quantification and medium-to-long-term strategies, speed is not for me. The goal pursued now, so temporarily use VeightNa for quantification.

Finally, let’s spit it out. I installed an open source project on github two days ago: Freqtrade. I spent two days learning docker to configure the windows and centos environment.

Blindness is over, the text begins~~

1. Install VeightNa

This article is installed in a windows environment
Ancona+python3.9

The official website gives the installation documentation, just follow the above configuration
https://www.vnpy.com/docs/cn/windows_install.html

The following file can be successfully run to indicate that the configuration is successful. It is worth noting that VeightNa has removed many packages in version 3.0 and needs to be downloaded manually, such as: vnpy_cta,vnpy_ctastrategy...vnpy.okex

run.py file

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

from vnpy_ctp import CtpGateway
from vnpy_ctastrategy import CtaStrategyApp
from vnpy_ctabacktester import CtaBacktesterApp
from vnpy_datamanager import DataManagerApp
from vnpy_okex import OkexGateway

def main():
    """Start VeighNa Trader"""
    qapp = create_qapp()

    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)
    main_engine.add_gateway(OkexGateway)
    main_engine.add_gateway(CtpGateway)
    main_engine.add_app(CtaStrategyApp)
    main_engine.add_app(CtaBacktesterApp)
    main_engine.add_app(DataManagerApp)

    main_window = MainWindow(main_engine, event_engine)
    main_window.showMaximized()

    qapp.exec()


if __name__ == "__main__":
    main()

We need to download the data of Bitcoin to the database. Since digital currency is prohibited from trading in China, vnpy_okex was removed in version 3.0, but we still use pip to download it as open source.

pip install vnpy_okex

After the download is complete, we need to manually modify the API address of okx, which was okx.com before, and now is okx.com

We also need to modify vnpy/trader/constant.py
Add OKEX='OKX'
The reason is still because: vnpy.com sued vnpy.cn on the grounds that the latter violated national regulations - trading digital currency, so, VeightNa does not support digital currency trading, and we added it here. . (Cough, I don't recommend everyone to speculate on coins, and teaching is only for the purpose of learning.)

After all preparations are done, connect to the exchange

There is also a small pit here. Manually setting the proxy will report an error.


I found relevant information on the Internet, wrote a test code, and regretted reporting the clientConnectorError when I joined the proxy.

import aiohttp
import asyncio

async def main():
    aiohttp.TCPConnector(verify_ssl=False)
    async with aiohttp.ClientSession() as session:
        async with session.get('https://python.org', proxy='http://127.0.0.1:12307') as response:
            print("Status:", response.status)
            print("Content-type:", response.headers['content-type'])

            html = await response.text()
            print("Body:", html[:15], "...")


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Solution:

import aiohttp
import asyncio

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())  # Add this line to solve the problem that the proxy request https will report an error

async def main():
    aiohttp.TCPConnector(verify_ssl=False)
    async with aiohttp.ClientSession() as session:
        async with session.get('https://python.org', proxy='http://127.0.0.1:12307') as response:
            print("Status:", response.status)
            print("Content-type:", response.headers['content-type'])

            html = await response.text()
            print("Body:", html[:15], "...")


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

It is found that no more errors are reported. python asynchronous programming has not yet been learned, so leave a hole here and fill it in later.

Corresponding to the project directory, find aiohttp/connector.py in site-packages, and add this line of code asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

problem solved.

2. Install MYSQL

The official gave the corresponding installation documentation https://www.vnpy.com/docs/cn/database.html

But the official installation of the mysql graphical interface, we don't need so much trouble, Please configure through this blog post , skip the installation step here.

Create vnpy database

 create database vnpy;

After this step, follow the official documentation to configure VeightNa to connect to the database

You also need to download the vnpy_mysql package before starting run.py

pip install vnpy_mysql -i https://pypi.douban.com/simple

start run.py
System - connect, select data management - download data

You can find that there is one more vnpy in the folder

Data can also be obtained through mysql query

You're done! There are still a lot of pits... python asynchronous programming is very interesting, we will take a special section to introduce it later! There are also mysql, mongodb and nosql, mainstream databases - relational, non-relational, and time series. We also take a section to introduce them. Looking forward to your subscription, please give me a like, your support is my motivation for updating!

Tags: Database MySQL

Posted by jimmayhugh on Sat, 04 Jun 2022 00:37:41 +0530