Hi James, bitcoin is an interesting one to trade, unfortunately you cannot actually buy bitcoin using Quantopian, it can only be used as a signal. You could use Quantopian's backtester Zipline locally, that would let you simulate trades using the bitcoin data.
I used bitcoin data from quandl and wrote a quick script to test a 5/20 MACD algo locally, maybe something like this will work for you.
import Quandl
from zipline import TradingAlgorithm
from zipline.api import *
# Gets a trailing window of prices
@batch_transform(window_length=20)
def _history(data):
return data['price']['BTC']
def initialize(context):
context.invested = False
def handle_data(context, data):
price_history = _history(data)
if price_history is None:
return
fastMA = price_history.tail(5).mean()
slowMA = price_history.mean()
macd = fastMA - slowMA
if macd > 0 and not context.invested:
order_target_percent('BTC', 1.0)
context.invested = True
else:
order_target('BTC', 0)
context.invested = False
if __name__ == '__main__':
# Download the price data from Quandl, you will want to get
# an auth token from them otherwise the requests will be limited.
data = Quandl.get('BAVERAGE/USD')
# Select the price column - 24 hour weighted average price
data = data[['24h Average']]
# Change the name to BTC
data.columns = ['BTC']
# Localize the dates to UTC otherwise you will get an error
data.index = data.index.tz_localize('UTC')
# initialize the trading algorithm with the
# initialize and handle_data functions
algo = TradingAlgorithm(initialize=initialize,
handle_data=handle_data)
# Run it. results will contain all the stats for the test.
results = algo.run(data)
# Plot it
results.portfolio_value.plot()