Ejemplo n.º 1
0
    def cover(self, code: str, price: float, volume: float) -> bool:
        """
        做空股票:平仓
        """
        if volume < 0.1:
            print(f"     ==> cover {code} fail,volume = 0")
            return False

        shortPos = self.getShortPosition(code)
        hasVolume = shortPos.getPosAvailable() / self.a_hand_size

        if (hasVolume < volume):
            print(f"     <== cover {code} fail,仓位不够:需要:{volume},可用{hasVolume}")
            return False
        symbol = utils.to_vt_symbol(code)
        self.__initTrade(code, symbol)
        vt_order_ids = self.strategy.cover(symbol, price, volume, False)

        ###冻结仓位,今天不可以再交易
        lockingDagta = PortfolioImpl.TradingLocking(order_id=vt_order_ids[0], code=code)
        lockingDagta.lock_pos = volume * self.a_hand_size
        shortPos.pos_lock += volume * self.a_hand_size
        self.__locking_data[lockingDagta.order_id] = lockingDagta

        return True
Ejemplo n.º 2
0
    def _on_today_end(self,bars: Dict[str, BarData]):
        self.cancel_all_order()

        code_ids = list(self.__daylyTradeCodeSet.keys())
        for code in code_ids:
            bar = self.market.getRealTime().getKBar(code)
            if (not bar is None):
                symbol = utils.to_vt_symbol(code)
                bars[symbol] = bar

        ##所有的订单都得处理完
        assert len(self.engine.active_limit_orders) == 0
        self.__daylyTradeCodeSet.clear()
        self.__locking_data.clear()
Ejemplo n.º 3
0
    def short(self, code: str, price: float, volume: float) -> bool:
        """
          做空股票:开仓
        """
        if volume < 0.1:
            print(f"     ==> short {code} fail,volume = 0")
            return False

        need_capital = self._compute_commission(price, volume) + price * volume * self.a_hand_size
        if (need_capital >= self.valid_captical):
            print(f"     ==> short {code} fail,可用资金不够:需要:{need_capital},可用{self.valid_captical}")
            return False
        symbol = utils.to_vt_symbol(code)
        self.__initTrade(code, symbol)
        self.valid_captical = self.valid_captical - need_capital
        vt_order_ids = self.strategy.short(symbol, price, volume, False)

        ###冻结资金
        lockingDagta = PortfolioImpl.TradingLocking(order_id=vt_order_ids[0], code=code)
        lockingDagta.lock_price = need_capital
        self.__locking_data[lockingDagta.order_id] = lockingDagta
        return True