def initialize_deal_data(cls, event): # 代码编号相关 # self.trade_id = Empty.EMPTY_STRING.value # 成交编号 # self.instrument = Empty.EMPTY_STRING.value # 合约代码 # self.exchange = Empty.EMPTY_STRING.value # 交易所代码 # self.order_id = Empty.EMPTY_STRING.value # 订单编号 # 成交相关 # self.deal_price = Empty.EMPTY_FLOAT.value # 成交价格 # self.direction = Empty.EMPTY_STRING.value # 成交方向 # self.offset = Empty.EMPTY_STRING.value # 成交开平 # self.deal_volume = Empty.EMPTY_INT.value # 成交数量 # self.deal_time = Empty.EMPTY_STRING.value # 成交时间 if Environment.current_order_data.status == Status.NOT_TRADED.value: Environment.current_deal_data.account_id = Environment.current_order_data.account_id Environment.current_deal_data.trade_id = generate_random_id( topic=ID.DEAL_ID.value) Environment.current_deal_data.instrument = Environment.current_order_data.instrument Environment.current_deal_data.exchange = Environment.current_order_data.exchange Environment.current_deal_data.order_id = Environment.current_order_data.order_id Environment.current_deal_data.deal_price = Environment.current_order_data.order_price Environment.current_deal_data.offset = Environment.current_order_data.offset Environment.current_deal_data.deal_volume = Environment.current_order_data.total_volume Environment.current_deal_data.deal_time = Environment.current_order_data.order_time
def run(self, save_trade_record=False): self.initialize() # 初始化 account_data if self.account: for account in self.account: Environment.current_account_data = AccountData() Environment.current_account_data.account_id = generate_random_id.generate_random_id(account) Environment.current_account_data.total_balance = self.capital[account] Environment.current_account_data.available = self.capital[account] # Environment.logger(Environment.current_account_data.account_id, Environment.current_account_data.available) Environment.bar_account_data_list.append(Environment.current_account_data) # if self.run_mode == RunMode.TRADE.value: # self.end = self._get_data.get_end_time_tag(benchmark=self.benchmark, period=Period.DAILY.value) # 缓存数据开关,和bar_index的计算 if self.period == Period.DAILY.value: self.daily_data_cache = True elif self.period == Period.ONE_MIN.value: self.one_min_data_cache = True # security_list = copy.copy(self.universe) security_list = list(set(security_list)) if self._daily_data_cache: Environment.daily_data = self._get_data.cache_all_stock_data(dividend_type=self.rights_adjustment) Environment.index_daily_data = self._get_data.cache_all_index_data() if self.one_min_data_cache: Environment.one_min_data = self._get_data.cache_all_stock_data(period=Period.ONE_MIN.value) if self.period == Period.DAILY.value: Environment.benchmark_index = [i for i in Environment.index_daily_data['close'][self.benchmark].index if self.start <= i <= self.end] elif self.period == Period.ONE_MIN.value: Environment.benchmark_index = [data_transfer.date_to_millisecond(str(int(i)), '%Y%m%d') for i in Environment.one_min_data['open'].ix[self.benchmark].index if i >= data_transfer.date_str_to_int(self.start)] self.bar_index = 0 while True: try: # Environment.logger(self.time_tag, Environment.benchmark_index) self.time_tag = Environment.benchmark_index[self.bar_index] except IndexError: if self.run_mode == RunMode.BACKTESTING.value: if save_trade_record: run_backtesting_analysis_engine(self) break # elif self.run_mode == RunMode.TRADE.value: # 读取最新tick, 更新最新的分钟或者日线 # if 读取最新tick, 更新最新的分钟或者日线 == done: # daily_data.append(new_day_data) # self.bar_index += 1 # benchmark_index.append(new_day_time_tag) else: run_bar_engine(self)
def order_shares(self, stock_code="", shares=1, price_type=PriceType.LIMIT.value, order_price=None, account_id=""): """下单函数""" # 代码编号相关 Environment.current_order_data.account_id = account_id Environment.current_order_data.order_id = generate_random_id( ID.ORDER_ID.value) stock_code_split = stock_code.split('.') Environment.current_order_data.instrument = stock_code_split[0] Environment.current_order_data.exchange = stock_code_split[1] # 报单相关 Environment.current_order_data.price_type = price_type Environment.current_order_data.order_price = order_price if shares > 0: Environment.current_order_data.offset = Offset.OPEN.value else: Environment.current_order_data.offset = Offset.CLOSE.value # Environment.logger.info("shares < 0"*5, Environment.current_order_data.offset) Environment.current_order_data.total_volume = abs(shares) Environment.current_order_data.deal_volume = 0 Environment.current_order_data.status = Status.NOT_REPORTED.value # CTP相关 Environment.current_order_data.order_time = self._strategy.time_tag for account_data in Environment.bar_account_data_list: if account_data.account_id[:-9] == account_id: Environment.current_order_data.session_id = account_data.account_id run_mission_engine(strategy=self._strategy) if self._strategy.run_mode == RunMode.BACKTESTING.value: if Environment.is_send_order: run_broker_engine(strategy=self._strategy) elif self._strategy.run_mode == RunMode.TRADE.value: """过真实的交易,只做send_order""" #send_order() pass
def run(self, save_trade_record=False): self.initialize() # 初始化 account_data if self.account: for account in self.account: Environment.current_account_data = AccountData() Environment.current_account_data.account_id = generate_random_id.generate_random_id( account) Environment.current_account_data.total_balance = self.capital[ account] Environment.current_account_data.available = self.capital[ account] Environment.bar_account_data_list.append( Environment.current_account_data) if self.run_mode == RunMode.TRADE.value: self.end = self._get_data.get_end_timetag( benchmark=self.benchmark, period=Period.DAILY.value) # 缓存数据开关,和bar_index的计算 if self.period == Period.DAILY.value: self.daily_data_cache = True elif self.period == Period.ONE_MIN.value: self.one_min_data_cache = True stock_list = copy.copy(self.universe) stock_list.append(self.benchmark) stock_list = list(set(stock_list)) if self._daily_data_cache: Environment.daily_data = self._get_data.get_all_market_data( stock_code=stock_list, field=["open", "high", "low", "close", "volumn", "amount"], end=self.end, period=Period.DAILY.value) if self.one_min_data_cache: Environment.one_min_data = self._get_data.get_all_market_data( stock_code=stock_list, field=["open", "high", "low", "close", "volumn", "amount"], end=self.end, period=Period.ONE_MIN.value) if self.period == Period.DAILY.value: Environment.benchmark_index = [ data_transfer.date_to_millisecond(str(int(i)), '%Y%m%d') for i in Environment.daily_data["open"].ix[self.benchmark].index if i >= data_transfer.date_str_to_int(self.start) ] elif self.period == Period.ONE_MIN.value: Environment.benchmark_index = [ data_transfer.date_to_millisecond(str(int(i)), '%Y%m%d') for i in Environment.one_min_data["open"].ix[self.benchmark].index if i >= data_transfer.date_str_to_int(self.start) ] # print(self.benchmark, self.start, self.end, self.period, self.rights_adjustment, self.run_mode) self.bar_index = 0 while True: try: self.timetag = Environment.benchmark_index[self.bar_index] except IndexError: if self.run_mode == RunMode.BACKTESTING.value: if save_trade_record: run_backtesting_analysis_engine(self) break elif self.run_mode == RunMode.TRADE.value: '''读取最新tick, 更新最新的分钟或者日线 if 读取最新tick, 更新最新的分钟或者日线 == done: daily_data.append(new_day_data) self.bar_index += 1 benchmark_index.append(new_day_timetag) ''' pass else: date = int( data_transfer.millisecond_to_date(millisecond=self.timetag, format="%Y%m%d")) run_bar_engine(self) @abstractmethod def initialize(self): pass @abstractmethod def handle_bar(self, event): pass