Spaces:
Sleeping
Sleeping
Delete helper.py
Browse files
helper.py
DELETED
|
@@ -1,280 +0,0 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
-
|
| 4 |
-
# check if the library folder already exists, to avoid building everytime you load the pahe
|
| 5 |
-
# import streamlit as st
|
| 6 |
-
import requests
|
| 7 |
-
import os
|
| 8 |
-
import sys
|
| 9 |
-
import subprocess
|
| 10 |
-
if not os.path.isdir("/tmp/ta-lib"):
|
| 11 |
-
|
| 12 |
-
# Download ta-lib to disk
|
| 13 |
-
with open("/tmp/ta-lib-0.4.0-src.tar.gz", "wb") as file:
|
| 14 |
-
response = requests.get(
|
| 15 |
-
"http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz"
|
| 16 |
-
)
|
| 17 |
-
file.write(response.content)
|
| 18 |
-
# get our current dir, to configure it back again. Just house keeping
|
| 19 |
-
default_cwd = os.getcwd()
|
| 20 |
-
os.chdir("/tmp")
|
| 21 |
-
# untar
|
| 22 |
-
os.system("tar -zxvf ta-lib-0.4.0-src.tar.gz")
|
| 23 |
-
os.chdir("/tmp/ta-lib")
|
| 24 |
-
os.system("ls -la /app/equity/")
|
| 25 |
-
# build
|
| 26 |
-
os.system("./configure --prefix=/home/appuser")
|
| 27 |
-
os.system("make")
|
| 28 |
-
# install
|
| 29 |
-
os.system("make install")
|
| 30 |
-
# back to the cwd
|
| 31 |
-
os.chdir(default_cwd)
|
| 32 |
-
sys.stdout.flush()
|
| 33 |
-
|
| 34 |
-
# add the library to our current environment
|
| 35 |
-
from ctypes import *
|
| 36 |
-
|
| 37 |
-
lib = CDLL("/home/appuser/lib/libta_lib.so.0.0.0")
|
| 38 |
-
# import library
|
| 39 |
-
try:
|
| 40 |
-
import talib as ta
|
| 41 |
-
except ImportError:
|
| 42 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "--global-option=build_ext", "--global-option=-L/home/appuser/lib/", "--global-option=-I/home/appuser/include/", "ta-lib"])
|
| 43 |
-
finally:
|
| 44 |
-
import talib as ta
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def format_date(df):
|
| 48 |
-
format = '%Y-%m-%d %H:%M:%S'
|
| 49 |
-
df['Datetime'] = pd.to_datetime(df['date'] + ' ' + df['time'], format=format)
|
| 50 |
-
df = df.set_index(pd.DatetimeIndex(df['Datetime']))
|
| 51 |
-
df = df.drop('Datetime', axis=1)
|
| 52 |
-
|
| 53 |
-
return df
|
| 54 |
-
|
| 55 |
-
# https://stackoverflow.com/questions/39684548/convert-the-string-2-90k-to-2900-or-5-2m-to-5200000-in-pandas-dataframe
|
| 56 |
-
def replace_vol(df):
|
| 57 |
-
df.volume = (df.volume.replace(r'[KM]+$', '', regex=True).astype(float) * \
|
| 58 |
-
df.volume.str.extract(r'[\d\.]+([KM]+)', expand=False)
|
| 59 |
-
.fillna(1)
|
| 60 |
-
.replace(['K','M'], [10**3, 10**6]).astype(int))
|
| 61 |
-
return df
|
| 62 |
-
|
| 63 |
-
def get_all_features(df):
|
| 64 |
-
#get_overlap_studies
|
| 65 |
-
# BBANDS - Bollinger Bands
|
| 66 |
-
df['bbub'], df['bbmb'], df['bblb'] = ta.BBANDS(df['close'])
|
| 67 |
-
|
| 68 |
-
# DEMA - Double Exponential Moving Average
|
| 69 |
-
df['DEMA_100'] = ta.DEMA(df['close'],timeperiod=100)
|
| 70 |
-
df['DEMA_30'] = ta.DEMA(df['close'],timeperiod=30)
|
| 71 |
-
df['DEMA_5'] = ta.DEMA(df['close'],timeperiod=5)
|
| 72 |
-
|
| 73 |
-
# EMA - Exponential Moving Average
|
| 74 |
-
df['EMA_100'] = ta.EMA(df['close'],timeperiod=100)
|
| 75 |
-
df['EMA_30'] = ta.EMA(df['close'],timeperiod=30)
|
| 76 |
-
df['EMA_5'] = ta.EMA(df['close'],timeperiod=5)
|
| 77 |
-
|
| 78 |
-
# HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline
|
| 79 |
-
df['HT_TRENDLINE'] = ta.HT_TRENDLINE(df['close'])
|
| 80 |
-
|
| 81 |
-
# KAMA - Kaufman Adaptive Moving Average
|
| 82 |
-
df['KAMA'] = ta.KAMA(df['close'])
|
| 83 |
-
|
| 84 |
-
# MA - Moving average
|
| 85 |
-
df['MA_100'] = ta.MA(df['close'],timeperiod=100)
|
| 86 |
-
df['MA_30'] = ta.MA(df['close'],timeperiod=30)
|
| 87 |
-
df['MA_5'] = ta.MA(df['close'],timeperiod=5)
|
| 88 |
-
|
| 89 |
-
# MAMA - MESA Adaptive Moving Average
|
| 90 |
-
df['MAMA'], df['FAMA'] = ta.MAMA(df['close'])
|
| 91 |
-
|
| 92 |
-
# MIDPOINT - MidPoint over period
|
| 93 |
-
df['MIDPOINT'] = ta.MIDPOINT(df['close'])
|
| 94 |
-
|
| 95 |
-
# MIDPRICE - Midpoint Price over period
|
| 96 |
-
df['MIDPRICE'] = ta.MIDPRICE(df.high, df.low, timeperiod=14)
|
| 97 |
-
|
| 98 |
-
# SAR - Parabolic SAR
|
| 99 |
-
df['SAR'] = ta.SAR(df.high, df.low, acceleration=0, maximum=0)
|
| 100 |
-
|
| 101 |
-
# SAREXT - Parabolic SAR - Extended
|
| 102 |
-
df['SAREXT'] = ta.SAREXT(df.high, df.low, startvalue=0, offsetonreverse=0, accelerationinitlong=0, accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0)
|
| 103 |
-
|
| 104 |
-
# SMA - Simple Moving Average
|
| 105 |
-
df['SMA_100'] = ta.SMA(df['close'],timeperiod=100)
|
| 106 |
-
df['SMA_30'] = ta.SMA(df['close'],timeperiod=30)
|
| 107 |
-
df['SMA_5'] = ta.SMA(df['close'],timeperiod=5)
|
| 108 |
-
|
| 109 |
-
# T3 - Triple Exponential Moving Average (T3)
|
| 110 |
-
df['T3'] = ta.T3(df.close, timeperiod=5, vfactor=0)
|
| 111 |
-
|
| 112 |
-
# TEMA - Triple Exponential Moving Average
|
| 113 |
-
df['TEMA_100'] = ta.TEMA(df['close'],timeperiod=100)
|
| 114 |
-
df['TEMA_30'] = ta.TEMA(df['close'],timeperiod=30)
|
| 115 |
-
df['TEMA_5'] = ta.TEMA(df['close'],timeperiod=5)
|
| 116 |
-
|
| 117 |
-
# TRIMA - Triangular Moving Average
|
| 118 |
-
df['TRIMA_100'] = ta.TRIMA(df['close'],timeperiod=100)
|
| 119 |
-
df['TRIMA_30'] = ta.TRIMA(df['close'],timeperiod=30)
|
| 120 |
-
df['TRIMA_5'] = ta.TRIMA(df['close'],timeperiod=5)
|
| 121 |
-
|
| 122 |
-
# WMA - Weighted Moving Average
|
| 123 |
-
df['WMA_100'] = ta.WMA(df['close'],timeperiod=100)
|
| 124 |
-
df['WMA_30'] = ta.WMA(df['close'],timeperiod=30)
|
| 125 |
-
df['WMA_5'] = ta.WMA(df['close'],timeperiod=5)
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
#get_momentum_indicator
|
| 129 |
-
# ADX - Average Directional Movement Index
|
| 130 |
-
df['ADX'] = ta.ADX(df.high, df.low, df.close, timeperiod=14)
|
| 131 |
-
|
| 132 |
-
# ADXR - Average Directional Movement Index Rating
|
| 133 |
-
df['ADXR'] = ta.ADXR(df.high, df.low, df.close, timeperiod=14)
|
| 134 |
-
|
| 135 |
-
# APO - Absolute Price Oscillator
|
| 136 |
-
df['APO'] = ta.APO(df.close, fastperiod=12, slowperiod=26, matype=0)
|
| 137 |
-
|
| 138 |
-
# AROON - Aroon
|
| 139 |
-
df['AROON_DWN'],df['AROON_UP'] = ta.AROON(df.high, df.low, timeperiod=14)
|
| 140 |
-
|
| 141 |
-
# AROONOSC - Aroon Oscillator
|
| 142 |
-
df['AROONOSC'] = ta.AROONOSC(df.high, df.low, timeperiod=14)
|
| 143 |
-
|
| 144 |
-
# BOP - Balance Of Power
|
| 145 |
-
df['BOP'] = ta.BOP(df.open, df.high, df.low, df.close)
|
| 146 |
-
|
| 147 |
-
# CCI - Commodity Channel Index
|
| 148 |
-
df['CCI'] = ta.CCI(df.high, df.low, df.close, timeperiod=14)
|
| 149 |
-
|
| 150 |
-
# CMO - Chande Momentum Oscillator
|
| 151 |
-
df['CMO']= ta.CMO(df.close, timeperiod=14)
|
| 152 |
-
|
| 153 |
-
# DX - Directional Movement Index
|
| 154 |
-
df['DX'] = ta.DX(df.high, df.low, df.close, timeperiod=14)
|
| 155 |
-
|
| 156 |
-
# MACD - Moving Average Convergence/Divergence
|
| 157 |
-
df['MACD'], df['MACD_SGNL'], df['MACD_HIST'] = ta.MACD(df.close, fastperiod=12, slowperiod=26, signalperiod=9)
|
| 158 |
-
|
| 159 |
-
# MACDFIX - Moving Average Convergence/Divergence Fix 12/26
|
| 160 |
-
df['MACDF'], df['MACDF_SGNL'], df['MACDF_HIST'] = ta.MACDFIX(df.close)
|
| 161 |
-
|
| 162 |
-
# MFI - Money Flow Index
|
| 163 |
-
df['MFI'] = ta.MFI(df.high, df.low, df.close, df.volume, timeperiod=14)
|
| 164 |
-
|
| 165 |
-
# MINUS_DI - Minus Directional Indicator
|
| 166 |
-
df['MINUS_DI'] = ta.MINUS_DI(df.high, df.low, df.close, timeperiod=14)
|
| 167 |
-
|
| 168 |
-
# MINUS_DM - Minus Directional Movement
|
| 169 |
-
df['MINUS_DM'] = ta.MINUS_DM(df.high, df.low, timeperiod=14)
|
| 170 |
-
|
| 171 |
-
# MOM - Momentum
|
| 172 |
-
df['MOM'] = ta.MOM(df.close, timeperiod=10)
|
| 173 |
-
|
| 174 |
-
# PLUS_DI - Plus Directional Indicator
|
| 175 |
-
df['PLUS_DI'] = ta.PLUS_DI(df.high, df.low, df.close, timeperiod=14)
|
| 176 |
-
|
| 177 |
-
# PLUS_DM - Plus Directional Indicator
|
| 178 |
-
df['PLUS_DM'] = ta.PLUS_DM(df.high, df.low, timeperiod=14)
|
| 179 |
-
|
| 180 |
-
# PPO - Percentage Price Oscillator
|
| 181 |
-
df['PPO'] = ta.PPO(df.close, fastperiod=12, slowperiod=26, matype=0)
|
| 182 |
-
|
| 183 |
-
# ROC - Rate of change : ((price/prevPrice)-1)*100
|
| 184 |
-
df['ROC'] = ta.ROC(df.close, timeperiod=10)
|
| 185 |
-
|
| 186 |
-
# ROCP - Rate of change Percentage: (price-prevPrice)/prevPrice
|
| 187 |
-
df['ROCP'] = ta.ROCP(df.close, timeperiod=10)
|
| 188 |
-
|
| 189 |
-
# ROCR - Rate of change Percentage: (price-prevPrice)/prevPrice
|
| 190 |
-
df['ROCR'] = ta.ROCR(df.close, timeperiod=10)
|
| 191 |
-
|
| 192 |
-
# ROCR100 - Rate of change ratio 100 scale: (price/prevPrice)*100
|
| 193 |
-
df['ROCR100'] = ta.ROCR100(df.close, timeperiod=10)
|
| 194 |
-
|
| 195 |
-
# RSI - Relative Strength Index
|
| 196 |
-
df['RSI'] = ta.RSI(df.close, timeperiod=14)
|
| 197 |
-
|
| 198 |
-
# STOCH - Stochastic
|
| 199 |
-
df['STOCH_SLWK'], df['STOCH_SLWD'] = ta.STOCH(df.high, df.low, df.close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
|
| 200 |
-
|
| 201 |
-
# STOCHF - Stochastic Fast
|
| 202 |
-
df['STOCH_FSTK'], df['STOCH_FSTD'] = ta.STOCHF(df.high, df.low, df.close, fastk_period=5, fastd_period=3, fastd_matype=0)
|
| 203 |
-
|
| 204 |
-
# STOCHRSI - Stochastic Relative Strength Index
|
| 205 |
-
df['STOCHRSI_FSTK'], df['STOCHRSI_FSTD'] = ta.STOCHRSI(df.close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0)
|
| 206 |
-
|
| 207 |
-
# TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA
|
| 208 |
-
df['TRIX'] = ta.TRIX(df.close, timeperiod=30)
|
| 209 |
-
|
| 210 |
-
# ULTOSC - Ultimate Oscillator
|
| 211 |
-
df['ULTOSC'] = ta.ULTOSC(df.high, df.low, df.close, timeperiod1=7, timeperiod2=14, timeperiod3=28)
|
| 212 |
-
|
| 213 |
-
# WILLR - Williams' %R
|
| 214 |
-
df['WILLR'] = ta.WILLR(df.high, df.low, df.close, timeperiod=14)
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
# get_volume_indicator
|
| 218 |
-
# AD - Chaikin A/D Line
|
| 219 |
-
df['AD'] = ta.AD(df.high, df.low, df.close, df.volume)
|
| 220 |
-
|
| 221 |
-
# ADOSC - Chaikin A/D Oscillator
|
| 222 |
-
df['ADOSC'] = ta.ADOSC(df.high, df.low, df.close, df.volume, fastperiod=3, slowperiod=10)
|
| 223 |
-
|
| 224 |
-
# OBV - On Balance Volume
|
| 225 |
-
df['OBV'] = ta.OBV(df.close, df.volume)
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
# get_volatility_indicator
|
| 229 |
-
# ATR - Average True Range
|
| 230 |
-
df['ATR'] = ta.ATR(df.high, df.low, df.close, timeperiod=14)
|
| 231 |
-
|
| 232 |
-
# NATR - Normalized Average True Range
|
| 233 |
-
df['NATR'] = ta.NATR(df.high, df.low, df.close, timeperiod=14)
|
| 234 |
-
|
| 235 |
-
# TRANGE - True Range
|
| 236 |
-
df['TRANGE'] = ta.TRANGE(df.high, df.low, df.close)
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
# get_transform_price
|
| 240 |
-
# AVGPRICE - Average Price
|
| 241 |
-
df['AVGPRICE'] = ta.AVGPRICE(df.open, df.high, df.low, df.close)
|
| 242 |
-
|
| 243 |
-
# MEDPRICE - Median Price
|
| 244 |
-
df['MEDPRICE'] = ta.MEDPRICE(df.high, df.low)
|
| 245 |
-
|
| 246 |
-
# TYPPRICE - Typical Price
|
| 247 |
-
df['TYPPRICE'] = ta.TYPPRICE(df.high, df.low, df.close)
|
| 248 |
-
|
| 249 |
-
# WCLPRICE - Weighted Close Price
|
| 250 |
-
df['WCLPRICE'] = ta.WCLPRICE(df.high, df.low, df.close)
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
# get_cycle_indicator
|
| 254 |
-
# HT_DCPERIOD - Hilbert Transform - Dominant Cycle Period
|
| 255 |
-
df['HT_DCPERIOD'] = ta.HT_DCPERIOD(df.close)
|
| 256 |
-
|
| 257 |
-
# HT_DCPHASE - Hilbert Transform - Dominant Cycle Phase
|
| 258 |
-
df['HT_DCPHASE'] = ta.HT_DCPHASE(df.close)
|
| 259 |
-
|
| 260 |
-
# HT_PHASOR - Hilbert Transform - Phasor Components
|
| 261 |
-
df['HT_PHASOR_IP'], df['HT_PHASOR_QD'] = ta.HT_PHASOR(df.close)
|
| 262 |
-
|
| 263 |
-
# HT_SINE - Hilbert Transform - SineWave
|
| 264 |
-
df['HT_SINE'], df['HT_SINE_LEADSINE'] = ta.HT_SINE(df.close)
|
| 265 |
-
|
| 266 |
-
# HT_TRENDMODE - Hilbert Transform - Trend vs Cycle Mode
|
| 267 |
-
df['HT_TRENDMODE'] = ta.HT_TRENDMODE(df.close)
|
| 268 |
-
|
| 269 |
-
return df
|
| 270 |
-
|
| 271 |
-
def feature_main(df):
|
| 272 |
-
df['time'] = df['time'].map(lambda x: np.sum(list(map(int, str(x).split(':')))))
|
| 273 |
-
|
| 274 |
-
df = get_all_features(df)
|
| 275 |
-
values = {}
|
| 276 |
-
for col in df.columns:
|
| 277 |
-
idx = df.reset_index()[col].first_valid_index()
|
| 278 |
-
values[col] = df.iloc[idx][col]
|
| 279 |
-
df = df.fillna(value=values)
|
| 280 |
-
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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