def __init__(self, feed, fastSMA, slowSMA): strategy.BacktestingStrategy.__init__(self, feed, 1000) ds = feed["orcl"].getPriceDataSeries() self.__fastSMADS = ma.SMA(ds, fastSMA) self.__slowSMADS = ma.SMA(ds, slowSMA) self.__longPos = None self.__shortPos = None self.__finalValue = None
def __init__(self, feed, smaPeriod): strategy.BacktestingStrategy.__init__(self, feed) assert (smaPeriod > 3) self.__lead = "^n225" self.__lag = "spy" self.__adjClose = feed[self.__lead].getAdjCloseDataSeries() # Exit signal is more sensitive than entry. self.__fastSMA = ma.SMA(self.__adjClose, int(smaPeriod / 2)) self.__slowSMA = ma.SMA(self.__adjClose, smaPeriod) self.__pos = None
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)
def __init__(self, feed, instrument, entrySMA, exitSMA, rsiPeriod, overBoughtThreshold, overSoldThreshold): super(RSI2, self).__init__(feed) self.__instrument = instrument # We'll use adjusted close values, if available, instead of regular close values. if feed.barsHaveAdjClose(): self.setUseAdjustedValues(True) self.__priceDS = feed[instrument].getPriceDataSeries() self.__entrySMA = ma.SMA(self.__priceDS, entrySMA) self.__exitSMA = ma.SMA(self.__priceDS, exitSMA) self.__rsi = rsi.RSI(self.__priceDS, rsiPeriod) self.__overBoughtThreshold = overBoughtThreshold self.__overSoldThreshold = overSoldThreshold self.__longPos = None self.__shortPos = None
def __init__(self, feed, instrument, smaPeriod): super(SMACrossOver, self).__init__(feed) self.__instrument = instrument self.__position = None # We'll use adjusted close values instead of regular close values. self.setUseAdjustedValues(True) self.__prices = feed[instrument].getPriceDataSeries() self.__sma = ma.SMA(self.__prices, smaPeriod)
def __init__(self, dataSeries, period, numStdDev, maxLen=None): self.__sma = ma.SMA(dataSeries, period, maxLen=maxLen) self.__stdDev = stats.StdDev(dataSeries, period, maxLen=maxLen) self.__upperBand = dataseries.SequenceDataSeries(maxLen) self.__lowerBand = dataseries.SequenceDataSeries(maxLen) self.__numStdDev = numStdDev # It is important to subscribe after sma and stddev since we'll use those values. dataSeries.getNewValueEvent().subscribe(self.__onNewValue)
def testStockChartsSMA_BoundedSeq(self): # Test data from http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages common.test_from_csv(self, "sc-sma-10.csv", lambda inputDS: ma.SMA(inputDS, 10), maxLen=1) common.test_from_csv(self, "sc-sma-10.csv", lambda inputDS: ma.SMA(inputDS, 10), maxLen=2) common.test_from_csv(self, "sc-sma-10.csv", lambda inputDS: ma.SMA(inputDS, 10), maxLen=4) common.test_from_csv(self, "sc-sma-10.csv", lambda inputDS: ma.SMA(inputDS, 10), maxLen=1000)
def testEventWindow(self): ds = dataseries.SequenceDataSeries() smaEW = ma.SMAEventWindow(10) sma = ma.SMA(ds, 10) smaEW.onNewValue(None, None) # This value should get skipped for i in xrange(100): ds.append(i) smaEW.onNewValue(None, i) self.assertEqual(sma[-1], smaEW.getValue()) smaEW.onNewValue(None, None) # This value should get skipped
def __init__(self, feed, instrumentsByClass, initialCash): super(MarketTiming, self).__init__(feed, initialCash) self.setUseAdjustedValues(True) self.__instrumentsByClass = instrumentsByClass self.__rebalanceMonth = None self.__sharesToBuy = {} # Initialize indicators for each instrument. self.__sma = {} for assetClass in instrumentsByClass: for instrument in instrumentsByClass[assetClass]: priceDS = feed[instrument].getPriceDataSeries() self.__sma[instrument] = ma.SMA(priceDS, 200)
def __init__(self, feed, brk): super(Strategy, self).__init__(feed, brk) smaPeriod = 20 self.__instrument = "BTC" self.__prices = feed[self.__instrument].getCloseDataSeries() self.__sma = ma.SMA(self.__prices, smaPeriod) self.__bid = None self.__ask = None self.__position = None self.__posSize = 0.05 # Subscribe to order book update events to get bid/ask prices to trade. feed.getOrderBookUpdateEvent().subscribe(self.__onOrderBookUpdate)
def __init__(self, barDataSeries, period, dSMAPeriod=3, useAdjustedValues=False, maxLen=None): assert dSMAPeriod > 1, "dSMAPeriod must be > 1" assert isinstance(barDataSeries, bards.BarDataSeries), \ "barDataSeries must be a dataseries.bards.BarDataSeries instance" super(StochasticOscillator, self).__init__(barDataSeries, SOEventWindow(period, useAdjustedValues), maxLen) self.__d = ma.SMA(self, dSMAPeriod, maxLen)
def __init__(self, feed): super(BuyOnGap, self).__init__() stdDevPeriod = 90 smaPeriod = 20 self.__returns = {} self.__stdDev = {} self.__ma = {} for instrument in feed.getRegisteredInstruments(): priceDS = feed[instrument].getAdjCloseDataSeries() # Returns over the adjusted close values. self.__returns[instrument] = roc.RateOfChange(priceDS, 1) # StdDev over those returns. self.__stdDev[instrument] = stats.StdDev( self.__returns[instrument], stdDevPeriod) # MA over the adjusted close values. self.__ma[instrument] = ma.SMA(priceDS, smaPeriod)
def __init__(self, feed, instrument): super(MyStrategy, self).__init__(feed) self.__rsi = rsi.RSI(feed[instrument].getCloseDataSeries(), 14) self.__sma = ma.SMA(self.__rsi, 15) self.__instrument = instrument
def __buildSMA(self, period, values, smaMaxLen=None): seqDs = dataseries.SequenceDataSeries() ret = ma.SMA(seqDs, period, smaMaxLen) for value in values: seqDs.append(value) return ret
def testNinjaTraderSMA(self): common.test_from_csv(self, "nt-sma-15.csv", lambda inputDS: ma.SMA(inputDS, 15), 3)
def __init__(self, feed, instrument): super(MyStrategy, self).__init__(feed) # We want a 15 period SMA over the closing prices. self.__sma = ma.SMA(feed[instrument].getCloseDataSeries(), 15) self.__instrument = instrument