TA-Libと組み込みのパラボリックSAR指標を比較
コード backtrader
サマリー
このサンプル戦略は、単一の価格系列に2つのパラボリックSAR指標を初期化します。高値と安値から算出するTA-LibのSARと、Backtrader組み込みのPSARです。任意で開始日と終了日を指定できるCSVフィードから過去の市場データを読み込み、Backtraderのエンジンで戦略を実行します。段階ごとに実行するオプションや、ローソク足の描画機能もあります。
このコードは、バックテストの枠組みで2つの実装を比較するための指標設定と基本的なワークフローを示します。エントリーや決済のルール、取引の実行、パフォーマンスの報告、SARシグナルの解釈方法は扱っていません。そのため、テクニカル指標とバックテストの小規模な例としては役立ちますが、どちらの指標や、それを使う戦略が利益を上げるという証拠はありません。
主なアイデア
- 同じ価格データに対して、TA-LibのSARとBacktraderのPSARを初期化します。
- 任意の日付範囲を指定して、CSVファイルから過去の価格を読み込みます。
- バックテストは一括実行することも、データを段階ごとに処理することもできます。
- 任意の描画機能でデータをローソク足として表示できますが、取引ルールや結果は示していません。
タグ
全文
# tablibsartest.py
```py
#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015-2023 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse
import datetime
import backtrader as bt
class TALibStrategy(bt.Strategy):
def __init__(self):
bt.talib.SAR(self.data.high, self.data.low)
bt.ind.PSAR()
def runstrat(args=None):
args = parse_args(args)
cerebro = bt.Cerebro()
dkwargs = dict()
if args.fromdate:
fromdate = datetime.datetime.strptime(args.fromdate, '%Y-%m-%d')
dkwargs['fromdate'] = fromdate
if args.todate:
todate = datetime.datetime.strptime(args.todate, '%Y-%m-%d')
dkwargs['todate'] = todate
data0 = bt.feeds.YahooFinanceCSVData(dataname=args.data0, **dkwargs)
cerebro.adddata(data0)
cerebro.addstrategy(TALibStrategy)
cerebro.run(runonce=not args.use_next, stdstats=False)
if args.plot:
pkwargs = dict(style='candle')
if args.plot is not True: # evals to True but is not True
npkwargs = eval('dict(' + args.plot + ')') # args were passed
pkwargs.update(npkwargs)
cerebro.plot(**pkwargs)
def parse_args(pargs=None):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Sample for sizer')
parser.add_argument('--data0', required=False,
default='../../datas/yhoo-1996-2015.txt',
help='Data to be read in')
parser.add_argument('--fromdate', required=False,
default='2005-01-01',
help='Starting date in YYYY-MM-DD format')
parser.add_argument('--todate', required=False,
default='2006-12-31',
help='Ending date in YYYY-MM-DD format')
parser.add_argument('--use-next', required=False, action='store_true',
help=('Use next (step by step) '
'instead of once (batch)'))
# Plot options
parser.add_argument('--plot', '-p', nargs='?', required=False,
metavar='kwargs', const=True,
help=('Plot the read data applying any kwargs passed\n'
'\n'
'For example (escape the quotes if needed):\n'
'\n'
' --plot style="candle" (to plot candles)\n'))
if pargs is not None:
return parser.parse_args(pargs)
return parser.parse_args()
if __name__ == '__main__':
runstrat()
```出典を明記したうえで、ライセンスに従って全文を掲載しています。 ライセンス: GPL-3.0
この要約は原文をもとにStratmillのリサーチエージェントが作成したもので、出典の複製ではありません。