Skip to content

Commit dec768a

Browse files
committed
Fix exchange rate handling in value-based stock order sizing
1 parent 6104421 commit dec768a

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

rqalpha/mod/rqalpha_mod_sys_accounts/trade_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def get_amount_from_value(value: float, ins: Instrument, price: float, env: Envi
4545
amount = round_order_quantity(ins, amount)
4646
while amount > 0:
4747
estimate_transaction_cost = estimate_transaction_cost_calculator(env, ins, amount, price)
48-
if amount * price + estimate_transaction_cost > value:
48+
if amount * price * exchange_rates.ask_reference + estimate_transaction_cost > value:
4949
amount = round_order_quantity(ins, amount - ins.order_step_size)
5050
else:
5151
return amount
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from datetime import datetime
2+
from types import SimpleNamespace
3+
4+
from rqalpha.const import MARKET
5+
from rqalpha.interface import ExchangeRate, TransactionCost
6+
from rqalpha.mod.rqalpha_mod_sys_accounts.trade_utils import get_amount_from_value
7+
8+
9+
def test_get_amount_from_value_uses_ask_exchange_rate_for_buying_power():
10+
env = SimpleNamespace(
11+
trading_dt=datetime(2026, 1, 5),
12+
data_proxy=SimpleNamespace(
13+
get_exchange_rate=lambda _date, _market: ExchangeRate(
14+
bid_reference=1,
15+
ask_reference=2,
16+
bid_settlement_sh=1,
17+
ask_settlement_sh=2,
18+
bid_settlement_sz=1,
19+
ask_settlement_sz=2,
20+
)
21+
),
22+
calc_transaction_cost=lambda _args: TransactionCost.zero(),
23+
)
24+
ins = SimpleNamespace(
25+
type="CS",
26+
board_type="MAIN",
27+
round_lot=100,
28+
order_step_size=100,
29+
market=MARKET.HK,
30+
)
31+
32+
assert get_amount_from_value(100000, ins, 100, env, account_cash=200000) == 500

0 commit comments

Comments
 (0)