在博客上發表評論之後,對繪圖做了一點補充(幸運的是只有幾行代碼)。
- 在任何其他指標上繪製任何指標的能力
一個潛在的用例:
通過將一些指標繪製在一起並有更多空間來欣賞 OHLC 條來節省寶貴的屏幕空間
示例:連接隨機和 RSI 圖
當然必須考慮一些事情:
如果指標的縮放比例差異太大,則某些指標將不可見。
示例:MACD 在 0.0 正負 0.5 附近振盪,繪製在 0-100 範圍內移動的隨機指標上。
第一個實現是在提交時的開發分支中…14252c6
一個示例腳本(完整代碼見下文)讓我們看看效果
筆記
因為示例策略什麼都不做,除非通過命令行開關激活,否則標準觀察者將被刪除。
首先腳本在沒有開關的情況下運行。
在數據上繪製一個簡單移動平均線
MACD、隨機指標和 RSI 繪製在自己的軸/子圖上
執行:
$ ./plot-same-axis.py
還有圖表。
第二次執行改變了全景圖:
簡單移動平均線移至子圖
MACD 隱藏
RSI 在隨機指標上繪製(y 軸範圍兼容:0-100)
這是通過將指標的
plotinfo.plotmaster值設置為要繪製到的另一個指標來實現的。在這種情況下,由於
__init__中的局部變量被命名為stoc代表 Stochastic 和rsi代表 RSI,它看起來像:rsi.plotinfo.plotmaster = stoc
執行:
$ ./plot-same-axis.py --smasubplot --nomacdplot --rsioverstoc
圖表。
為了檢查比例尺的不兼容性,讓我們嘗試在 SMA 上繪製 rsi:
$ ./plot-same-axis.py --rsiovermacd
圖表。
RSI 標籤與數據和 SMA 一起顯示,但比例在 3400-4200 範圍內,因此……沒有 RSI 的痕跡。
進一步徒勞的嘗試是將 SMA 放在子圖上,然後再次在 SMA 上繪製 RSI
$ ./plot-same-axis.py –rsiovermacd –smasubplot
圖表。
標籤很清楚,但 RSI 中剩下的只是 SMA 圖底部的一條淡藍色線。
筆記
添加了在另一個指標上繪製的多線指標
在另一個方向上,讓我們在另一個指標上繪製一個多線指標。讓我們在 RSI 上繪製隨機指標:
$ ./plot-same-axis.py --stocrsi
有用。 Stochastic標籤出現,2行K%和D%也出現。但是這些線沒有“命名”,因為我們已經獲得了指標的名稱。
在代碼中,當前設置為:
stoc.plotinfo.plotmaster = rsi
要顯示隨機線的名稱而不是名稱,我們還需要:
stoc.plotinfo.plotlinelabels = True
這已被參數化,並且新的執行顯示了它:
$ ./plot-same-axis.py --stocrsi --stocrsilabels
圖表現在在 RSI線的名稱下方顯示隨機線的名稱。
腳本用法:
$ ./plot-same-axis.py --help
usage: plot-same-axis.py [-h] [--data DATA] [--fromdate FROMDATE]
[--todate TODATE] [--stdstats] [--smasubplot]
[--nomacdplot]
[--rsioverstoc | --rsioversma | --stocrsi]
[--stocrsilabels] [--numfigs NUMFIGS]
Plotting Example
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
--stdstats, -st Show standard observers
--smasubplot, -ss Put SMA on own subplot/axis
--nomacdplot, -nm Hide the indicator from the plot
--rsioverstoc, -ros Plot the RSI indicator on the Stochastic axis
--rsioversma, -rom Plot the RSI indicator on the SMA axis
--stocrsi, -strsi Plot the Stochastic indicator on the RSI axis
--stocrsilabels Plot line names instead of indicator name
--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
class PlotStrategy(bt.Strategy):
'''
The strategy does nothing but create indicators for plotting purposes
'''
params = dict(
smasubplot=False, # default for Moving averages
nomacdplot=False,
rsioverstoc=False,
rsioversma=False,
stocrsi=False,
stocrsilabels=False,
)
def __init__(self):
sma = btind.SMA(subplot=self.params.smasubplot)
macd = btind.MACD()
# In SMA we passed plot directly as kwarg, here the plotinfo.plot
# attribute is changed - same effect
macd.plotinfo.plot = not self.params.nomacdplot
# Let's put rsi on stochastic/sma or the other way round
stoc = btind.Stochastic()
rsi = btind.RSI()
if self.params.stocrsi:
stoc.plotinfo.plotmaster = rsi
stoc.plotinfo.plotlinelabels = self.p.stocrsilabels
elif self.params.rsioverstoc:
rsi.plotinfo.plotmaster = stoc
elif self.params.rsioversma:
rsi.plotinfo.plotmaster = sma
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(PlotStrategy,
smasubplot=args.smasubplot,
nomacdplot=args.nomacdplot,
rsioverstoc=args.rsioverstoc,
rsioversma=args.rsioversma,
stocrsi=args.stocrsi,
stocrsilabels=args.stocrsilabels)
# And run it
cerebro.run(stdstats=args.stdstats)
# Plot
cerebro.plot(numfigs=args.numfigs, volume=False)
def parse_args():
parser = argparse.ArgumentParser(description='Plotting Example')
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('--stdstats', '-st', action='store_true',
help='Show standard observers')
parser.add_argument('--smasubplot', '-ss', action='store_true',
help='Put SMA on own subplot/axis')
parser.add_argument('--nomacdplot', '-nm', action='store_true',
help='Hide the indicator from the plot')
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument('--rsioverstoc', '-ros', action='store_true',
help='Plot the RSI indicator on the Stochastic axis')
group.add_argument('--rsioversma', '-rom', action='store_true',
help='Plot the RSI indicator on the SMA axis')
group.add_argument('--stocrsi', '-strsi', action='store_true',
help='Plot the Stochastic indicator on the RSI axis')
parser.add_argument('--stocrsilabels', action='store_true',
help='Plot line names instead of indicator name')
parser.add_argument('--numfigs', '-n', default=1,
help='Plot using numfigs figures')
return parser.parse_args()
if __name__ == '__main__':
runstrategy()