示例#1
0
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.')
示例#2
0
 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
示例#3
0
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.')
示例#4
0
import btalib
import pandas as pd

df = pd.read_csv('data/ohlc/SPSC.txt', parse_dates=True, index_col="Date")

sma = btalib.sma(df)
rsi = btalib.rsi(df)
macd = btalib.macd(df)

#SMA15 = btalib.sma(df, period=15)
#SMA50 = btalib.sma(df, period=50)
#SMA200 = btalib.sma(df, period=200)

#if SMA50 > SMA200:

df['sma'] = sma.df
df['rsi'] = rsi.df

#macd = ema(data, 12) - ema(data, 26)
#signal = ema(macd, 9)
#histogram = macd - signal

df['macd'] = macd.df['macd']
df['signal'] = macd.df['signal']
df['histogram'] = macd.df['histogram']

oversold_days = df[df['rsi'] < 30]
overbought_days = df[df['rsi'] > 60]
print(oversold_days)
print(overbought_days)
示例#5
0
def price_history(symbol_pair, time_step):

    client = client_test_net()

    if time_step == '5m':
        klines = client.get_historical_klines(symbol_pair,
                                              Client.KLINE_INTERVAL_5MINUTE,
                                              "2 day ago UTC")
    elif time_step == '15m':
        klines = client.get_historical_klines(symbol_pair,
                                              Client.KLINE_INTERVAL_15MINUTE,
                                              "6 day ago UTC")
    elif time_step == '30m':
        klines = client.get_historical_klines(symbol_pair,
                                              Client.KLINE_INTERVAL_30MINUTE,
                                              "12 day ago UTC")
    else:
        klines = client.get_historical_klines(symbol_pair,
                                              Client.KLINE_INTERVAL_1HOUR,
                                              "24 day ago UTC")
#   Remove unnecessary data
    for line in klines:
        del line[6:]
#   Generate csv file of RAW data
    df1 = pd.DataFrame(
        klines, columns=['date', 'open', 'high', 'low', 'close', 'volume'])
    df1.set_index('date', inplace=True)
    df1.to_csv('{}_history_{}.csv'.format(symbol_pair, time_step))
    #   Prepare to calculate indicator
    df = pd.read_csv('{}_history_{}.csv'.format(symbol_pair, time_step),
                     index_col=0)
    df.index = pd.to_datetime(df.index, unit='ms') + pd.DateOffset(hours=7)
    #   Average of high-low *note: is high-low-close better?
    df['avg'] = (df['high'] + df['low']) / 2
    #   Calculate moving average using Pandas
    df['50ma'] = df.close.rolling(50).mean()
    df['200ma'] = df.close.rolling(200).mean()
    #   VWAP
    df['vwap'] = 0
    volume = df['volume']
    high = df['high']
    low = df['low']
    close = df['close']
    typical_price = (high + low + close) / 3
    for i in range(len(df)):
        if df.index.hour[i] == 23 and df.index.minute[i] == 0:
            df.loc[i:, 'vwap'] = (
                volume[i:] * typical_price[i:]).cumsum() / volume[i:].cumsum()


#   EMA
    ema200 = btalib.ema(df.close, period=200)
    #   MACD
    macd = btalib.macd(df.close, pfast=12, pslow=26, psignal=9)

    df = df.join([ema200.df, macd.df])
    #   Export RAW calculated file
    df.to_csv('{}_indicator_{}.csv'.format(symbol_pair, time_step))
    #   Remove blank data from calculated file
    df_plot = df[199:-1]

    df_plot.to_csv('{}_indicator_cutted_{}.csv'.format(symbol_pair, time_step))
    return df_plot
示例#6
0
 def macd(self):
     for frame in self._stock_prices.stock_frames:
         macd = bt.macd(frame.data).df
         frame.data['macd'] = macd
示例#7
0
def generate_history(symbol_pair,
                     time_step,
                     day_ago=None,
                     date_start=None,
                     date_end=None,
                     until_now=True):
    day_ago = str(day_ago)

    client = client_test_net()

    if until_now:
        klines = client.get_historical_klines(symbol_pair, time_step,
                                              '{} day ago UTC'.format(day_ago))

    else:
        klines = client.get_historical_klines(symbol_pair, time_step,
                                              date_start, date_end)

    for line in klines:
        del line[6:]
#   Generate csv file of RAW data
    df1 = pd.DataFrame(
        klines, columns=['date', 'open', 'high', 'low', 'close', 'volume'])
    df1.set_index('date', inplace=True)
    df1.to_csv('{}_long_history_{}.csv'.format(symbol_pair, time_step))
    #   Prepare to calculate indicator
    df = pd.read_csv('{}_long_history_{}.csv'.format(symbol_pair, time_step),
                     index_col=0)
    df.index = pd.to_datetime(df.index, unit='ms') + pd.DateOffset(hours=7)
    #   Average of high-low *note: is close-open better?
    df['avg'] = (df['high'] + df['low']) / 2
    #   Calculate moving average using Pandas
    df['50ma'] = df.close.rolling(50).mean()
    df['200ma'] = df.close.rolling(200).mean()
    #   VWAP
    df['vwap'] = 0
    volume = df['volume']
    high = df['high']
    low = df['low']
    close = df['close']
    typical_price = (high + low + close) / 3
    for i in range(len(df)):
        if df.index.hour[i] == 23 and df.index.minute[i] == 0:
            df.loc[i:, 'vwap'] = (
                volume[i:] * typical_price[i:]).cumsum() / volume[i:].cumsum()


#   EMA
    ema200 = btalib.ema(df.close, period=200)
    #   MACD
    macd = btalib.macd(df.close, pfast=12, pslow=26, psignal=9)

    df = df.join([ema200.df, macd.df])
    #   Export RAW calculated file
    df.to_csv('{}_history_indicator_{}.csv'.format(symbol_pair, time_step))
    #   Remove blank data from calculated file
    df_plot = df[199:-1]

    df_plot.to_csv('{}_history_indicator_cutted_{}.csv'.format(
        symbol_pair, time_step))
    return df_plot
# 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())
示例#9
0
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
示例#10
0
    def create_indicator(cls, klines: pd.DataFrame) -> pd.DataFrame:
        macd = btalib.macd(klines.copy())

        return macd.df