def test_get_FX_latest_date(): """Test returned date is in last week (could be that today not yet updated or is not a working day) """ today = datetime.today() last_week = today - timedelta(days=7) assert datetime.strptime(FX().get_FX_latest()['date'][0], '%Y-%m-%d') > last_week assert datetime.strptime(FX().get_FX_latest()['date'][0], '%Y-%m-%d') <= today
def test_init_target_fail(): """Test non str/list of str/None inputs of target_currency raise TypeError """ with pytest.raises(TypeError): fx = FX( target_currency=["JPY", True] )
def test_init_source_fail(): """Test non str/list of str inputs of source_currency raise TypeError """ with pytest.raises(TypeError): fx = FX( source_currency=["USD", "GBP", 567] )
def test_init(): """Test to verify user set instance attributes """ fx = FX( source_currency=["USD", "GBP", "EUR"], target_currency=["JPY", "CAD"] ) assert [fx.source_currency, fx.target_currency] == [["USD", "GBP", "EUR"], ["JPY", "CAD"]]
def main(sample=100, dim=30, interval=10): if sample < 2 * dim: sample = 2 * dim print('sample is changed to', 2 * dim) with FX() as api: mean_rate = 0. queue = deque([], maxlen=dim) # The first row of the data matrix for i in range(dim): queue.append(api.get_ask_rate()) mean_rate = (i * mean_rate + queue[-1]) / (i + 1) time.sleep(interval) x = [] y = [] # The following rows of the data matrix for i in range(sample): y.append(api.get_ask_rate()) x.append(queue) queue.popleft() queue.append(y[-1]) mean_rate = ((dim - 1) * mean_rate + queue[-1]) / dim time.sleep(interval) start = time.time() # Drop and Fetch data and Predict while time.time() - start < 1e4: y.append(api.get_ask_rate()) x.append(queue) queue.popleft() queue.append(y[-1]) mean_rate = ((dim - 1) * mean_rate + queue[-1]) / dim X = np.array(x) - mean_rate Y = np.array(y) - mean_rate beta = np.linalg.pinv(X.T @ X) @ X.T @ Y y_hat = np.zeros(2 * dim) y_hat[:dim] = Y[-dim:] for t in range(dim): y_hat[dim + t] = beta @ y_hat[t : dim + t] argmin = int(np.argmin(y_hat[dim:])) + 1 argmax = int(np.argmax(y_hat[dim:])) + 1 now = datetime.datetime.now() print( 'min:', now + datetime.timedelta(seconds=interval*argmin), y_hat[dim + argmin] - Y[-1]) print( 'max:', now + datetime.timedelta(seconds=interval*argmax), y_hat[dim + argmax] - Y[-1]) time.sleep(interval)
def test_get_FX_date_range_non_empty(): """Test returned DataFrame is non empty """ assert FX().get_FX_date_range(start_at="2019-07-23", end_at="2019-07-29").shape[0] > 0
def test_get_FX_date_value_fail(): """Test non "YYYY-MM-DD" format inputs of date raise ValueError """ with pytest.raises(ValueError): FX().get_FX_date(date="2019-07-63")
def test_get_FX_date_type_fail(): """Test non str inputs of date raise TypeError """ with pytest.raises(TypeError): FX().get_FX_date(date=2019)
def test_get_FX_date_type(): """Test dtypes of returned DataFrame """ assert FX().get_FX_date(date="2020-07-01").dtypes.apply(lambda x: x.name).to_dict() == {'date': 'object', 'source_currency': 'object', 'target_currency': 'object', 'exchange_rate_to_target': 'float64'}
def test_get_FX_date_date(): """Test returned date is same as input date """ assert FX().get_FX_date(date="2020-03-13")['date'][0] == "2020-03-13"
def test_get_FX_date_non_empty(): """Test returned DataFrame is non empty """ assert FX().get_FX_date(date="2019-07-23").shape[0] > 0
def test_get_FX_date_range_end_type_fail(): """Test non str inputs of end_at raise TypeError """ with pytest.raises(TypeError): FX().get_FX_date_range(start_at="2020-03-14", end_at=False)
def test_get_FX_latest_non_empty(): """Test returned DataFrame is non empty """ assert FX().get_FX_latest().shape[0] > 0
def test_get_FX_date_range_end_value_fail(): """Test non "YYYY-MM-DD" format inputs of end_at raise ValueError """ with pytest.raises(ValueError): FX().get_FX_date_range(start_at="2020-03-14", end_at="2020-03-176")
def test_get_FX_date_range_date(): """Test returned dates are subset of input dates N.B. wouldn't expect full match as history only updated on working days """ assert set(FX().get_FX_date_range(start_at="2020-03-14", end_at="2020-03-17")['date'].unique().tolist()) <= set(["2020-03-14", "2020-03-15", "2020-03-16", "2020-03-17"])
def test_get_FX_latest_HTTPError(): """Test HTTPException raised on bad query """ with pytest.raises(requests.HTTPError): FX(source_currency='ads').get_FX_latest()
def test_default_init(): """Test to verify default instance attributes """ fx = FX() assert [fx.source_currency, fx.target_currency] == [["GBP"], None]