Skip to content

Instantly share code, notes, and snippets.

@rodrigo-brito
Created July 4, 2019 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigo-brito/4f779001647ce1ce7235a7d254b7e75b to your computer and use it in GitHub Desktop.
Save rodrigo-brito/4f779001647ce1ce7235a7d254b7e75b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import time
import backtrader as bt
import datetime as dt
class Example(bt.Strategy):
def __init__(self):
self.ema_12 = bt.indicators.ExponentialMovingAverage(plotname='EMA_12', period=12)
self.ema_26 = bt.indicators.ExponentialMovingAverage(plotname='EMA_26', period=26)
self.talib_macd = bt.talib.MACD(self.data, plotname='TA_MACD')
self.macd = bt.indicators.MACD()
def next(self):
print("EMA12 = %.2f EMA26 = %.2f DIFF = %.2f" % (self.ema_12.ema[0], self.ema_26.ema[0], float(self.ema_12.ema[0] - self.ema_26.ema[0])))
print("STD MACD = %.2f" % (self.macd.macd[0]))
print("TALIB MACD = %.2f" % (self.talib_macd.macd[0]))
print("--------------")
class CustomDataset(bt.feeds.GenericCSVData):
params = (
('time', -1),
('datetime', 0),
('open', 1),
('high', 2),
('low', 3),
('close', 4),
('volume', 5),
('openinterest', -1),
)
def main():
cerebro = bt.Cerebro(quicknotify=True)
data = CustomDataset(
name="BTC",
dataname="dataset/binance_nov_18_may_19_btc.csv",
timeframe=bt.TimeFrame.Minutes,
fromdate=dt.datetime(2019, 4, 13),
todate=dt.datetime(2019, 5, 13),
nullvalue=0.0
)
#cerebro.adddata(data)
cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=30)
cerebro.addstrategy(Example)
cerebro.run()
cerebro.plot()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("finished.")
except Exception as err:
print("Finished with error: ", err)
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment