1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-15 00:36:55 +08:00

fix bugs of random strategy

This commit is contained in:
Young
2021-06-27 12:27:15 +00:00
committed by you-n-g
parent e78cdd4a08
commit c907d8deb4
2 changed files with 12 additions and 10 deletions

View File

@@ -51,7 +51,7 @@ def collect_data_loop(start_time, end_time, trade_strategy: BaseStrategy, trade_
while not trade_executor.finished(): while not trade_executor.finished():
_trade_decision: BaseTradeDecision = trade_strategy.generate_trade_decision(_execute_result) _trade_decision: BaseTradeDecision = trade_strategy.generate_trade_decision(_execute_result)
_execute_result = yield from trade_executor.collect_data(_trade_decision) _execute_result = yield from trade_executor.collect_data(_trade_decision)
bar.update(trade_executor.trade_calendar.get_trade_step()) bar.update(1)
if return_value is not None: if return_value is not None:
all_executors = trade_executor.get_all_executors() all_executors = trade_executor.get_all_executors()

View File

@@ -635,6 +635,7 @@ class RandomOrderStrategy(BaseStrategy):
self.volume_ratio = volume_ratio self.volume_ratio = volume_ratio
self.market = market self.market = market
exch: Exchange = self.common_infra.get("trade_exchange") exch: Exchange = self.common_infra.get("trade_exchange")
# TODO: this can't be online
self.volume = D.features(D.instruments(market), ["Mean(Ref($volume, 1), 10)"], start_time=exch.start_time, end_time=exch.end_time) self.volume = D.features(D.instruments(market), ["Mean(Ref($volume, 1), 10)"], start_time=exch.start_time, end_time=exch.end_time)
self.volume_df = self.volume.iloc[:, 0].unstack() self.volume_df = self.volume.iloc[:, 0].unstack()
@@ -644,13 +645,14 @@ class RandomOrderStrategy(BaseStrategy):
order_list = [] order_list = []
for direction in Order.SELL, Order.BUY: for direction in Order.SELL, Order.BUY:
for stock_id, volume in self.volume_df[step_time_start].dropna().sample(frac=self.sample_ratio).items(): if step_time_start in self.volume_df:
order_list.append( for stock_id, volume in self.volume_df[step_time_start].dropna().sample(frac=self.sample_ratio).items():
self.common_infra.get("trade_exchange").create_order( order_list.append(
code=stock_id, self.common_infra.get("trade_exchange").create_order(
amount=volume * self.volume_ratio, code=stock_id,
start_time=step_time_start, amount=volume * self.volume_ratio,
end_time=step_time_end, start_time=step_time_start,
direction=direction, # 1 for buy end_time=step_time_end,
)) direction=direction, # 1 for buy
))
return TradeDecisionWO(order_list, self) return TradeDecisionWO(order_list, self)