def test__calculate_pl(symbol, currency_unit): periods = 3 dates = pd.date_range(start="2018-12-01", periods=periods) amounts = [1.0, -1.0] t00 = tr.Transaction(timestamp=dates[0], amount=amounts[0]) t11 = tr.Transaction(timestamp=dates[1], amount=amounts[1]) t10 = tr.Transaction(timestamp=dates[1], amount=amounts[0]) t01 = tr.Transaction(timestamp=dates[0], amount=amounts[1]) t20 = tr.Transaction(timestamp=dates[2], amount=amounts[0]) mkt = backlight.datasource.from_dataframe( pd.DataFrame(index=dates, data=[[0], [1], [2]], columns=["mid"]), symbol, currency_unit, ) trade = tr.make_trade([t00, t11]) assert module._calculate_pl(trade, mkt) == 1.0 trade = tr.make_trade([t00, t01]) assert module._calculate_pl(trade, mkt) == 0.0 trade = tr.make_trade([t11, t20]) assert module._calculate_pl(trade, mkt) == -1.0 trade = tr.make_trade([t00, t10, t20]) assert module._calculate_pl(trade, mkt) == 3.0
def test_make_trade(): periods = 2 dates = pd.date_range(start="2018-12-01", periods=periods) amounts = range(periods) t00 = module.Transaction(timestamp=dates[0], amount=amounts[0]) t11 = module.Transaction(timestamp=dates[1], amount=amounts[1]) t01 = module.Transaction(timestamp=dates[0], amount=amounts[1]) trade = module.make_trade([t00, t11]) expected = pd.Series(index=dates, data=amounts[:2], name="amount") pd.testing.assert_series_equal(trade, expected) trade = module.make_trade([t00, t01]) expected = pd.Series(index=[dates[0]], data=[amounts[0] + amounts[1]], name="amount") pd.testing.assert_series_equal(trade, expected) trade = module.make_trade([t11, t01, t00]) expected = pd.Series(index=dates, data=[amounts[0] + amounts[1], amounts[1]], name="amount") pd.testing.assert_series_equal(trade, expected)