예제 #1
0
 def generateTechIndObjects(self, histDF, indicators):
     from realtime_talib import Indicator
     return [Indicator(histDF, ind, *args) for ind, args in indicators]
예제 #2
0
def calculate_indicators(ohlcv_df):
    ohlcv_df = ohlcv_df.drop(["Volume (BTC)", "Weighted Price"], axis=1)
    ohlcv_df.columns = ["Date", "Open", "High", "Low", "Close", "Volume"]

    temp_ohlcv_df = ohlcv_df.copy()

    # Converts ISO 8601 timestamps to UNIX
    unix_times = [
        int(dp.parse(temp_ohlcv_df.iloc[index]["Date"]).strftime('%s'))
        for index in range(temp_ohlcv_df.shape[0])
    ]
    temp_ohlcv_df["Date"] = pd.Series(unix_times).values

    # Converts column headers to lowercase and sorts rows in chronological order
    temp_ohlcv_df.columns = ["date", "open", "high", "low", "close", "volume"]
    temp_ohlcv_df = temp_ohlcv_df.iloc[::-1]

    # Rate of Change Ratio
    rocr3 = Indicator(temp_ohlcv_df, "ROCR", 3).getHistorical()[::-1]
    rocr6 = Indicator(temp_ohlcv_df, "ROCR", 6).getHistorical()[::-1]

    # Average True Range
    atr = Indicator(temp_ohlcv_df, "ATR", 14).getHistorical()[::-1]

    # On-Balance Volume
    obv = Indicator(temp_ohlcv_df, "OBV").getHistorical()[::-1]

    # Triple Exponential Moving Average
    trix = Indicator(temp_ohlcv_df, "TRIX", 20).getHistorical()[::-1]

    # Momentum
    mom1 = Indicator(temp_ohlcv_df, "MOM", 1).getHistorical()[::-1]
    mom3 = Indicator(temp_ohlcv_df, "MOM", 3).getHistorical()[::-1]

    # Average Directional Index
    adx14 = Indicator(temp_ohlcv_df, "ADX", 14).getHistorical()[::-1]
    adx20 = Indicator(temp_ohlcv_df, "ADX", 20).getHistorical()[::-1]

    # Williams %R
    willr = Indicator(temp_ohlcv_df, "WILLR", 14).getHistorical()[::-1]

    # Relative Strength Index
    rsi6 = Indicator(temp_ohlcv_df, "RSI", 6).getHistorical()[::-1]
    rsi12 = Indicator(temp_ohlcv_df, "RSI", 12).getHistorical()[::-1]

    # Moving Average Convergence Divergence
    macd, macd_signal, macd_hist = Indicator(temp_ohlcv_df, "MACD", 12, 26,
                                             9).getHistorical()
    macd, macd_signal, macd_hist = macd[::-1], macd_signal[::-1], macd_hist[::
                                                                            -1]

    # Exponential Moving Average
    ema6 = Indicator(temp_ohlcv_df, "MA", 6, 1).getHistorical()[::-1]
    ema12 = Indicator(temp_ohlcv_df, "MA", 12, 1).getHistorical()[::-1]

    # Append indicators to the input datasets
    min_length = min(len(mom1), len(mom3), len(adx14), len(adx20), len(willr),
                     len(rsi6), len(rsi12), len(macd), len(macd_signal),
                     len(macd_hist), len(ema6), len(ema12), len(rocr3),
                     len(rocr6), len(atr), len(obv), len(trix))
    ohlcv_df = ohlcv_df[:min_length].drop(["Open", "High", "Low"], axis=1)

    ohlcv_df["MOM (1)"] = pd.Series(mom1[:min_length]).values
    ohlcv_df["MOM (3)"] = pd.Series(mom3[:min_length]).values
    ohlcv_df["ADX (14)"] = pd.Series(adx14[:min_length]).values
    ohlcv_df["ADX (20)"] = pd.Series(adx20[:min_length]).values
    ohlcv_df["WILLR"] = pd.Series(willr[:min_length]).values
    ohlcv_df["RSI (6)"] = pd.Series(rsi6[:min_length]).values
    ohlcv_df["RSI (12)"] = pd.Series(rsi12[:min_length]).values
    ohlcv_df["MACD"] = pd.Series(macd[:min_length]).values
    ohlcv_df["MACD (Signal)"] = pd.Series(macd_signal[:min_length]).values
    ohlcv_df["MACD (Historical)"] = pd.Series(macd_hist[:min_length]).values
    ohlcv_df["EMA (6)"] = pd.Series(ema6[:min_length]).values
    ohlcv_df["EMA (12)"] = pd.Series(ema12[:min_length]).values
    ohlcv_df["ROCR (3)"] = pd.Series(rocr3[:min_length]).values
    ohlcv_df["ROCR (6)"] = pd.Series(rocr6[:min_length]).values
    ohlcv_df["ATR (14)"] = pd.Series(atr[:min_length]).values
    ohlcv_df["OBV"] = pd.Series(obv[:min_length]).values
    ohlcv_df["TRIX (20)"] = pd.Series(trix[:min_length]).values

    return ohlcv_df
예제 #3
0
	def generateTechIndObjects(self, histDF):
		if (self.techInds != {}): 
			from realtime_talib import Indicator
			self.techInds = [Indicator(histDF,k,*v) for k,v in self.techInds.items()]
예제 #4
0
	def generateIndicators(self, indicators):
		if (indicators != {}): 
			from realtime_talib import Indicator
			self.techInds = [Indicator(histDF,k,*v) for k,v in self.techInds.items()]
예제 #5
0
def calculate_indicators(ohlcv):
    """Extracts technical indicators from OHLCV data."""
    print("\tCalculating technical indicators")

    ohlcv = ohlcv.drop(["Volume (BTC)", "Weighted Price"], axis=1)
    ohlcv.columns = ["Date", "Open", "High", "Low", "Close", "Volume"]

    temp_ohlcv = ohlcv.copy()

    # Converts ISO 8601 timestamps to UNIX
    unix_times = [
        int((dp.parse(temp_ohlcv.iloc[index]["Date"])).strftime("%s"))
        for index in range(temp_ohlcv.shape[0])
    ]
    temp_ohlcv["Date"] = (pd.Series(unix_times)).values

    # Converts column headers to lowercase and sorts rows in chronological order
    temp_ohlcv.columns = ["date", "open", "high", "low", "close", "volume"]
    temp_ohlcv = temp_ohlcv.iloc[::-1]

    # Rate of Change Ratio
    # roc3 = ((Indicator(temp_ohlcv, "ROCR", 3)).getHistorical(lag=1))[::-1]
    # roc6 = ((Indicator(temp_ohlcv, "ROCR", 6)).getHistorical(lag=1))[::-1]

    # Average True Range
    # atr = ((Indicator(temp_ohlcv, "ATR", 14)).getHistorical(lag=1))[::-1]

    # On-Balance Volume
    # obv = ((Indicator(temp_ohlcv, "OBV", 6)).getHistorical(lag=1))[::-1]

    # Triple Exponential Moving Average
    # trix = ((Indicator(temp_ohlcv, "TRIX", 30)).getHistorical(lag=1))[::-1]

    # Momentum
    mom1 = ((Indicator(temp_ohlcv, "MOM", 1)).getHistorical(lag=1))[::-1]
    mom3 = ((Indicator(temp_ohlcv, "MOM", 3)).getHistorical(lag=1))[::-1]

    # Average Directional Index
    adx14 = ((Indicator(temp_ohlcv, "ADX", 14)).getHistorical(lag=1))[::-1]
    adx20 = ((Indicator(temp_ohlcv, "ADX", 20)).getHistorical(lag=1))[::-1]

    # Williams %R
    willr = ((Indicator(temp_ohlcv, "WILLR", 14)).getHistorical(lag=1))[::-1]

    # Relative Strength Index
    rsi6 = ((Indicator(temp_ohlcv, "RSI", 6)).getHistorical(lag=1))[::-1]
    rsi12 = ((Indicator(temp_ohlcv, "RSI", 12)).getHistorical(lag=1))[::-1]

    # Moving Average Convergence Divergence
    macd, macd_signal, macd_hist = (Indicator(temp_ohlcv, "MACD", 12, 26,
                                              9)).getHistorical(lag=1)
    macd, macd_signal, macd_hist = macd[::-1], macd_signal[::-1], macd_hist[::
                                                                            -1]

    # Exponential Moving Average
    ema6 = ((Indicator(temp_ohlcv, "MA", 6, 1)).getHistorical(lag=1))[::-1]
    ema12 = ((Indicator(temp_ohlcv, "MA", 12, 1)).getHistorical(lag=1))[::-1]

    # Append indicators to the input datasets
    min_length = min(len(mom1), len(mom3), len(adx14), len(adx20), len(willr),
                     len(rsi6), len(rsi12), len(macd), len(macd_signal),
                     len(macd_hist), len(ema6), len(ema12))
    ohlcv = ohlcv[:min_length].drop(["Open", "High", "Low"], axis=1)

    ohlcv["MOM (1)"], ohlcv["MOM (3)"], ohlcv["ADX (14)"] = (pd.Series(
        mom1[:min_length])).values, (pd.Series(
            mom3[:min_length])).values, (pd.Series(adx14[:min_length])).values
    ohlcv["ADX (20)"], ohlcv["WILLR"], ohlcv["RSI (6)"] = (pd.Series(
        adx20[:min_length])).values, (pd.Series(
            willr[:min_length])).values, (pd.Series(rsi6[:min_length])).values
    ohlcv["RSI (12)"], ohlcv["MACD"], ohlcv["MACD (Signal)"] = (pd.Series(
        rsi12[:min_length])).values, (pd.Series(
            macd[:min_length])).values, (pd.Series(
                macd_signal[:min_length])).values
    ohlcv["MACD (Historical)"], ohlcv["EMA (6)"], ohlcv["EMA (12)"] = (
        pd.Series(macd_hist[:min_length])).values, (pd.Series(
            ema6[:min_length])).values, (pd.Series(ema12[:min_length])).values

    return ohlcv
예제 #6
0
 def generateTechIndObjects(self, histDF):
     if (self.techInds != {}):
         self.techInds = [
             Indicator(histDF, k, *v) for k, v in self.techInds.items()
         ]
예제 #7
0
    def dateToUNIX(self, date):  #format: "YYYYMMDD hhmmss"
        ts = ciso8601.parse_datetime(date)
        return time.mktime(ts.timetuple())

    def getCryptoHistoricalData(self, currencyPair, startDate, endDate):

        stDateUNIX = self.dateToUNIX(startDate)
        eDateUNIX = self.dateToUNIX(endDate)
        poloniexJsonURL = self.POLO_HIST_DATA.format(currencyPair, stDateUNIX,
                                                     eDateUNIX, self.interval)
        poloniexJson = requests.get(poloniexJsonURL).json()

        histDataframe = pd.DataFrame.from_records(poloniexJson)
        histDataframe.drop('quoteVolume', axis=1, inplace=True)
        histDataframe.drop('weightedAverage', axis=1, inplace=True)
        histDataframe['date'] = histDataframe['date'].astype(float)

        return histDataframe[[
            "date", "open", "high", "low", "close", "volume"
        ]]


newPL = Pipeline(300)
OHLCV = newPL.getCryptoHistoricalData('USDT_BTC', '20170122', '20170823')
BBANDS = Indicator(OHLCV, 'BBANDS', 10, 2, 2, 1)
upper, ema, lower = BBANDS.getHistorical(lag=150)
print(len(upper), len(ema), len(lower))
price, date = OHLCV['open'], OHLCV['date']
plt.plot(price[61331], upper[9198449:9198599], ema[9198449:9198599],
         lower[9198449:9198599])