Skip to content

Commit

Permalink
修复没行情的时候下单会报 RuntimeError 的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuizi7 committed Oct 22, 2018
1 parent 0cbfb2d commit 018fe08
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from decimal import Decimal, getcontext

import six
import numpy as np

from rqalpha.api.api_base import decorate_api_exc, instruments, cal_style, register_api
from rqalpha.const import DEFAULT_ACCOUNT_TYPE, EXECUTION_PHASE, SIDE, ORDER_TYPE, POSITION_EFFECT
Expand Down Expand Up @@ -343,7 +344,14 @@ def order_target_value(id_or_ins, cash_amount, price=None, style=None):
if cash_amount == 0:
return _sell_all_stock(order_book_id, position.sellable, style)

return order_value(order_book_id, cash_amount - position.market_value, style=style)
try:
market_value = position.market_value
except RuntimeError:
order_result = order_value(order_book_id, np.nan, style=style)
if order_result:
raise
else:
return order_value(order_book_id, cash_amount - market_value, style=style)


@export_as_api
Expand Down Expand Up @@ -400,7 +408,14 @@ def order_target_percent(id_or_ins, percent, price=None, style=None):
if percent == 0:
return _sell_all_stock(order_book_id, position.sellable, style)

return order_value(order_book_id, account.total_value * percent - position.market_value, style=style)
try:
market_value = position.market_value
except RuntimeError:
order_result = order_value(order_book_id, np.nan, style=style)
if order_result:
raise
else:
return order_value(order_book_id, account.total_value * percent - market_value, style=style)


@export_as_api
Expand Down

0 comments on commit 018fe08

Please sign in to comment.