def test__calendar(self, mock_get): """get the calendar information for an instrument.""" tid = "_v3_forexlabs_calendar" td = TestData(responses, tid) r = labs.Calendar(params=td.params) mock_get.register_uri('GET', "{}/{}".format(api.api_url, r), text=json.dumps(td.resp)) api.request(r) self.assertTrue(td.resp == r.response)
def add_calendar_data(self): params = { "instrument": list(self.symbol), "period": 217728000 } #period seems to capture the length of future in seconds, default was 86400 r = labs.Calendar(params=params) self.df_calendar = pd.DataFrame(self.api.request(r)) path = '..\\..\\datastore\\_{0}\\{1}\\calendar.pkl'.format( self.account_type, self.symbol) self.df_calendar.to_pickle(path)
def get_calendar_request( days: int, instrument: Union[str, None] = None) -> Union[pd.DataFrame, bool]: """Request data of significant events calendar. Parameters ---------- instrument : Union[str, None] The loaded currency pair, by default None days : int Number of days in advance Returns ------- Union[pd.DataFrame, bool] Calendar events data or False """ if instrument is None: console.print( "Error: An instrument should be loaded before running this command." ) return False parameters = {"instrument": instrument, "period": str(days * 86400 * -1)} if client is None: return False try: request = forexlabs.Calendar(params=parameters) response = client.request(request) l_data = [] for i in enumerate(response): if "forecast" in response[i[0]]: forecast = response[i[0]]["forecast"] if response[i[0]]["unit"] != "Index": forecast += response[i[0]]["unit"] else: forecast = "" if "market" in response[i[0]]: market = response[i[0]]["market"] if response[i[0]]["unit"] != "Index": market += response[i[0]]["unit"] else: market = "" if "actual" in response[i[0]]: actual = response[i[0]]["actual"] if response[i[0]]["unit"] != "Index": actual += response[i[0]]["unit"] else: actual = "" if "previous" in response[i[0]]: previous = response[i[0]]["previous"] if response[i[0]]["unit"] != "Index": previous += response[i[0]]["unit"] else: previous = "" if "impact" in response[i[0]]: impact = response[i[0]]["impact"] else: impact = "" l_data.append({ "Title": response[i[0]]["title"], "Time": datetime.fromtimestamp(response[i[0]]["timestamp"]), "Impact": impact, "Forecast": forecast, "Market Forecast": market, "Currency": response[i[0]]["currency"], "Region": response[i[0]]["region"], "Actual": actual, "Previous": previous, }) if len(l_data) == 0: df_calendar = pd.DataFrame() else: df_calendar = pd.DataFrame(l_data) return df_calendar except V20Error as e: d_error = json.loads(e.msg) console.print(d_error["message"], "\n") return False
def calendar(instrument, other_args: List[str]): parser = argparse.ArgumentParser( add_help=False, prog="calendar", description="Show Calendar Data", ) parser.add_argument( "-d", "--days", dest="days", action="store", type=int, default=7, required=False, help="The number of days to search for, up to 30 forward or backward " + "use negative numbers to search back. ", ) ns_parser = parse_known_args_and_warn(parser, other_args) if not ns_parser: return parameters = { "instrument": instrument, "period": str(ns_parser.days * 86400 * -1) } try: request = forexlabs.Calendar(params=parameters) response = client.request(request) l_data = [] # pylint: disable=C0200 for i in range(len(response)): if "forecast" in response[i]: forecast = response[i]["forecast"] if response[i]["unit"] != "Index": forecast += response[i]["unit"] else: forecast = "" if "market" in response[i]: market = response[i]["market"] if response[i]["unit"] != "Index": market += response[i]["unit"] else: market = "" if "actual" in response[i]: actual = response[i]["actual"] if response[i]["unit"] != "Index": actual += response[i]["unit"] else: actual = "" if "previous" in response[i]: previous = response[i]["previous"] if response[i]["unit"] != "Index": previous += response[i]["unit"] else: previous = "" if "impact" in response[i]: impact = response[i]["impact"] else: impact = "" l_data.append({ "Title": response[i]["title"], "Time": datetime.fromtimestamp(response[i]["timestamp"]), "Impact": impact, "Forecast": forecast, "Market Forecast": market, "Currency": response[i]["currency"], "Region": response[i]["region"], "Actual": actual, "Previous": previous, }) print(pd.DataFrame(l_data).to_string(index=False)) print("") except V20Error as e: # pylint: disable=W0123 d_error = eval(e.msg) print(d_error["message"], "\n")
def ForexlabsCalendar(access_token, params=None): # check 'Get calendar information.' r = forexlabs.Calendar(params=params) client = API(access_token=access_token) client.request(r) return readable_output(Munch(r.response)), Munch(r.response)