예제 #1
0
    def evaluate_quote_update(self, context, quote):
        """
        This method is called for every market data tick update on the requested symbols.
        """
        symbol_contexts = context.symbol_contexts[quote.symbol]

        # logging.debug("I'm evaluating the data for %s" % (quote, ))

        if len(symbol_contexts.quotes) > 25:  # i.e. we have enough data
            quoteTimes = [
                time.mktime(o.start_time.timetuple())
                for o in symbol_contexts.quotes
            ]
            closePrices = asarray(symbol_contexts.closes)

            ema_10 = financial.ema(closePrices[-10:], 10)
            ema_25 = financial.ema(closePrices[-15:], 15)
            symbolContext.ema_10.append(ema_10)
            symbolContext.ema_25.append(ema_25)

            if ema_10 > ema_25:
                if context.symbolContexts[quote.symbol].position is False:
                    # create a LONG position
                    logging.debug("Opening position on quote: %s" % (quote, ))
                    context.place_order(
                        Order(quote.symbol,
                              1,
                              Entry(Entry.Type.MARKET),
                              Direction.LONG,
                              stoploss=StopLoss(StopLoss.Type.FIXED, 20),
                              take_profit=25))
                    context.symbol_contexts[quote.symbol].position = True
            else:
                if context.symbol_contexts[quote.symbol].position is True:
                    context.symbol_contexts[quote.symbol].position = False
예제 #2
0
    def evaluate_quote_update(self, context, quote):
        """
        This method is called for every market data tick update on the requested symbols.
        """
        symbol_contexts = context.symbol_contexts[quote.symbol]

        # logging.debug("I'm evaluating the data for %s" % (quote, ))

        if len(symbol_contexts.quotes) > 25:  # i.e. we have enough data
            quoteTimes = [time.mktime(o.start_time.timetuple()) for o in symbol_contexts.quotes]
            closePrices = asarray(symbol_contexts.closes)

            ema_10 = financial.ema(closePrices[-10:], 10)
            ema_25 = financial.ema(closePrices[-15:], 15)
            symbolContext.ema_10.append(ema_10)
            symbolContext.ema_25.append(ema_25)

            if ema_10 > ema_25:
                if context.symbolContexts[quote.symbol].position is False:
                    # create a LONG position
                    logging.debug("Opening position on quote: %s" % (quote,))
                    context.place_order(
                        Order(
                            quote.symbol,
                            1,
                            Entry(Entry.Type.MARKET),
                            Direction.LONG,
                            stoploss=StopLoss(StopLoss.Type.FIXED, 20),
                            take_profit=25,
                        )
                    )
                    context.symbol_contexts[quote.symbol].position = True
            else:
                if context.symbol_contexts[quote.symbol].position is True:
                    context.symbol_contexts[quote.symbol].position = False
예제 #3
0
    def evaluate_quote_update(self, context, quote):
        """
        This method is called for every market data tick update on the requested symbols.
        """
        symbolContext = context.symbol_contexts[quote.symbol]

        # logging.debug("I'm evaluating the data for %s" % (quote, ))

        if len(symbolContext.quotes) > self.emaPeriod:  # i.e. we have enough data
            space = 5

            if quote.start_time.time() > datetime.time(21, 0) or quote.start_time.time() < datetime.time(7, 0):
                # not normal EURUSD active period
                return
            if quote.start_time.weekday() >= 5:
                # it's a weekend
                return
            if quote.start_time.weekday() == 4 and quote.start_time.time() > datetime.time(12, 0):
                # no positions after 12 on Friday
                return

            closePrices = asarray(symbolContext.closes)
            ema = financial.ema(closePrices[-self.emaPeriod:], self.emaPeriod)

            bar_height = symbolContext.high - symbolContext.low
            # bar_is_largest = bar_height > (symbolContext.highs[-1] - symbolContext.lows[-1])
            bar_is_lowest = symbolContext.low < min(list(symbolContext.lows)[-space:-1])
            bar_is_highest = symbolContext.high > max(list(symbolContext.highs)[-space:-1])

            buy_signal = bar_is_lowest and ((min(symbolContext.open, symbolContext.close) - symbolContext.low) * 1.5) >= (symbolContext.high - symbolContext.low)
            sell_signal = bar_is_highest and ((symbolContext.high - max(symbolContext.open, symbolContext.close)) * 1.5) >= (symbolContext.high - symbolContext.low)
            open_positions = list(context.open_positions())

            if quote.close > ema and sell_signal:
                # if context.quote_contexts[quote.symbol].position is False:
                    # create a LONG position
                if len(open_positions) != 0:
                    position = open_positions[0]
                    if position.order.direction is Direction.SHORT:
                        context.close_position(position)
                else:
                    logging.debug("Opening position on quote: %s" % (quote,))
                    context.place_order(Order(quote.symbol, 50, Entry(Entry.Type.MARKET), Direction.SHORT, stoploss=self.stopLoss, take_profit=self.takeProfit))
                # context.quote_contexts[quote.symbol].position = True
            elif quote.close < ema and buy_signal:
                if len(open_positions) != 0:
                    position = open_positions[0]
                    if position.order.direction is Direction.LONG:
                        context.close_position(position)
                else:
                    logging.debug("Opening position on quote: %s" % (quote,))
                    context.place_order(Order(quote.symbol, 50, Entry(Entry.Type.MARKET), Direction.SHORT, stoploss=self.stopLoss, take_profit=self.takeProfit))
예제 #4
0
    def evaluate_quote_update(self, context, quote):
        """
        This method is called for every market data tick update on the requested symbols.
        """
        symbolContext = context.symbol_contexts[quote.symbol]

        # logging.debug("I'm evaluating the data for %s" % (quote, ))

        if len(symbolContext.quotes
               ) > self.emaPeriod:  # i.e. we have enough data
            space = 5

            if quote.start_time.time() > datetime.time(
                    21, 0) or quote.start_time.time() < datetime.time(7, 0):
                # not normal EURUSD active period
                return
            if quote.start_time.weekday() >= 5:
                # it's a weekend
                return
            if quote.start_time.weekday(
            ) == 4 and quote.start_time.time() > datetime.time(12, 0):
                # no positions after 12 on Friday
                return

            closePrices = asarray(symbolContext.closes)
            ema = financial.ema(closePrices[-self.emaPeriod:], self.emaPeriod)

            bar_height = symbolContext.high - symbolContext.low
            # bar_is_largest = bar_height > (symbolContext.highs[-1] - symbolContext.lows[-1])
            bar_is_lowest = symbolContext.low < min(
                list(symbolContext.lows)[-space:-1])
            bar_is_highest = symbolContext.high > max(
                list(symbolContext.highs)[-space:-1])

            buy_signal = bar_is_lowest and (
                (min(symbolContext.open, symbolContext.close) -
                 symbolContext.low) * 1.5) >= (symbolContext.high -
                                               symbolContext.low)
            sell_signal = bar_is_highest and (
                (symbolContext.high -
                 max(symbolContext.open, symbolContext.close)) *
                1.5) >= (symbolContext.high - symbolContext.low)
            open_positions = list(context.open_positions())

            if quote.close > ema and sell_signal:
                # if context.quote_contexts[quote.symbol].position is False:
                # create a LONG position
                if len(open_positions) != 0:
                    position = open_positions[0]
                    if position.order.direction is Direction.SHORT:
                        context.close_position(position)
                else:
                    logging.debug("Opening position on quote: %s" % (quote, ))
                    context.place_order(
                        Order(quote.symbol,
                              50,
                              Entry(Entry.Type.MARKET),
                              Direction.SHORT,
                              stoploss=self.stopLoss,
                              take_profit=self.takeProfit))
                # context.quote_contexts[quote.symbol].position = True
            elif quote.close < ema and buy_signal:
                if len(open_positions) != 0:
                    position = open_positions[0]
                    if position.order.direction is Direction.LONG:
                        context.close_position(position)
                else:
                    logging.debug("Opening position on quote: %s" % (quote, ))
                    context.place_order(
                        Order(quote.symbol,
                              50,
                              Entry(Entry.Type.MARKET),
                              Direction.SHORT,
                              stoploss=self.stopLoss,
                              take_profit=self.takeProfit))