FIX marketwatch index countrycode problem

This commit is contained in:
TerenceLiu98 2021-06-24 13:25:57 +08:00
parent 09bfcdffd5
commit c641745736
1 changed files with 25 additions and 13 deletions

View File

@ -67,12 +67,12 @@ def forex(instrument="eurusd", startdate="2019-01-01", enddate="2021-01-01"):
return df return df
def index(instrument="vix", startdate="2019-01-01", enddate="2021-01-01"): def index(instrument="vix", startdate="2019-01-01", enddate="2021-01-01", countrycode=None):
startdate = datetime.strptime(startdate, "%Y-%m-%d").strftime("%m/%d/%y") startdate = datetime.strptime(startdate, "%Y-%m-%d").strftime("%m/%d/%y")
enddate = datetime.strptime(enddate, "%Y-%m-%d").strftime("%m/%d/%y") enddate = datetime.strptime(enddate, "%Y-%m-%d").strftime("%m/%d/%y")
df = pd.DataFrame() df = pd.DataFrame()
def _index(instrument="vix", startdate="01/01/2020", enddate="01/01/2021"): def _index(instrument="vix", startdate="01/01/2020", enddate="01/01/2021", countrycode=None):
""" """
https://www.marketwatch.com/investing/ https://www.marketwatch.com/investing/
""" """
@ -80,16 +80,28 @@ def index(instrument="vix", startdate="2019-01-01", enddate="2021-01-01"):
"index/{}/downloaddatapartial".format(instrument) "index/{}/downloaddatapartial".format(instrument)
ua = UserAgent(verify_ssl=False) ua = UserAgent(verify_ssl=False)
request_header = {"User-Agent": ua.random} request_header = {"User-Agent": ua.random}
request_params = urlencode({ if countrycode == None:
"startdate": r"{}".format(startdate), request_params = urlencode({
"enddate": r"{}".format(enddate), "startdate": r"{}".format(startdate),
"daterange": "d30", "enddate": r"{}".format(enddate),
"frequency": "p1d", "daterange": "d30",
"csvdownload": "true", "frequency": "p1d",
"downloadpartial": "false", "csvdownload": "true",
"newdates": "false"}, quote_via=quote) "downloadpartial": "false",
r = requests.get(tmp_url, params=request_params.replace( "newdates": "false"}, quote_via=quote)
"%2F", "/").replace("%20", " ").replace("%3A", ":"), headers=request_header) elif countrycode != None:
request_params = urlencode({
"startdate": r"{}".format(startdate),
"enddate": r"{}".format(enddate),
"daterange": "d30",
"frequency": "p1d",
"csvdownload": "true",
"downloadpartial": "false",
"newdates": "false",
"countrycode": "{}".format(countrycode)}, quote_via=quote)
r = requests.get(tmp_url, params=request_params.replace("%2F", "/").replace("%20", " ").replace("%3A", ":"), \
headers=request_header)
data_text = r.content data_text = r.content
df = pd.read_csv(io.StringIO(data_text.decode('utf-8'))) df = pd.read_csv(io.StringIO(data_text.decode('utf-8')))
Date = [] Date = []
@ -112,7 +124,7 @@ def index(instrument="vix", startdate="2019-01-01", enddate="2021-01-01"):
tmp_df = _index( tmp_df = _index(
instrument=instrument, instrument=instrument,
startdate=tmp_startdate, startdate=tmp_startdate,
enddate=tmp_enddate) enddate=tmp_enddate, countrycode=countrycode)
if i == int(startdate[6:10]): if i == int(startdate[6:10]):
df = tmp_df df = tmp_df
else: else: