Auquan / auquantoolbox

Backtesting toolbox for trading strategies
https://toolbox.auquan.com/
Apache License 2.0
108 stars 44 forks source link

KeyError: "name='B', domain=None, path=None" #5

Open CodeCombiner opened 2 years ago

CodeCombiner commented 2 years ago

Hi, trying to run this lab from the course. https://github.com/GoogleCloudPlatform/training-data-analyst/blob/master/courses/ai-for-finance/solution/momentum_backtest_losing_money.ipynb

And I get this error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/tmp/ipykernel_21116/1996377170.py in <module>
      1 tf = MyTradingFunctions()
      2 tsParams = MyTradingParams(tf)
----> 3 tradingSystem = TradingSystem(tsParams)
      4 results = tradingSystem.startTrading()

~/.local/lib/python3.7/site-packages/backtester/trading_system.py in __init__(self, tsParams)
     33         self.executionSystem = None
     34         self.orderPlacer = None
---> 35         self.dataParser = self.tsParams.getDataParser()
     36         self.executionSystem = self.tsParams.getExecutionSystem()
     37         self.orderPlacer = self.tsParams.getOrderPlacer()

/tmp/ipykernel_21116/1453009385.py in getDataParser(self)
     19             instrumentIds = instrumentIds,
     20             startDateStr = self.__startDate,
---> 21             endDateStr = self.__endDate,
     22         )
     23 

~/.local/lib/python3.7/site-packages/backtester/dataSource/yahoo_data_source.py in __init__(self, cachedFolderName, dataSetId, instrumentIds, startDateStr, endDateStr, event, adjustPrice, downloadId, liveUpdates, pad)
    117         self.event = event
    118         if liveUpdates:
--> 119             self._allTimes, self._groupedInstrumentUpdates = self.getGroupedInstrumentUpdates()
    120             self.processGroupedInstrumentUpdates()
    121             self._bookDataFeatureKeys = self.__bookDataByFeature.keys()

~/.local/lib/python3.7/site-packages/backtester/dataSource/data_source.py in getGroupedInstrumentUpdates(self)
     65             print('Processing data for stock: %s' % (instrumentId))
     66             fileName = self.getFileName(instrumentId)
---> 67             if not self.downloadAndAdjustData(instrumentId, fileName):
     68                 continue
     69             with open(fileName) as f:

~/.local/lib/python3.7/site-packages/backtester/dataSource/yahoo_data_source.py in downloadAndAdjustData(self, instrumentId, fileName)
    131     def downloadAndAdjustData(self, instrumentId, fileName):
    132         if not os.path.isfile(fileName):
--> 133             if not downloadFileFromYahoo(self._startDate, self._endDate, instrumentId, fileName):
    134                 logError('Skipping %s:' % (instrumentId))
    135                 return False

~/.local/lib/python3.7/site-packages/backtester/dataSource/data_source_utils.py in downloadFileFromYahoo(startDate, endDate, instrumentId, fileName, event)
     28 def downloadFileFromYahoo(startDate, endDate, instrumentId, fileName, event='history'):
     29     logInfo('Downloading %s' % fileName)
---> 30     cookie, crumb = getCookieForYahoo(instrumentId)
     31     start = int(mktime(startDate.timetuple()))
     32     end = int(mktime(endDate.timetuple()))

~/.local/lib/python3.7/site-packages/backtester/dataSource/data_source_utils.py in getCookieForYahoo(instrumentId)
     15     req = requests.get(url)
     16     txt = req.content
---> 17     cookie = req.cookies['B']
     18     pattern = re.compile('.*"CrumbStore":\{"crumb":"(?P<crumb>[^"]+)"\}')
     19 

/opt/conda/lib/python3.7/site-packages/requests/cookies.py in __getitem__(self, name)
    326         .. warning:: operation is O(n), not O(1).
    327         """
--> 328         return self._find_no_duplicates(name)
    329 
    330     def __setitem__(self, name, value):

/opt/conda/lib/python3.7/site-packages/requests/cookies.py in _find_no_duplicates(self, name, domain, path)
    397         if toReturn:
    398             return toReturn
--> 399         raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
    400 
    401     def __getstate__(self):

KeyError: "name='B', domain=None, path=None"
CodeCombiner commented 2 years ago

Fixed by solution from previos issue https://github.com/Auquan/auquantoolbox/issues/3

AndreyRzhaksinskiy commented 2 years ago

I have the same issue "name='B', domain=None, path=None" in https://github.com/Auquan/Tutorials/blob/master/Pairs%20Trading.ipynb could you fix it?

DavidPuigV commented 2 years ago

hi all, I still have same keyError. I tried all solutions I found in the net....

Grinboss commented 1 year ago

I also have the same problem...desperate

MicheleCarta commented 1 month ago

i used to replace the logic to fetch the data and i get rid of the old implementation in data_source_utils.py add the imports

import yfinance as yf
import pandas as pd
def downloadFileFromYahoo(startDate, endDate, instrumentId, fileName, interval):
    logInfo('Downloading %s' % fileName)
    data = yf.download(instrumentId, startDate, endDate, interval)
    with open(fileName, 'wb') as f:
        data.to_csv(fileName)
        return True
    return False

Then run pip install -e .

Finally in your jupyter or IDE install the library

%pip install -U auquan_toolbox --user

in the import section

from backtester.trading_system_parameters import TradingSystemParameters
from backtester.features.feature import Feature
from backtester.dataSource.yahoo_data_source import YahooStockDataSource
from backtester.timeRule.custom_time_rule import CustomTimeRule
from backtester.executionSystem.simple_execution_system import SimpleExecutionSystem
from backtester.orderPlacer.backtesting_order_placer import BacktestingOrderPlacer
from backtester.trading_system import TradingSystem
from backtester.constants import *