def _getCachedCSV(self,instrument:orm.Instrument,year,month):
fname = f'HISTDATA_COM_ASCII_{instrument.code.upper()}_T_{year}{month:02d}.zip'
cachedir = '__histdata_cache'
local = f'{cachedir}/{fname}'
print(local)
if not os.path.isfile(local):
url = f"https://www.histdata.com/download-free-forex-historical-data/?/ascii/tick-data-quotes/{instrument.code.lower()}/{year}/{month}"
page = requests.get(url)
dom:BeautifulSoup = BeautifulSoup(page.text)
if not os.path.isdir(cachedir):
os.makedirs(cachedir)
form = dom.find(id="file_down")
payload = dict()
for input in form.find_all("input"):
payload[input['name']] = input['value']
formurl = f"https://www.histdata.com{form['action']}"
response = requests.post(formurl,data=payload,allow_redirects=True,headers={'referer':url})
with open(local,'wb') as dst:
shutil.copyfileobj(io.BytesIO(response.content),dst)
zip = zipfile.ZipFile(file=open(local,'rb'))
return zip.open(zip.filelist[0].filename)