Skip to main content

交易策略回测

项目描述

早前 1.6.1 (β)

✨ 使用 Python 进行交易策略回测的框架✨

当前版本不支持追踪止损、止损限价和 OCO 订单。
预计在 1.xx 版本中。

特征

  • 市价单/限价单管理
  • 使用 CSV 或 Binance API 作为数据源
  • 最多可以在 16 个时间范围内表示相同的数据
    (很少有短蜡烛被压缩为更长的蜡烛)
  • 简要交易历史统计(赢率、平均利润等)
  • 将交易导出到 csv

这就是它的样子 - MACD 策略

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 查看哈希

已上传 source

内置分布

backintime-1.6.2-py3-none-any.whl (32.4 kB 查看哈希

已上传 py3