def write_technical_analysis(symbols_list): for symbol in symbols_list: try: file_path = f'data/ohlc/{symbol}.csv' dataframe = pd.read_csv(file_path, parse_dates=True, index_col='Timestamp') sma6 = btalib.sma(dataframe, period=6) sma9 = btalib.sma(dataframe, period=9) sma10 = btalib.sma(dataframe, period=10) sma20 = btalib.sma(dataframe, period=20) rsi = btalib.rsi(dataframe) macd = btalib.macd(dataframe) sub_SMA6_SMA20 = btalib.sub(sma6, sma20) dataframe['SMA-6'] = round(sma6.df, 3) dataframe['SMA-9'] = round(sma9.df, 3) dataframe['SMA-10'] = round(sma10.df, 3) dataframe['SMA-20'] = round(sma20.df, 3) dataframe['RSI'] = round(rsi.df, 2) dataframe['MACD'] = round(macd.df['macd'], 2) dataframe['Signal'] = round(macd.df['signal'], 2) dataframe['Histogram'] = macd.df['histogram'] dataframe['Delta_SMA6_SMA20'] = round(sub_SMA6_SMA20.df, 4) f = open(file_path, 'w+') dataframe.to_csv(file_path, sep=',', index=True) f.close() except: print(f'{symbol} is not writing the technical data.')
def calculate_indicator(self): # RSI 15 Indicator if self.indicator == 'RSI': self.klines['rsi'] = btalib.rsi(self.klines.close, period=15).df # SMA 12-50 Indicator elif self.indicator == 'SMA': self.klines['12sma'] = btalib.sma(self.klines.close, period=12).df self.klines['50sma'] = btalib.sma(self.klines.close, period=50).df # MACD 12-26-9 Indicator elif self.indicator == 'MACD': self.klines['macd'], self.klines['macdSignal'], self.klines[ 'macdHist'] = btalib.macd(self.klines.close, pfast=12, pslow=26, psignal=9) else: return None
def getFftnMinData(self): fftnMinBarUrl = '{}/15Min?symbols={}&limit=120'.format(config.BARS_URL, self.tickers) r = requests.get(fftnMinBarUrl, headers=config.HEADERS) data = r.json() for symbol in data: filename = 'data/15MinOHLC/{}.json'.format(symbol) f = open(filename, 'w+') f.write('{' + '"{}": '.format(symbol) + json.dumps(data[symbol], indent=4) + '}') f.close() for symbol in data: filename = 'data/15MinOHLC/{}.txt'.format(symbol) f = open(filename, 'w+') f.write('Date,Open,High,Low,Close,Volume,OpenInterest\n') tNow = datetime.now() tDelta = timedelta(days=119) tStart = tNow - tDelta tdAdd = timedelta(days=1) tPresent = tStart for bar in data[symbol]: day = tPresent.strftime('%Y-%m-%d') line = '{},{},{},{},{},{},{}\n'.format(day, bar['o'], bar['h'], bar['l'], bar['c'], bar['v'], 0.00) f.write(line) tPresent = tPresent + tdAdd f.close() print('Fetched fifteen minute data from alpaca api') for symbol in config.symbols: df = pd.read_csv('data/15MinOHLC/{}.txt'.format(symbol), parse_dates=True, index_col='Date') rsi = btalib.rsi(df) df['rsi'] = rsi.df stochastic = btalib.stochastic(df) df['stoc k'] = stochastic.df['k'] df['stoc d'] = stochastic.df['d'] macd = btalib.macd(df) df['macd'] = macd.df['macd'] df['signal'] = macd.df['signal'] df['histogram'] = macd.df['histogram'] df.to_csv(path_or_buf='data/15MinOHLC/{}.txt'.format(symbol), index ='Entry') #, index='Date' print('Finished generating rsi and stochastic oscillator.')
import btalib import pandas as pd import pandas_datareader.data as pdr import datetime as dt df = pdr.DataReader('BTC-USD', 'yahoo', start=(dt.datetime(2020, 1, 1)), end=(dt.datetime(2020, 10, 1))) #df = pd.read_csv('data/ohlc/AAPL.csv', parse_dates=True, index_col='Date') sma = btalib.sma(df, period=50) df['sma'] = sma.df rsi = btalib.rsi(df) df['rsi'] = rsi.df print(df)
def ema(self): for frame in self._stock_prices.stock_frames: ema = bt.rsi(frame.data).df frame.data['ema'] = ema
def rsi(self): for frame in self._stock_prices.stock_frames: rsi = bt.rsi(frame.data).df frame.data['rsi'] = rsi
def create_indicator(cls, klines: pd.DataFrame, periode: int = 14): """create RSI indicator""" rsiInd = btalib.rsi(klines.copy(), period=periode) # rsiInd.df.to_csv(f"{KLINEPATH}", index=True, na_rep=0) # enregistrer dans le fichier return rsiInd.df
# get the highest closing price in 2020 max_val = btc_df.close['2020'].max() print(mean) print(max_val) # technical indicators using bta-lib # sma sma = btalib.sma(btc_df.close) print(sma.df) # create sma and attach as column to original df btc_df['sma'] = btalib.sma(btc_df.close, period=20).df print(btc_df.tail()) # calculate rsi and macd rsi = btalib.rsi(btc_df, period=14) macd = btalib.macd(btc_df, pfast=20, pslow=50, psignal=13) print(rsi.df) print(macd.df) # access last rsi value print(rsi.df.rsi[-1]) # join the rsi and macd calculations as columns in original df btc_df = btc_df.join([rsi.df, macd.df]) print(btc_df.tail())
def tech_ind_features(data): """ Generate technical indicators * :param data(pd.DataFrame): Raw data for processing :return transformed_df(pd.DataFrame): Dataframe of features, sample balanced and normalised """ data['smoothed_close'] = data.close.rolling(9).mean().rolling(21).mean().shift(-15) data['dx'] = np.diff(data['smoothed_close'], prepend=data['smoothed_close'][0]) data['dx_signal'] = pd.Series(data['dx']).rolling(9).mean() data['ddx'] = np.diff(np.diff(data['smoothed_close']), prepend=data['smoothed_close'][0:2]) data['labels'] = np.zeros(len(data)) data['labels'].iloc[[(data.ddx < 0.1) & (data.dx <= 0) & (data.dx_signal > 0)]] = 1 data['labels'].iloc[[(data.ddx > -0.075) & (data.dx >= 0) & (data.dx_signal < 0)]] = 2 #Filter and drop all columns except close price, volume and date (for indexing) relevant_cols = list( compress( data.columns, [False if i in [1, 3, 4, 5, 6, len(data.columns)-1] else True for i in range(len(data.columns))] ) ) data = data.drop(columns=relevant_cols).rename(columns={'open_date_time': 'date'}) data.set_index('date', inplace = True) #Define relevant periods for lookback/feature engineering periods = [ 9, 14, 21, 30, 45, 60, 90, 100, 120 ] #Construct technical features for image synthesis for period in periods: data[f'ema_{period}'] = btalib.ema( data.close, period = period ).df['ema'] data[f'ema_{period}_dx'] = np.append(np.nan, np.diff(btalib.ema( data.close, period = period ).df['ema'])) data[f'rsi_{period}'] = btalib.rsi( data.close, period = period ).df['rsi'] data[f'cci_{period}'] = btalib.cci( data.high, data.low, data.close, period = period ).df['cci'] data[f'macd_{period}'] = btalib.macd( data.close, pfast = period, pslow = period*2, psignal = int(period/3) ).df['macd'] data[f'signal_{period}'] = btalib.macd( data.close, pfast = period, pslow = period*2, psignal = int(period/3) ).df['signal'] data[f'hist_{period}'] = btalib.macd( data.close, pfast = period, pslow = period*2, psignal = int(period/3) ).df['histogram'] data[f'volume_{period}'] = btalib.sma( data.volume, period = period ).df['sma'] data[f'change_{period}'] = data.close.pct_change(periods = period) data = data.drop(data.query('labels == 0').sample(frac=.90).index) data = data.replace([np.inf, -np.inf], np.nan).dropna() data_trimmed = data.loc[:, 'ema_9':] data_trimmed = pd.concat( [data_trimmed, data_trimmed.shift(1), data_trimmed.shift(2)], axis = 1 ) mm_scaler = MinMaxScaler(feature_range=(0, 1)) transformed_data = mm_scaler.fit_transform(data_trimmed[24:]) transformed_data = np.c_[ transformed_data, pd.to_numeric( data.labels[24:], downcast = 'signed' ).to_list() ] return transformed_data