# gopy **Repository Path**: zhangyaoo/gopy ## Basic Information - **Project Name**: gopy - **Description**: 量化交易框架 1. 行情模块 2. 网关模块 3. 策略模块 4. 监控模块 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2022-08-29 - **Last Updated**: 2024-03-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 量化交易框架 服务器信息: Linux xxxxxxx 4.19.0-20-amd64 #1 SMP Debian 4.19.235-1 (2022-03-17) x86_64 GNU/Linux #### 数据中心 数据驱动 wscoin : 主要通过数据订阅对标平台的websocket完成数据推送, #### 策略模块 策略框架 contract_market: 主要存放市场的做市策略,并且通过命令行来指定启动的市场和对应的策略 建议:通过市场来进行部署 #### 交互网关 下单网关 framework_strategy: 主要完成API和服务端的交互,不仅仅局限于下单,同时里面自己根据已有的数据做缓存适配 建议:根据自己的业务场景考虑来做负载 #### 监控 监控预警 monitor-market: 定期同时源市场和目标市场做市场名匹配 monitor-monitor: 定期的通过获取depth的时间戳来判断数据超时并进行二次数据恢复 #### 环境构建 准备程序依赖的基础环境 1. mysql 2. redis 3. es 4. kibana 5. nginx | 根据自己的业务场景考虑来做负载 ``` # mysql redis es kibana构建 cd script ./start.sh # msyql 需要创建对应DBName : strategy # nginx 构建 cd nginx docker build nginx-apline:pub . # 构建镜像 ./nginx.sh # 部署时候注意配置文件的挂载位置: nginx.conf 需要准备一份在挂载目录里 # es-index 索引定时清除配置详见脚本内说明 ``` #### 部署 ##### 部署顺序 1. monitor-makret 2. wscoin 3. framework_strategy 4. contract_market 5. monitor-monitor 6. logstashCollect 如果添加负载的话 - 3a : framework_strategy 集群搭建 - 4a : ngxin 搭建并指定framework_strategy 集群 ##### 部署工具推荐: 1. PM2 2. Docker ##### 以PM2部署说明: 准备部署环境依赖中间件 - redis - rabbitMQ - mysql [中间件使用,根据业务来考虑是否添加] 1. 部署monitor-makret ``` pm2 start --name monitorMarket --interpreter python3 main.py ``` 2. 部署wscoin ``` pm2 start binance.json pm2 start ftx.json pm2 start xtContract.json pm2 start xtContractUser.json ``` 3. 部署framework_strategy ``` 修改settings.RPC_PORT = 8081 pm2 start --name frameworkStrategy --interpreter python3 manager.py 修改settings.RPC_PORT = 8082 pm2 start --name frameworkStrategy --interpreter python3 manager.py 修改settings.RPC_PORT = 8083 pm2 start --name frameworkStrategy --interpreter python3 manager.py ``` 4. 部署nginx ``` 请参考nginx-readme.md ``` 5. 部署contract_market ``` pm2 start --name contractMarket --interpreter python3 main.py ``` 6. 部署monitor-monitor ``` pm2 start --name monitor --interpreter python3 monitor.py ``` 7. 部署logstashCollect ``` 请参考logstashCollect-readme.md文档 关于配置的file就是目标路径,请注意相对和绝对路径问题,同时确保ES能够连接 注意部署的版本kibana, es, logstash,如果使用其他的版本请尽量保证保本一致性问题 ``` #### 注意事项 1. pm2的日志最好做好清除处理避免堆积问题 2. 关于各个模块里面的日志自带保留清除功能 3. 日志信息请到ES中查询,请管理好es相关的日志保留时间 4. logstashCollect日志采集,要根据自己需求场景来配置 5. 关于wscoin的logPath位置请确保正确否则可能会引发异常 #### 其他 ##### wscoinPPROF 关于wscoin中的程序都启动了pprof服务自行可以在*.json中看到端口来进行分析访问 ``` 使用举例: go tool pprof -http=":8082" ./cron-heapgo tool pprof http://localhost:6060/debug/pprof/profile?seconds=60.out go tool pprof http://localhost:8081/debug/pprof/profile?seconds=60 go tool pprof -http=":8088" cron-heap.out go tool pprof http://localhost:8088/debug/pprof/profile?seconds=60 go tool pprof http://localhost:8088/debug/pprof/profile go tool pprof -inuse_space -cum http://localhost:8099/debug/pprof/heap ``` ##### ntopng 关于ntopng配置 ``` sudo apt install ntopng ntopng -i eth0 --http-port 9999 即可通过 http://xxxxx:9999 访问查看网络连接和流量信息 当然使用prometheus更好 ``` ##### PM2 关于pm2 ``` pm2 -h ``` ##### pm2-gui安装 ``` # Linux npm install pm2-gui -g # You can track lifecycle by pm2-ant ``` Edit the pm2-gui/pm2-gui.ini file or copy the config example to /etc/pm2-gui.ini (starting with pm2-gui start /etc/pm2-gui.ini): ``` ; ; Home directory of pm2. ; pm2 = ~/.pm2 ; ; The monitor will fetch system CPU/Memory amount after this certain time interval. ; it could be milliseconds or various time formats(https://github.com/zeit/ms) ; refresh = 5s ; ; Port of Web server and socket agent. ; port = 12000 ; ; A value indicates whether or not run the pm2-gui as damonization. ; daemonize = false ; ; A value indicates whether or not the action buttons (i.e. `restart`, `stop all`...) should be displayed on web page. ; readonly = false ; ; The monitor will fetch process CPU/Memory amount after this certain time interval. ; it could be milliseconds or various time formats(https://github.com/zeit/ms) ; process_refresh = 3s [log] ; ; Log directory. ; dir = ./logs ; ; A value indicates whether or not display the [INFO], [ERROR].. prefixes before log message. ; prefix = true ; ; A value indicates whether or not display the local date string before log message. ; date = false ; ; Log level, one of debug, log, info, warn, error. ; level = info ; ; Socket.io origins check, e.g.: ; origins = 'example.com:* http://example.com:* http://www.example.com:8088' ; By default: ; origins = *:* [agent] ; ; This authorization will be used to authorize socket / web connections if it's set. ; authorization = admin ; ; A value indicates whether agent offline or not. ; ; offline = true [remotes] ; ; the dashboard and web server will use this section to connect remoting socket server ; server_name = [authorization@]host:port ; ; pm2@171 = AuTh@https://192.168.1.171:9002/sockserv test = localhost ; pm2@173 = 192.168.1.173:9000 ; ``` ##### supervisord 关于supervisord 部署 ``` # 安装 sudo apt install supervisor # 检查是否启动 pgrep supervisord # 如果上一步没发现进程号,那么可以执行下面的命令 supervisord -c /etc/supervisor/supervisord.conf ``` ###### 配置文件一般在如下位置: > vim /etc/supervisor/supervisord.conf 我们可以看到文件的最后有这么一句话 [include] files = /etc/supervisor/conf.d/*.conf 也就是说配置文件会引入/etc/supervisor/conf.d/目录下所有conf后缀的文件. 创建自定义的服务配置文件 所以我们可以在/etc/supervisor/conf.d/下建立文件ipfs.conf ``` ; 设置进程的名称,使用 supervisorctl 来管理进程时需要使用该进程名 [program:ipfs] directory = /usr/local/bin ; 程序的启动目录 command=/usr/local/bin/ipfs daemon ; 启动命令 最好绝对路径 autostart = true ; 在 supervisord 启动的时候也自动启动 numprocs=1 ; 默认为1 process_name=%(program_name)s ; 默认为 %(program_name)s,即 [program:x] 中的 x user=root ; 使用 root 用户来启动该进程 autorestart=true ; 程序崩溃时自动重启,重启次数是有限制的,默认为3次 redirect_stderr=true ; 重定向输出的日志 stdout_logfile = /var/log/supervisord/tornado_server.log loglevel=info ``` 保存配置文件,重启 supervisorctl reload ###### 常用的命令如下: ``` #关闭所有任务 supervisorctl shutdown # 启动某个进程 supervisorctl start programxxx # 重启某个进程 supervisorctl restart programxxx # 停止全部进程 注:start、restart、stop都不会载入最新的配置文件 supervisorctl stop all # 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。 supervisorctl reload # 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。 supervisorctl update ```