コード例 #1
0
ファイル: fillstrategy.py プロジェクト: Chin-I/pyalgotrade
    def fillMarketOrder(self, broker_, order, bar):
        # Calculate the fill size for the order.
        fillSize = self.__calculateFillSize(broker_, order, bar)
        if fillSize == 0:
            broker_.getLogger().debug(
                "Not enough volume to fill %s market order [%s] for %s share/s" % (
                    order.getInstrument(),
                    order.getId(),
                    order.getRemaining()
                )
            )
            return None

        # Unless its a fill-on-close order, use the open price.
        if order.getFillOnClose():
            price = bar.getClose(broker_.getUseAdjustedValues())
        else:
            price = bar.getOpen(broker_.getUseAdjustedValues())
        assert price is not None

        # Don't slip prices when the bar represents the trading activity of a single trade.
        if bar.getFrequency() != pyalgotrade.bar.Frequency.TRADE:
            price = self.__slippageModel.calculatePrice(
                order, price, fillSize, bar, self.__volumeUsed[order.getInstrument()]
            )
        return FillInfo(price, fillSize)
コード例 #2
0
    def onBars(self, bars):
        if self.__instrument not in bars or len(
                self.resampled_bars) < self.maxLength * 3 or 'old' in bars[
                    self.__instrument].getExtraColumns() or not self.running:
            return

        bar = bars[self.__instrument]

        if bar.getFrequency() == self._resampledBF.getFrequency():
            super().log_orderfill(self._resampledBF.getFrequency())

            # prices_high = bar_ds_high_to_numpy(self.resampled_bars, self.lb)
            # prices_low = bar_ds_low_to_numpy(self.resampled_bars, self.lb)
            #
            # ph = np.max(prices_high)
            # pl = np.min(prices_low)

            prices_close = bar_ds_close_to_numpy(self.resampled_bars, self.lb)
            ph = np.min(prices_close)
            pl = np.max(prices_close)

            self.entry("Long", self.__instrument, self.long, self.__posSize,
                       ph - self.i)
            self.entry("Lng", self.__instrument, not self.long, self.__posSize,
                       pl + self.i)
コード例 #3
0
ファイル: tickFillStrategy.py プロジェクト: zzprice/xuefu
    def fillMarketOrder(self, broker_, order, bar):
        # Calculate the fill size for the order.
        # redefined
        fillSize = self.__calculateFillSize(broker_, order, bar)
        if fillSize == 0:
            broker_.getLogger().debug(
                "Not enough volume to fill %s market order [%s] for %s share/s"
                % (order.getInstrument(), order.getId(), order.getRemaining()))
            return None

        # here fill on close is changed its meaning ,it means whether use the opposite value
        if order.getFillOnClose():
            if order.getAction() == pyalgotrade.broker.Order.Action.BUY:
                price = bar.getAp()
            else:  #sell short
                price = bar.getBp()
        else:
            if order.getAction(
            ) == pyalgotrade.broker.Order.Action.BUY:  #not use the opposite value
                price = bar.getBp()
            else:
                price = bar.getAp()
        assert price is not None

        # Don't slip prices when the bar represents the trading activity of a single trade.
        if bar.getFrequency() != pyalgotrade.bar.Frequency.TRADE:
            price = self.__slippageModel.calculatePrice(
                order, price, fillSize, bar,
                self.__volumeUsed[order.getInstrument()])
        return FillInfo(price, fillSize)
コード例 #4
0
    def fillMarketOrder(self, broker_, order, bar):
        broker_.getLogger().debug('fillMarketOrder')
        # Calculate the fill size for the order.
        fillSize = self.__calculateFillSize(broker_, order, bar)
        if fillSize == 0:
            broker_.getLogger().debug(
                "Not enough volume to fill %s market order [%s] for %s share/s"
                % (order.getInstrument(), order.getId(), order.getRemaining()))
            return None

        # Unless its a fill-on-close order, use the open price.
        if order.getFillOnClose():
            broker_.getLogger().debug('fillMarketOrder -> getFillOnClose()')
            price = bar.getClose(broker_.getUseAdjustedValues())
        else:
            broker_.getLogger().debug('fillMarketOrder -> getFillOnOpen()')
            price = bar.getOpen(broker_.getUseAdjustedValues())
        assert price is not None

        # Don't slip prices when the bar represents the trading activity of a single trade.
        if bar.getFrequency() != pyalgotrade.bar.Frequency.TRADE:
            price = self.__slippageModel.calculatePrice(
                order, price, fillSize, bar,
                self.__volumeUsed[order.getInstrument()])
        broker_.getLogger().debug('fillMarketOrder, return FillInfo')
        return FillInfo(price, fillSize)
コード例 #5
0
ファイル: backtesting.py プロジェクト: arippbbc/pyalgotrade
 def onBars(self, dateTime, bars):
     # Update the volume available for each instrument.
     volumeLeft = {}
     for instrument in bars.getInstruments():
         bar = bars[instrument]
         if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
             volumeLeft[instrument] = bar.getVolume()
         elif self.__volumeLimit is not None:
             volumeLeft[instrument] = bar.getVolume() * self.__volumeLimit
     self.__volumeLeft = volumeLeft
コード例 #6
0
    def onBars(self, broker_, bars):
        volumeLeft = {}

        for instrument in bars.getInstruments():
            bar = bars[instrument]
            # Update the volume available for each instrument.
            if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
                volumeLeft[instrument] = bar.getVolume()
            elif self.__volumeLimit is not None:
                volumeLeft[instrument] = bar.getVolume() * self.__volumeLimit
            # Update the volume used for each instrument.
            self.__volumeUsed[instrument] = 0.0

        self.__volumeLeft = volumeLeft
コード例 #7
0
ファイル: fillstrategy.py プロジェクト: Chin-I/pyalgotrade
    def onBars(self, broker_, bars):
        volumeLeft = {}

        for instrument in bars.getInstruments():
            bar = bars[instrument]
            # Reset the volume available for each instrument.
            if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
                volumeLeft[instrument] = bar.getVolume()
            elif self.__volumeLimit is not None:
                # We can't round here because there is no order to request the instrument traits.
                volumeLeft[instrument] = bar.getVolume() * self.__volumeLimit
            # Reset the volume used for each instrument.
            self.__volumeUsed[instrument] = 0.0

        self.__volumeLeft = volumeLeft
コード例 #8
0
ファイル: fillstrategy.py プロジェクト: immm007/TSystem
    def onBars(self, broker_, bars):
        volumeLeft = {}

        for instrument in bars.getInstruments():
            bar = bars[instrument]
            # Reset the volume available for each instrument.
            if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
                volumeLeft[instrument] = bar.getVolume()
            elif self.__volumeLimit is not None:
                # We can't round here because there is no order to request the instrument traits.
                volumeLeft[instrument] = bar.getVolume() * self.__volumeLimit
            # Reset the volume used for each instrument.
            self.__volumeUsed[instrument] = 0.0

        self.__volumeLeft = volumeLeft
コード例 #9
0
 def onBarsFrequency(self, dt, bars):
     #这里需要划分交易所
     try:
         k = []
         Frequency = None
         for key in bars.keys():
             bar = bars[key]
             Frequency = bar.getFrequency()
             
             #更新self.__kline
             k = self._kline[Frequency].get(key)
             
         
     except Exception as e: 
         logging.error('%r', e)
コード例 #10
0
ファイル: backtesting.py プロジェクト: harsha550/pyalgotrade
    def __getFillSize(self, order, bar):
        if self.__volumeLimit is not None:
            # If these are trade bars, then allow the whole volume to be used.
            if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
                maxQuantity = bar.getVolume()
            else:
                maxQuantity = bar.getVolume() * self.__volumeLimit

            if order.getQuantity() > maxQuantity:
                # Partial fills not supported yet.
                ret = None
            else:
                ret = order.getQuantity()
        else:
            ret = order.getQuantity()
        return ret
コード例 #11
0
    def onBars(self, broker_, bars):
        self.__volumeUsed = {}
        self.__volumeLeft = {}

        # Reset volumes.
        for bar in bars.getBars():
            instrument = bar.getInstrument()

            self.__volumeUsed[instrument] = 0.0

            if self.__volumeLimit is not None:
                # For TRADE bars we can use all the volume available.
                if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
                    volumeLeft = bar.getVolume()
                else:
                    volumeLeft = self.__instrumentTraits.round(
                        bar.getVolume() * self.__volumeLimit,
                        bar.getInstrument().symbol,
                        roundDown=True)
                self.__volumeLeft[instrument] = volumeLeft
コード例 #12
0
ファイル: fillstrategy.py プロジェクト: Chin-I/pyalgotrade
    def fillStopOrder(self, broker_, order, bar):
        ret = None

        # First check if the stop price was hit so the market order becomes active.
        stopPriceTrigger = None
        if not order.getStopHit():
            stopPriceTrigger = get_stop_price_trigger(
                order.getAction(),
                order.getStopPrice(),
                broker_.getUseAdjustedValues(),
                bar
            )
            order.setStopHit(stopPriceTrigger is not None)

        # If the stop price was hit, check if we can fill the market order.
        if order.getStopHit():
            # Calculate the fill size for the order.
            fillSize = self.__calculateFillSize(broker_, order, bar)
            if fillSize == 0:
                broker_.getLogger().debug("Not enough volume to fill %s stop order [%s] for %s share/s" % (
                    order.getInstrument(),
                    order.getId(),
                    order.getRemaining()
                ))
                return None

            # If we just hit the stop price we'll use it as the fill price.
            # For the remaining bars we'll use the open price.
            if stopPriceTrigger is not None:
                price = stopPriceTrigger
            else:
                price = bar.getOpen(broker_.getUseAdjustedValues())
            assert price is not None

            # Don't slip prices when the bar represents the trading activity of a single trade.
            if bar.getFrequency() != pyalgotrade.bar.Frequency.TRADE:
                price = self.__slippageModel.calculatePrice(
                    order, price, fillSize, bar, self.__volumeUsed[order.getInstrument()]
                )
            ret = FillInfo(price, fillSize)
        return ret
コード例 #13
0
    def fillStopOrder(self, broker_, order, bar):
        ret = None

        # First check if the stop price was hit so the market order becomes active.
        stopPriceTrigger = None
        if not order.getStopHit():
            stopPriceTrigger = get_stop_price_trigger(
                order.getAction(),
                order.getStopPrice(),
                broker_.getUseAdjustedValues(),
                bar
            )
            order.setStopHit(stopPriceTrigger is not None)

        # If the stop price was hit, check if we can fill the market order.
        if order.getStopHit():
            # Calculate the fill size for the order.
            fillSize = self.__calculateFillSize(broker_, order, bar)
            if fillSize == 0:
                broker_.getLogger().debug("Not enough volume to fill %s stop order [%s] for %s share/s" % (
                    order.getInstrument(),
                    order.getId(),
                    order.getRemaining()
                ))
                return None

            # If we just hit the stop price we'll use it as the fill price.
            # For the remaining bars we'll use the open price.
            if stopPriceTrigger is not None:
                price = stopPriceTrigger
            else:
                price = bar.getOpen(broker_.getUseAdjustedValues())
            assert price is not None

            # Don't slip prices when the bar represents the trading activity of a single trade.
            if bar.getFrequency() != pyalgotrade.bar.Frequency.TRADE:
                price = self.__slippageModel.calculatePrice(
                    order, price, fillSize, bar, self.__volumeUsed[order.getInstrument()]
                )
            ret = FillInfo(price, fillSize)
        return ret