-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
How to draw the data with histgram. #1022
Unanswered
peacerider
asked this question in
Q&A
-
I am now creating strategy using MACD.
So I would like to draw the MACD histogram as the plot type of histogram instead of a line.
Now, I can draw with plot of line.
from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import SMA, GOOG
import talib as ta
# MACD
def macd_macd(close, n1, n2, ns):
macd, macdsignal, macdhist = ta.MACD(close, fastperiod=n1, slowperiod=n2, signalperiod=ns)
return macd
def macd_signal(close, n1, n2, ns):
macd, macdsignal, macdhist = ta.MACD(close, fastperiod=n1, slowperiod=n2, signalperiod=ns)
return macdsignal
def macd_hist(close, n1, n2, ns):
macd, macdsignal, macdhist = ta.MACD(close, fastperiod=n1, slowperiod=n2, signalperiod=ns)
return macdhist
class SmaCross(Strategy):
n1 = 10
n2 = 20
fast = 12
slow = 26
signal = 9
def init(self):
close = self.data.Close
self.sma1 = self.I(SMA, close, self.n1)
self.sma2 = self.I(SMA, close, self.n2)
self.macd_ = macd_macd(close, self.fast, self.slow, self.signal)
self.macdsignal_ = macd_signal(close, self.fast, self.slow, self.signal)
self.macd, self.macdsignal = self.I(lambda: (self.macd_, self.macdsignal_), name="MACD")
self.macdhist = self.I(macd_hist, close, self.fast, self.slow, self.signal, name="MACD Histgram")
def next(self):
if crossover(self.sma1, self.sma2):
self.buy()
elif crossover(self.sma2, self.sma1):
self.sell()
bt = Backtest(GOOG, SmaCross,
cash=10000, commission=.002,
exclusive_orders=True)
output = bt.run()
bt.plot()
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment