隨著 1.1.7.88 版本的發布, backtrader有了一個新的補充:作家
這可能早就到期了,應該已經存在了,問題 #14中的討論也應該已經開始了開發。
但遲到總比沒有好。
Writer
實現嘗試與backtrader
環境中的其他對象保持一致
通過Cerebro添加
提供最合理的默認值
不要強迫用戶做太多事情
當然,更重要的是了解作者實際寫了什麼。那就是:
- 的 CSV 輸出
- `datas` added to the system (can be switched off) - `strategies` (a Strategy can have named lines) - `indicators` inside the strategies (only 1st level) - `observers` inside the strategies (only 1st level) Which `indicators` and `observers` output data to the CSV stream is controlled by the attribute: `csv` in each instance The defaults are: - Observers have `csv = True` - Indicators have `csv = False` The value can be overriden for any instance created inside a strategy
回測階段結束後, Writers
為Cerebro
實例添加一個新部分,並添加以下子部分:
系統中
datas
的屬性(名稱、壓縮、時間範圍)系統中
strategies
的屬性(行、參數)策略中
indicators
的屬性(行、參數)策略中
observers
的屬性(行,參數)具有以下屬性的分析器
參數
分析
考慮到所有這些,一個例子可能是展示writers
的力量(或弱點)的最簡單方法。
但在如何將它們添加到cerebro之前。
將
writer
參數用於cerebro
:cerebro = bt.Cerebro(writer=True)
這將創建一個默認實例。
具體補充:
cerebro = bt.Cerebro() cerebro.addwriter(bt.WriterFile, csv=False)
添加(現在唯一的writer )一個
WriterFile
類到writer列表,以便稍後用csv= False
實例化(不會在輸出中生成 csv 流。
多空策略的長期示例(完整代碼見下文),通過執行使用Close -SMA 交叉作為信號:
$ ./writer-test.py
圖表:
使用以下輸出:
=============================================================================== Cerebro: ----------------------------------------------------------------------------- - Datas: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - Data0: - Name: 2006-day-001 - Timeframe: Days - Compression: 1 ----------------------------------------------------------------------------- - Strategies: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - LongShortStrategy: ************************************************************************* - Params: - csvcross: False - printout: False - onlylong: False - stake: 1 - period: 15 ************************************************************************* - Indicators: ....................................................................... - SMA: - Lines: sma ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Params: - period: 15 ....................................................................... - CrossOver: - Lines: crossover - Params: None ************************************************************************* - Observers: ....................................................................... - Broker: - Lines: cash, value - Params: None ....................................................................... - BuySell: - Lines: buy, sell - Params: None ....................................................................... - Trades: - Lines: pnlplus, pnlminus - Params: None ************************************************************************* - Analyzers: ....................................................................... - Value: - Begin: 100000 - End: 100826.1 ....................................................................... - SQN: - Params: None ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Analysis: - sqn: 0.05 - trades: 22
運行後,我們對系統的設置方式以及分析人員最後所說的內容進行了完整的總結。在這種情況下,分析儀是
Value
是策略中的一個假分析器,它收集投資組合的開始和結束值由 Van K. Tharp 定義的
SQN
(或 SystemQualityNumber)(除了backtrader
1.1.7.88,它告訴我們它已經看到 22 筆交易併計算出 0.05 的sqn
。這實際上是相當低的。我們可以通過查看一整年後的小額利潤來弄清楚(幸運的是系統沒有虧損)
測試腳本允許我們將策略調整為long-only :
$ ./writer-test.py --onlylong --plot
圖表:
現在的輸出是:
=============================================================================== Cerebro: ----------------------------------------------------------------------------- - Datas: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - Data0: - Name: 2006-day-001 - Timeframe: Days - Compression: 1 ----------------------------------------------------------------------------- - Strategies: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - LongShortStrategy: ************************************************************************* - Params: - csvcross: False - printout: False - onlylong: True - stake: 1 - period: 15 ************************************************************************* - Indicators: ....................................................................... - SMA: - Lines: sma ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Params: - period: 15 ....................................................................... - CrossOver: - Lines: crossover - Params: None ************************************************************************* - Observers: ....................................................................... - Broker: - Lines: cash, value - Params: None ....................................................................... - BuySell: - Lines: buy, sell - Params: None ....................................................................... - Trades: - Lines: pnlplus, pnlminus - Params: None ************************************************************************* - Analyzers: ....................................................................... - Value: - Begin: 100000 - End: 102795.0 ....................................................................... - SQN: - Params: None ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Analysis: - sqn: 0.91 - trades: 11
可以看到策略“參數”的變化(onlylong 已變為True ),分析器講述了一個不同的故事:
期末價值從 100826.1 提高到 102795.0
SQN 看到的交易從 22 減少到 11
SQN 分數從 0.05 增長到 0.91,這要好得多
但是仍然看不到 CSV 輸出。讓我們運行腳本來打開它:
$ ./writer-test.py --onlylong --writercsv
更新輸出:
=============================================================================== Id,2006-day-001,len,datetime,open,high,low,close,volume,openinterest,LongShortStrategy,len,Broker,len,cash,value,Buy Sell,len,buy,sell,Trades,len,pnlplus,pnlminus 1,2006-day-001,1,2006-01-02 23:59:59+00:00,3578.73,3605.95,3578.73,3604.33,0.0,0.0,LongShortStrategy,1,Broker,1,1000 00.0,100000.0,BuySell,1,,,Trades,1,, 2,2006-day-001,2,2006-01-03 23:59:59+00:00,3604.08,3638.42,3601.84,3614.34,0.0,0.0,LongShortStrategy,2,Broker,2,1000 00.0,100000.0,BuySell,2,,,Trades,2,, ... ... ... 255,2006-day-001,255,2006-12-29 23:59:59+00:00,4130.12,4142.01,4119.94,4119.94,0.0,0.0,LongShortStrategy,255,Broker,255,100795.0,102795.0,BuySell,255,,,Trades,255,, =============================================================================== Cerebro: ----------------------------------------------------------------------------- ... ...
我們可以跳過大部分 csv 流和已經看到的摘要。 CSV 流已打印出以下內容
開頭的剖麵線分隔符
標題行
對應數據
請注意每個對像如何打印其“長度”。儘管在這種情況下它沒有提供太多信息,但如果使用多時間幀數據或重放數據,它會提供。
writer
默認執行以下操作:
沒有打印指標(簡單移動平均線和交叉點都沒有)
觀察者被打印出來
讓我們使用附加參數運行腳本,以將 CrossOver 指示器添加到 CSV 流中:
$ ./writer-test.py --onlylong --writercsv --csvcross
輸出:
=============================================================================== Id,2006-day-001,len,datetime,open,high,low,close,volume,openinterest,LongShortStrategy,len,CrossOver,len,crossover,B roker,len,cash,value,BuySell,len,buy,sell,Trades,len,pnlplus,pnlminus 1,2006-day-001,1,2006-01-02 23:59:59+00:00,3578.73,3605.95,3578.73,3604.33,0.0,0.0,LongShortStrategy,1,CrossOver,1,, Broker,1,100000.0,100000.0,BuySell,1,,,Trades,1,, ... ...
這顯示了作家的一些力量。該類的進一步文檔仍然是待辦事項。
同時,示例中使用的執行可能性和代碼。
用法:
$ ./writer-test.py --help usage: writer-test.py [-h] [--data DATA] [--fromdate FROMDATE] [--todate TODATE] [--period PERIOD] [--onlylong] [--writercsv] [--csvcross] [--cash CASH] [--comm COMM] [--mult MULT] [--margin MARGIN] [--stake STAKE] [--plot] [--numfigs NUMFIGS] MultiData Strategy optional arguments: -h, --help show this help message and exit --data DATA, -d DATA data to add to the system --fromdate FROMDATE, -f FROMDATE Starting date in YYYY-MM-DD format --todate TODATE, -t TODATE Starting date in YYYY-MM-DD format --period PERIOD Period to apply to the Simple Moving Average --onlylong, -ol Do only long operations --writercsv, -wcsv Tell the writer to produce a csv stream --csvcross Output the CrossOver signals to CSV --cash CASH Starting Cash --comm COMM Commission for operation --mult MULT Multiplier for futures --margin MARGIN Margin for each future --stake STAKE Stake to apply in each operation --plot, -p Plot the read data --numfigs NUMFIGS, -n NUMFIGS Plot using numfigs figures
和測試腳本。
from __future__ import (absolute_import, division, print_function, unicode_literals) import argparse import datetime # The above could be sent to an independent module import backtrader as bt import backtrader.feeds as btfeeds import backtrader.indicators as btind from backtrader.analyzers import SQN class LongShortStrategy(bt.Strategy): '''This strategy buys/sells upong the close price crossing upwards/downwards a Simple Moving Average. It can be a long-only strategy by setting the param "onlylong" to True ''' params = dict( period=15, stake=1, printout=False, onlylong=False, csvcross=False, ) def start(self): pass def stop(self): pass def log(self, txt, dt=None): if self.p.printout: dt = dt or self.data.datetime[0] dt = bt.num2date(dt) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # To control operation entries self.orderid = None # Create SMA on 2nd data sma = btind.MovAv.SMA(self.data, period=self.p.period) # Create a CrossOver Signal from close an moving average self.signal = btind.CrossOver(self.data.close, sma) self.signal.csv = self.p.csvcross def next(self): if self.orderid: return # if an order is active, no new orders are allowed if self.signal > 0.0: # cross upwards if self.position: self.log('CLOSE SHORT , %.2f' % self.data.close[0]) self.close() self.log('BUY CREATE , %.2f' % self.data.close[0]) self.buy(size=self.p.stake) elif self.signal < 0.0: if self.position: self.log('CLOSE LONG , %.2f' % self.data.close[0]) self.close() if not self.p.onlylong: self.log('SELL CREATE , %.2f' % self.data.close[0]) self.sell(size=self.p.stake) def notify_order(self, order): if order.status in [bt.Order.Submitted, bt.Order.Accepted]: return # Await further notifications if order.status == order.Completed: if order.isbuy(): buytxt = 'BUY COMPLETE, %.2f' % order.executed.price self.log(buytxt, order.executed.dt) else: selltxt = 'SELL COMPLETE, %.2f' % order.executed.price self.log(selltxt, order.executed.dt) elif order.status in [order.Expired, order.Canceled, order.Margin]: self.log('%s ,' % order.Status[order.status]) pass # Simply log # Allow new orders self.orderid = None def notify_trade(self, trade): if trade.isclosed: self.log('TRADE PROFIT, GROSS %.2f, NET %.2f' % (trade.pnl, trade.pnlcomm)) elif trade.justopened: self.log('TRADE OPENED, SIZE %2d' % trade.size) def runstrategy(): args = parse_args() # Create a cerebro cerebro = bt.Cerebro() # Get the dates from the args fromdate = datetime.datetime.strptime(args.fromdate, '%Y-%m-%d') todate = datetime.datetime.strptime(args.todate, '%Y-%m-%d') # Create the 1st data data = btfeeds.BacktraderCSVData( dataname=args.data, fromdate=fromdate, todate=todate) # Add the 1st data to cerebro cerebro.adddata(data) # Add the strategy cerebro.addstrategy(LongShortStrategy, period=args.period, onlylong=args.onlylong, csvcross=args.csvcross, stake=args.stake) # Add the commission - only stocks like a for each operation cerebro.broker.setcash(args.cash) # Add the commission - only stocks like a for each operation cerebro.broker.setcommission(commission=args.comm, mult=args.mult, margin=args.margin) cerebro.addanalyzer(SQN) cerebro.addwriter(bt.WriterFile, csv=args.writercsv, rounding=2) # And run it cerebro.run() # Plot if requested if args.plot: cerebro.plot(numfigs=args.numfigs, volume=False, zdown=False) def parse_args(): parser = argparse.ArgumentParser(description='MultiData Strategy') parser.add_argument('--data', '-d', default='../../datas/2006-day-001.txt', help='data to add to the system') parser.add_argument('--fromdate', '-f', default='2006-01-01', help='Starting date in YYYY-MM-DD format') parser.add_argument('--todate', '-t', default='2006-12-31', help='Starting date in YYYY-MM-DD format') parser.add_argument('--period', default=15, type=int, help='Period to apply to the Simple Moving Average') parser.add_argument('--onlylong', '-ol', action='store_true', help='Do only long operations') parser.add_argument('--writercsv', '-wcsv', action='store_true', help='Tell the writer to produce a csv stream') parser.add_argument('--csvcross', action='store_true', help='Output the CrossOver signals to CSV') parser.add_argument('--cash', default=100000, type=int, help='Starting Cash') parser.add_argument('--comm', default=2, type=float, help='Commission for operation') parser.add_argument('--mult', default=10, type=int, help='Multiplier for futures') parser.add_argument('--margin', default=2000.0, type=float, help='Margin for each future') parser.add_argument('--stake', default=1, type=int, help='Stake to apply in each operation') parser.add_argument('--plot', '-p', action='store_true', help='Plot the read data') parser.add_argument('--numfigs', '-n', default=1, help='Plot using numfigs figures') return parser.parse_args() if __name__ == '__main__': runstrategy()