{"id":17081838,"url":"https://github.com/yutiansut/qastrategy","last_synced_at":"2025-04-05T07:01:57.020Z","repository":{"id":54459244,"uuid":"205141760","full_name":"yutiansut/QAStrategy","owner":"yutiansut","description":"策略基类/ 支持QIFI协议","archived":false,"fork":false,"pushed_at":"2021-09-01T03:59:55.000Z","size":177,"stargazers_count":213,"open_issues_count":3,"forks_count":152,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-03-29T06:03:59.807Z","etag":null,"topics":["quantaxis","strategy"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yutiansut.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-29T10:48:30.000Z","updated_at":"2025-02-17T23:31:45.000Z","dependencies_parsed_at":"2022-08-13T16:20:17.567Z","dependency_job_id":null,"html_url":"https://github.com/yutiansut/QAStrategy","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yutiansut%2FQAStrategy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yutiansut%2FQAStrategy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yutiansut%2FQAStrategy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yutiansut%2FQAStrategy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yutiansut","download_url":"https://codeload.github.com/yutiansut/QAStrategy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247299829,"owners_count":20916190,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["quantaxis","strategy"],"created_at":"2024-10-14T12:57:00.667Z","updated_at":"2025-04-05T07:01:56.980Z","avatar_url":"https://github.com/yutiansut.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# QAStrategy\n策略基类/ 基于QAAccount/QACEPEnging/QASPMS/QAREALTIMECollector/QATRADER\n\n_QAStrategy支持[```QIFI```](http://github.com/quantaxis/qifi)协议_\n\n\u003e QAStrategy 的实盘使用天勤的下单网关(Open-Trade-Gateway) 如果是天勤的用户 你可以理解为这是另一个版本的tqsdk\n\n\u003e QAStrategy 的回测也兼容QUANTAXIS的QAAccount 以及QACommunity的可视化内容\n\n\nQAStrategy 是QUANTAXIS 第一个面向交易员/策略开发者的 用户友好型项目, 致力于降低使用门槛和成本,\n```快速编写/测试/模拟你的策略```\n\n当你用QAStrategy写完一个回测 你可以无缝的把他直接改成一个实时模拟策略\n\n\n\nQAStrategy 面向场景, 主要有3个策略基类\n\n\n(PS: 股票日内回转(有底仓的情况) QAStrategy也一并支持, 默认给予10万股, 使用debug_t0()/run_backtestt0())\n\n- QAStrategyCTABase  cta模板/ 单标的模板   支持股票/期货\n\n- QAStrategyStockBase  股票池模板/ 多标的模板  支持股票/期货\n\n- QAStrategyHedgeBase  对冲模板/ 双标的模板  (目前没写完)\n\n\n策略开发者/交易员 只需要面向你自己的主要方向, 选择一个你想要的模板, 继承并开发即可快速在2分钟内完成一个简单策略\n\n\n## 为什么使用QAStrategy\n\n1. 你们根本不会用quantaxis 大部分人还停留在pandas都不会用的阶段 \n\n2. quantaxis 项目过于灵活, 并且文档缺失较多 除非二次开发人员和我自己 都不推荐直接使用quantaxis\n\n3. 你应该专注在策略开发上 而不是先学个python\n\n4. QAStrategy是 无缝兼容回测/模拟/实盘的 你可以较为快速的直接上手\n\n5. 支持QAStrategy的周边手机APP即将上线, QACommunity桌面端也是无缝兼容的\n\n\n\u003e 书生造反 十年不成  不要总是在想这个难那个不好用了 just do it  现在开始比什么都重要 !#\n\n\u003e 如果你在QAStrategy的过程中遇到了任何问题 都可以直接发issue要求群主给你解决!\n\n\n## 如何使用QAStrategy\n\n我们推荐你使用QUANTAXIS的docker环境来直接上手\n\n如果你希望手动部署 可以参考QUANTAXIS项目中的issue  部署好行情网关 数据更新以及相应其他的设置(mq/db)\n\n\n当你通过QUANTAXIS DOCKER打开了 81界面, 即可进入研究选项\n\nQAStrategy是内置在本docker环境中的, 直接调用即可\n\n\n\n\n在每个策略基类中 有一些是 大家共享的公共变量  还有一些是基类自己的变量\n\n=====================================================================================\n\n## varibles  一些变量\n\n- self.market_data 此变量为公共变量 记录策略的历史数据 [回测/实时均可用]\n- self.send_order 此函数为公共函数 但是在不同的基类中, 参数不同\n- self.running_time  当前运行时间\n- self.acc 此变量为公共变量 代表了账户\n- self.market_datetime \n- self.bar_id  在回测中使用, 及bar的id数\n- self.latest_price  一个json格式的最新价格变量  一般在实时模拟中使用\n- self.isupdate\n- self.dt  当前时间(datetime的缩写)\n\n\n[可以在user_init中重写的变量]\n- self.username  此为回测的时候的账户名\n- self.password  此为回测的时候的密码   \n\n## 一些比较重要的变量[篇幅较长 在首页我就不展开讲 可以移步链接中的教程]\n\n### [持仓Position](doc/Position.md)\n\n### [账户 Account](doc/Account.md)\n\n### [数据 MarketData](doc/MarketData.md)\n\n### [订单 Order](doc/Order.md)\n\n\n\n\n\n\n\n\n\n\n## functions  常用函数\n\n- 画图函数 self.plot(name, data, format)\n- 获取当前code self.get_code()\n- self.ind2str(ind, ind_type)\n- 获取品种所在的交易所  self.get_exchange(code)\n- 获取品种持仓  self.get_positions(code)\n- 获取当前现金 self.get_cash()\n- 获取某个品种的marketdata  self.get_code_marketdata(code)\n- 获取当前的maretdata切片 self.get_current_marketdata()\n\n\n- 订阅数据 (实时模拟用/ 回测不需要) self.subscribe_data(code, frequence, data_host, data_port, data_user, data_password, model='py')\n- 用当日tick数据进行回测(期货)  self.debug_currenttick(freq)\n- 用历史tick数据进行回测(期货) self.debug_histick(freq)\n- 使用t0模式进行回测  self.debug_t0()\n- 回测(不存储账户数据的模式)  self.debug()\n- 回测(存储账户数据的模式) self.run_backtest()\n- 实时模拟(阻塞形式 不能同时多开很多个) self.run_sim()\n- 实时模拟(非阻塞模式  可以同时开很多个)  self.debug_sim()\n\n## inherit functions  常用继承函数 (一般来说 就是你需要自定义的函数)\n\n用户初始化函数\n\n```python\ndef user_init(self):\n```\n\n每日开盘前运行的函数 默认是自带的  你可以改写\n```python\ndef on_dailyopen(self):\n    pass\n```\n\n每日收盘后运行的函数 默认是自带的  你可以改写\n```python\ndef on_dailyclose(self):\n    pass\n```\n\n在你订阅分钟级别的数据的时候,  你需要继承并改写on_bar函数\n\n```python\ndef on_bar(self, bar):\n\n    print(bar)\n```\n\n\n在你订阅tick级别的数据的时候,  你需要继承并改写on_tick函数\n\n```python\ndef on_tick(self, tick):\n    pass\n```\n\n\n强制平仓函数 默认是自带的  你可以改写\n\n```python\ndef force_close(self):\n    pass\n```\n\n\n在发单后会运行的函数 默认是自带的  你可以改写\n```python\ndef check_order(self, direction, offset, code= None):\n    pass\n```\n\n当发单失败的时候运行的函数 默认是自带的  你可以改写\n\n```python\ndef on_ordererror(self, direction, offset, price, volume):\n    pass\n```\n=====================================================================================\n\n\n一个常见的示例如下  更多的示例可以参考  /example 中的例子\n\n\n```python\n\nfrom QAStrategy import QAStrategyCTABase\nimport QUANTAXIS as QA\n\nclass CCI(QAStrategyCTABase):\n    def on_bar(self, bar):\n        \"\"\"你的大部分策略逻辑都是在此写的\n        \"\"\"\n        res = self.cci()  \n        print(res.iloc[-1])\n        if res.CCI[-1] \u003c -100:\n            print('LONG')\n            if self.positions.volume_long == 0:\n                self.send_order('BUY', 'OPEN', price=bar['close'], volume=1)\n            if self.positions.volume_short \u003e 0:\n                self.send_order('BUY', 'CLOSE', price=bar['close'], volume=1)\n\n        elif res.CCI[-1] \u003e 100:\n            print('SHORT')\n            if self.positions.volume_short == 0:\n                self.send_order('SELL', 'OPEN', price=bar['close'], volume=1)\n            if self.positions.volume_long \u003e 0:\n                self.send_order('SELL', 'CLOSE', price=bar['close'], volume=1)\n\n    def cci(self,):\n        \"\"\"你可以自定义你想要的函数\n        \"\"\"\n        return QA.QA_indicator_CCI(self.market_data, 61)\n\n\nstrategy = CCI(code='rb2005', frequence='1min',\n                strategy_id='a3916de0-bd28-4b9c-bea1-94d91f1744ac')\nstrategy.run_backtest()\n\n```\n\n更多详细信息参考教程\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyutiansut%2Fqastrategy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyutiansut%2Fqastrategy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyutiansut%2Fqastrategy/lists"}