Esempio n. 1
0
    def init(self, base_data):
        base_data = base_data.sort_values("date")

        base_data[f"ma{self.ma_fast_days}"] = SMAIndicator(
            base_data["close"], self.ma_fast_days).sma_indicator()

        base_data[f"ma{self.ma_slow_days}"] = SMAIndicator(
            base_data["close"], self.ma_slow_days).sma_indicator()

        base_data["madiff"] = (base_data[f"ma{self.ma_fast_days}"] -
                               base_data[f"ma{self.ma_slow_days}"])
        base_data["bool_signal"] = base_data["madiff"].map(lambda x: 1
                                                           if x > 0 else -1)
        base_data["bool_signal_shift1"] = (
            base_data["bool_signal"].shift(1).fillna(0))

        base_data["bool_signal_shift1"] = base_data[
            "bool_signal_shift1"].astype(int)

        base_data["signal"] = 0
        base_data.loc[((base_data["bool_signal"] > 0)
                       & (base_data["bool_signal_shift1"] < 0)),
                      "signal", ] = 1
        base_data.loc[((base_data["bool_signal"] < 0)
                       & (base_data["bool_signal_shift1"] > 0)),
                      "signal", ] = -1
        return base_data
def AddIndicators(df):
    # Add Simple Moving Average (SMA) indicators
    df["sma7"] = SMAIndicator(close=df["Close"], window=7,
                              fillna=True).sma_indicator()
    df["sma25"] = SMAIndicator(close=df["Close"], window=25,
                               fillna=True).sma_indicator()
    df["sma99"] = SMAIndicator(close=df["Close"], window=99,
                               fillna=True).sma_indicator()

    # Add Bollinger Bands indicator
    indicator_bb = BollingerBands(close=df["Close"], window=20, window_dev=2)
    df['bb_bbm'] = indicator_bb.bollinger_mavg()
    df['bb_bbh'] = indicator_bb.bollinger_hband()
    df['bb_bbl'] = indicator_bb.bollinger_lband()

    # Add Parabolic Stop and Reverse (Parabolic SAR) indicator
    indicator_psar = PSARIndicator(high=df["High"],
                                   low=df["Low"],
                                   close=df["Close"],
                                   step=0.02,
                                   max_step=2,
                                   fillna=True)
    df['psar'] = indicator_psar.psar()

    # Add Moving Average Convergence Divergence (MACD) indicator
    df["MACD"] = macd(close=df["Close"],
                      window_slow=26,
                      window_fast=12,
                      fillna=True)  # mazas

    # Add Relative Strength Index (RSI) indicator
    df["RSI"] = rsi(close=df["Close"], window=14, fillna=True)  # mazas

    return df
Esempio n. 3
0
def get_moving_average(i, window: int):
    """
    returns the moving average of indicator i
    i: pandas series
    windows: [20, 50, 200]
    """
    sma = SMAIndicator(close=i, window=window)
    return sma.sma_indicator()
def AddIndicators(df):
    # Add Simple Moving Average (SMA) indicators
    df["sma7"] = SMAIndicator(close=df["Close"], window=7,
                              fillna=True).sma_indicator()
    df["sma25"] = SMAIndicator(close=df["Close"], window=25,
                               fillna=True).sma_indicator()
    df["sma99"] = SMAIndicator(close=df["Close"], window=99,
                               fillna=True).sma_indicator()

    return df
Esempio n. 5
0
def get_trend_indicators(df, threshold=0.5, plot=False):
    df_trend = df.copy()
    
    # add custom trend indicators
    df_trend["sma7"] = SMAIndicator(close=df["Close"], window=7, fillna=True).sma_indicator()
    df_trend["sma25"] = SMAIndicator(close=df["Close"], window=25, fillna=True).sma_indicator()
    df_trend["sma99"] = SMAIndicator(close=df["Close"], window=99, fillna=True).sma_indicator()

    df_trend = add_trend_ta(df_trend, high="High", low="Low", close="Close")

    return DropCorrelatedFeatures(df_trend, threshold, plot)
Esempio n. 6
0
 def create_trade_sign(self, stock_price: pd.DataFrame) -> pd.DataFrame:
     stock_price = stock_price.sort_values("date")
     stock_price[f"ma{self.ma_days}"] = SMAIndicator(
         stock_price["close"], self.ma_days).sma_indicator()
     stock_price["bias"] = (
         (stock_price["close"] - stock_price[f"ma{self.ma_days}"]) /
         stock_price[f"ma{self.ma_days}"]) * 100
     stock_price[f"max_last_k_days{self.last_k_days}"] = (
         stock_price["close"].shift(1).rolling(
             window=self.last_k_days).max())
     stock_price[f"min_last_k_days{self.last_k_days}"] = (
         stock_price["close"].shift(1).rolling(
             window=self.last_k_days).min())
     stock_price["signal"] = 0
     stock_price.loc[((stock_price["bias"] < self.bais_lower)
                      &
                      (stock_price["close"] >
                       stock_price[f"max_last_k_days{self.last_k_days}"])),
                     "signal", ] = 1
     stock_price.loc[((stock_price["bias"] > self.bais_upper)
                      &
                      (stock_price["close"] <
                       stock_price[f"min_last_k_days{self.last_k_days}"])),
                     "signal", ] = -1
     return stock_price
Esempio n. 7
0
    def init(self, base_data):
        base_data = base_data.sort_values("date")

        base_data[f"ma{self.ma_days}"] = SMAIndicator(
            base_data["close"], self.ma_days).sma_indicator()

        base_data["bias"] = (
            (base_data["close"] - base_data[f"ma{self.ma_days}"]) /
            base_data[f"ma{self.ma_days}"]) * 100

        base_data[f"max_last_k_days{self.last_k_days}"] = (
            base_data["close"].shift(1).rolling(window=self.last_k_days).max())

        base_data[f"min_last_k_days{self.last_k_days}"] = (
            base_data["close"].shift(1).rolling(window=self.last_k_days).min())

        base_data["signal"] = 0
        base_data.loc[((base_data["bias"] < self.bais_lower)
                       & (base_data["close"] >
                          base_data[f"max_last_k_days{self.last_k_days}"])),
                      "signal", ] = 1
        base_data.loc[((base_data["bias"] > self.bais_upper)
                       & (base_data["close"] <
                          base_data[f"min_last_k_days{self.last_k_days}"])),
                      "signal", ] = -1

        return base_data
def add_price_moving_features(df, windows=[50, 100, 200]):
    for w in windows:
        df[f"ma_{w}"] = SMAIndicator(close=df["close"], window=w,
                                     fillna=False).sma_indicator()

        df[f"ema_{w}"] = EMAIndicator(close=df["close"],
                                      window=w,
                                      fillna=False).ema_indicator()
    return df
Esempio n. 9
0
    def add_indicators(self,data):
        warnings.filterwarnings("ignore",category=RuntimeWarning)
        # SMA n =20
        data['ma'] = SMAIndicator(data['Close'],20).sma_indicator()
        data['rsi'] = RSIIndicator(data['Close'],14).rsi()
        data['macd'] = MACD(data['Close'],26,12,9).macd()
        data['macd_signal'] = MACD(data['Close'],26,12,9).macd_signal()
        data['macd_hist'] = MACD(data['Close'],26,12,9).macd_diff()
        data['adx'] = ADXIndicator(data['High'],data['Low'],data['Close'],14).adx()        
        data['adx_neg'] = ADXIndicator(data['High'],data['Low'],data['Close'],14).adx_neg()
        data['adx_pos'] = ADXIndicator(data['High'],data['Low'],data['Close'],14).adx_pos()

        return data
Esempio n. 10
0
 def create_trade_sign(self, stock_price: pd.DataFrame) -> pd.DataFrame:
     stock_price = stock_price.sort_values("date")
     stock_price[f"ma{self.ma_fast_days}"] = SMAIndicator(
         stock_price["close"], self.ma_fast_days).sma_indicator()
     stock_price[f"ma{self.ma_slow_days}"] = SMAIndicator(
         stock_price["close"], self.ma_slow_days).sma_indicator()
     stock_price["madiff"] = (stock_price[f"ma{self.ma_fast_days}"] -
                              stock_price[f"ma{self.ma_slow_days}"])
     stock_price["bool_signal"] = stock_price["madiff"].map(
         lambda x: 1 if x > 0 else -1)
     stock_price["bool_signal_shift1"] = (
         stock_price["bool_signal"].shift(1).fillna(0))
     stock_price["bool_signal_shift1"] = stock_price[
         "bool_signal_shift1"].astype(int)
     stock_price["signal"] = 0
     stock_price.loc[((stock_price["bool_signal"] > 0)
                      & (stock_price["bool_signal_shift1"] < 0)),
                     "signal", ] = 1
     stock_price.loc[((stock_price["bool_signal"] < 0)
                      & (stock_price["bool_signal_shift1"] > 0)),
                     "signal", ] = -1
     return stock_price
Esempio n. 11
0
    def create_trade_sign(self, stock_price: pd.DataFrame) -> pd.DataFrame:
        stock_price = stock_price.sort_values("date")
        stock_price[f"ma{self.ma_days}"] = SMAIndicator(
            stock_price["close"], self.ma_days).sma_indicator()
        stock_price["bias"] = (
            (stock_price["close"] - stock_price[f"ma{self.ma_days}"]) /
            stock_price[f"ma{self.ma_days}"]) * 100
        stock_price = stock_price.dropna()
        stock_price.index = range(len(stock_price))
        stock_price["signal"] = stock_price["bias"].map(
            lambda x: 1
            if x < self.bias_lower else (-1 if x > self.bias_upper else 0))

        stock_price["signal"] = stock_price["signal"].fillna(0)
        return stock_price
Esempio n. 12
0
    def init(self, base_data):
        base_data = base_data.sort_values("date")

        base_data[f"ma{self.ma_days}"] = SMAIndicator(
            base_data["close"], self.ma_days).sma_indicator()
        base_data["bias"] = (
            (base_data["close"] - base_data[f"ma{self.ma_days}"]) /
            base_data[f"ma{self.ma_days}"]) * 100
        base_data = base_data.dropna()
        base_data.index = range(len(base_data))

        base_data["signal"] = base_data["bias"].map(
            lambda x: 1
            if x < self.bias_lower else (-1 if x > self.bias_upper else 0))

        base_data["signal"] = base_data["signal"].fillna(0)
        return base_data
Esempio n. 13
0
def add_trend_indicators(data: pd.DataFrame) -> pd.DataFrame:
    """Adds the trend indicators.

    Parameters
    ----------
    data : pd.DataFrame
        A dataframe with daily stock values. Must include: open, high,
        low, close and volume. It should also be sorted in a descending
        manner.

    Returns
    -------
    pd.DataFrame
        The input dataframe with the indicators added.
    """
    adx = ADXIndicator(data['high'], data['low'], data['close'])
    ema = EMAIndicator(data['close'])
    ema_200 = EMAIndicator(data['close'], n=200)
    ichimoku = IchimokuIndicator(data['high'], data['low'])
    macd = MACD(data['close'])
    sma = SMAIndicator(data['close'], n=14)
    sma_200 = SMAIndicator(data['close'], n=200)

    data.loc[:, 'adx'] = adx.adx()
    data.loc[:, 'adx_pos'] = adx.adx_pos()
    data.loc[:, 'adx_neg'] = adx.adx_neg()
    data.loc[:, 'ema'] = ema.ema_indicator()
    data.loc[:, 'ema_200'] = ema_200.ema_indicator()
    data.loc[:, 'ichimoku_a'] = ichimoku.ichimoku_a()
    data.loc[:, 'ichimoku_b'] = ichimoku.ichimoku_b()
    data.loc[:, 'ichimoku_base_line'] = ichimoku.ichimoku_base_line()
    data.loc[:, 'ichimoku_conversion_line'] = (
        ichimoku.ichimoku_conversion_line())
    data.loc[:, 'macd'] = macd.macd()
    data.loc[:, 'macd_diff'] = macd.macd_diff()
    data.loc[:, 'macd_signal'] = macd.macd_signal()
    data.loc[:, 'sma'] = sma.sma_indicator()
    data.loc[:, 'sma_200'] = sma_200.sma_indicator()

    return data
Esempio n. 14
0
    def getDailyData(self, symbol, since, debug=False):
        try:
            print("GET DAILY DATA")
            print("using key [%d/%d] = %s " %
                  (Market.keyindex, len(
                      Market.apikeys), Market.apikeys[Market.keyindex]))
            data, meta_data = self.ts[Market.keyindex].get_daily(
                symbol=symbol, outputsize='full')
            Market.keyindex = (Market.keyindex + 1) % len(Market.apikeys)

            self.data = data
            self.meta_data = meta_data
            self.data = self.data.sort_index()

            # map values to standard OHLC format
            self.data['O'] = self.data['1. open']
            self.data['H'] = self.data['2. high']
            self.data['L'] = self.data['3. low']
            self.data['C'] = self.data['4. close']
            self.data['V'] = self.data['5. volume']

            if debug:
                print("===VALUES===")
                print(self.data)

            # add trend indicator.
            self.data = ta.utils.dropna(self.data)
            self.data['sma20'] = SMAIndicator(close=self.data['C'],
                                              n=20,
                                              fillna=True).sma_indicator()
            self.data['sma50'] = SMAIndicator(close=self.data['C'],
                                              n=50,
                                              fillna=True).sma_indicator()
            self.data['sma100'] = SMAIndicator(close=self.data['C'],
                                               n=100,
                                               fillna=True).sma_indicator()
            self.data['sma200'] = SMAIndicator(close=self.data['C'],
                                               n=200,
                                               fillna=True).sma_indicator()

            self.data['ema9'] = EMAIndicator(close=self.data['C'],
                                             n=9,
                                             fillna=True).ema_indicator()
            self.data['ema20'] = EMAIndicator(close=self.data['C'],
                                              n=20,
                                              fillna=True).ema_indicator()

            self.data = ta.add_all_ta_features(self.data,
                                               open="O",
                                               high="H",
                                               low="L",
                                               close="C",
                                               volume="V",
                                               fillna=True)
            self.data = self.data.loc[self.data.index >= since]

            candle_names = talib.get_function_groups()['Pattern Recognition']
            if debug:
                print(candle_names)

            included_items = ('CDLDOJI', 'CDLHAMMER', 'CDLEVENINGSTAR',
                              'CDLHANGINGMAN', 'CDLSHOOTINGSTAR')
            candle_names = [
                candle for candle in candle_names if candle in included_items
            ]

            for candle in candle_names:
                self.data[candle] = getattr(talib, candle)(self.data['O'],
                                                           self.data['H'],
                                                           self.data['L'],
                                                           self.data['C'])

            self.data['action'] = 0

            if debug:
                print("===VALUES===")
                print(self.data)

        except:
            print("ERROR GETTING MARKET DATA")
            time.sleep(60)
            self.data = None

        # max 5 api calls per minutes
        #time.sleep(60 / (2 * len(Market.apikeys)))
        #time.sleep(60 / 4)

        return self.data
Esempio n. 15
0
df2 = pd.DataFrame()
SYMBOL = "'BPCL                     '"
df2 = pd.DataFrame()
for i in df_unique_symbols:
    #SYMBOL = "'ALPSINDUS                     '"
    #print(SYMBOL)
    SYMBOL = f'\'{i}\''
    #print(SYMBOL)
    df = pd.read_sql_query(
        f'select {low_price}  low_price, {open_price} open_price,{macd} MACD,{macd_signal} MACD_SIGNAL,'
        f'{close_price} close_price , {date_recorded} date_recorded ,{ema_indicator} ema_indicator,'
        f'{high_price} high_price , {volume1} volume , {SYMBOL} symbol'
        f' from nifty_500_technical_analysis where nifty_500_technical_analysis.symbol  = {SYMBOL}',
        conn)
    #40 days SMA calculation
    indicator = SMAIndicator(close=df["close_price"], n=40, fillna=False)

    SMA_INDICATOR = "SMA_indicator"

    # print(indicator)
    df[SMA_INDICATOR] = indicator.sma_indicator()
    try:
        df['sma_line_Crossover'] = np.where(
            df['close_price'] > df['SMA_indicator'], 1, 0)
        print(df)
        df['sma_line_Crossover'] = np.where(
            df['close_price'] < df['SMA_indicator'], -1,
            df['sma_line_Crossover'])
        print(df)
        df['SMA_buy_sell'] = (2 * (np.sign(df['sma_line_Crossover'] -
                                           df['sma_line_Crossover'].shift(1))))
Esempio n. 16
0
    indicator_bb = BollingerBands(close=df['cls'], window=20, window_dev=2)
    indicator_macd = MACD(close=df['cls'],
                          window_fast=12,
                          window_slow=26,
                          window_sign=9)
    indicator_rsi14 = RSIIndicator(close=df['cls'], window=14)
    indicator_cci20 = cci(high=df['hgh'],
                          low=df['low'],
                          close=df['cls'],
                          window=20,
                          constant=0.015)
    indicator_obv = OnBalanceVolumeIndicator(close=df['cls'],
                                             volume=df['vol'],
                                             fillna=True)

    indicator_vol_sma20 = SMAIndicator(close=df['vol'], window=20)

    indicator_ema03 = EMAIndicator(close=df['cls'], window=3)
    indicator_ema05 = EMAIndicator(close=df['cls'], window=5)
    indicator_ema08 = EMAIndicator(close=df['cls'], window=8)
    indicator_ema10 = EMAIndicator(close=df['cls'], window=10)
    indicator_ema12 = EMAIndicator(close=df['cls'], window=12)
    indicator_ema15 = EMAIndicator(close=df['cls'], window=15)
    indicator_ema30 = EMAIndicator(close=df['cls'], window=30)
    indicator_ema35 = EMAIndicator(close=df['cls'], window=35)
    indicator_ema40 = EMAIndicator(close=df['cls'], window=40)
    indicator_ema45 = EMAIndicator(close=df['cls'], window=45)
    indicator_ema50 = EMAIndicator(close=df['cls'], window=50)
    indicator_ema60 = EMAIndicator(close=df['cls'], window=60)

    # Add Bollinger Band high indicator
Esempio n. 17
0
def add_trend_ta(
    df: pd.DataFrame,
    high: str,
    low: str,
    close: str,
    fillna: bool = False,
    colprefix: str = "",
    vectorized: bool = False,
) -> pd.DataFrame:
    """Add trend technical analysis features to dataframe.

    Args:
        df (pandas.core.frame.DataFrame): Dataframe base.
        high (str): Name of 'high' column.
        low (str): Name of 'low' column.
        close (str): Name of 'close' column.
        fillna(bool): if True, fill nan values.
        colprefix(str): Prefix column names inserted
        vectorized(bool): if True, use only vectorized functions indicators

    Returns:
        pandas.core.frame.DataFrame: Dataframe with new features.
    """

    # MACD
    indicator_macd = MACD(close=df[close],
                          window_slow=26,
                          window_fast=12,
                          window_sign=9,
                          fillna=fillna)
    df[f"{colprefix}trend_macd"] = indicator_macd.macd()
    df[f"{colprefix}trend_macd_signal"] = indicator_macd.macd_signal()
    df[f"{colprefix}trend_macd_diff"] = indicator_macd.macd_diff()

    # SMAs
    df[f"{colprefix}trend_sma_fast"] = SMAIndicator(
        close=df[close], window=12, fillna=fillna).sma_indicator()
    df[f"{colprefix}trend_sma_slow"] = SMAIndicator(
        close=df[close], window=26, fillna=fillna).sma_indicator()

    # EMAs
    df[f"{colprefix}trend_ema_fast"] = EMAIndicator(
        close=df[close], window=12, fillna=fillna).ema_indicator()
    df[f"{colprefix}trend_ema_slow"] = EMAIndicator(
        close=df[close], window=26, fillna=fillna).ema_indicator()

    # Vortex Indicator
    indicator_vortex = VortexIndicator(high=df[high],
                                       low=df[low],
                                       close=df[close],
                                       window=14,
                                       fillna=fillna)
    df[f"{colprefix}trend_vortex_ind_pos"] = indicator_vortex.vortex_indicator_pos(
    )
    df[f"{colprefix}trend_vortex_ind_neg"] = indicator_vortex.vortex_indicator_neg(
    )
    df[f"{colprefix}trend_vortex_ind_diff"] = indicator_vortex.vortex_indicator_diff(
    )

    # TRIX Indicator
    df[f"{colprefix}trend_trix"] = TRIXIndicator(close=df[close],
                                                 window=15,
                                                 fillna=fillna).trix()

    # Mass Index
    df[f"{colprefix}trend_mass_index"] = MassIndex(high=df[high],
                                                   low=df[low],
                                                   window_fast=9,
                                                   window_slow=25,
                                                   fillna=fillna).mass_index()

    # DPO Indicator
    df[f"{colprefix}trend_dpo"] = DPOIndicator(close=df[close],
                                               window=20,
                                               fillna=fillna).dpo()

    # KST Indicator
    indicator_kst = KSTIndicator(
        close=df[close],
        roc1=10,
        roc2=15,
        roc3=20,
        roc4=30,
        window1=10,
        window2=10,
        window3=10,
        window4=15,
        nsig=9,
        fillna=fillna,
    )
    df[f"{colprefix}trend_kst"] = indicator_kst.kst()
    df[f"{colprefix}trend_kst_sig"] = indicator_kst.kst_sig()
    df[f"{colprefix}trend_kst_diff"] = indicator_kst.kst_diff()

    # Ichimoku Indicator
    indicator_ichi = IchimokuIndicator(
        high=df[high],
        low=df[low],
        window1=9,
        window2=26,
        window3=52,
        visual=False,
        fillna=fillna,
    )
    df[f"{colprefix}trend_ichimoku_conv"] = indicator_ichi.ichimoku_conversion_line(
    )
    df[f"{colprefix}trend_ichimoku_base"] = indicator_ichi.ichimoku_base_line()
    df[f"{colprefix}trend_ichimoku_a"] = indicator_ichi.ichimoku_a()
    df[f"{colprefix}trend_ichimoku_b"] = indicator_ichi.ichimoku_b()

    # Schaff Trend Cycle (STC)
    df[f"{colprefix}trend_stc"] = STCIndicator(
        close=df[close],
        window_slow=50,
        window_fast=23,
        cycle=10,
        smooth1=3,
        smooth2=3,
        fillna=fillna,
    ).stc()

    if not vectorized:
        # Average Directional Movement Index (ADX)
        indicator_adx = ADXIndicator(high=df[high],
                                     low=df[low],
                                     close=df[close],
                                     window=14,
                                     fillna=fillna)
        df[f"{colprefix}trend_adx"] = indicator_adx.adx()
        df[f"{colprefix}trend_adx_pos"] = indicator_adx.adx_pos()
        df[f"{colprefix}trend_adx_neg"] = indicator_adx.adx_neg()

        # CCI Indicator
        df[f"{colprefix}trend_cci"] = CCIIndicator(
            high=df[high],
            low=df[low],
            close=df[close],
            window=20,
            constant=0.015,
            fillna=fillna,
        ).cci()

        # Ichimoku Visual Indicator
        indicator_ichi_visual = IchimokuIndicator(
            high=df[high],
            low=df[low],
            window1=9,
            window2=26,
            window3=52,
            visual=True,
            fillna=fillna,
        )
        df[f"{colprefix}trend_visual_ichimoku_a"] = indicator_ichi_visual.ichimoku_a(
        )
        df[f"{colprefix}trend_visual_ichimoku_b"] = indicator_ichi_visual.ichimoku_b(
        )

        # Aroon Indicator
        indicator_aroon = AroonIndicator(close=df[close],
                                         window=25,
                                         fillna=fillna)
        df[f"{colprefix}trend_aroon_up"] = indicator_aroon.aroon_up()
        df[f"{colprefix}trend_aroon_down"] = indicator_aroon.aroon_down()
        df[f"{colprefix}trend_aroon_ind"] = indicator_aroon.aroon_indicator()

        # PSAR Indicator
        indicator_psar = PSARIndicator(
            high=df[high],
            low=df[low],
            close=df[close],
            step=0.02,
            max_step=0.20,
            fillna=fillna,
        )
        # df[f'{colprefix}trend_psar'] = indicator.psar()
        df[f"{colprefix}trend_psar_up"] = indicator_psar.psar_up()
        df[f"{colprefix}trend_psar_down"] = indicator_psar.psar_down()
        df[f"{colprefix}trend_psar_up_indicator"] = indicator_psar.psar_up_indicator(
        )
        df[f"{colprefix}trend_psar_down_indicator"] = indicator_psar.psar_down_indicator(
        )

    return df
Esempio n. 18
0
            indicator_bb = BollingerBands(close=df["adj_close"], window=20, window_dev=2)
            # Add Bollinger Bands features
            df['bb_bbm'] = indicator_bb.bollinger_mavg()
            df['bb_bbh'] = indicator_bb.bollinger_hband()
            df['bb_bbl'] = indicator_bb.bollinger_lband()

            # EMA Indicator
            indicator_ema_200 = EMAIndicator(close=df["adj_close"], window=200)
            df['ema_200'] = indicator_ema_200.ema_indicator()
            indicator_ema_100 = EMAIndicator(close=df["adj_close"], window=100)
            df['ema_100'] = indicator_ema_100.ema_indicator()
            indicator_ema_50 = EMAIndicator(close=df["adj_close"], window=50)
            df['ema_50'] = indicator_ema_50.ema_indicator()

            # SMA Indicator
            indicator_sma_200 = SMAIndicator(close=df["adj_close"], window=200)
            df['sma_200'] = indicator_sma_200.sma_indicator()
            indicator_sma_100 = SMAIndicator(close=df["adj_close"], window=100)
            df['sma_100'] = indicator_sma_100.sma_indicator()
            indicator_sma_50 = SMAIndicator(close=df["adj_close"], window=50)
            df['sma_50'] = indicator_sma_50.sma_indicator()

            # RSI Indicator
            indicator_rsi_6 = RSIIndicator(close=df["adj_close"], window=6)
            df['rsi_6'] = indicator_rsi_6.rsi()
            indicator_rsi_14 = RSIIndicator(close=df["adj_close"], window=14)
            df['rsi_14'] = indicator_rsi_14.rsi()

            # MACD Indicator
            indicator_macd = MACD(close=df["adj_close"])
            df['macd'] = indicator_macd.macd()
Esempio n. 19
0
    def getDailyData(self, symbol, since, debug=False):
        #try:
        print("GET IBKR INTRADAY DATA")
        now = datetime.now()

        print("CREATE CONTRACT FOR %s" % symbol)
        contract = IBMarket.conn.createStockContract(symbol)

        dirname = os.path.dirname(__file__)
        filename = "data/daily/" + symbol + "_" + now.strftime(
            "%Y_%m_%d") + "_1_day.csv"

        if os.path.isfile(filename) == True:
            os.remove(filename)

        print("REQUEST HISTORICAL DATA FOR %s SINCE %s" % (symbol, since))
        IBMarket.market_data_available[symbol] = False
        IBMarket.conn.requestHistoricalData(contract,
                                            rth=self.rth,
                                            resolution="1 day",
                                            lookback="12 M",
                                            csv_path='data/daily/')

        print("WAIT FOR DATA...")
        while IBMarket.market_data_available[symbol] == False:
            time.sleep(5)
        IBMarket.market_data_available[symbol] = False

        print("RENAME CSV FILE TO %s" % filename)
        os.rename("data/daily/" + symbol + ".csv", filename)

        print("READ CSV FILE:%s" % filename)
        self.data = pd.read_csv(filename,
                                index_col='datetime',
                                parse_dates=True)
        self.data = self.data.sort_index()
        if debug:
            print("===VALUES===")
            print(self.data)

        print("===CALCULATE ALL TA===")
        self.data = ta.utils.dropna(self.data)
        self.data['sma20'] = SMAIndicator(close=self.data['C'],
                                          n=20,
                                          fillna=True).sma_indicator()
        self.data['sma50'] = SMAIndicator(close=self.data['C'],
                                          n=50,
                                          fillna=True).sma_indicator()
        self.data['sma100'] = SMAIndicator(close=self.data['C'],
                                           n=100,
                                           fillna=True).sma_indicator()
        self.data['sma200'] = SMAIndicator(close=self.data['C'],
                                           n=200,
                                           fillna=True).sma_indicator()

        self.data['ema9'] = EMAIndicator(close=self.data['C'],
                                         n=9,
                                         fillna=True).ema_indicator()
        self.data['ema20'] = EMAIndicator(close=self.data['C'],
                                          n=20,
                                          fillna=True).ema_indicator()

        self.data = ta.add_all_ta_features(self.data,
                                           open="O",
                                           high="H",
                                           low="L",
                                           close="C",
                                           volume="V",
                                           fillna=True)

        self.data = self.data.loc[self.data.index >= since]

        print("===CALCULATE DOJI===")
        candle_names = talib.get_function_groups()['Pattern Recognition']
        included_items = ('CDLDOJI', 'CDLHAMMER', 'CDLEVENINGSTAR',
                          'CDLHANGINGMAN', 'CDLSHOOTINGSTAR')
        candle_names = [
            candle for candle in candle_names if candle in included_items
        ]

        for candle in candle_names:
            self.data[candle] = getattr(talib,
                                        candle)(self.data['O'], self.data['H'],
                                                self.data['L'], self.data['C'])

        if debug:
            print("===VALUES===")
            print(self.data[[
                'C', 'V', 'WAP', 'sma20', 'sma50', 'sma100', 'sma200',
                'momentum_rsi', 'trend_cci', 'momentum_stoch_signal',
                'trend_adx'
            ]])

        #except:
        #	print("ERROR GETTING MARKET DATA")
        #	time.sleep(60)
        #	self.data = None

        #30 request / 10 mins -> need to wait 20 secs
        time.sleep(20)
        return self.data
Esempio n. 20
0
    def getIntraDayData(self, symbol, since, period, live=False, debug=False):
        #try:
        print("GET IBKR INTRADAY DATA")
        if live == True:
            lookback = int(period * 1600 / 60 / 24)
        else:
            dtime = datetime.now() - datetime.strptime(since, '%Y-%m-%d')
            #lookback = dtime.total_seconds() / 60 / 60 / 24
            lookback = 7

        now = datetime.now()

        print("CREATE CONTRACT FOR %s" % symbol)
        contract = IBMarket.conn.createStockContract(symbol)

        dirname = os.path.dirname(__file__)
        filename = "data/intraday/" + symbol + "_" + now.strftime(
            "%Y_%m_%d") + "_" + str(period) + "_min.csv"

        if os.path.isfile(filename) == True:
            os.remove(filename)

        print(
            "REQUEST HISTORICAL DATA FOR %s, LOOKBACK %d DAYS, SINCE %s, RTH:%d"
            % (symbol, lookback, since, self.rth))
        IBMarket.market_data_available[symbol] = False
        IBMarket.conn.requestHistoricalData(contract,
                                            rth=self.rth,
                                            resolution=str(period) + " mins",
                                            lookback=str(lookback) + " D",
                                            csv_path='data/intraday/')

        print("WAIT FOR DATA...")
        while IBMarket.market_data_available[symbol] == False:
            time.sleep(5)
        IBMarket.market_data_available[symbol] = False

        print("RENAME CSV FILE")
        os.rename("data/intraday/" + symbol + ".csv", filename)

        print("READ CSV DATA")
        self.data = pd.read_csv(filename,
                                index_col='datetime',
                                parse_dates=True)
        self.data = self.data.sort_index()
        self.data = self.data[:-1]
        if debug:
            print("===VALUES===")
            print(self.data)

        print("===CALCULATE SMA===")
        # add trend indicator.
        #self.data['sma20'] = self.data['C'].rolling(20, min_periods=20).mean()
        #self.data['sma50'] = self.data['C'].rolling(50, min_periods=50).mean()
        #self.data['sma100'] = self.data['C'].rolling(100, min_periods=100).mean()
        #self.data['sma200'] = self.data['C'].rolling(200, min_periods=200).mean()

        print("===CALCULATE ALL TA===")
        self.data = ta.utils.dropna(self.data)
        self.data['sma20'] = SMAIndicator(close=self.data['C'],
                                          n=20,
                                          fillna=True).sma_indicator()
        self.data['sma50'] = SMAIndicator(close=self.data['C'],
                                          n=50,
                                          fillna=True).sma_indicator()
        self.data['sma100'] = SMAIndicator(close=self.data['C'],
                                           n=100,
                                           fillna=True).sma_indicator()
        self.data['sma200'] = SMAIndicator(close=self.data['C'],
                                           n=200,
                                           fillna=True).sma_indicator()

        self.data['ema9'] = EMAIndicator(close=self.data['C'],
                                         n=9,
                                         fillna=True).ema_indicator()
        self.data['ema20'] = EMAIndicator(close=self.data['C'],
                                          n=20,
                                          fillna=True).ema_indicator()

        self.data = ta.add_all_ta_features(self.data,
                                           open="O",
                                           high="H",
                                           low="L",
                                           close="C",
                                           volume="V",
                                           fillna=True)

        self.data['mvwap'] = self.data['volume_vwap'].rolling(
            14, min_periods=14).mean()
        self.data = self.data.loc[self.data.index >= since]

        print("===CALCULATE DOJI===")
        candle_names = talib.get_function_groups()['Pattern Recognition']
        if debug:
            print(candle_names)

        included_items = ('CDLDOJI', 'CDLHAMMER', 'CDLEVENINGSTAR',
                          'CDLHANGINGMAN', 'CDLSHOOTINGSTAR')
        candle_names = [
            candle for candle in candle_names if candle in included_items
        ]

        for candle in candle_names:
            self.data[candle] = getattr(talib,
                                        candle)(self.data['O'], self.data['H'],
                                                self.data['L'], self.data['C'])

        if debug:
            print("===VALUES===")
            print(self.data)

        #except:
        #    print("ERROR GETTING MARKET DATA")
        #    time.sleep(60)
        #    self.data = None

        return self.data
Esempio n. 21
0
def add_trend_ta(df: pd.DataFrame,
                 high: str,
                 low: str,
                 close: str,
                 fillna: bool = False,
                 colprefix: str = "") -> pd.DataFrame:
    """Add trend technical analysis features to dataframe.

    Args:
        df (pandas.core.frame.DataFrame): Dataframe base.
        high (str): Name of 'high' column.
        low (str): Name of 'low' column.
        close (str): Name of 'close' column.
        fillna(bool): if True, fill nan values.
        colprefix(str): Prefix column names inserted

    Returns:
        pandas.core.frame.DataFrame: Dataframe with new features.
    """

    # MACD
    indicator_macd = MACD(close=df[close],
                          n_slow=26,
                          n_fast=12,
                          n_sign=9,
                          fillna=fillna)
    df[f'{colprefix}trend_macd'] = indicator_macd.macd()
    df[f'{colprefix}trend_macd_signal'] = indicator_macd.macd_signal()
    df[f'{colprefix}trend_macd_diff'] = indicator_macd.macd_diff()

    # SMAs
    df[f'{colprefix}trend_sma_fast'] = SMAIndicator(
        close=df[close], n=12, fillna=fillna).sma_indicator()
    df[f'{colprefix}trend_sma_slow'] = SMAIndicator(
        close=df[close], n=26, fillna=fillna).sma_indicator()

    # EMAs
    df[f'{colprefix}trend_ema_fast'] = EMAIndicator(
        close=df[close], n=12, fillna=fillna).ema_indicator()
    df[f'{colprefix}trend_ema_slow'] = EMAIndicator(
        close=df[close], n=26, fillna=fillna).ema_indicator()

    # Average Directional Movement Index (ADX)
    indicator = ADXIndicator(high=df[high],
                             low=df[low],
                             close=df[close],
                             n=14,
                             fillna=fillna)
    df[f'{colprefix}trend_adx'] = indicator.adx()
    df[f'{colprefix}trend_adx_pos'] = indicator.adx_pos()
    df[f'{colprefix}trend_adx_neg'] = indicator.adx_neg()

    # Vortex Indicator
    indicator = VortexIndicator(high=df[high],
                                low=df[low],
                                close=df[close],
                                n=14,
                                fillna=fillna)
    df[f'{colprefix}trend_vortex_ind_pos'] = indicator.vortex_indicator_pos()
    df[f'{colprefix}trend_vortex_ind_neg'] = indicator.vortex_indicator_neg()
    df[f'{colprefix}trend_vortex_ind_diff'] = indicator.vortex_indicator_diff()

    # TRIX Indicator
    indicator = TRIXIndicator(close=df[close], n=15, fillna=fillna)
    df[f'{colprefix}trend_trix'] = indicator.trix()

    # Mass Index
    indicator = MassIndex(high=df[high],
                          low=df[low],
                          n=9,
                          n2=25,
                          fillna=fillna)
    df[f'{colprefix}trend_mass_index'] = indicator.mass_index()

    # CCI Indicator
    indicator = CCIIndicator(high=df[high],
                             low=df[low],
                             close=df[close],
                             n=20,
                             c=0.015,
                             fillna=fillna)
    df[f'{colprefix}trend_cci'] = indicator.cci()

    # DPO Indicator
    indicator = DPOIndicator(close=df[close], n=20, fillna=fillna)
    df[f'{colprefix}trend_dpo'] = indicator.dpo()

    # KST Indicator
    indicator = KSTIndicator(close=df[close],
                             r1=10,
                             r2=15,
                             r3=20,
                             r4=30,
                             n1=10,
                             n2=10,
                             n3=10,
                             n4=15,
                             nsig=9,
                             fillna=fillna)
    df[f'{colprefix}trend_kst'] = indicator.kst()
    df[f'{colprefix}trend_kst_sig'] = indicator.kst_sig()
    df[f'{colprefix}trend_kst_diff'] = indicator.kst_diff()

    # Ichimoku Indicator
    indicator = IchimokuIndicator(high=df[high],
                                  low=df[low],
                                  n1=9,
                                  n2=26,
                                  n3=52,
                                  visual=False,
                                  fillna=fillna)
    df[f'{colprefix}trend_ichimoku_a'] = indicator.ichimoku_a()
    df[f'{colprefix}trend_ichimoku_b'] = indicator.ichimoku_b()
    indicator = IchimokuIndicator(high=df[high],
                                  low=df[low],
                                  n1=9,
                                  n2=26,
                                  n3=52,
                                  visual=True,
                                  fillna=fillna)
    df[f'{colprefix}trend_visual_ichimoku_a'] = indicator.ichimoku_a()
    df[f'{colprefix}trend_visual_ichimoku_b'] = indicator.ichimoku_b()

    # Aroon Indicator
    indicator = AroonIndicator(close=df[close], n=25, fillna=fillna)
    df[f'{colprefix}trend_aroon_up'] = indicator.aroon_up()
    df[f'{colprefix}trend_aroon_down'] = indicator.aroon_down()
    df[f'{colprefix}trend_aroon_ind'] = indicator.aroon_indicator()

    # PSAR Indicator
    indicator = PSARIndicator(high=df[high],
                              low=df[low],
                              close=df[close],
                              step=0.02,
                              max_step=0.20,
                              fillna=fillna)
    df[f'{colprefix}trend_psar'] = indicator.psar()
    df[f'{colprefix}trend_psar_up'] = indicator.psar_up()
    df[f'{colprefix}trend_psar_down'] = indicator.psar_down()
    df[f'{colprefix}trend_psar_up_indicator'] = indicator.psar_up_indicator()
    df[f'{colprefix}trend_psar_down_indicator'] = indicator.psar_down_indicator(
    )

    return df
Esempio n. 22
0
def get_sma(data, days):
    """
    Function to get the EMA of a ticker
    """
    sma = SMAIndicator(close=data, n=days)
    return sma.sma_indicator()
    def handle(self, *args, **options):
        # import pdb
        # pdb.set_trace()
        if not options['update']:
            NSETechnical.objects.all().delete()
        symbols = Symbol.objects.all()
        for symbol in symbols:
            nse_history_data = NSEHistoricalData.objects.filter(
                symbol__symbol_name=symbol).order_by('timestamp')
            if not nse_history_data:
                continue
            nse_technical = pd.DataFrame(
                list(
                    nse_history_data.values('timestamp', 'open', 'high', 'low',
                                            'close', 'total_traded_quantity')))
            '''
                Moving average convergence divergence
            '''
            indicator_macd = MACD(close=nse_technical['close'],
                                  window_slow=26,
                                  window_fast=12,
                                  window_sign=9,
                                  fillna=False)
            nse_technical["trend_macd"] = indicator_macd.macd()
            nse_technical["trend_macd_signal"] = indicator_macd.macd_signal()
            nse_technical["trend_macd_diff"] = indicator_macd.macd_diff()
            '''
                Simple Moving Average
            '''
            nse_technical["trend_sma_fast"] = SMAIndicator(
                close=nse_technical['close'], window=12,
                fillna=False).sma_indicator()
            nse_technical["trend_sma_slow"] = SMAIndicator(
                close=nse_technical['close'], window=26,
                fillna=False).sma_indicator()
            '''
                Exponential Moving Average
            '''
            nse_technical["trend_ema_fast"] = EMAIndicator(
                close=nse_technical['close'], window=12,
                fillna=False).ema_indicator()
            nse_technical["trend_ema_slow"] = EMAIndicator(
                close=nse_technical['close'], window=26,
                fillna=False).ema_indicator()
            '''
                Ichimoku Indicator
            '''
            indicator_ichi = IchimokuIndicator(
                high=nse_technical['high'],
                low=nse_technical['low'],
                window1=9,
                window2=26,
                window3=52,
                visual=False,
                fillna=False,
            )
            nse_technical[
                "trend_ichimoku_conv"] = indicator_ichi.ichimoku_conversion_line(
                )
            nse_technical[
                "trend_ichimoku_base"] = indicator_ichi.ichimoku_base_line()
            nse_technical["trend_ichimoku_a"] = indicator_ichi.ichimoku_a()
            nse_technical["trend_ichimoku_b"] = indicator_ichi.ichimoku_b()
            indicator_ichi_visual = IchimokuIndicator(
                high=nse_technical['high'],
                low=nse_technical['low'],
                window1=9,
                window2=26,
                window3=52,
                visual=True,
                fillna=False,
            )
            nse_technical[
                "trend_visual_ichimoku_a"] = indicator_ichi_visual.ichimoku_a(
                )
            nse_technical[
                "trend_visual_ichimoku_b"] = indicator_ichi_visual.ichimoku_b(
                )
            '''
                Bollinger Band
            '''
            indicator_bb = BollingerBands(close=nse_technical['close'],
                                          window=20,
                                          window_dev=2,
                                          fillna=False)
            nse_technical["volatility_bbm"] = indicator_bb.bollinger_mavg()
            nse_technical["volatility_bbh"] = indicator_bb.bollinger_hband()
            nse_technical["volatility_bbl"] = indicator_bb.bollinger_lband()
            nse_technical["volatility_bbw"] = indicator_bb.bollinger_wband()
            nse_technical["volatility_bbp"] = indicator_bb.bollinger_pband()
            nse_technical[
                "volatility_bbhi"] = indicator_bb.bollinger_hband_indicator()
            nse_technical[
                "volatility_bbli"] = indicator_bb.bollinger_lband_indicator()
            '''
                Accumulation Distribution Index
            '''
            nse_technical["volume_adi"] = AccDistIndexIndicator(
                high=nse_technical['high'],
                low=nse_technical['low'],
                close=nse_technical['close'],
                volume=nse_technical['total_traded_quantity'],
                fillna=False).acc_dist_index()
            '''
                Money Flow Index
            '''
            nse_technical["volume_mfi"] = MFIIndicator(
                high=nse_technical['high'],
                low=nse_technical['low'],
                close=nse_technical['close'],
                volume=nse_technical['total_traded_quantity'],
                window=14,
                fillna=False,
            ).money_flow_index()
            '''
                Relative Strength Index (RSI)
            '''
            nse_technical["momentum_rsi"] = RSIIndicator(
                close=nse_technical['close'], window=14, fillna=False).rsi()
            '''
                Stoch RSI (StochRSI)
            '''
            indicator_srsi = StochRSIIndicator(close=nse_technical['close'],
                                               window=14,
                                               smooth1=3,
                                               smooth2=3,
                                               fillna=False)
            nse_technical["momentum_stoch_rsi"] = indicator_srsi.stochrsi()
            nse_technical["momentum_stoch_rsi_k"] = indicator_srsi.stochrsi_k()
            nse_technical["momentum_stoch_rsi_d"] = indicator_srsi.stochrsi_d()

            nse_technical.replace({np.nan: None}, inplace=True)
            nse_technical.replace([np.inf, -np.inf], None, inplace=True)
            list_to_create = []
            list_to_update = []
            for index in range(len(nse_history_data) - 1, -1, -1):
                data = nse_history_data[index]
                if data.technicals:
                    break
                technical = NSETechnical(
                    nse_historical_data=data,
                    trend_macd=nse_technical['trend_macd'][index],
                    trend_macd_signal=nse_technical['trend_macd_signal']
                    [index],
                    trend_macd_diff=nse_technical['trend_macd_diff'][index],
                    trend_sma_fast=nse_technical['trend_sma_fast'][index],
                    trend_sma_slow=nse_technical['trend_sma_slow'][index],
                    trend_ema_fast=nse_technical['trend_ema_fast'][index],
                    trend_ema_slow=nse_technical['trend_ema_slow'][index],
                    trend_ichimoku_conv=nse_technical['trend_ichimoku_conv']
                    [index],
                    trend_ichimoku_base=nse_technical['trend_ichimoku_base']
                    [index],
                    trend_ichimoku_a=nse_technical['trend_ichimoku_a'][index],
                    trend_ichimoku_b=nse_technical['trend_ichimoku_b'][index],
                    trend_visual_ichimoku_a=nse_technical[
                        'trend_visual_ichimoku_a'][index],
                    trend_visual_ichimoku_b=nse_technical[
                        'trend_visual_ichimoku_b'][index],
                    volatility_bbm=nse_technical['volatility_bbm'][index],
                    volatility_bbh=nse_technical['volatility_bbh'][index],
                    volatility_bbl=nse_technical['volatility_bbl'][index],
                    volatility_bbw=nse_technical['volatility_bbw'][index],
                    volatility_bbp=nse_technical['volatility_bbp'][index],
                    volatility_bbhi=nse_technical['volatility_bbhi'][index],
                    volatility_bbli=nse_technical['volatility_bbli'][index],
                    volume_adi=nse_technical['volume_adi'][index],
                    volume_mfi=nse_technical['volume_mfi'][index],
                    momentum_rsi=nse_technical['momentum_rsi'][index],
                    momentum_stoch_rsi=nse_technical['momentum_stoch_rsi']
                    [index],
                    momentum_stoch_rsi_k=nse_technical['momentum_stoch_rsi_k']
                    [index],
                    momentum_stoch_rsi_d=nse_technical['momentum_stoch_rsi_d']
                    [index])
                data.technicals = True
                list_to_update.append(data)
                list_to_create.append(technical)
            NSETechnical.objects.bulk_create(list_to_create)
            NSEHistoricalData.objects.bulk_update(list_to_update,
                                                  ['technicals'])
            print(f"Technicals updated for {symbol}")
    def __init__(self, symbols):

        # data = json.loads(symbols)
        # df_stock = pd.json_normalize(symbols)
        # df_stock = pd.read_csv(fn,names = ['sym']).drop_duplicates()
        df_stock = pd.DataFrame(symbols)
        ls_stock = df_stock['sym'].to_list()

        df_stock = df_stock.reset_index()

        df_stock.columns = ['sort', 'sym']

        df_stock.head()

        # In[3]:

        start = dt.date.today() + relativedelta(days=-150)
        end = dt.date.today() + relativedelta(days=-0)

        ls_tickers = ls_stock

        ls_df = []
        for ticker in ls_tickers:
            try:
                df = web.DataReader(ticker, 'yahoo', start, end)
            except Exception as e:
                print(str(e))
                continue
            df['sym'] = ticker
            ls_df.append(df.copy())

        df_price = pd.concat(ls_df).reset_index()
        df_price.columns = [
            'dte', 'hgh', 'low', 'opn', 'cls', 'vol', 'cls_adj', 'sym'
        ]
        df_price.sort_values(['sym', 'dte'], inplace=True)

        df_price = df_price[['dte', 'sym', 'hgh', 'low', 'cls', 'vol']].copy()

        df_price['curr'] = end

        df_price['curr'] = pd.to_datetime(df_price['curr'])
        df_price['dte'] = pd.to_datetime(df_price['dte'])

        df_price['ndays'] = (df_price['curr'] - df_price['dte']).dt.days

        df_price['ndays'] = df_price.groupby(['sym'])['ndays'].rank()

        df_price[df_price['sym'] == 'SPY'].head()

        # In[4]:

        ls_df = []
        ls_tickers = ls_stock

        for ticker in ls_tickers:

            #df = dropna(df_price[df_price['sym']==ticker])
            df = df_price[df_price['sym'] == ticker].copy()

            indicator_bb = BollingerBands(close=df['cls'],
                                          window=20,
                                          window_dev=2)
            indicator_macd = MACD(close=df['cls'],
                                  window_fast=12,
                                  window_slow=26,
                                  window_sign=9)
            indicator_rsi14 = RSIIndicator(close=df['cls'], window=14)
            indicator_cci20 = cci(high=df['hgh'],
                                  low=df['low'],
                                  close=df['cls'],
                                  window=20,
                                  constant=0.015)
            indicator_obv = OnBalanceVolumeIndicator(close=df['cls'],
                                                     volume=df['vol'],
                                                     fillna=True)

            indicator_vol_sma20 = SMAIndicator(close=df['vol'], window=20)

            indicator_ema03 = EMAIndicator(close=df['cls'], window=3)
            indicator_ema05 = EMAIndicator(close=df['cls'], window=5)
            indicator_ema08 = EMAIndicator(close=df['cls'], window=8)
            indicator_ema10 = EMAIndicator(close=df['cls'], window=10)
            indicator_ema12 = EMAIndicator(close=df['cls'], window=12)
            indicator_ema15 = EMAIndicator(close=df['cls'], window=15)
            indicator_ema30 = EMAIndicator(close=df['cls'], window=30)
            indicator_ema35 = EMAIndicator(close=df['cls'], window=35)
            indicator_ema40 = EMAIndicator(close=df['cls'], window=40)
            indicator_ema45 = EMAIndicator(close=df['cls'], window=45)
            indicator_ema50 = EMAIndicator(close=df['cls'], window=50)
            indicator_ema60 = EMAIndicator(close=df['cls'], window=60)

            # Add Bollinger Band high indicator
            df['bb_bbhi'] = indicator_bb.bollinger_hband_indicator()

            # Add Bollinger Band low indicator
            df['bb_bbli'] = indicator_bb.bollinger_lband_indicator()

            #df['macd'] = indicator_macd.macd()
            df['macd'] = indicator_macd.macd_diff()
            #df['macd_signal'] = indicator_macd.macd_signal()

            df['obv'] = indicator_obv.on_balance_volume()

            df['vol_sma20'] = indicator_vol_sma20.sma_indicator()

            df['ema03'] = indicator_ema03.ema_indicator()
            df['ema05'] = indicator_ema05.ema_indicator()
            df['ema08'] = indicator_ema08.ema_indicator()
            df['ema10'] = indicator_ema10.ema_indicator()
            df['ema12'] = indicator_ema12.ema_indicator()
            df['ema15'] = indicator_ema15.ema_indicator()
            df['ema30'] = indicator_ema30.ema_indicator()
            df['ema35'] = indicator_ema35.ema_indicator()
            df['ema40'] = indicator_ema40.ema_indicator()
            df['ema45'] = indicator_ema45.ema_indicator()
            df['ema50'] = indicator_ema50.ema_indicator()
            df['ema60'] = indicator_ema60.ema_indicator()

            df['rsi14'] = indicator_rsi14.rsi()
            df['cci20'] = indicator_cci20

            ls_df.append(df.copy())

        df = pd.concat(ls_df)

        df['score_vol_sma20'] = df[['vol',
                                    'vol_sma20']].apply(lambda x: x[0] / x[1],
                                                        axis=1)

        df['emash_min'] = df[[
            'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15'
        ]].min(axis=1)
        df['emash_max'] = df[[
            'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15'
        ]].max(axis=1)
        df['emash_avg'] = df[[
            'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15'
        ]].mean(axis=1)

        #df['score_short'] = df[['cls','emash_min','emash_max','emash_min']].apply(lambda x: 100 * (x[0]-x[1])/(x[2]-x[3]),axis=1)

        df['emalg_min'] = df[[
            'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60'
        ]].min(axis=1)
        df['emalg_max'] = df[[
            'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60'
        ]].max(axis=1)
        df['emalg_avg'] = df[[
            'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60'
        ]].mean(axis=1)

        #df['score_long'] = df[['cls','emalg_min','emalg_max','emalg_min']].apply(lambda x: 100 * (x[0]-x[1])/(x[2]-x[3]),axis=1)

        df['ema_min'] = df[[
            'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15', 'ema30',
            'ema35', 'ema40', 'ema45', 'ema50', 'ema60'
        ]].min(axis=1)
        df['ema_max'] = df[[
            'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15', 'ema30',
            'ema35', 'ema40', 'ema45', 'ema50', 'ema60'
        ]].max(axis=1)

        df['score_ovlp_ema'] = df[[
            'emash_min', 'emalg_max', 'ema_max', 'ema_min'
        ]].apply(lambda x: 100 * (x[0] - x[1]) / (x[2] - x[3]), axis=1)

        df = pd.merge(df_stock, df, on=['sym'],
                      how='inner').sort_values(['sort', 'ndays'])

        decimals = pd.Series([1, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0],
                             index=[
                                 'cls', 'ndays', 'vol', 'score_vol_sma20',
                                 'bb_bbhi', 'bb_bbli', 'macd', 'obv', 'rsi14',
                                 'cci20', 'score_ovlp_ema'
                             ])

        cols = [
            'ndays', 'dte', 'sort', 'sym', 'cls', 'vol', 'score_vol_sma20',
            'bb_bbhi', 'bb_bbli', 'macd', 'obv', 'rsi14', 'cci20',
            'score_ovlp_ema'
        ]

        df = df[df['ndays'] <= 10][cols].round(decimals).copy()

        print(df['score_ovlp_ema'].min(), df['score_ovlp_ema'].max())

        df[df['sym'] == 'QQQ'].head(50)
        self.df = df
Esempio n. 25
0
def SMA(df,n):
    sma = SMAIndicator(df['Close'],n)
    df['SMA-'+str(n)] = sma.sma_indicator()
    return df