1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-14 08:16:54 +08:00

add print cash in verbose mode and code format

This commit is contained in:
Young
2021-07-20 05:12:22 +00:00
parent 92f2891664
commit 4e862f7d1f
4 changed files with 37 additions and 46 deletions

View File

@@ -160,7 +160,7 @@ class Account:
self.accum_info.add_return_value(profit) # note here do not consider cost self.accum_info.add_return_value(profit) # note here do not consider cost
def update_order(self, order, trade_val, cost, trade_price): def update_order(self, order, trade_val, cost, trade_price):
if not self.is_port_metr_enabled(): if self.current.skip_update():
# TODO: supporting polymorphism for account # TODO: supporting polymorphism for account
# updating order for infinite position is meaningless # updating order for infinite position is meaningless
return return

View File

@@ -512,7 +512,7 @@ class Exchange:
def _get_factor_or_raise_erorr(self, factor: float = None, stock_id: str = None, start_time=None, end_time=None): def _get_factor_or_raise_erorr(self, factor: float = None, stock_id: str = None, start_time=None, end_time=None):
"""Please refer to the docs of get_amount_of_trade_unit""" """Please refer to the docs of get_amount_of_trade_unit"""
if factor is None: if factor is None:
if stock_id is not None and start_time is not None and end_time is not None : if stock_id is not None and start_time is not None and end_time is not None:
factor = self.get_factor(stock_id=stock_id, start_time=start_time, end_time=end_time) factor = self.get_factor(stock_id=stock_id, start_time=start_time, end_time=end_time)
else: else:
raise ValueError(f"`factor` and (`stock_id`, `start_time`, `end_time`) can't both be None") raise ValueError(f"`factor` and (`stock_id`, `start_time`, `end_time`) can't both be None")
@@ -537,15 +537,16 @@ class Exchange:
the end time of trading range the end time of trading range
""" """
if not self.trade_w_adj_price and self.trade_unit is not None: if not self.trade_w_adj_price and self.trade_unit is not None:
factor = self._get_factor_or_raise_erorr(factor=factor, factor = self._get_factor_or_raise_erorr(
stock_id=stock_id, factor=factor, stock_id=stock_id, start_time=start_time, end_time=end_time
start_time=start_time, )
end_time=end_time)
return self.trade_unit / factor return self.trade_unit / factor
else: else:
return None return None
def round_amount_by_trade_unit(self, deal_amount, factor: float = None, stock_id: str = None, start_time=None, end_time=None): def round_amount_by_trade_unit(
self, deal_amount, factor: float = None, stock_id: str = None, start_time=None, end_time=None
):
"""Parameter """Parameter
Please refer to the docs of get_amount_of_trade_unit Please refer to the docs of get_amount_of_trade_unit
@@ -555,10 +556,9 @@ class Exchange:
""" """
if not self.trade_w_adj_price and self.trade_unit is not None: if not self.trade_w_adj_price and self.trade_unit is not None:
# the minimal amount is 1. Add 0.1 for solving precision problem. # the minimal amount is 1. Add 0.1 for solving precision problem.
factor = self._get_factor_or_raise_erorr(factor=factor, factor = self._get_factor_or_raise_erorr(
stock_id=stock_id, factor=factor, stock_id=stock_id, start_time=start_time, end_time=end_time
start_time=start_time, )
end_time=end_time)
return (deal_amount * factor + 0.1) // self.trade_unit * self.trade_unit / factor return (deal_amount * factor + 0.1) // self.trade_unit * self.trade_unit / factor
return deal_amount return deal_amount

View File

@@ -495,30 +495,22 @@ class SimulatorExecutor(BaseExecutor):
execute_result.append((order, trade_val, trade_cost, trade_price)) execute_result.append((order, trade_val, trade_cost, trade_price))
if self.verbose: if self.verbose:
if order.direction == Order.SELL: # sell if order.direction == Order.SELL: # sell
print( action = "sell"
"[I {:%Y-%m-%d %H:%M:%S}]: sell {}, price {:.2f}, amount {}, deal_amount {}, factor {}, value {:.2f}.".format(
trade_start_time,
order.stock_id,
trade_price,
order.amount,
order.deal_amount,
order.factor,
trade_val,
)
)
else: else:
print( action = "buy"
"[I {:%Y-%m-%d %H:%M:%S}]: buy {}, price {:.2f}, amount {}, deal_amount {}, factor {}, value {:.2f}.".format( print(
trade_start_time, "[I {:%Y-%m-%d %H:%M:%S}]: {} {}, price {:.2f}, amount {}, deal_amount {}, factor {}, value {:.2f}, cach {:.2f}.".format(
order.stock_id, trade_start_time,
trade_price, action,
order.amount, order.stock_id,
order.deal_amount, trade_price,
order.factor, order.amount,
trade_val, order.deal_amount,
) order.factor,
trade_val,
self.trade_account.get_cash(),
) )
)
else: else:
if self.verbose: if self.verbose:
print("[W {:%Y-%m-%d %H:%M:%S}]: {} wrong.".format(trade_start_time, order.stock_id)) print("[W {:%Y-%m-%d %H:%M:%S}]: {} wrong.".format(trade_start_time, order.stock_id))

View File

@@ -63,9 +63,9 @@ class TWAPStrategy(BaseStrategy):
stock_id=order.stock_id, start_time=trade_start_time, end_time=trade_end_time stock_id=order.stock_id, start_time=trade_start_time, end_time=trade_end_time
): ):
continue continue
_amount_trade_unit = self.trade_exchange.get_amount_of_trade_unit(stock_id=order.stock_id, _amount_trade_unit = self.trade_exchange.get_amount_of_trade_unit(
start_time=order.start_time, stock_id=order.stock_id, start_time=order.start_time, end_time=order.end_time
end_time=order.end_time) )
_order_amount = None _order_amount = None
# considering trade unit # considering trade unit
if _amount_trade_unit is None: if _amount_trade_unit is None:
@@ -169,9 +169,9 @@ class SBBStrategyBase(BaseStrategy):
self.trade_trend[order.stock_id] = _pred_trend self.trade_trend[order.stock_id] = _pred_trend
continue continue
# get amount of one trade unit # get amount of one trade unit
_amount_trade_unit = self.trade_exchange.get_amount_of_trade_unit(stock_id=order.stock_id, _amount_trade_unit = self.trade_exchange.get_amount_of_trade_unit(
start_time=order.start_time, stock_id=order.stock_id, start_time=order.start_time, end_time=order.end_time
end_time=order.end_time) )
if _pred_trend == self.TREND_MID: if _pred_trend == self.TREND_MID:
_order_amount = None _order_amount = None
# considering trade unit # considering trade unit
@@ -471,9 +471,9 @@ class ACStrategy(BaseStrategy):
if sig_sam is None or np.isnan(sig_sam): if sig_sam is None or np.isnan(sig_sam):
# no signal, TWAP # no signal, TWAP
_amount_trade_unit = self.trade_exchange.get_amount_of_trade_unit(stock_id=order.stock_id, _amount_trade_unit = self.trade_exchange.get_amount_of_trade_unit(
start_time=order.start_time, stock_id=order.stock_id, start_time=order.start_time, end_time=order.end_time
end_time=order.end_time) )
if _amount_trade_unit is None: if _amount_trade_unit is None:
# divide the order into equal parts, and trade one part # divide the order into equal parts, and trade one part
_order_amount = self.trade_amount[order.stock_id] / (trade_len - trade_step) _order_amount = self.trade_amount[order.stock_id] / (trade_len - trade_step)
@@ -494,10 +494,9 @@ class ACStrategy(BaseStrategy):
np.sinh(kappa * (trade_len - trade_step)) - np.sinh(kappa * (trade_len - trade_step - 1)) np.sinh(kappa * (trade_len - trade_step)) - np.sinh(kappa * (trade_len - trade_step - 1))
) / np.sinh(kappa * trade_len) ) / np.sinh(kappa * trade_len)
_order_amount = order.amount * amount_ratio _order_amount = order.amount * amount_ratio
_order_amount = self.trade_exchange.round_amount_by_trade_unit(_order_amount, _order_amount = self.trade_exchange.round_amount_by_trade_unit(
stock_id=order.stock_id, _order_amount, stock_id=order.stock_id, start_time=order.start_time, end_time=order.end_time
start_time=order.start_time, )
end_time=order.end_time)
if order.direction == order.SELL: if order.direction == order.SELL:
# sell all amount at last # sell all amount at last