예제 #1
0
 def calc_patel_TI(self, days):
     """ Calculate the technical indicators from Patel et. al. for n days
         in the past
     Args:
         days (int): Days in the past over which to
             calculate relative strength index
     """
     # TA.SMA has min_periods=days-1 for some reason
     # Changed it to min_periods=days
     self.df['MA'] = TA.SMA(self.df, days)
     # Equal to my implementation
     self.df['WMA'] = TA.WMA(self.df, days)
     # Equal
     self.df['MOM'] = TA.MOM(self.df, days)
     # Had highest_high - ohlc['close']
     # After changing they are equal
     self.df['STOCH'] = TA.STOCH(self.df, days)
     # They didn't have period when calling STOCH, instead it was used for 3
     # STOCHD is actually the mean over 3 days
     self.df['STOCHD'] = TA.STOCHD(self.df, days)
     # They used ewm, changed it to simple rolling
     # They also had ohlc['close'].diff()[1:] which resulted in returning
     # one less row
     # Changed min periods
     self.df['RSI'] = TA.RSI(self.df, days)
     # TODO: What do they mean in the paper
     # Changed min periods
     self.df['MACD'] = TA.MACD(self.df, signal=days)['MACD']
     self.df['WILLIAMS'] = TA.WILLIAMS(self.df, days)
     self.df['ADL'] = TA.ADL(self.df)
     self.df['CCI'] = TA.CCI(self.df, days)
     # Drop columns we no longer need
     self.df.drop(['open', 'high', 'low', 'volume'], axis=1, inplace=True)
     # Drop rows with nan
     self.df.dropna(inplace=True)
예제 #2
0
    def get_adl(data):
        """Calculate the accumulation/distribution line of given dataframe.

        :param data: a dataframe in OHLC format
        :return: a Pandas series
        """
        if data is None:
            raise EmptyDataError("[!] Invalid data value")

        result = TA.ADL(data)
        if result is None:
            raise IndicatorException
        return result
예제 #3
0
def test_adl():

    adl = TA.ADL(ohlc)

    assert isinstance(adl, series.Series)
    assert adl.values[-1] == 1221072523.7384958
예제 #4
0
def test_adl():

    adl = TA.ADL(ohlc).round(decimals=8)

    assert isinstance(adl, series.Series)
    assert adl.values[-1] == 303320.96403697
    # Sell when the short crosses below the medium and really sell when it crosses below the long
    # So we have a number of things to implement (signal is +1 when short above medium, +2 short above long, +3 when
    # medium above long as well. Similarly in the opposite direction. Positive is buy, negative is sell)
    EMAV_conditions = [
        (prices["Exp_mav_sh"] > prices["Exp_mav_med"]) & (prices["Exp_mav_sh"] < prices["Exp_mav_lon"]),
        (prices["Exp_mav_sh"] > prices["Exp_mav_med"]) & (prices["Exp_mav_sh"] > prices["Exp_mav_lon"]),
        (prices["Exp_mav_sh"] > prices["Exp_mav_lon"]) & (prices["Exp_mav_med"] > prices["Exp_mav_lon"]),
        (prices["Exp_mav_sh"] < prices["Exp_mav_med"]) & (prices["Exp_mav_sh"] < prices["Exp_mav_lon"]),
        (prices["Exp_mav_sh"] < prices["Exp_mav_med"]) & (prices["Exp_mav_sh"] < prices["Exp_mav_lon"]),
        (prices["Exp_mav_sh"] < prices["Exp_mav_lon"]) & (prices["Exp_mav_med"] < prices["Exp_mav_lon"]),
    ]
    EMAV_choices = [1.0, 2.0, 3.0, -1.0, -2.0, -3.0]
    prices["Exp_mav-signal"] = np.select(EMAV_conditions, EMAV_choices, default=0.0)

    # Accumulation and Distribution - Marc Chaikin to determine flow in or out of a security
    prices["Money Flow Multiplier"] = TA.ADL(ohlcv)
    # MACD oscillator
    prices["MACDb-finta"] = TA.CHAIKIN(ohlcv)
    prices["MFI"] = TA.MFI(ohlc)


    prices["CMFV"] = prices['Volume'] * ((prices['Adj Close'] - prices['Adj Low']) -
                                         (prices['Adj High']-prices['Adj Close'])) / \
                     (prices['Adj High'] - prices['Adj Low'])
    # It shows how strong the trend is, if the price is rising but the indicator is falling, this indicates that
    #    buying or accumulation volume many not be enough to support the price rise and a decline may be forthcoming
    prices["CMFV-rolling"] = prices["CMFV"].rolling(window=20).sum()
    prices["CMFV-signal"] = prices["CMFV-rolling"] - prices["CMFV-rolling"].shift(periods=5)

    # Moving average convergence divergence
    # A divergence is where the price doesnt agree with the indicator
예제 #6
0
def test_adl():

    adl = TA.ADL(ohlc)

    assert isinstance(adl, series.Series)
    assert adl.values[-1] == 303320.96403697244
예제 #7
0
def technical_indicators(tiDF, usdflag):
    # tiDF 'date_time', 'Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Stock'

    ohlcv = tiDF[[
        'date_time', 'Date', 'Open', 'High', 'Low', 'Close', 'Volume'
    ]]

    for i in ohlcv.columns:
        ohlcv = ohlcv.rename(columns={str(i): str(i.lower())})

    ohlcv.rename(columns={'date_time': 'Date', 'date': 'day'}, inplace=True)
    ohlcv.set_index('Date', inplace=True)

    ohlcv['sma2'] = TA.SMA(ohlcv, 2)
    ohlcv['sma3'] = TA.SMA(ohlcv, 3)
    ohlcv['sma4'] = TA.SMA(ohlcv, 4)
    ohlcv['sma5'] = TA.SMA(ohlcv, 5)
    ohlcv['sma6'] = TA.SMA(ohlcv, 6)
    ohlcv['sma7'] = TA.SMA(ohlcv, 7)

    ohlcv['rsi'] = TA.RSI(ohlcv)
    ohlcv['cci'] = TA.CCI(ohlcv)
    ohlcv['adx'] = TA.ADX(ohlcv)

    ohlcv['vpt'] = TA.VPT(ohlcv)
    ohlcv['efi'] = TA.EFI(ohlcv)
    ohlcv['wobv'] = TA.WOBV(ohlcv)
    ohlcv['vzo'] = TA.VZO(ohlcv)
    ohlcv['pzo'] = TA.PZO(ohlcv)
    ohlcv['tp'] = TA.TP(ohlcv)
    ohlcv['adl'] = TA.ADL(ohlcv)
    ohlcv['smma'] = TA.SMMA(ohlcv)
    ohlcv['tr'] = TA.TR(ohlcv)
    ohlcv['sar'] = TA.SAR(ohlcv)
    ohlcv['vwap'] = TA.VWAP(ohlcv)
    ohlcv['ssma'] = TA.SSMA(ohlcv)
    ohlcv['dema'] = TA.DEMA(ohlcv)
    ohlcv['tema'] = TA.TEMA(ohlcv)
    ohlcv['trix'] = TA.TRIX(ohlcv)

    stock = tiDF['Stock'].unique().tolist()[0]
    ohlcv['Stock'] = stock

    ohlcv = ohlcv.dropna()

    numTimes = ohlcv.groupby('day').count()
    shortDays = numTimes[numTimes.sma7 != 7].index.values
    ohlcv = ohlcv[~ohlcv['day'].isin(shortDays)]

    ohlcv = ohlcv.drop(['high', 'low', 'close', 'day'], axis=1)
    ohlcv.reset_index(level=0, inplace=True)
    for i in ohlcv.columns:
        ohlcv = ohlcv.rename(columns={str(i): str(i.capitalize())})

    ohlcv.rename(columns={'Date': 'date_time'}, inplace=True)

    pd.options.display.max_columns = None
    pd.options.display.max_rows = None

    if usdflag:
        totale = pd.read_csv("total.csv")
        totale['date_time'] = to_datetime(totale['date_time'],
                                          format="%Y-%m-%d %H:%M:%S")
        result = pd.merge(ohlcv, totale, how='inner', on=['date_time'])
    else:
        result = ohlcv.copy()

    print(result.info())
    print(result.head(5))
    return result