Exemplo n.º 1
0
 def create(stg_run_id, init_cash: int, md: dict):
     """
     根据 md 及 初始化资金 创建对象,默认日期为当前md数据-1天
     :param stg_run_id: 
     :param init_cash: 
     :param md: 
     :return: 
     """
     trade_date = str_2_date(md['ActionDay']) - timedelta(days=1)
     trade_time = pd_timedelta_2_timedelta(md['ActionTime'])
     trade_millisec = int(md.setdefault('ActionMillisec', 0))
     trade_price = float(md['close'])
     acc_status_info = AccountStatusInfo(stg_run_id=stg_run_id,
                                         trade_date=trade_date,
                                         trade_time=trade_time,
                                         trade_millisec=trade_millisec,
                                         available_cash=init_cash,
                                         balance_tot=init_cash,
                                         )
     if UPDATE_OR_INSERT_PER_ACTION:
         # 更新最新持仓纪录
         with with_db_session(engine_abat, expire_on_commit=False) as session:
             session.add(acc_status_info)
             session.commit()
     return acc_status_info
Exemplo n.º 2
0
 def create(stg_run_id, init_cash: int, md: dict):
     """
     根据 md 及 初始化资金 创建对象,默认日期为当前md数据-1天
     :param stg_run_id: 
     :param init_cash: 
     :param md: 
     :return: 
     """
     trade_date = str_2_date(md['ActionDay']) - timedelta(days=1)
     trade_time = pd_timedelta_2_timedelta(md['ActionTime'])
     trade_millisec = int(md.setdefault('ActionMillisec', 0))
     trade_price = float(md['close'])
     acc_status_info = AccountStatusInfo(stg_run_id=stg_run_id,
                                         trade_date=trade_date,
                                         trade_time=trade_time,
                                         trade_millisec=trade_millisec,
                                         available_cash=init_cash,
                                         balance_tot=init_cash,
                                         )
     if UPDATE_OR_INSERT_PER_ACTION:
         # 更新最新持仓纪录
         with with_db_session(engine_abat, expire_on_commit=False) as session:
             session.add(acc_status_info)
             session.commit()
     return acc_status_info
Exemplo n.º 3
0
    def update_by_pos_status_info(self, pos_status_info_dic, md: dict):
        """
        根据 持仓列表更新账户信息
        :param pos_status_info_dic: 
        :return: 
        """
        account_status_info = self.create_by_self()
        # 上一次更新日期、时间
        # trade_date_last, trade_time_last, trade_millisec_last = \
        #     account_status_info.trade_date, account_status_info.trade_time, account_status_info.trade_millisec
        # 更新日期、时间
        trade_date = md['ActionDay']
        trade_time = pd_timedelta_2_timedelta(md['ActionTime'])
        trade_millisec = int(md.setdefault('ActionMillisec', 0))

        available_cash_chg = 0
        curr_margin = 0
        close_profit = 0
        position_profit = 0
        floating_pl_chg = 0
        margin_chg = 0
        floating_pl_cum = 0
        for instrument_id, pos_status_info in pos_status_info_dic.items():
            curr_margin += pos_status_info.margin
            if pos_status_info.position == 0:
                close_profit += pos_status_info.floating_pl
            else:
                position_profit += pos_status_info.floating_pl
            floating_pl_chg += pos_status_info.floating_pl_chg
            margin_chg += pos_status_info.margin_chg
            floating_pl_cum += pos_status_info.floating_pl_cum

        available_cash_chg = floating_pl_chg - margin_chg
        account_status_info.curr_margin = curr_margin
        # # 对于同一时间,平仓后又开仓的情况,不能将close_profit重置为0
        # if trade_date == trade_date_last and trade_time == trade_time_last and trade_millisec == trade_millisec_last:
        #     account_status_info.close_profit += close_profit
        # else:
        # 一个单位时段只允许一次,不需要考虑上面的情况
        account_status_info.close_profit = close_profit

        account_status_info.position_profit = position_profit
        account_status_info.available_cash += available_cash_chg
        account_status_info.floating_pl_cum = floating_pl_cum
        account_status_info.balance_tot = account_status_info.available_cash + curr_margin

        account_status_info.trade_date = trade_date
        account_status_info.trade_time = trade_time
        account_status_info.trade_millisec = trade_millisec
        if UPDATE_OR_INSERT_PER_ACTION:
            # 更新最新持仓纪录
            with with_db_session(engine_abat, expire_on_commit=False) as session:
                session.add(account_status_info)
                session.commit()
        return account_status_info
Exemplo n.º 4
0
    def update_by_pos_status_info(self, pos_status_info_dic, md: dict):
        """
        根据 持仓列表更新账户信息
        :param pos_status_info_dic: 
        :return: 
        """
        account_status_info = self.create_by_self()
        # 上一次更新日期、时间
        # trade_date_last, trade_time_last, trade_millisec_last = \
        #     account_status_info.trade_date, account_status_info.trade_time, account_status_info.trade_millisec
        # 更新日期、时间
        trade_date = md['ActionDay']
        trade_time = pd_timedelta_2_timedelta(md['ActionTime'])
        trade_millisec = int(md.setdefault('ActionMillisec', 0))

        available_cash_chg = 0
        curr_margin = 0
        close_profit = 0
        position_profit = 0
        floating_pl_chg = 0
        margin_chg = 0
        floating_pl_cum = 0
        for instrument_id, pos_status_info in pos_status_info_dic.items():
            curr_margin += pos_status_info.margin
            if pos_status_info.position == 0:
                close_profit += pos_status_info.floating_pl
            else:
                position_profit += pos_status_info.floating_pl
            floating_pl_chg += pos_status_info.floating_pl_chg
            margin_chg += pos_status_info.margin_chg
            floating_pl_cum += pos_status_info.floating_pl_cum

        available_cash_chg = floating_pl_chg - margin_chg
        account_status_info.curr_margin = curr_margin
        # # 对于同一时间,平仓后又开仓的情况,不能将close_profit重置为0
        # if trade_date == trade_date_last and trade_time == trade_time_last and trade_millisec == trade_millisec_last:
        #     account_status_info.close_profit += close_profit
        # else:
        # 一个单位时段只允许一次,不需要考虑上面的情况
        account_status_info.close_profit = close_profit

        account_status_info.position_profit = position_profit
        account_status_info.available_cash += available_cash_chg
        account_status_info.floating_pl_cum = floating_pl_cum
        account_status_info.balance_tot = account_status_info.available_cash + curr_margin

        account_status_info.trade_date = trade_date
        account_status_info.trade_time = trade_time
        account_status_info.trade_millisec = trade_millisec
        if UPDATE_OR_INSERT_PER_ACTION:
            # 更新最新持仓纪录
            with with_db_session(engine_abat, expire_on_commit=False) as session:
                session.add(account_status_info)
                session.commit()
        return account_status_info
Exemplo n.º 5
0
 def create_by_dic(order_dic):
     order_info = OrderInfo()
     order_info.order_date = order_dic['TradingDay']
     order_info.order_time = pd_timedelta_2_timedelta(order_dic['InsertTime'])
     order_info.order_millisec = 0
     order_info.direction = Direction.create_by_direction(order_dic['Direction'])
     order_info.action = Action.create_by_offsetflag(order_dic['CombOffsetFlag'])
     order_info.instrument_id = order_dic['InstrumentID']
     order_info.order_price = order_dic['LimitPrice']
     order_info.order_vol = order_dic['VolumeTotalOriginal']
     order_info.traded_vol = order_dic['VolumeTraded']
     order_info.margin = 0
     return order_info
Exemplo n.º 6
0
 def create_by_dic(order_dic):
     order_info = OrderInfo()
     order_info.order_date = order_dic['TradingDay']
     order_info.order_time = pd_timedelta_2_timedelta(order_dic['InsertTime'])
     order_info.order_millisec = 0
     order_info.direction = Direction.create_by_direction(order_dic['Direction'])
     order_info.action = Action.create_by_offsetflag(order_dic['CombOffsetFlag'])
     order_info.instrument_id = order_dic['InstrumentID']
     order_info.order_price = order_dic['LimitPrice']
     order_info.order_vol = order_dic['VolumeTotalOriginal']
     order_info.traded_vol = order_dic['VolumeTraded']
     order_info.margin = 0
     return order_info
Exemplo n.º 7
0
    def update_by_md(self, md: dict):
        """
        创建新的对象,根据 trade_info 更新相关信息
        :param md: 
        :return: 
        """
        trade_date = md['ActionDay']
        trade_time = pd_timedelta_2_timedelta(md['ActionTime'])
        trade_millisec = int(md.setdefault('ActionMillisec', 0))
        trade_price = float(md['close'])
        instrument_id = md['InstrumentID']
        pos_status_info = self.create_by_self()
        pos_status_info.cur_price = trade_price
        pos_status_info.trade_date = trade_date
        pos_status_info.trade_time = trade_time
        pos_status_info.trade_millisec = trade_millisec

        # 计算 floating_pl margin
        # instrument_info = Config.instrument_info_dic[instrument_id]
        # multiple = instrument_info['VolumeMultiple']
        # margin_ratio = instrument_info['LongMarginRatio']
        multiple, margin_ratio = 1, 1
        position = pos_status_info.position
        cur_price = pos_status_info.cur_price
        avg_price = pos_status_info.avg_price
        pos_status_info.margin = position * cur_price * multiple * margin_ratio
        pos_status_info.margin_chg = pos_status_info.margin - self.margin
        if pos_status_info.direction == Direction.Long:
            pos_status_info.floating_pl = (cur_price - avg_price) * position * multiple
        else:
            pos_status_info.floating_pl = (avg_price - cur_price) * position * multiple
        pos_status_info.floating_pl_chg = pos_status_info.floating_pl - self.floating_pl
        pos_status_info.floating_pl_cum += pos_status_info.floating_pl_chg

        if UPDATE_OR_INSERT_PER_ACTION:
            # 更新最新持仓纪录
            with with_db_session(engine_abat, expire_on_commit=False) as session:
                session.add(pos_status_info)
                session.commit()
        return pos_status_info
Exemplo n.º 8
0
    def update_by_md(self, md: dict):
        """
        创建新的对象,根据 trade_info 更新相关信息
        :param md: 
        :return: 
        """
        trade_date = md['ActionDay']
        trade_time = pd_timedelta_2_timedelta(md['ActionTime'])
        trade_millisec = int(md.setdefault('ActionMillisec', 0))
        trade_price = float(md['close'])
        instrument_id = md['InstrumentID']
        pos_status_info = self.create_by_self()
        pos_status_info.cur_price = trade_price
        pos_status_info.trade_date = trade_date
        pos_status_info.trade_time = trade_time
        pos_status_info.trade_millisec = trade_millisec

        # 计算 floating_pl margin
        # instrument_info = Config.instrument_info_dic[instrument_id]
        # multiple = instrument_info['VolumeMultiple']
        # margin_ratio = instrument_info['LongMarginRatio']
        multiple, margin_ratio = 1, 1
        position = pos_status_info.position
        cur_price = pos_status_info.cur_price
        avg_price = pos_status_info.avg_price
        pos_status_info.margin = position * cur_price * multiple * margin_ratio
        pos_status_info.margin_chg = pos_status_info.margin - self.margin
        if pos_status_info.direction == Direction.Long:
            pos_status_info.floating_pl = (cur_price - avg_price) * position * multiple
        else:
            pos_status_info.floating_pl = (avg_price - cur_price) * position * multiple
        pos_status_info.floating_pl_chg = pos_status_info.floating_pl - self.floating_pl
        pos_status_info.floating_pl_cum += pos_status_info.floating_pl_chg

        if UPDATE_OR_INSERT_PER_ACTION:
            # 更新最新持仓纪录
            with with_db_session(engine_abat, expire_on_commit=False) as session:
                session.add(pos_status_info)
                session.commit()
        return pos_status_info