0

When I run this file to extract Bitcoin data from an exchange into JSON format:

import os
import pytz
from datetime import datetime
import json

from catalyst.api import record, symbol, symbols
from catalyst.utils.run_algo import run_algorithm

def initialize(context):
    # Portfolio assets list
    context.asset = symbol('btc_usd') # Bitcoin on Bitfinex

def handle_data(context, data):
    # Variables to record for a given asset: price and volume

    currentTimeString = str(data.current_dt)
    currentData = {
                   'open':   data.current(context.asset, 'open'),
                   'high':   data.current(context.asset, 'high'),
                   'low':    data.current(context.asset, 'low'),
                   'close':  data.current(context.asset, 'close'),
                   'volume': data.current(context.asset, 'volume'),
                   }
    context.allData[currentTimeString] = currentData


def analyze(context=None, results=None):
    # Generate DataFrame with Price and Volume only
    data = results[['open','high','low','close','price','volume']]

    # Save results in CSV file
#    filename = os.path.splitext(os.path.basename(__file__))[0]
#    data.to_csv(filename + '.csv')

    with open('data.json', 'w') as outfile:
        json.dump(context.allData, outfile)

''' Bitcoin data is available on Poloniex since 2015-3-1.
     Dates vary for other tokens. In the example below, we choose the
     full month of July of 2017.
'''
start = datetime(2018, 1, 1, 0, 0, 0, 0, pytz.utc)
end = datetime(2018, 1, 31, 0, 0, 0, 0, pytz.utc)
results = run_algorithm(initialize=initialize,
                                handle_data=handle_data,
                                analyze=analyze,
                                start=start,
                                end=end,
                                exchange_name='bitfinex',
                                capital_base=10000,
                                quote_currency = 'usd')

I receive the following error:

  File "C:/Users/XYZ/Code/grabdata.py", line 31, in handle_data
    context.allData[currentTimeString] = currentData

AttributeError: 'ExchangeTradingAlgorithmBacktest' object has no attribute 'allData'

EDIT: Posted the wrong error code at first, it's updated now.

Is there a bug in my code, or am I missing a reference to context.allData in initialize? I'm trying to speed up the backtest by reading from a file instead of ingesting from an exchange, it seems to take too long. Using the catalyst framework which is built from Quantopian/Zipline: https://enigma.co/catalyst/index.html

Goose
  • 2,130
  • 10
  • 32
  • 43
  • Which line is `data = context.allData` ? Can you paste the entire stacktrace? – Andy Hayden Jan 30 '19 at 22:31
  • Sorry, please check the updated post, I posted the wrong stack at first, here is the correct stack trace - https://pastebin.com/RaF9LPYt – Goose Jan 31 '19 at 00:05

0 Answers0