From 6bc98b5ac1377a57426bc7da936af25c6bd974ad Mon Sep 17 00:00:00 2001
From: mayuanchi <mayuanchi1989@163.com>
Date: Sun, 14 Jul 2024 21:28:58 +0800
Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=E5=88=9B=E5=BB=BA?=
 =?UTF-8?q?=E5=8F=82=E6=95=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .github/workflows/release.yml | 92 +++++++++++++++++++++++++----------
 pyproject.toml                |  4 +-
 qmtbt/qmtstore.py             | 41 ++++------------
 3 files changed, 79 insertions(+), 58 deletions(-)

diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 307beca..e96e4f1 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -1,38 +1,78 @@
 name: Python package
+
 on:
   push:
-    tags:
-      - "v*.*.*"
+    branches:
+      - main
+
 jobs:
-  build:
-    runs-on: ubuntu-latest
+  Test:
+    runs-on: windows-latest
+    permissions: write-all
+
+    env:
+      POETRY_HTTP_TIMEOUT: 600
+
     steps:
-      - uses: actions/checkout@v2
+      - name: Checkout repository
+        uses: actions/checkout@v4
+        with:
+          fetch-depth: 0  # Ensure full history for semantic-release
 
-      - name: Set up Python
-        uses: actions/setup-python@v2
+      - name: Set up Python 3.9
+        uses: actions/setup-python@v5
+        with:
+          python-version: '3.9'  # You can specify your required Python version here
 
-      - name: Install dependencies
+      - name: Install Poetry and Build
         run: |
-          python -m pip install --upgrade pip
-          pip install pyyaml
-          pip install auto-changelog
+          pip install poetry setuptools cython
+          python setup.py build_ext --inplace
+        shell: bash
 
-      - name: Build and publish to PyPI
-        uses: JRubics/poetry-publish@v1.13
+      - name: Set up Python 3.10
+        uses: actions/setup-python@v5
         with:
-          ignore_dev_requirements: "yes"
-          pypi_token: ${{ secrets.PYPI_TOKEN }}
+          python-version: '3.10'  # You can specify your required Python version here
 
-      - name: Changelog
-        uses: ardalanamini/auto-changelog@master
-        id  : changelog
+      - name: Check release status
+        id: release-status
+        run: |
+          pip install python-semantic-release
+          if semantic-release --noop --strict version; then
+            echo "::set-output name=released::true"
+          else
+            echo "::set-output name=released::false"
+          fi
+        shell: bash
+        env:
+          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
 
-      - name: Create Release
-        uses: softprops/action-gh-release@v1
-        env :
-          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-        with:
-          body      : |
-            ${{ steps.changelog.outputs.changelog }}
-          prerelease: ${{ steps.changelog.outputs.prerelease }}
\ No newline at end of file
+      - name: Semantic Release Version
+        if: steps.release-status.outputs.released == 'true'
+        run: |
+          semantic-release version
+        shell: bash
+        env:
+          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+          PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
+
+      - name: Release to PyPI
+        if: steps.release-status.outputs.released == 'true'
+        run: |
+          poetry config pypi-token.pypi $PYPI_TOKEN
+          poetry publish --build
+        shell: bash
+        env:
+          PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
+
+      - name: Release to GitHub
+        if: steps.release-status.outputs.released == 'true'
+        run: |
+          git fetch --tags
+          for file in ./dist/*; do
+            gh release upload "${{ steps.release-status.outputs.tag }}" $file
+          done
+        shell: bash
+        env:
+          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
\ No newline at end of file
diff --git a/pyproject.toml b/pyproject.toml
index 4b6d899..919b816 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -6,10 +6,12 @@ authors = ["pluto <mayuanchi1029@gmail.com>"]
 readme = "README.md"
 
 [tool.poetry.dependencies]
-python = "^3.8"
+python = ">=3.9, <3.13"
 backtrader = "^1.9.78.123"
 pandas = "^2.0.2"
 schedule = "^1.2.0"
+xtquant = "^240613.1.1"
+matplotlib = "^3.9.0"
 
 
 [tool.poetry.group.dev.dependencies]
diff --git a/qmtbt/qmtstore.py b/qmtbt/qmtstore.py
index b5e8837..bb565fd 100644
--- a/qmtbt/qmtstore.py
+++ b/qmtbt/qmtstore.py
@@ -1,6 +1,5 @@
 import random
-import sys
-import importlib.util
+from xtquant import xtdata, xttrader, xttype
 
 import backtrader as bt
 from backtrader.metabase import MetaParams
@@ -39,42 +38,22 @@ def getbroker(self, *args, **kwargs):
         '''Returns broker with *args, **kwargs from registered ``BrokerCls``'''
         return self.__class__.BrokerCls(*args, **kwargs)
     
-    def __init__(self, mini_qmt_path, xtquant_path, account, token=None):
+    def __init__(self):
 
-        self.mini_qmt_path = mini_qmt_path
-        self.xtquant_path = xtquant_path
-        self.account = account
+        self.mini_qmt_path = ''
         self.code_list = []
         self.last_tick = None
-        self.token = token
-
-        # 创建模块spec
-        spec = importlib.util.spec_from_file_location('xtquant', f'{self.xtquant_path}\__init__.py')
-
-        # 创建模块实例
-        module = importlib.util.module_from_spec(spec)
-
-        # 加载模块到当前Python环境
-        spec.loader.exec_module(module)
-
-        sys.modules['xtquant'] = module
-
-        from xtquant import xtdata, xttrader, xttype
-        
-        self.xtdata = xtdata
-        self.xttrader = xttrader
-        self.xttype = xttype
-        self.xt_trader = None
+        self.token = None
     
-    def connect(self):
+    def connect(self, mini_qmt_path, account):
 
         try:
-            self.xtdata.connect()
+            xtdata.connect()
         except:
             return -1
 
         session_id = int(random.randint(100000, 999999))
-        xt_trader = self.xttrader.XtQuantTrader(self.mini_qmt_path, session_id)
+        xt_trader = xttrader.XtQuantTrader(mini_qmt_path, session_id)
 
         xt_trader.start()
 
@@ -83,7 +62,7 @@ def connect(self):
         if connect_result == 0:
             print('连接成功')
 
-            self.stock_account = self.xttype.StockAccount(self.account)
+            self.stock_account = xttype.StockAccount(account)
 
             xt_trader.subscribe(self.stock_account)
 
@@ -122,8 +101,8 @@ def _fetch_history(self, symbol, period, start_time='', end_time='', count=-1, d
 
         """
         if download:
-            self.xtdata.download_history_data(stock_code=symbol, period=period, start_time=start_time, end_time=end_time)
-        res = self.xtdata.get_local_data(stock_list=[symbol], period=period, start_time=start_time, end_time=end_time, count=count, dividend_type=dividend_type)
+            xtdata.download_history_data(stock_code=symbol, period=period, start_time=start_time, end_time=end_time)
+        res = xtdata.get_local_data(stock_list=[symbol], period=period, start_time=start_time, end_time=end_time, count=count, dividend_type=dividend_type)
         res = res[symbol]
         if period == 'tick':
             res = self._auto_expand_array_columns(res)