Example #1
0
    def getRSI(self, cretenInterval, symbol, period):
        self.setupIndMap(symbol, cretenInterval)
        indKey = self.getIndKey('RSI', period)
        if not indKey in self.indMap[symbol][cretenInterval]:
            self.indMap[symbol][cretenInterval][indKey] = RSI(
                period, self.getCandles(symbol, cretenInterval))

        return self.indMap[symbol][cretenInterval][indKey]
Example #2
0
    def set_indicators(self):
        macd = MACD(fast_period=12, slow_period=26, signal_period=9)
        rsi = RSI(n=14)
        bb = BollingerBands(n=20)
        atr = ATR(n=14)
        adx = ADX(n=14)
        obv = OBV(n=14)

        self.indicators = [bb, macd, rsi, atr, adx, obv]
Example #3
0
    def __init__(self,
                 rsiPeriod,
                 stochPeriod,
                 smoothingPeriodK,
                 smoothingPeriodD,
                 timeSeries=None):
        super(StochRSI, self).__init__()

        self.stochPeriod = stochPeriod
        self.smoothingPeriodK = smoothingPeriodK
        self.smoothingPeriodD = smoothingPeriodD
        self.valuesK = []
        self.valuesSlowK = []
        self.valuesD = []
        self.rsi = RSI(rsiPeriod)

        self.addSubIndicator(self.rsi)

        self.initialize(timeSeries)
Example #4
0
    def __init__(self):
        # Regression()

        ## Fractals
        self.long_fractal = Fractal(left_shift=10,
                                    right_shift=long_fractal_shift,
                                    plot=True)
        self.short_fractal = Fractal(
            marker="$-$",
            marker_color="black",
            left_shift=2,
            right_shift=short_fractal_shift,
            plot=True,
        )
        # ## Signals
        l = Internals(self.data,
                      self.long_fractal,
                      shift=long_fractal_shift,
                      threshold=threshold)
        s = Internals(
            self.data,
            self.short_fractal,
            shift=short_fractal_shift,
            threshold=threshold,
        )
        self.Internals = InternalsSum(s, l, threshold=threshold)
        #
        self.rsi = RSI(self.data, self.short_fractal)
        self.macd = MACD(self.data, self.short_fractal)
        self.willy = Willy(self.data, self.short_fractal)
        #
        self.Location = WilliamSignal(self.data, WilliamAlligator(), plot=True)
        #
        self.structure_fractal = Fractal(marker="$-$",
                                         marker_color="orange",
                                         left_shift=30,
                                         right_shift=4,
                                         plot=True)
        self.Structure = Structure(self.data,
                                   self.structure_fractal,
                                   shift=2,
                                   plot=True)
        #
        ## Direction
        self.Direction = Direction(self.data, self.Structure, self.Internals,
                                   self.Location)

        self.Trend = Trend(self.data, self.Direction)
        #
        super(EducatedGambler, self).__init__()
Example #5
0
    def __init__(self):
        (d, f, s) = (self.data, self.data1, self.p.shift)
        rsi = RSI(d, f, shift=s)
        macd = MACD(d, f, shift=s)
        willy = Willy(d, f, shift=s)

        self.rsi = rsi.l.signal
        self.macd = macd.l.signal
        self.willy = willy.l.signal

        self.rsi_divergence = rsi.l.divergence
        self.willy_divergence = willy.l.divergence

        (self.ll, self.hh, self.shift) = (0, 0, -1 * self.p.shift)
Example #6
0
def test_indicator(horizon=5,
                   test_one_indicator=True,
                   verbose=False,
                   plotting=False):
    fhand = pd.read_csv("spy_list.csv")
    spy_list = list(fhand.Symbols)
    spy_length = len(spy_list)
    use_prices = False

    # Get Indicators
    if test_one_indicator:
        upper_length = 1
        opt_var = range(2, 21)
        indicators = [[SimpleMA(i)] for i in opt_var]
    else:
        indicators = [
            Bollinger(4),
            Bollinger(5),
            Bollinger(19),
            ExponentialMA(2),
            ExponentialMA(4),
            Lag(1),
            Lag(3),
            Momentum(2),
            Momentum(3),
            SimpleMA(4),
            SimpleMA(10),
            RSI(10),
            Volatility(5),
            Weekdays()
        ]
        for i in indicators:
            print i.name
        empty_list = []
        upper_length = 5
        for j in range(1, upper_length + 1):
            for i in combinations(indicators, j):
                empty_list.append(list(i))
        indicators = empty_list
        opt_var = range(len(indicators))


##                opt_var = [[ind.name for ind in indicator] for indicator in indicators]

# create a learner and train it
    learner = lrl.LinRegLearner()

    best_ind_dict = {}
    all_ind_dict = {}

    for i, indicator in enumerate(indicators):
        # Collect scoring metrics for each learner for later comparison
        cors, rmsestrain, rmsestest = [], [], []
        if use_prices: mapestrain, mapestest = [], []

        best_rmse = np.inf
        for symbol in spy_list:
            # Get stock data
            filename = "webdata/{}.csv".format(symbol)
            try:
                try:
                    predicted, c, train_cor, train_mape, test_mape, train_rmse, test_rmse = run_test(
                        symbol, indicator, learner)
                except ValueError:
                    spy_length -= 1
                    continue
                if test_rmse < best_rmse:
                    best_rmse = test_rmse
                    best_indicator_set = ", ".join(
                        [ind.name for ind in indicator])
                indicator_set = ", ".join([ind.name for ind in indicator])
                if test_rmse < 4.0:
                    all_ind_dict[indicator_set] = all_ind_dict.get(
                        indicator_set, 0.0) + test_rmse
                rmsestrain.append(train_rmse)
                rmsestest.append(test_rmse)
                if use_prices:
                    mapestrain.append(train_mape)
                    mapestest.append(test_mape)
                cors.append(c)
            except Exception, e:
                print str(e)

        if len(indicators) > 4 and plotting:
            plt.plot(range(len(cors)), cors)
            plt.xticks(range(len(cors)), opt_var)
            plt.ylabel("Correlation")
            plt.xlabel("Model Complexity")
            plt.show()

        # Plot testing & training RMSE on the same plot to
        # show how error behaves with different indicators.
        if plotting:
            plot_error_curves(opt_var,
                              rmsestrain,
                              rmsestest,
                              error_type="RMSE")
            if use_prices:
                # Plot testing & training MAPE on the same plot to
                # show how error behaves with different indicators.
                plot_error_curves(opt_var,
                                  mapestrain,
                                  mapestest,
                                  error_type="MAPE")

        best_ind_dict[best_indicator_set] = [
            best_ind_dict.get(best_indicator_set, [0, 0.0])[0] + 1,
            best_ind_dict.get(best_indicator_set, [0, 0.0])[1] + best_rmse
        ]
Example #7
0
class StochRSI(Indicator):
    def __init__(self,
                 rsiPeriod,
                 stochPeriod,
                 smoothingPeriodK,
                 smoothingPeriodD,
                 timeSeries=None):
        super(StochRSI, self).__init__()

        self.stochPeriod = stochPeriod
        self.smoothingPeriodK = smoothingPeriodK
        self.smoothingPeriodD = smoothingPeriodD
        self.valuesK = []
        self.valuesSlowK = []
        self.valuesD = []
        self.rsi = RSI(rsiPeriod)

        self.addSubIndicator(self.rsi)

        self.initialize(timeSeries)

    def _calculate(self):

        if len(self.rsi.getValues()) < self.stochPeriod:
            return

        recentRsi = self.rsi[-1 * self.stochPeriod:]

        maxHigh = max(recentRsi)
        minLow = min(recentRsi)

        if maxHigh == minLow:
            k = 100.0
        else:
            k = 100.0 * (self.rsi[-1] - minLow) / (maxHigh - minLow)

        self.valuesK.append(k)

        if len(self.valuesK) >= self.smoothingPeriodK:
            self.valuesSlowK.append(
                float(sum(self.valuesK[-1 * self.smoothingPeriodK:])) /
                self.smoothingPeriodK)

        if len(self.valuesSlowK) >= self.smoothingPeriodD:
            self.valuesD.append(
                float(sum(self.valuesSlowK[-1 * self.smoothingPeriodD:])) /
                self.smoothingPeriodD)

    def removeValue(self):
        super(StochRSI, self).removeValue()

        if len(self.valuesK) > 0:
            self.valuesK.pop(-1)

        if len(self.valuesSlowK) > 0:
            self.valuesSlowK.pop(-1)

        if len(self.valuesD) > 0:
            self.valuesD.pop(-1)

    def removeAll(self):
        super(StochRSI, self).removeAll()

        self.valuesK = []
        self.valuesSlowK = []
        self.valuesD = []

    def getValuesK(self):
        return self.valuesK

    def getValuesSlowK(self):
        return self.valuesSlowK

    def getValuesD(self):
        return self.valuesD
Example #8
0
    def set_indicators(self):
        rsi = RSI(n=14)

        self.indicators = [rsi]
Example #9
0
    def set_indicators(self):
        macd = MACD(fast_period=12, slow_period=26, signal_period=9)
        rsi = RSI(n=14)

        self.indicators = [rsi, macd]