交易策略回测
项目描述
早前 1.6.1 (β)
✨ 使用 Python 进行交易策略回测的框架✨
当前版本不支持追踪止损、止损限价和 OCO 订单。
预计在 1.xx 版本中。
特征
- 市价单/限价单管理
- 使用 CSV 或 Binance API 作为数据源
- 最多可以在 16 个时间范围内表示相同的数据
(很少有短蜡烛被压缩为更长的蜡烛) - 简要交易历史统计(赢率、平均利润等)
- 将交易导出到 csv
这就是它的样子 - MACD 策略
from backintime import TradingStrategy, Timeframes
from backintime.oscillators.macd import macd
'''
Extend TradingStrategy class and implement __call__ method
to have your own strategy
'''
class MacdStrategy(TradingStrategy):
# declare required oscillators here for later use
using_oscillators = ( macd(Timeframes.H4), )
def __call__(self):
# runs each time a new candle closes
macd = self.oscillators.get('MACD_H4')
if not self.position and macd.crossover_up():
self._buy() # buy at market
elif self.position and macd.crossover_down():
self._sell() # sell at market
回溯测试如下(使用币安 API 数据):
# add the following import to the ones above
from backintime import BinanceApiCandles
feed = BinanceApiCandles('BTCUSDT', Timeframes.H4)
backtester = Backtester(MacdStrategy, feed)
backtester.run_test(since='2020-01-01', start_money=10000)
# the result is available as a printable instance
res = backtester.results()
print(res)
# and also can be saved to a csv file
res.to_csv('filename.csv', sep=';', summary=True)
或者,您可以使用本地计算机上的 csv 文件作为源
from backintime import TimeframeDump, TimeframeDumpScheme
# specify column indexes in input csv
columns = TimeframeDumpScheme(
open_time=0, close_time=6,
open=1, high=3, low=4,
close=2, volume=5)
feed = TimeframeDump('h4.csv', Timeframes.H4, columns)
backtester = Backtester(MacdStrategy, feed)
backtester.run_test('2020-01-01', 10000)
print(backtester.results())
安装
pip install backintime
执照
麻省理工学院
作者
阿基姆·穆赫塔罗夫@akim_int80h
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
backintime-1.6.2.tar.gz
(20.4 kB
查看哈希)
内置分布
backintime-1.6.2-py3-none-any.whl
(32.4 kB
查看哈希)
关
backintime -1.6.2.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | dbccc9a98839419063fe9357ecb0446fe862ce6a70734cc419f23166b2af4397 |
|
| MD5 | a4442864e4cc43fd8036259c7685bbb8 |
|
| 布莱克2-256 | aa872d30444b7224ab0ced448045328105d7975c8a302c008cbbc18ddaa3e3ad |
关
backintime -1.6.2-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | ce003552b6f4a784a9aeb0409c8736de8ad024ed67abffcd1c719a7e1dba5d56 |
|
| MD5 | d2e9fdc096d23ec96af51a9e6cd5bbf9 |
|
| 布莱克2-256 | 662c4ca0d98aff9e4ecff0c298e7a55f6dc4d62c8445be5691d75d4e862ff4d4 |