Skip to content

Instantly share code, notes, and snippets.

@rokcarl rokcarl/app.py Secret
Created Feb 28, 2018

Embed
What would you like to do?
from __future__ import (absolute_import, division, print_function, unicode_literals)
import pandas as pd
import backtrader as bt
from strategies import hodl
from strategies import sma_crossover
from api import crypto_data
btc_usd = crypto_data.get_feed("BTC/USD", exchange="CCCAGG", resolution="day", length=182)
cerebro = bt.Cerebro()
cerebro.broker.setcash(100000.0)
cerebro.broker.setcommission(commission=0.001)
cerebro.addstrategy(sma_crossover.SMA_CrossOver)
cerebro.addstrategy(hodl.Hodl)
cerebro.adddata(btc_usd, name="BTC")
cerebro.run()
cerebro.plot(volume=False)
from __future__ import (absolute_import, division, print_function,unicode_literals)
import backtrader as bt
class Hodl(bt.Strategy):
def __init__(self):
pass
def next(self):
stake = 1 / len(self.datas)
#for d in self.datas:
self.order_target_percent(target=stake, data=self.datas[0])
from __future__ import (absolute_import, division, print_function,unicode_literals)
import backtrader as bt
import backtrader.indicators as btind
class SMA_CrossOver(bt.Strategy):
params = (
('fast', 10),
('slow', 30),
('_movav', btind.MovAv.SMA)
)
def __init__(self):
sma_fast = self.p._movav(period=self.p.fast)
sma_slow = self.p._movav(period=self.p.slow)
self.buysig = btind.CrossOver(sma_fast, sma_slow)
def next(self):
stake = 1 / len(self.datas)
if self.buysig > 0:
self.order_target_percent(target=stake, data=self.datas[0])
elif self.buysig < 0:
self.order_target_percent(target=0.0, data=self.datas[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.