def morning_auction(self, context: SimContext, data: SimDataService): dt: str = context.get_dt() if dt not in self.__dt_map: return shares: dict = context.get_holding() for (ts_code, share) in shares.items(): # type: str, SimShareHold p: SimDailyPrice = context.get_today_price(ts_code) context.sell_share(ts_code, p.down_limit)
def before_trade(self, context: SimContext, data: SimDataService): dt: str = context.get_dt() if dt not in self.__dt_map: return to_buy: list = self.__dt_map[dt] # 尽量等权买入 for i in range(len(to_buy)): max_num = len(to_buy) - i ts_code = to_buy[i].ts_code p: SimDailyPrice = context.get_today_price(ts_code) context.buy_amap(ts_code, p.open, context.get_cash() / max_num)
def after_trade(self, context: SimContext, data: SimDataService): # 清除已补的缺口,或30天前的缺口 dt: str = context.get_dt() old_dt: str = date_utils.format_delta(dt, -30) for ts_code in self.target: p: SimDailyPrice = context.get_today_price(ts_code) if p is None or p.is_trade == 0: # 未上市 或 停牌就不用看了 continue self.last_trade_price[ts_code] = p ts_gap: list = self.gap[ts_code] if len(ts_gap) == 0: continue to_remove: list = [] for gap_price in ts_gap: # type: SimDailyPrice if gap_price.trade_date < old_dt or p.high >= gap_price.low: to_remove.append(gap_price) if len(to_remove) > 0: for r in to_remove: # type: SimDailyPrice ts_gap.remove(r) log.info('Gap clear: %s %s %.2f. Gap num: %d' % (ts_code, dt, gap_price.low, len(ts_gap))) self.yesterday_buy = set() just_buy: dict = context.get_holding_just_buy() for ts_code in just_buy: # type: str self.yesterday_buy.add(ts_code) self.before_yesterday_buy = set() old_holdings: dict = context.get_holding() for ts_code in old_holdings: # type: str self.before_yesterday_buy.add(ts_code) # 新的缺口数目标应该是 max(剩余缺口数+1,标准缺口数) for ts_code in self.target: # type: str new_gap_num: int = len(self.gap[ts_code]) + 1 if new_gap_num < self.standard_gap_num: new_gap_num = self.standard_gap_num self.gap_num_target[ts_code] = new_gap_num
def morning_auction(self, context: SimContext, data: SimDataService): dt: str = context.get_dt() shares: list = context.get_holding() self.selling_orders = [] for (ts_code, share) in shares.items(): # type: str, SimShareHold p: SimDailyPrice = context.get_today_price(ts_code) gap: list = self.gap[ts_code] for gap_price in gap: gap_price.update_adj('qfq', p.adj) if len(gap) < self.gap_num_target[ts_code] - 1: # 买入目标缺口数 = 当前缺口数 + 1。已经补了一个缺口,无条件卖出 self.selling_orders.append( context.sell_share(ts_code, p.down_limit)) elif ts_code in self.before_yesterday_buy: # 第三日无条件卖出 self.selling_orders.append( context.sell_share(ts_code, p.down_limit)) else: # 按补缺卖出 min_low = p.up_limit for gap_price in self.gap[ts_code]: if gap_price.low < min_low: min_low = gap_price.low self.selling_orders.append(context.sell_share( ts_code, min_low))
def morning_auction(self, context: SimContext, data: SimDataService): dt = context.get_dt() price: SimDailyPrice = data.get_qfq_price(['000050.SZ'], dt, dt, dt)[0] context.buy_amap('000050.SZ', price.pre_close) if dt == '20200527': s: SimShareHold = context.get_hold('000050.SZ') context.sell_share(s.get_ts_code(), price.pre_close)
def morning_auction(self, context: SimContext, data: SimDataService): price: SimDailyPrice = context.get_today_price(self.__target) s: SimShareHold = context.get_hold(self.__target) if context.is_first_day(): predict_num = context.get_max_can_buy(context.get_cash(), price.pre_close) self.__buy_order = context.buy_amap(self.__target, price.pre_close) # 可买入数量应考虑了手续费 assert self.__buy_order.get_num() == predict_num elif context.is_last_day(): self.__sell_order = context.sell_share(s.get_ts_code(), price.pre_close) elif s is not None: assert decimal_utils.equals(price.pre_close, s.get_current_price())
def __init__(self, handler: SimHandler, sd: dict(type=str, help="回测开始日期"), ed: dict(type=str, help="回测结束日期"), cash: dict(type=Decimal, help="起始现金") = 500000, charge: dict(type=Decimal, help="交易费率") = 0.00025, tax: dict(type=Decimal, help="印花税率") = 0.001, pass_tax: dict(type=Decimal, help="过户费率") = 0.00002): self.__cash = cash self.__sd = sd self.__ed = ed self.h: SimHandler = handler self.d: SimDataService = SimDataService() self.c: SimContext = SimContext(self.d, sd, ed, cash, charge, tax, pass_tax) self.__dr: dict = {} self.h.init(self.c, self.d) self.c.finish_init()
def init(self, context: SimContext, data: SimDataService): ts_code_set: set = set() for i in self.__list: # type: NormalLoopbackShare ts_code_set.add(i.ts_code) context.register_code(ts_code_set)
def before_trade(self, context: SimContext, data: SimDataService): holding: dict = context.get_holding() dt: str = context.get_dt() to_buy = [] to_sell = [] for ts_code in self.target: ts_gap: list = self.gap[ts_code] p: SimDailyPrice = context.get_today_price(ts_code) if ts_code in self.last_trade_price and p.is_trade == 1: lp: SimDailyPrice = self.__get_last_trade_day_price(ts_code, p) if p.open < lp.low: # 缺口 ts_gap.append(lp) log.info('New gap: %s %s %.2f. Gap num: %d' % (ts_code, context.get_dt(), lp.low, len(ts_gap))) if len(ts_gap) >= self.gap_num_target[ts_code]: if ts_code in holding: # 已经持有了,还在往下跳缺口,垃圾货要赶紧卖了 to_sell.append(ts_code) else: to_buy.append(ts_code) for ts_code in to_sell: # type: str # 取消已有的卖出订单,然后无条件卖出 for o in self.selling_orders: # type SimOrder if o.get_ts_code() == ts_code: context.retrieve_order(o) p: SimDailyPrice = context.get_today_price(ts_code) context.sell_share(ts_code, p.down_limit) # 尽量等权买入 for i in range(len(to_buy)): max_num = len(to_buy) - i ts_code = to_buy[i] p: SimDailyPrice = context.get_today_price(ts_code) context.buy_amap(ts_code, p.open, context.get_cash() / max_num)
def after_trade(self, context: SimContext, data: SimDataService): price: SimDailyPrice = context.get_today_price(self.__target) s: SimShareHold = context.get_hold(self.__target) if s is not None: assert decimal_utils.equals(price.close, s.get_current_price())
def before_trade(self, context: SimContext, data: SimDataService): price: SimDailyPrice = context.get_today_price(self.__target) s: SimShareHold = context.get_hold(self.__target) if context.is_first_day(): # 这天应该能按开盘价成交 assert self.__buy_order.is_deal() assert decimal_utils.equals(self.__buy_order.get_deal_price(), price.open) charge: Decimal = context.get_charge() pass_t: Decimal = context.get_pass_tax() self.__buy_cost: Decimal = (self.__buy_order.get_num() * self.__buy_order.get_deal_price()) * \ (1 + charge + pass_t) # 手续费计算验证 assert decimal_utils.equals(self.__buy_cost, self.__buy_order.get_deal_cost()) elif context.is_last_day(): # 这天应该能按开盘价成交 assert self.__sell_order.is_deal() assert decimal_utils.equals(self.__sell_order.get_deal_price(), price.open) # 全卖光了 assert s.get_num() == 0 assert s.get_can_sell() == 0 charge: Decimal = context.get_charge() pass_t: Decimal = context.get_pass_tax() tax: Decimal = context.get_tax() self.__sell_cost = (self.__sell_order.get_num() * self.__sell_order.get_deal_price()) * \ (charge + pass_t + tax) # 手续费计算验证 assert decimal_utils.equals(self.__sell_cost, self.__sell_order.get_deal_cost()) earn = self.__sell_order.get_num( ) * self.__sell_order.get_deal_price() assert decimal_utils.equals( context.get_cash(), context.get_init_cash() + earn - self.__buy_order.get_deal_cost() - self.__sell_order.get_deal_cost()) elif s is not None: assert decimal_utils.equals(price.open, s.get_current_price())