def trades(): trades = [] index = [ "2018-06-06 00:00:00", "2018-06-06 00:01:00", "2018-06-06 00:02:00", "2018-06-06 00:03:00", "2018-06-06 00:03:00", "2018-06-06 00:04:00 ", "2018-06-06 00:05:00", "2018-06-06 00:05:00", "2018-06-06 00:06:00 ", "2018-06-06 00:06:00 ", "2018-06-06 00:07:00 ", "2018-06-06 00:08:00 ", "2018-06-06 00:09:00 ", "2018-06-06 00:09:00 ", ] trade = pd.Series( index=pd.to_datetime(index), data=[1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1], name="amount", ) ids = [0, 1, 0, 1, 2, 3, 2, 4, 3, 5, 4, 5, 6, 6] currency_unit = Currency.JPY trades.append(make_trades("USDJPY", [trade], currency_unit, [ids])) trades.append(make_trades("EURJPY", [trade], currency_unit, [ids])) trades.append(make_trades("USDJPY", [trade], currency_unit, [ids])) return trades
def trades(symbol, currency_unit): data = [1.0, -2.0, 1.0, 2.0, -4.0, 2.0, 1.0, 0.0, 1.0, 0.0] index = pd.date_range(start="2018-06-06", freq="1D", periods=len(data)) trades = [] for i in range(0, len(data), 2): trade = pd.Series(index=index[i : i + 2], data=data[i : i + 2], name="amount") trades.append(trade) trades = tr.make_trades(symbol, trades, currency_unit) return trades
def test_calculate_trade_performance_for_sametime_trade_case(): symbol = "USDJPY" data = [[1.0], [2.0], [3.0]] df = pd.DataFrame( index=pd.date_range(start="2019-06-06", freq="3S", periods=len(data)), data=data, columns=["mid"], ) mid = backlight.datasource.from_dataframe(df, symbol, Currency.USD) data = [-1.0, 1.0] sr = pd.Series( index=[ pd.Timestamp("2019-06-06 00:00:03"), pd.Timestamp("2019-06-06 00:00:03"), ], data=data, name="amount", ) trade = sr trades = make_trades(symbol, [trade], Currency.USD) metrics = module.calculate_trade_performance(trades, mid) expected_cnt_trade = 1 expected_cnt_win = 0 expected_cnt_lose = 0 expected_total_pl = 0 expected_win_pl = 0 expected_lose_pl = 0 expected_trade_amount = 0 # expected_sharpe = nan expected_avg_pl = 0 # expected_total_pl / expected_trade_amount expected_max_drawdown = 0 expected_avg_win_pl = 0 # expected_win_pl / expected_cnt_win expected_avg_lose_pl = 0 # expected_lose_pl / expected_cnt_lose expected_avg_pl_per_trade = expected_total_pl / expected_cnt_trade expected_win_ratio = expected_cnt_win / expected_cnt_trade expected_lose_ratio = expected_cnt_lose / expected_cnt_trade assert metrics.loc["metrics", "cnt_trade"] == expected_cnt_trade assert metrics.loc["metrics", "cnt_win"] == expected_cnt_win assert metrics.loc["metrics", "cnt_lose"] == expected_cnt_lose assert metrics.loc["metrics", "total_win_pl"] == expected_win_pl assert metrics.loc["metrics", "total_win_pl"] == expected_win_pl assert metrics.loc["metrics", "total_win_pl"] == expected_win_pl assert metrics.loc["metrics", "total_lose_pl"] == expected_lose_pl assert metrics.loc["metrics", "cnt_amount"] == expected_trade_amount assert metrics.loc["metrics", "avg_pl_per_amount"] == expected_avg_pl assert math.isnan(metrics.loc["metrics", "sharpe"]) assert metrics.loc["metrics", "max_drawdown"] == expected_max_drawdown assert metrics.loc["metrics", "avg_win_pl"] == expected_avg_win_pl assert metrics.loc["metrics", "avg_lose_pl"] == expected_avg_lose_pl assert metrics.loc["metrics", "avg_pl_per_trade"] == expected_avg_pl_per_trade assert metrics.loc["metrics", "win_ratio"] == expected_win_ratio assert metrics.loc["metrics", "lose_ratio"] == expected_lose_ratio
def trades(symbol, currency_unit): data = [1.0, -2.0, 1.0, 2.0, -4.0, 2.0, 1.0, 0.0, 1.0, 0.0] sr = pd.Series( index=pd.date_range(start="2018-06-06", freq="1min", periods=len(data)), data=data, name="amount", ) trade = sr trades = make_trades(symbol, [trade], currency_unit) return trades
def trades(): trades = [] periods = 10 for symbol in ["USDJPY", "EURJPY", "EURUSD"]: currency_unit = Currency[symbol[-3:]] trade = pd.Series( index=pd.date_range(start="2018-06-06", freq="1min", periods=periods), data=[1, 1, -1, -1, 1, 1, -1, 1, -1, -1], name="amount", ) ids = [0, 1, 0, 1, 2, 3, 2, 4, 3, 4] trades.append(make_trades(symbol, [trade], currency_unit, [ids])) return trades
def positions(): symbol = "USDJPY" currency_unit = Currency.JPY data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0], [7.0], [8.0], [9.0], [9.0]] df = pd.DataFrame( index=pd.date_range(start="2018-06-06", freq="1min", periods=len(data)), data=data, columns=["mid"], ) market = backlight.datasource.from_dataframe(df, symbol, currency_unit) data = [1.0, -2.0, 1.0, 2.0, -4.0, 2.0, 1.0, 0.0, 1.0, 0.0] index = pd.date_range(start="2018-06-06", freq="1min", periods=len(data)) trades = [] for i in range(0, len(data), 2): trade = pd.Series(index=index[i : i + 2], data=data[i : i + 2], name="amount") trades.append(trade) trades = make_trades(symbol, trades, currency_unit) return backlight.positions.calculate_positions(trades, market)
def _calc_pl(trade: pd.Series, mkt: MarketData) -> float: mkt = mkt.loc[trade.index, :] trades = make_trades(mkt.symbol, [trade]) positions = backlight.positions.calc_positions(trades, mkt) pl = calc_pl(positions) return _sum(pl)
def test_exit_by_trailing_stop(market, signal, entries): symbol = "usdjpy" data = [ [1.0], # 00:00:00 [2.0], # 00:01:00 [3.0], # 00:02:00 [4.0], # 00:03:00 [5.0], # 00:04:00 [4.0], # 00:05:00 [3.0], # 00:06:00 [2.0], # 00:07:00 [1.0], # 00:08:00 [0.0], # 00:09:00 [1.0], # 00:10:00 ] periods = len(data) market = backlight.datasource.from_dataframe( pd.DataFrame( index=pd.date_range(start="2018-06-06", freq="1min", periods=periods), data=data, columns=["mid"], ), symbol, ) entries = make_trades( symbol, ( make_trade([Transaction(pd.Timestamp("2018-06-06 00:00:00"), 1.0) ]), make_trade( [Transaction(pd.Timestamp("2018-06-06 00:00:00"), -1.0)]), make_trade([Transaction(pd.Timestamp("2018-06-06 00:00:00"), 0.0) ]), make_trade([Transaction(pd.Timestamp("2018-06-06 00:03:00"), 1.0) ]), make_trade([Transaction(pd.Timestamp("2018-06-06 00:03:00"), 0.5) ]), make_trade( [Transaction(pd.Timestamp("2018-06-06 00:03:00"), -1.0)]), ), ) initial_stop = 2.0 trailing_stop = 1.0 trades = module.exit_by_trailing_stop(market, entries, initial_stop, trailing_stop) expected = make_trades( symbol, ( make_trade([ Transaction(pd.Timestamp("2018-06-06 00:00:00"), 1.0), Transaction(pd.Timestamp("2018-06-06 00:05:00"), -1.0), # trail stop ]), make_trade([ Transaction(pd.Timestamp("2018-06-06 00:00:00"), -1.0), Transaction(pd.Timestamp("2018-06-06 00:02:00"), 1.0), # loss cut ]), make_trade([Transaction(pd.Timestamp("2018-06-06 00:00:00"), 0.0) ]), make_trade([ Transaction(pd.Timestamp("2018-06-06 00:03:00"), 1.0), Transaction(pd.Timestamp("2018-06-06 00:05:00"), -1.0), # trail stop ]), make_trade([ Transaction(pd.Timestamp("2018-06-06 00:03:00"), 0.5), Transaction(pd.Timestamp("2018-06-06 00:05:00"), -0.5), # loss cut ]), make_trade([ Transaction(pd.Timestamp("2018-06-06 00:03:00"), -1.0), Transaction(pd.Timestamp("2018-06-06 00:10:00"), 1.0), # trail stop ]), ), ) pd.testing.assert_frame_equal(trades, expected)
def _calculate_pl(trade: pd.Series, mkt: MarketData) -> float: mkt = mkt.loc[trade.index.unique(), :] trades = make_trades(mkt.symbol, [trade], mkt.currency_unit) positions = backlight.positions.calculate_positions(trades, mkt) pl = calculate_pl(positions) return _sum(pl)