Example #1
0
    def __init__(self, feed, instrument, smaPeriod):
        super(MyStrategy, self).__init__(feed, 1000)
        self.__position = None
        self.__instrument = instrument
        # We'll use adjusted close values instead of regular close values.
        self.setUseAdjustedValues(True)

        ## Add calculators
        self.__calculators = {
            'sma':
            ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod),
            'bol':
            bollinger.BollingerBands(feed[instrument].getPriceDataSeries(),
                                     smaPeriod, 1),
            'ta_dpo':
            technical.EventBasedFilter(feed[instrument].getPriceDataSeries(),
                                       indicators.Ta_dpo(40)),
            'ta_tsi':
            technical.EventBasedFilter(feed[instrument].getPriceDataSeries(),
                                       indicators.Ta_tsi(40)),
            'ta_trix':
            technical.EventBasedFilter(feed[instrument].getPriceDataSeries(),
                                       indicators.Ta_trix(40))
        }

        ## Add indicators
        self.__indicators = {
            'sma':
            lambda: self._MyStrategy__calculators['sma'][-1],
            'bol_low':
            lambda: self._MyStrategy__calculators['bol'].getLowerBand()[-1],
            'bol_mid':
            lambda: self._MyStrategy__calculators['bol'].getMiddleBand()[-1],
            'bol_high':
            lambda: self._MyStrategy__calculators['bol'].getUpperBand()[-1],
            'ta_dpo':
            lambda: self._MyStrategy__calculators['ta_dpo'][-1],
            'ta_tsi':
            lambda: self._MyStrategy__calculators['ta_dpo'][-1],
            'ta_trix':
            lambda: self._MyStrategy__calculators['ta_dpo'][-1]
        }
Example #2
0
from pyalgotrade import dataseries
from pyalgotrade import technical

# An EventWindow is responsible for making calculations using a window of values.
class Accumulator(technical.EventWindow):
	def getValue(self):
		ret = 0
		for value in self.getValues():
			if value != None:
				ret += value
		return ret

# Build a sequence based DataSeries.
seqDS = dataseries.SequenceDataSeries()
# Wrap it with a filter that will get fed as new values get added to the underlying DataSeries.
accum = technical.EventBasedFilter(seqDS, Accumulator(3))

# Put in some values.
for i in range(0, 50):
	seqDS.append(i)

# Get some values.
print accum[0] # Not enough values yet.
print accum[1] # Not enough values yet.
print accum[2] # Ok, now we should have at least 3 values.
print accum[3]

# Get the last value, which should be equal to 49 + 48 + 47.
print accum[-1]

Example #3
0
        bcoeffs = []
        for i in xrange(flen):
            bcoeffs.append((1 - alpha)**i)
        b = np.asarray(bcoeffs).astype(np.float64)
        a = np.asarray([sum(bcoeffs)]).astype(np.float64)
        out = signal.lfilter(b, a, data)
        return out

    def getValue(self):
        ret = None
        if self.windowFull():
            these_vals = self.getValues()
            filt = self.EMAfilter(these_vals, self.getWindowSize(), .5)
            ret = filt[-1]
        return ret


# Build a sequence based DataSeries.
seqDS = dataseries.SequenceDataSeries()
# Wrap it with a filter that will get fed as new values get added to the
# underlying DataSeries.
accum = technical.EventBasedFilter(seqDS, EMA(16))

# Put in some values.
for i in range(0, 50):
    seqDS.append(i)

# Get some values.
for i in xrange(50):
    print accum[i]