qtrade
qtrade copied to clipboard
Support multiple questrade accounts with different tokens
We have different accounts, my partner and myself. The API right now doesn't support different YAML files for the token cache, but what I want to do is to aggregate our positions for further analysis.
Thank you for raising this. I will need to think how to do this best given I would need to connect the yaml files to the specific class instances. Shouldn't be too hard, but I will put it on my backlog.
@jborchma thanks for looking into it. I ended up implementing it in a "hacky" way around your API, per below.
The idea I had was to rename the saved yml
with the last 5 chars per token and conduct the look-up in that way. I also sort of addressed the #18 a little bit, so wondering if this is something you would want to consider merging into the master.
import os
from pathlib import Path
import requests
from qtrade import Questrade
from util.config import config
def get_qtrades() -> list[Questrade]:
"""
Get the list of Questrade object
"""
qts = []
for token in config['questrade_tokens']:
yml_name = "token_" + token[-5:] + '.yml'
p = Path(__file__).parent.with_name(yml_name)
try:
qtrade = Questrade(access_code=token)
default_path = Path(__file__).parent.with_name("access_token.yml")
os.rename(default_path, p)
except requests.exceptions.HTTPError as ex:
if ex.response.status_code == 400:
qtrade = Questrade(token_yaml=p) # load the token from the yaml file
try:
qtrade.get_account_id() # test if the token is still valid
except requests.exceptions.HTTPError as ex:
if ex.response.status_code == 401: # token expired, refresh it
qtrade.refresh_access_token(from_yaml=True, yaml_path=p)
else:
raise ex
else:
raise ex
qts.append(qtrade)
return qts