alexgolec / tda-api

A TD Ameritrade API client for Python. Includes historical data for equities and ETFs, options chains, streaming order book data, complex order construction, and more.
https://tda-api.readthedocs.io
MIT License
1.26k stars 338 forks source link

KeyError: 'candles' #319

Closed jianlao8 closed 2 years ago

jianlao8 commented 2 years ago

Hello,

When I run my python code in Jupyter-notebook, some times an error "KeyError: 'candles'" happened and the program terminated. It may come from tda-api function "Client.get_price_history_every_five_minutes(symbol, *, start_datetime=None, end_datetime=None, need_extended_hours_data=None)" . I would like someone to help to resolve this issue.

Thank you!

Following is the error:

KeyError Traceback (most recent call last) /tmp/ipykernel_1658/1991461943.py in 3 run_bot() 4 manager.every(5).minutes.at(':15').do(run_bot) ----> 5 manager.start()

~/.local/lib/python3.9/site-packages/vectorbt/utils/schedule_.py in start(self, sleep) 243 try: 244 while True: --> 245 self.scheduler.run_pending() 246 time.sleep(sleep) 247 except (KeyboardInterrupt, asyncio.CancelledError):

~/.local/lib/python3.9/site-packages/schedule/init.py in run_pending(self) 98 runnable_jobs = (job for job in self.jobs if job.should_run) 99 for job in sorted(runnable_jobs): --> 100 self._run_job(job) 101 102 def run_all(self, delay_seconds: int = 0) -> None:

~/.local/lib/python3.9/site-packages/schedule/init.py in _run_job(self, job) 170 171 def _run_job(self, job: "Job") -> None: --> 172 ret = job.run() 173 if isinstance(ret, CancelJob) or ret is CancelJob: 174 self.cancel_job(job)

~/.local/lib/python3.9/site-packages/schedule/init.py in run(self) 659 660 logger.debug("Running job %s", self) --> 661 ret = self.job_func() 662 self.last_run = datetime.datetime.now() 663 self._schedule_next_run()

/tmp/ipykernel_1658/3859186269.py in run_bot() 2 print(f"Fetching new bars for {datetime.now().isoformat()} ") 3 ----> 4 df = get_his(stock) 5 # df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms') 6

/tmp/ipykernel_1658/3698826988.py in get_his(stock) 12 except KeyError: 13 logging.error("Nothing is real but I can't quit...", exc_info=True) ---> 14 h = resp.json()['candles'] 15 df = pd.DataFrame.from_records(h) 16 df['datetime'] = pd.to_datetime(df['datetime'], unit='ms').dt.tz_localize("GMT").dt.tz_convert('America/New_York').dt.tz_localize(None).copy()

KeyError: 'candles'