Lean icon indicating copy to clipboard operation
Lean copied to clipboard

History is Broken for GDAX backtesting

Open lacvapps opened this issue 8 years ago • 3 comments

Expected Behavior

self.History('BTCUSD', 60, Resolution.Minute) Should return historical data

Actual Behavior

it returns '1, Culture=neutral, PublicKeyToken=null]]'

Reproducing the Problem

def Initialize(self): self.hist_data = self.History('BTCUSD', 10, Resolution.Minute) print(self.hist_data[-1])

System Information

Ubuntu 16.04 Python 3.6.4 Latest Master

Checklist

  • [x] I have completely filled out this template
  • [x] I have confirmed that this issue exists on the current master branch
  • [x] I have confirmed that this is not a duplicate issue by searching [issues]
  • [x] I have provided detailed steps to reproduce the issue

lacvapps avatar Mar 28 '18 22:03 lacvapps

Thanks @lacvapps for the issue! Can you please confirm:

  • Have the data in place?
  • You've got the AddCrypto in your algorithm (demo algo even better)
  • Backtesting or live mode?
  • Is it resolved with self.History( ['BTCUSD'], 10, Resolution.Minute) array notation?

jaredbroad avatar Mar 28 '18 22:03 jaredbroad

@jaredbroad Thank you for the quick response I modified the basic crypto algorithm just now to test these things. I have the data downloaded and am able to run backtests without using History, It does not work with either notation. I receive this error when using the following code.

Have the data in place? Yes You've got the AddCrypto in your algorithm (demo algo even better) Yes - Attached Demo Backtesting or live mode? Backtesting Is it resolved with self.History( ['BTCUSD'], 10, Resolution.Minute) array notation? Nope

returned a result with an error set or print(self.hist_data[-1]) TypeError : unindexable object

`

` class BasicTemplateCryptoAlgorithm(QCAlgorithm):

def Initialize(self):

    self.SetStartDate(2018, 3, 7)  #Set Start Date
    self.SetEndDate(2018, 3, 7)    #Set End Date
    self.SetCash(100000)          #Set Strategy Cash

    self.SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash)

    # Find more symbols here: http://quantconnect.com/data
    self.AddCrypto("BTCUSD", Resolution.Minute)
    self.hist_data = self.History(['BTCUSD'], 60, Resolution.Minute)
    print(self.hist_data)

def OnData(self, data):
    '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.

    Arguments:
        data: Slice object keyed by symbol containing the stock data
    '''
    if self.Time.minute == 0:class BasicTemplateCryptoAlgorithm(QCAlgorithm):
        if not self.Portfolio.Invested:
            self.SetHoldings("BTCUSD", 1)
        else:
            self.Liquidate()

        btcHoldings = self.Portfolio.CashBook["BTC"].Amount
        usdCash = self.Portfolio.CashBook["USD"].Amount
        self.Log("{} - BTC holdings: {} - USD cash: {}".format(self.Time, btcHoldings, usdCash))

`

lacvapps avatar Mar 28 '18 23:03 lacvapps

In order to get a pandas.DataFrame from a History method call, we need to use the array notation, like @jaredbroad proposed, otherwise we get C# IEnumerable object. That said, the data frame from the History method is a multi-index data frame. The first index is the Symbol as a string and the second one is the datetime (for derivatives, there are other indexes). We can see them when we plot the data frame (symbol and time indexes):

self.hist_data = self.History(['BTCUSD'], 60, Resolution.Minute)
self.Log(str(self.hist_data))

Output snippet:

                                    open     volume
symbol time
BTCUSD 2016-10-07 03:01:00+00:00  611.99  20.313877
       2016-10-07 03:02:00+00:00  611.97   0.404191
       2016-10-07 03:03:00+00:00  611.97   2.836366
       2016-10-07 03:04:00+00:00  611.98   5.631499
       2016-10-07 03:05:00+00:00  611.99   1.553840

You are getting print(self.hist_data[-1]) TypeError : unindexable object because you are trying to access data from a data frame with an index where this notation is used to access data from an array (pandas.Series). So you should extract the series from the data frame first:

self.hist_data = self.History(['BTCUSD'], 60, Resolution.Minute)
series = self.hist_data.loc['BTCUSD']['close']
self.Log(str(series[-1]))

Output: 612.25.

AlexCatarino avatar Mar 29 '18 15:03 AlexCatarino