def PlotEMA(stock_ticker, close, window_long, window_short): # Getting values of EMA from the `ta` module EMA_Long = ema_indicator(close=close, window=int(window_long), fillna=False) EMA_Short = ema_indicator(close=close, window=int(window_short), fillna=False) # Creating DataFrame df = pd.DataFrame() # Adding all the required columns to the DataFrame df['Close'] = close df['EMA_Long'] = EMA_Long.dropna() df['EMA_Short'] = EMA_Short.dropna() df['Signal'] = 0 # Giving buy signals df.loc[(df['EMA_Short'] > df['EMA_Long']), 'Signal'] = 1 df['Position'] = df['Signal'].diff() # Plot EMA's wrt Close price plt.figure(figsize=(7, 6)) df['Close'].plot(color='k', lw=1, label='Close Price') df['EMA_Short'].plot(color='b', lw=1, label=f'{window_short}-period EMA') df['EMA_Long'].plot(color='g', lw=1, label=f'{window_long}-period EMA') # Plot Buy/Sell Indicators plt.plot(df[df['Position'] == 1].index, df['EMA_Short'][df['Position'] == 1], '^', markersize=15, color='g', label='buy') plt.plot(df[df['Position'] == -1].index, df['EMA_Short'][df['Position'] == -1], 'v', markersize=15, color='r', label='sell') plt.ylabel('Price in Rupees', fontsize=15) plt.xlabel('Date', fontsize=15) plt.title(f'EMA Crossover: {stock_ticker}', fontsize=20) plt.legend() plt.grid() try: plt.savefig(f'image/{stock_ticker}/EMA_{stock_ticker}.png', bbox_inches='tight') except: directory = f'image/{stock_ticker}' for f in os.listdir(stock_ticker): os.remove(os.path.join(directory, f)) plt.savefig(f'image/{stock_ticker}/EMA_{stock_ticker}.png', bbox_inches='tight')
def CCI(df0, n=160): df = df0 df['cci'] = ema_indicator(cci(df['high'], df['low'], df['close'], n, 0.015, fillna=False), 4, fillna=False) return df
def EMAvg(close, window_long, window_short): # Getting values of EMA from the `ta` module EMA_Long = ema_indicator(close=close, window=int(window_long), fillna=False) EMA_Short = ema_indicator(close=close, window=int(window_short), fillna=False) EMA_Long = EMA_Long.dropna() EMA_Short = EMA_Short.dropna() # Creating DataFrame df = pd.DataFrame() # Adding all the required columns to the DataFrame df['Close'] = close df['EMA_Long'] = EMA_Long df['EMA_Short'] = EMA_Short df['Signal'] = 0 # Giving buy signals df.loc[(df['EMA_Short'] > df['EMA_Long']), 'Signal'] = 1 df['Position'] = df['Signal'].diff() return df['Close'], df['Position']
def EMA(df0, n=4): df = df0 df['ema'] = ema_indicator(df['close'], n, fillna=False) return df
def calculate_Trend_Indicators(): JSON_sent = request.get_json() if len(JSON_sent[0]) > 0: df = pd.DataFrame(JSON_sent[0]) _, sma, ema, macdDict, macdSignal, adxDict, adxPos, adxNeg, VIpos, VIneg, trixDict, mi, dpoDict = JSON_sent if sma['displaySMA']: indicator_sma = sma_indicator(close=df['close'], n=sma['nForSMA']) df['sma'] = indicator_sma if ema['displayEMA']: indicator_ema = ema_indicator(close=df['close'], n=ema['nForEMA']) df['ema'] = indicator_ema if macdDict['displayMACD']: indicator_macd = macd(close=df['close'], n_slow=macdDict['nSlowForMACD'], n_fast=macdDict['nFastForMACD']) df['macd'] = indicator_macd if macdSignal['displayMACDsignal']: indicator_macdSignal = macd_signal( close=df['close'], n_slow=macdSignal['nSlowForMACDsignal'], n_fast=macdSignal['nFastForMACDsignal'], n_sign=macdSignal['nSignForMACDsignal']) df['macds'] = indicator_macdSignal if adxDict['displayADX']: indicator_ADX = adx(high=df['high'], low=df['low'], close=df['close'], n=adxDict['nForADX']) df['adx'] = indicator_ADX if adxPos['displayADXP']: indicator_ADXpositive = adx_pos(high=df['high'], low=df['low'], close=df['close'], n=adxPos['nForADXP']) df['adxp'] = indicator_ADXpositive if adxNeg['displayADXN']: indicator_ADXnegative = adx_neg(high=df['high'], low=df['low'], close=df['close'], n=adxNeg['nForADXN']) df['adxn'] = indicator_ADXnegative if VIpos['displayVIPOS']: indicator_VIpositive = vortex_indicator_pos(high=df['high'], low=df['low'], close=df['close'], n=VIpos['nForVIPOS']) df['vipos'] = indicator_VIpositive if VIneg['displayVINEG']: indicator_VInegative = vortex_indicator_neg(high=df['high'], low=df['low'], close=df['close'], n=VIneg['nForVINEG']) df['vineg'] = indicator_VInegative if trixDict['displayTRIX']: indicator_TRIX = trix(close=df['close'], n=trixDict['nForTRIX']) df['trix'] = indicator_TRIX if mi['displayMI']: indicator_MassIndex = mass_index(high=df['high'], low=df['low'], n=mi['nForMI'], n2=mi['n2ForMI']) df['mi'] = indicator_MassIndex if dpoDict['displayDPO']: indicator_dpo = dpo(close=df['close'], n=dpoDict['nForDPO']) df['dpo'] = indicator_dpo df.fillna(0, inplace=True) return (json.dumps(df.to_dict('records'))) else: df = pd.DataFrame([]) return (json.dumps(df.to_dict('records')))
df["ADX_indicator"] = "0" print("The ADX error is coming for SYMBOL" + f'{i}') # df['ADX'] = indicator.adx() #print(df) #print(df) """OBV""" indicator = on_balance_volume(close=df["close_price"], volume=df["volume"], fillna=True) #print(indicator) df["obv_indicator"] = indicator #print(df) """EMA""" indicator = ema_indicator(close=df["close_price"], n=12, fillna=True) #print(indicator) df["EMA_indicator"] = indicator #print(df) df2 = df2.append(df) df2.to_csv('MACD_price.csv', mode='w', header=False) table_name1 = 'TECHNICAL_ANALYSIS1' def pg_load_table(file_path, table_name1, dbname, host, port, user, pwd): ''' This function upload csv to a target table ''' try: conn = psycopg2.connect(dbname=dbname,
def __init__(self, N_day): super().__init__(indicator=lambda x: ema_indicator(x, N_day)) self.lookback_window = N_day