Name | mocktime JSON |
Version |
0.3.1
JSON |
| download |
home_page | |
Summary | patch to mock time and datetime |
upload_time | 2023-11-27 05:28:54 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.7 |
license | BSD-3-Clause |
keywords |
mock
datetime
apscheduler
schedule
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# mocktime
通过对`time.time()`和`datetime.now()`的修改,实现模拟时间功能。可用于历史事件回测,或未来事件调试。
## 项目起源
1. 本项目来自于使用定时任务调度框架实现程序化交易回测的一个设想
2. 策略一般由行情事件驱动,但在开盘前和收盘后无行情,只能靠定时任务来驱动。如:`before_trading`,`after_trading`
3. K线行情事件本质上可认为也是固定频率的定时任务。如:`on_bar`
4. 定时任务框架一般只能用于实盘,因为它的时间机制是**基于操作系统**,而回测的时间是**基于历史数据**
5. 框架中大量的使用到`datetime.now()`, 它无法根据历史进行修改,而本项目就是为了打破这个限制而做的尝试
6. 最先支持了`apscheduler`,之后支持了日志库`logging`和`loguru`
7. 编写了`schedule`的示例后,想到可能还有其它调度库需要模拟时间
8. 后来还想到模拟时间功能可以快速的检测任务规则是否编写正确,很有必要独立成一个项目
## 原理
1. `time.time()`打补丁,使其可以修改成任意时间,这样其它模块就可以取到历史时间
2. `datetime.now()`中使用了`time.time()`,替换它成历史时间
3. 每个任务执行完后更新`time.time()`为下次任务的触发时间
## 安装
```bash
pip install mocktime -U
```
# 示例
```python
import time
from datetime import datetime
import mocktime
if __name__ == '__main__':
mocktime.configure(mock=True, tick=False)
print('真实时间:', mocktime.now(), mocktime.time())
print('默认模拟时间,带时区:', datetime.now(), time.time())
mocktime.configure(mock=False, tick=False)
print('还原成真实时间:', datetime.now(), time.time())
mocktime.configure(mock=True, tick=False)
mocktime.time_update(datetime(2023, 1, 2, 3, 4, 5))
print('更新模拟时间:', datetime.now())
mocktime.time_add(5.1)
print('模拟时间+5.1s:', datetime.now())
mocktime.configure(mock=True, tick=True)
time.sleep(5)
print('开启时间流逝,等5s,时间更新:', datetime.now())
mocktime.configure(mock=True, tick=False)
time.sleep(5)
print('关闭时间流逝,等5s,时间不变:', datetime.now())
mocktime.configure(mock=True, tick=True)
print('开启时间流逝后再更新时间:', datetime.now())
mocktime.time_update(datetime(2023, 1, 2, 3, 4, 5))
time.sleep(5)
print('从新设置的时间开始流逝:', datetime.now())
```
输出如下
```text
真实时间: 2023-11-24 15:37:16.168329 1700811436.1683288
默认模拟时间,带时区: 1970-01-01 08:00:00 0.0
还原成真实时间: 2023-11-24 15:37:16.168329 1700811436.1683288
更新模拟时间: 2023-01-02 03:04:05
模拟时间+5.1s: 2023-01-02 03:04:10.100000
开启时间流逝,等5s,时间更新: 2023-01-02 03:04:15.106348
关闭时间流逝,等5s,时间不变: 2023-01-02 03:04:15.106348
开启时间流逝后再更新时间: 2023-01-02 03:04:15.106348
从新设置的时间开始流逝: 2023-01-02 03:04:10.010793
```
## 调度任务示例
### apscheduler `examples/apscheduler/demo.py`
1. 导入`mocktime`后,设置好过去的开始与结束时间,但任务并没有继续执行。因为模拟时间是固定不变的
2. 需继承`BlockingScheduler`,在每批次的任务执行完后立即更新时间`mocktime.time_add`
### schedule `examples/schedule/demo.py`
1. 同样遇到需更新模拟时间问题
2. 在循环中取`get_next_run`,然后更新时间`mocktime.time_update`
### dagster `examples/dagster/usercustomize.py`
1. 难点在于`mocktime`需要跨进程,通过`mocktime.multiprocess`实现
2. 由于跨进程通讯复杂,所以只实现了`tick=True`的功能
## 模拟时间三种运行模式
1. 真实时间模式。当前真实时间开始、正常速度执行。可以不需`mocktime`参与
> mocktime.configure(mock=False, tick=False)
2. 加速模拟模式。任意时间开始(历史和未来都可以),加速执行。需`mocktime`修改时间,还需在任务循环中更新`mocktime.time_update`
。一般用于历史回测
> mocktime.configure(mock=True, tick=False)
3. 时间流逝模式。任意时间开始(历史和未来都可以),正常速度执行。仅需`mocktime`修改时间。一般用于任务触发测试,可免于修改操作系统时间
> mocktime.configure(mock=True, tick=True)
4. 不存在的模式。时间流逝,但又不是模拟模式,不就是真实时间吗?!
> mocktime.configure(mock=False, tick=True) # 当tick=True时,强行mock=True
5. 跨进程模式。只是时间流逝模式`configure(mock=True, tick=True)`的一种应用场景
> mocktime.multiprocess(r'd:\a.pkl', datetime(2022, 1, 1).timestamp())
## 参考资料
1.《深度剖析CPython解释器》34.
侵入Python虚拟机,动态修改底层数据结构和运行时 - https://www.cnblogs.com/traditional/p/15489296.html
Raw data
{
"_id": null,
"home_page": "",
"name": "mocktime",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "mock,datetime,apscheduler,schedule",
"author": "",
"author_email": "wukan <wu-kan@163.com>",
"download_url": "",
"platform": null,
"description": "# mocktime\r\n\r\n\u901a\u8fc7\u5bf9`time.time()`\u548c`datetime.now()`\u7684\u4fee\u6539\uff0c\u5b9e\u73b0\u6a21\u62df\u65f6\u95f4\u529f\u80fd\u3002\u53ef\u7528\u4e8e\u5386\u53f2\u4e8b\u4ef6\u56de\u6d4b\uff0c\u6216\u672a\u6765\u4e8b\u4ef6\u8c03\u8bd5\u3002\r\n\r\n## \u9879\u76ee\u8d77\u6e90\r\n\r\n1. \u672c\u9879\u76ee\u6765\u81ea\u4e8e\u4f7f\u7528\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u6846\u67b6\u5b9e\u73b0\u7a0b\u5e8f\u5316\u4ea4\u6613\u56de\u6d4b\u7684\u4e00\u4e2a\u8bbe\u60f3\r\n2. \u7b56\u7565\u4e00\u822c\u7531\u884c\u60c5\u4e8b\u4ef6\u9a71\u52a8\uff0c\u4f46\u5728\u5f00\u76d8\u524d\u548c\u6536\u76d8\u540e\u65e0\u884c\u60c5\uff0c\u53ea\u80fd\u9760\u5b9a\u65f6\u4efb\u52a1\u6765\u9a71\u52a8\u3002\u5982\uff1a`before_trading`,`after_trading`\r\n3. K\u7ebf\u884c\u60c5\u4e8b\u4ef6\u672c\u8d28\u4e0a\u53ef\u8ba4\u4e3a\u4e5f\u662f\u56fa\u5b9a\u9891\u7387\u7684\u5b9a\u65f6\u4efb\u52a1\u3002\u5982\uff1a`on_bar`\r\n4. \u5b9a\u65f6\u4efb\u52a1\u6846\u67b6\u4e00\u822c\u53ea\u80fd\u7528\u4e8e\u5b9e\u76d8\uff0c\u56e0\u4e3a\u5b83\u7684\u65f6\u95f4\u673a\u5236\u662f**\u57fa\u4e8e\u64cd\u4f5c\u7cfb\u7edf**\uff0c\u800c\u56de\u6d4b\u7684\u65f6\u95f4\u662f**\u57fa\u4e8e\u5386\u53f2\u6570\u636e**\r\n5. \u6846\u67b6\u4e2d\u5927\u91cf\u7684\u4f7f\u7528\u5230`datetime.now()`, \u5b83\u65e0\u6cd5\u6839\u636e\u5386\u53f2\u8fdb\u884c\u4fee\u6539\uff0c\u800c\u672c\u9879\u76ee\u5c31\u662f\u4e3a\u4e86\u6253\u7834\u8fd9\u4e2a\u9650\u5236\u800c\u505a\u7684\u5c1d\u8bd5\r\n6. \u6700\u5148\u652f\u6301\u4e86`apscheduler`\uff0c\u4e4b\u540e\u652f\u6301\u4e86\u65e5\u5fd7\u5e93`logging`\u548c`loguru`\r\n7. \u7f16\u5199\u4e86`schedule`\u7684\u793a\u4f8b\u540e\uff0c\u60f3\u5230\u53ef\u80fd\u8fd8\u6709\u5176\u5b83\u8c03\u5ea6\u5e93\u9700\u8981\u6a21\u62df\u65f6\u95f4\r\n8. \u540e\u6765\u8fd8\u60f3\u5230\u6a21\u62df\u65f6\u95f4\u529f\u80fd\u53ef\u4ee5\u5feb\u901f\u7684\u68c0\u6d4b\u4efb\u52a1\u89c4\u5219\u662f\u5426\u7f16\u5199\u6b63\u786e\uff0c\u5f88\u6709\u5fc5\u8981\u72ec\u7acb\u6210\u4e00\u4e2a\u9879\u76ee\r\n\r\n## \u539f\u7406\r\n\r\n1. `time.time()`\u6253\u8865\u4e01\uff0c\u4f7f\u5176\u53ef\u4ee5\u4fee\u6539\u6210\u4efb\u610f\u65f6\u95f4\uff0c\u8fd9\u6837\u5176\u5b83\u6a21\u5757\u5c31\u53ef\u4ee5\u53d6\u5230\u5386\u53f2\u65f6\u95f4\r\n2. `datetime.now()`\u4e2d\u4f7f\u7528\u4e86`time.time()`\uff0c\u66ff\u6362\u5b83\u6210\u5386\u53f2\u65f6\u95f4\r\n3. \u6bcf\u4e2a\u4efb\u52a1\u6267\u884c\u5b8c\u540e\u66f4\u65b0`time.time()`\u4e3a\u4e0b\u6b21\u4efb\u52a1\u7684\u89e6\u53d1\u65f6\u95f4\r\n\r\n## \u5b89\u88c5\r\n\r\n```bash\r\npip install mocktime -U\r\n```\r\n\r\n# \u793a\u4f8b\r\n\r\n```python\r\nimport time\r\nfrom datetime import datetime\r\n\r\nimport mocktime\r\n\r\nif __name__ == '__main__':\r\n mocktime.configure(mock=True, tick=False)\r\n print('\u771f\u5b9e\u65f6\u95f4:', mocktime.now(), mocktime.time())\r\n print('\u9ed8\u8ba4\u6a21\u62df\u65f6\u95f4\uff0c\u5e26\u65f6\u533a:', datetime.now(), time.time())\r\n\r\n mocktime.configure(mock=False, tick=False)\r\n print('\u8fd8\u539f\u6210\u771f\u5b9e\u65f6\u95f4:', datetime.now(), time.time())\r\n\r\n mocktime.configure(mock=True, tick=False)\r\n mocktime.time_update(datetime(2023, 1, 2, 3, 4, 5))\r\n print('\u66f4\u65b0\u6a21\u62df\u65f6\u95f4:', datetime.now())\r\n\r\n mocktime.time_add(5.1)\r\n print('\u6a21\u62df\u65f6\u95f4+5.1s:', datetime.now())\r\n\r\n mocktime.configure(mock=True, tick=True)\r\n time.sleep(5)\r\n print('\u5f00\u542f\u65f6\u95f4\u6d41\u901d\uff0c\u7b495s\uff0c\u65f6\u95f4\u66f4\u65b0:', datetime.now())\r\n mocktime.configure(mock=True, tick=False)\r\n time.sleep(5)\r\n print('\u5173\u95ed\u65f6\u95f4\u6d41\u901d\uff0c\u7b495s\uff0c\u65f6\u95f4\u4e0d\u53d8:', datetime.now())\r\n\r\n mocktime.configure(mock=True, tick=True)\r\n print('\u5f00\u542f\u65f6\u95f4\u6d41\u901d\u540e\u518d\u66f4\u65b0\u65f6\u95f4:', datetime.now())\r\n mocktime.time_update(datetime(2023, 1, 2, 3, 4, 5))\r\n time.sleep(5)\r\n print('\u4ece\u65b0\u8bbe\u7f6e\u7684\u65f6\u95f4\u5f00\u59cb\u6d41\u901d:', datetime.now())\r\n\r\n\r\n```\r\n\r\n\u8f93\u51fa\u5982\u4e0b\r\n\r\n```text\r\n\u771f\u5b9e\u65f6\u95f4: 2023-11-24 15:37:16.168329 1700811436.1683288\r\n\u9ed8\u8ba4\u6a21\u62df\u65f6\u95f4\uff0c\u5e26\u65f6\u533a: 1970-01-01 08:00:00 0.0\r\n\u8fd8\u539f\u6210\u771f\u5b9e\u65f6\u95f4: 2023-11-24 15:37:16.168329 1700811436.1683288\r\n\u66f4\u65b0\u6a21\u62df\u65f6\u95f4: 2023-01-02 03:04:05\r\n\u6a21\u62df\u65f6\u95f4+5.1s: 2023-01-02 03:04:10.100000\r\n\u5f00\u542f\u65f6\u95f4\u6d41\u901d\uff0c\u7b495s\uff0c\u65f6\u95f4\u66f4\u65b0: 2023-01-02 03:04:15.106348\r\n\u5173\u95ed\u65f6\u95f4\u6d41\u901d\uff0c\u7b495s\uff0c\u65f6\u95f4\u4e0d\u53d8: 2023-01-02 03:04:15.106348\r\n\u5f00\u542f\u65f6\u95f4\u6d41\u901d\u540e\u518d\u66f4\u65b0\u65f6\u95f4: 2023-01-02 03:04:15.106348\r\n\u4ece\u65b0\u8bbe\u7f6e\u7684\u65f6\u95f4\u5f00\u59cb\u6d41\u901d: 2023-01-02 03:04:10.010793\r\n```\r\n\r\n## \u8c03\u5ea6\u4efb\u52a1\u793a\u4f8b\r\n\r\n### apscheduler `examples/apscheduler/demo.py`\r\n\r\n1. \u5bfc\u5165`mocktime`\u540e\uff0c\u8bbe\u7f6e\u597d\u8fc7\u53bb\u7684\u5f00\u59cb\u4e0e\u7ed3\u675f\u65f6\u95f4\uff0c\u4f46\u4efb\u52a1\u5e76\u6ca1\u6709\u7ee7\u7eed\u6267\u884c\u3002\u56e0\u4e3a\u6a21\u62df\u65f6\u95f4\u662f\u56fa\u5b9a\u4e0d\u53d8\u7684\r\n2. \u9700\u7ee7\u627f`BlockingScheduler`\uff0c\u5728\u6bcf\u6279\u6b21\u7684\u4efb\u52a1\u6267\u884c\u5b8c\u540e\u7acb\u5373\u66f4\u65b0\u65f6\u95f4`mocktime.time_add`\r\n\r\n### schedule `examples/schedule/demo.py`\r\n\r\n1. \u540c\u6837\u9047\u5230\u9700\u66f4\u65b0\u6a21\u62df\u65f6\u95f4\u95ee\u9898\r\n2. \u5728\u5faa\u73af\u4e2d\u53d6`get_next_run`\uff0c\u7136\u540e\u66f4\u65b0\u65f6\u95f4`mocktime.time_update`\r\n\r\n### dagster `examples/dagster/usercustomize.py`\r\n\r\n1. \u96be\u70b9\u5728\u4e8e`mocktime`\u9700\u8981\u8de8\u8fdb\u7a0b,\u901a\u8fc7`mocktime.multiprocess`\u5b9e\u73b0\r\n2. \u7531\u4e8e\u8de8\u8fdb\u7a0b\u901a\u8baf\u590d\u6742\uff0c\u6240\u4ee5\u53ea\u5b9e\u73b0\u4e86`tick=True`\u7684\u529f\u80fd\r\n\r\n## \u6a21\u62df\u65f6\u95f4\u4e09\u79cd\u8fd0\u884c\u6a21\u5f0f\r\n\r\n1. \u771f\u5b9e\u65f6\u95f4\u6a21\u5f0f\u3002\u5f53\u524d\u771f\u5b9e\u65f6\u95f4\u5f00\u59cb\u3001\u6b63\u5e38\u901f\u5ea6\u6267\u884c\u3002\u53ef\u4ee5\u4e0d\u9700`mocktime`\u53c2\u4e0e\r\n > mocktime.configure(mock=False, tick=False)\r\n2. \u52a0\u901f\u6a21\u62df\u6a21\u5f0f\u3002\u4efb\u610f\u65f6\u95f4\u5f00\u59cb(\u5386\u53f2\u548c\u672a\u6765\u90fd\u53ef\u4ee5)\uff0c\u52a0\u901f\u6267\u884c\u3002\u9700`mocktime`\u4fee\u6539\u65f6\u95f4\uff0c\u8fd8\u9700\u5728\u4efb\u52a1\u5faa\u73af\u4e2d\u66f4\u65b0`mocktime.time_update`\r\n \u3002\u4e00\u822c\u7528\u4e8e\u5386\u53f2\u56de\u6d4b\r\n > mocktime.configure(mock=True, tick=False)\r\n3. \u65f6\u95f4\u6d41\u901d\u6a21\u5f0f\u3002\u4efb\u610f\u65f6\u95f4\u5f00\u59cb(\u5386\u53f2\u548c\u672a\u6765\u90fd\u53ef\u4ee5)\uff0c\u6b63\u5e38\u901f\u5ea6\u6267\u884c\u3002\u4ec5\u9700`mocktime`\u4fee\u6539\u65f6\u95f4\u3002\u4e00\u822c\u7528\u4e8e\u4efb\u52a1\u89e6\u53d1\u6d4b\u8bd5\uff0c\u53ef\u514d\u4e8e\u4fee\u6539\u64cd\u4f5c\u7cfb\u7edf\u65f6\u95f4\r\n > mocktime.configure(mock=True, tick=True)\r\n4. \u4e0d\u5b58\u5728\u7684\u6a21\u5f0f\u3002\u65f6\u95f4\u6d41\u901d\uff0c\u4f46\u53c8\u4e0d\u662f\u6a21\u62df\u6a21\u5f0f\uff0c\u4e0d\u5c31\u662f\u771f\u5b9e\u65f6\u95f4\u5417\uff1f\uff01\r\n > mocktime.configure(mock=False, tick=True) # \u5f53tick=True\u65f6\uff0c\u5f3a\u884cmock=True\r\n5. \u8de8\u8fdb\u7a0b\u6a21\u5f0f\u3002\u53ea\u662f\u65f6\u95f4\u6d41\u901d\u6a21\u5f0f`configure(mock=True, tick=True)`\u7684\u4e00\u79cd\u5e94\u7528\u573a\u666f\r\n > mocktime.multiprocess(r'd:\\a.pkl', datetime(2022, 1, 1).timestamp())\r\n\r\n## \u53c2\u8003\u8d44\u6599\r\n\r\n1.\u300a\u6df1\u5ea6\u5256\u6790CPython\u89e3\u91ca\u5668\u300b34.\r\n\u4fb5\u5165Python\u865a\u62df\u673a\uff0c\u52a8\u6001\u4fee\u6539\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u548c\u8fd0\u884c\u65f6 - https://www.cnblogs.com/traditional/p/15489296.html\r\n",
"bugtrack_url": null,
"license": "BSD-3-Clause",
"summary": "patch to mock time and datetime",
"version": "0.3.1",
"project_urls": null,
"split_keywords": [
"mock",
"datetime",
"apscheduler",
"schedule"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a6e6b6dba593c2d3367731abfe6ba4bf9cd04cede1419d29379570a0e9de10ca",
"md5": "23a1e6f0ec01455b33783b370e22b38a",
"sha256": "c625282d80cdf5b742db7025b7ab0696ec4f446d5937d71dfc6f6d794a21e468"
},
"downloads": -1,
"filename": "mocktime-0.3.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "23a1e6f0ec01455b33783b370e22b38a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 11907,
"upload_time": "2023-11-27T05:28:54",
"upload_time_iso_8601": "2023-11-27T05:28:54.249653Z",
"url": "https://files.pythonhosted.org/packages/a6/e6/b6dba593c2d3367731abfe6ba4bf9cd04cede1419d29379570a0e9de10ca/mocktime-0.3.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-27 05:28:54",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "mocktime"
}