Exemplo n.º 1
0
 def testCrossAboveWithSMA(self):
     ds1 = dataseries.SequenceDataSeries()
     ds2 = dataseries.SequenceDataSeries()
     sma1 = ma.SMA(ds1, 15)
     sma2 = ma.SMA(ds2, 25)
     for i in range(100):
         ds1.append(i)
         ds2.append(50)
         if i == 58:
             self.assertEqual(cross.cross_above(sma1[:], sma2[:], -2, None),
                              1)
         else:
             self.assertEqual(cross.cross_above(sma1[:], sma2[:], -2, None),
                              0)
Exemplo n.º 2
0
 def onBars(self, bars):
     # If a position was not opened, check if we should enter a long position.
     if self.__position is None:
         if cross.cross_above(self.__prices, self.__sma) > 0:
             shares = int(self.getBroker().getCash() * 0.9 / bars[self.__instrument].getPrice())
             # Enter a buy market order. The order is good till canceled.
             self.__position = self.enterLong(self.__instrument, shares, True)
     # Check if we have to exit the position.
     elif not self.__position.exitActive() and cross.cross_below(self.__prices, self.__sma) > 0:
         self.__position.exitMarket()
Exemplo n.º 3
0
 def onBars(self, bars):
     if bars.getBar(self.__lead):
         if cross.cross_above(self.__adjClose,
                              self.__slowSMA) == 1 and self.__pos is None:
             shares = self.__calculatePosSize()
             if shares:
                 self.__pos = self.enterLong(self.__lag, shares)
         elif cross.cross_below(
                 self.__adjClose,
                 self.__fastSMA) == 1 and self.__pos is not None:
             self.__pos.exitMarket()
Exemplo n.º 4
0
    def testCrossAboveMany(self):
        count = 100
        values1 = [-1 if i % 2 == 0 else 1 for i in range(count)]
        values2 = [0 for i in range(count)]

        # Check first value
        self.assertEqual(cross.cross_above(values1, values2, 0, 0), 0)

        # Check every 2 values.
        period = 2
        for i in range(1, count):
            if i % 2 == 0:
                self.assertEqual(
                    cross.cross_above(values1, values2, i - period + 1, i + 1),
                    0)
            else:
                self.assertEqual(
                    cross.cross_above(values1, values2, i - period + 1, i + 1),
                    1)

        # Check every 4 values.
        period = 4
        for i in range(3, count):
            if i % 2 == 0:
                self.assertEqual(
                    cross.cross_above(values1, values2, i - period + 1, i + 1),
                    1)
            else:
                self.assertEqual(
                    cross.cross_above(values1, values2, i - period + 1, i + 1),
                    2)

        # Check for all values.
        self.assertEqual(cross.cross_above(values1, values2, 0, count),
                         count / 2)
Exemplo n.º 5
0
    def onBars(self, bars):
        bar = bars.getBar("orcl")
        self.printDebug("%s: O=%s H=%s L=%s C=%s" % (bar.getDateTime(), bar.getOpen(), bar.getHigh(), bar.getLow(), bar.getClose()))

        if cross.cross_above(self.__fastSMADS, self.__slowSMADS) == 1:
            if self.__shortPos:
                self.exitShortPosition(bars, self.__shortPos)
            assert(self.__longPos is None)
            self.__longPos = self.enterLongPosition(bars)
        elif cross.cross_below(self.__fastSMADS, self.__slowSMADS) == 1:
            if self.__longPos:
                self.exitLongPosition(bars, self.__longPos)
            assert(self.__shortPos is None)
            self.__shortPos = self.enterShortPosition(bars)
Exemplo n.º 6
0
    def onBars(self, bars):
        bar = bars[self.__instrument]
        self.info("Price: %s. Volume: %s." % (bar.getClose(), bar.getVolume()))

        # Wait until we get the current bid/ask prices.
        if self.__ask is None:
            return

        # If a position was not opened, check if we should enter a long position.
        if self.__position is None:
            if cross.cross_above(self.__prices, self.__sma) > 0:
                self.info("Entry signal. Buy at %s" % (self.__ask))
                self.__position = self.enterLongLimit(self.__instrument,
                                                      self.__ask,
                                                      self.__posSize, True)
        # Check if we have to close the position.
        elif not self.__position.exitActive() and cross.cross_below(
                self.__prices, self.__sma) > 0:
            self.info("Exit signal. Sell at %s" % (self.__bid))
            self.__position.exitLimit(self.__bid)
Exemplo n.º 7
0
 def exitLongSignal(self):
     return cross.cross_above(
         self.__priceDS,
         self.__exitSMA) and not self.__longPos.exitActive()
Exemplo n.º 8
0
    def testCrossAboveOnce(self):
        values1 = self.__buildSeqDS([1, 1, 1, 10, 1, 1, 1])
        values2 = self.__buildSeqDS([2, 2, 2, 2, 2, 2, 2])

        # Check every 2 values.
        self.assertEqual(cross.cross_above(values1, values2, 0, 2), 0)
        self.assertEqual(cross.cross_above(values1, values2, 1, 3), 0)
        self.assertEqual(cross.cross_above(values1, values2, 2, 4), 1)
        self.assertEqual(cross.cross_above(values1, values2, 3, 5), 0)
        self.assertEqual(cross.cross_above(values1, values2, 4, 6), 0)
        self.assertEqual(cross.cross_above(values1, values2, 5, 7), 0)

        # Check every 3 values.
        self.assertEqual(cross.cross_above(values1, values2, 0, 3), 0)
        self.assertEqual(cross.cross_above(values1, values2, 1, 4), 1)
        self.assertEqual(cross.cross_above(values1, values2, 2, 5), 1)
        self.assertEqual(cross.cross_above(values1, values2, 3, 6), 0)
        self.assertEqual(cross.cross_above(values1, values2, 4, 7), 0)

        # Check for all values.
        self.assertEqual(cross.cross_above(values1, values2, 0, 7), 1)
        self.assertEqual(cross.cross_above(values1, values2, 0, -1), 1)
Exemplo n.º 9
0
 def testWithLists(self):
     self.assertEqual(cross.cross_above([1, 2], [1, 1], -2), 0)
     self.assertEqual(cross.cross_above([0, 1, 2], [1, 1, 1], -3), 1)
     self.assertEqual(cross.cross_above([0, 0, 0, 1, 2], [1, 1, 1], -3), 1)
     self.assertEqual(cross.cross_above([0, 0, 0, 1, 2], [1, 1], -3), 0)
     self.assertEqual(cross.cross_above([0, 0, 0, 0, 2], [1, 1], -3), 1)