Back to Community
Noob request - bitcoin momentum

Hi all,
I'm a student whos just starting to program, but I'm interested in trading and love this website. I'm hoping to use the idea that bitcoin would be good for momentum trading as it is purely sentiment based not value-based.
I've tried adding bitcoin data - http://pastebin.com/44z36pie - with an existing moving average algorithm, but I get an error "CParserError: Error tokenizing data. C error: Expected 1 fields in line 27, saw 2, There was a runtime error on line 13."
Can one of you guys help throw these two together for me?
Thank you so much!!!!
JD

4 responses

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()

Awesome! Thanks!!

David this is an interesting post. How i can keep a live trading on the BTC zipline algo. If I understand zipline runs as a backtest not a live production? is there any parameters to be send to keep running in travelling and pull data from Quandl?

Thanks
EG

Errors..... File "", line 18
results.portfolio_value.plot()
^
SyntaxError: invalid syntax