def triangles_reg_sf(bottom, top, step=1, filtering=False): # On perd la 1ere valeur: variations = np.diff( down_sample(top[::step], step) - down_sample(bottom[::step], step)) if filtering else np.diff( top[::step] - bottom[::step]) inversion_tendance_index = [ i + 1 for (i, x) in enumerate(list(variations[1:] * variations[:-1])) if x < 0 ] data_splitter = lambda data, split_idx: [data[0:split_idx[0]]] + [ data[split_idx[i]:split_idx[i + 1]] for i in range(len(split_idx) - 1) ] + [data[split_idx[-1]:]] if not inversion_tendance_index: return [], [], [] split_idx = data_splitter(range(len(bottom)), inversion_tendance_index) split_bottom_data, split_top_data = data_splitter( bottom, inversion_tendance_index), data_splitter(top, inversion_tendance_index) bottom_partial_interp, top_partial_interp = map( st.linregress, zip(split_idx, split_bottom_data)), map(st.linregress, zip(split_idx, split_top_data)) return split_idx, bottom_partial_interp, top_partial_interp
def Fibonnacci_sf(open, high, low, close, step=1, filtering=False): """ Retrouve les niveaux plus haut et plus bas et retrouve les niveaux fibo. """ lsub_data = down_sample(low, step) if filtering else low[::step] hsub_data = down_sample(high, step) if filtering else high[::step] t_subdata = range(0, len(data), step) # @todo fast MM_n for data lowers = [] highers = [] prev_high = 0.0 prev_low = sys.float_info.max for n, price in enumerate(lsub_data): if price < prev_low: prev_low = price else: # store a lower lowers.append((n * step, prev_low)) prev_low = sys.float_info.max for n, price in enumerate(hsub_data): if price > prev_high: prev_high = price else: # store a higher highers.append((n * step, prev_high)) prev_high = 0.0 return highers, lowers
def RSI_n_sf(N, data, step=1, filtering=False): # from the RSI indicator sub_data = down_sample(data, step) if filtering else data[::step] t_subdata = range(0, len(data), step) variations = np.diff(sub_data) #variations = np.diff(sub_data) / sub_data[:-1] # h = np.array(list(map(lambda x: max(x,0), variations))) # b = np.array(list(map(lambda x: abs(min(x,0)), variations))) # or that to avoid zeros h = np.array(list(map(lambda x: max(x, 0.000001), variations))) b = np.array(list(map(lambda x: abs(min(x, -0.000001)), variations))) # exp or linear # hn = np.interp(range(len(data)), t_subdata[1:], MMexp_n(N, h)) # bn = np.interp(range(len(data)), t_subdata[1:], MMexp_n(N, b)) hn = np.interp(range(len(data)), t_subdata[1:], MM_n(N, h)) bn = np.interp(range(len(data)), t_subdata[1:], MM_n(N, b)) # prefer to return in normalized 0..1 rsi = hn / (hn + bn) return rsi
def Stochastic_sf(N, data, N_D=3, step=1, filtering=False): """ Calcul des stochastiques. N est le nombre de periodes a observer pour repérer le min et le max du cours. N_D est le nombre d'echantillons de K a utiliser pour le calcul de D step permet de ne selectionner qu'un echantillon sur step dans data. filtering permet de filtrer ou non les donnees avant d'appliquer la selection. Retourne les stochastiques K, D interpolees lineairement ; meme taille que data. """ sub_data = down_sample(data, step) if filtering else data[::step] K = np.zeros(len(sub_data)) t_subdata = range(0, len(data), step) for (j, d) in enumerate(sub_data): i = min(j, N) highest = max(sub_data[j - i:j + 1]) lowest = min(sub_data[j - i:j + 1]) if highest == lowest: highest += 0.000000001 K[j] = (d - lowest) / (highest - lowest) # +epsilon to avoid 0 D = MM_n(N_D, K) return np.interp(range(len(data)), t_subdata, K), np.interp(range(len(data)), t_subdata, D)
def RSI_n_sf(N, data, step=1, filtering=False): """ Calcule le RSI sur N periodes en prenant un echantillon tous les step avec ou sans filtrage prealable Retourne un array de la même taille que data. Lorsque step > 1, les valeurs sont interpolees lineairement. """ # have a difference with others algo, it seems doubling N make the deals... but what's the problem sub_data = down_sample(data, step) if filtering else data [::step] t_subdata = range(0,len(data),step) variations = np.diff(sub_data) #variations = np.diff(sub_data) / sub_data[:-1] # h = np.array(list(map(lambda x: max(x,0), variations))) # b = np.array(list(map(lambda x: abs(min(x,0)), variations))) # or that to avoid zeros h = np.array(list(map(lambda x: max(x,0.000000001), variations))) b = np.array(list(map(lambda x: abs(min(x,-0.000000001)), variations))) # exp or linear # hn = np.interp(range(len(data)), t_subdata[1:], MMexp_n(N, h)) # bn = np.interp(range(len(data)), t_subdata[1:], MMexp_n(N, b)) hn = np.interp(range(len(data)), t_subdata[1:], MM_n(N, h)) bn = np.interp(range(len(data)), t_subdata[1:], MM_n(N, b)) # prefer to return in normalized 0..1 rsi = hn/(hn+bn) return rsi
def EMA_n_sf(N, data, step=1, filtering=False): """ Calcule une EMA sur N periodes en prenant un echantillon tous les step avec ou sans filtrage prealable Retourne un array de la même taille que data. Lorsque step > 1, les valeurs sont interpolees lineairement. """ sub_data = down_sample(data, step) if filtering else data[::step] t_subdata = range(0, len(data), step) ema = MMexp_n(N, sub_data) return ema
def VWMA_n_sf(N, prices, volumes, step=1, filtering=False): """ Calcule une VWMA sur N periodes en prenant un echantillon tous les step avec ou sans filtrage prealable Retourne un array de la meme taille que data. Lorsque step > 1, les valeurs sont interpolees lineairement. """ p_sub_data = down_sample( prices, step) if filtering else np.array(prices)[::step] v_sub_data = down_sample( volumes, step) if filtering else np.array(volumes)[::step] t_subdata = range(0, len(prices), step) # cannot deal with zero volume, then set it to 1 will have no effect on the result, juste give a price for i, v in enumerate(v_sub_data): if v <= 0: v_sub_data[i] = 1.0 pvs = MM_n(N, p_sub_data * v_sub_data) vs = MM_n(N, v_sub_data) return pvs / vs
def WMA_n_sf(N, prices, step=1, filtering=False): """ Calcule une WMA sur N periodes en prenant un echantillon tous les step avec ou sans filtrage prealable Retourne un array de la même taille que data. Lorsque step > 1, les valeurs sont interpolees lineairement. """ sub_data = down_sample(prices, step) if filtering else np.array(prices) [::step] t_subdata = range(0,len(prices),step) weights = np.array([x for x in range(1, len(sub_data)+1)]) wma = MM_n(N, sub_data*weights) / weights return wma
def Price_sf(method, data, step=1, filtering=False): prices = np.array([]) if method == PriceIndicator.PRICE_CLOSE: # average of bid/ofr close price c_prices = [x.close for x in data] # t_subdata = range(0,len(data),step) c_sub_data = down_sample( c_prices, step) if filtering else np.array(c_prices[::step]) # @todo interpolate sub_data prices = c_sub_data elif method == PriceIndicator.PRICE_HLC3: h_prices = [x.high for x in data] l_prices = [x.low for x in data] c_prices = [x.close for x in data] # t_subdata = range(0,len(data),step) h_sub_data = down_sample( h_prices, step) if filtering else np.array(h_prices[::step]) l_sub_data = down_sample( l_prices, step) if filtering else np.array(l_prices[::step]) c_sub_data = down_sample( c_prices, step) if filtering else np.array(c_prices[::step]) # @todo interpolate sub_data prices = (h_sub_data + l_sub_data + c_sub_data) / 3.0 elif method == PriceIndicator.PRICE_OHLC4: o_prices = [x.open for x in data] h_prices = [x.high for x in data] l_prices = [x.low for x in data] c_prices = [x.close for x in data] # t_subdata = range(0,len(data),step) o_sub_data = down_sample( o_prices, step) if filtering else np.array(o_prices[::step]) h_sub_data = down_sample( h_prices, step) if filtering else np.array(h_prices[::step]) l_sub_data = down_sample( l_prices, step) if filtering else np.array(l_prices[::step]) c_sub_data = down_sample( c_prices, step) if filtering else np.array(c_prices[::step]) # @todo interpolate sub_data prices = (o_sub_data + h_sub_data + l_sub_data + c_sub_data) / 4.0 return prices
def MACD_sf(N_short, N_long, data, step=1, filtering=False): """ Calcul du MACD avec les 2 parametres N_short et N_long step permet de sélectionner 1 echantillon sur step avec filtrage ou non """ sub_data = down_sample(data, step) if filtering else data[::step] t_subdata = range(0, len(data), step) # wiki dit exp, donc pourquoi MM_n ? mms = MMexp_n(N_short, sub_data) mml = MMexp_n(N_long, sub_data) return np.interp(range(len(data)), t_subdata, mms - mml)
def Volume_sf(method, data, step=1, filtering=False): if method == VolumeIndicator.VOLUME_TICK: tick_volumes = [x.volume for x in data] sub_data = down_sample(tick_volumes, step) if filtering else np.array( tick_volumes[::step]) # todo interpolate # t_subdata = range(0,len(data),step) return sub_data return np.array([])
def MMT_n_sf(N, data, step=1, filtering=False): """ Calcule un momentum sur N periodes en prenant un echantillon tous les step avec ou sans filtrage prealable Retourne un array de la même taille que data. Lorsque step > 1, les valeurs sont interpolees lineairement. """ sub_data = down_sample(data, step) if filtering else data[::step] t_subdata = range(0, len(data), step) # rajoute les N premieres valeures manquantes mmt = np.array( [x - sub_data[0] for x in sub_data[:N]] + list(np.array(sub_data[N:]) - np.array(sub_data[0:-1 - N + 1]))) return mmt # interp
def HMA_n_sf(N, data, step=1, filtering=False): """ Calcule un HMA sur N periodes en prenant un echantillon tous les step avec ou sans filtrage prealable Retourne un array de la même taille que data. Lorsque step > 1, les valeurs sont interpolees lineairement. """ sub_data = down_sample(data, step) if filtering else data [::step] t_subdata = range(0,len(data),step) N_2 = int(N / 2) N_sqrt = int(math.sqrt(N)) weights = np.array([x for x in range(1, len(sub_data)+1)]) # 1) calculate a WMA with period n / 2 and multiply it by 2 hma12 = 2 * MM_n(N_2, sub_data*weights) / MM_n(N_2, weights) # 2) calculate a WMA for period n and subtract if from step 1 hma12 = hma12 - (MM_n(N, sub_data*weights) / MM_n(N, weights)) # 3) calculate a WMA with period sqrt(n) using the data from step 2 hma = (MM_n(N_sqrt, hma12*weights) / MM_n(N_sqrt, weights)) return hma
def BB_sf(N, data, step=1, filtering=False): """ Calcul des bandes de Bollinger N est le nombre de periodes à observer pour calculer la MM et sigma step permet de selectionner un echantillon tout les steps, avec filtrage ou non Retourne 3 courbes : Bollinger bas ; MM_N ; Bollinger haut, interpolees linéairement """ sub_data = down_sample(data, step) if filtering else data[::step] t_subdata = range(0, len(data), step) mm = MM_n(N, sub_data) up_bol = copy.deepcopy(mm) bottom_bol = copy.deepcopy(mm) for (j, d) in enumerate(sub_data): i = min(j, N) sample = sub_data[j - i:j + 1] sigma = 0 if j == 0 else np.sqrt(stat.variance(sample, up_bol[j])) up_bol[j] = up_bol[j] + 2 * sigma bottom_bol[j] = bottom_bol[j] - 2 * sigma return (np.interp(range(len(data)), t_subdata, bottom_bol), np.interp(range(len(data)), t_subdata, mm), np.interp(range(len(data)), t_subdata, up_bol))
def PivotPoint_sf(method, _open, high, low, close, step=1, filtering=False): """ Retrouve les niveaux plus haut et plus bas et retrouve les niveaux fibo. """ lsub_data = down_sample(low, step) if filtering else np.array( low[::step]) hsub_data = down_sample(high, step) if filtering else np.array( high[::step]) csub_data = down_sample(close, step) if filtering else np.array( close[::step]) if method == PivotPointIndicator.METHOD_CLASSICAL_OHLC or method == PivotPointIndicator.METHOD_CLASSICAL_HLO: osub_data = down_sample(_open, step) if filtering else np.array( _open[::step]) if method == PivotPointIndicator.METHOD_CAMARILLA: pivot = csub_data s1 = csub_data - (hsub_data - lsub_data) * 1.1 / 12 s2 = csub_data - (hsub_data - lsub_data) * 1.1 / 6 s3 = csub_data - (hsub_data - lsub_data) * 1.1 / 4 r1 = csub_data + (hsub_data - lsub_data) * 1.1 / 12 r2 = csub_data + (hsub_data - lsub_data) * 1.1 / 6 r3 = csub_data + (hsub_data - lsub_data) * 1.1 / 4 elif method == PivotPointIndicator.METHOD_FIBONACCI: pivot = csub_data s1 = csub_data - (hsub_data - lsub_data) * 0.382 s2 = csub_data - (hsub_data - lsub_data) * 0.618 s3 = csub_data - (hsub_data - lsub_data) * 0.764 r1 = csub_data + (hsub_data - lsub_data) * 0.382 r2 = csub_data + (hsub_data - lsub_data) * 0.618 r3 = csub_data + (hsub_data - lsub_data) * 0.764 else: # classical or woodie if method == PivotPointIndicator.METHOD_CLASSICAL: pivot = (hsub_data + lsub_data + csub_data) / 3.0 elif method == PivotPointIndicator.METHOD_CLASSICAL_OHLC: pivot = (hsub_data + lsub_data + csub_data + osub_data) / 4.0 elif method == PivotPointIndicator.METHOD_CLASSICAL_OHL: pivot = (hsub_data + lsub_data + osub_data) / 3.0 elif method == PivotPointIndicator.METHOD_WOODIE: pivot = (hsub_data + lsub_data + 2.0 * csub_data) / 4.0 s1 = (2.0 * pivot) - hsub_data s2 = pivot - (hsub_data - lsub_data) s3 = lsub_data - 2.0 * (hsub_data - pivot) r1 = (2.0 * pivot) - lsub_data r2 = pivot + (hsub_data - lsub_data) r3 = hsub_data + 2.0 * (pivot - lsub_data) return pivot, (s1, s2, s3), (r1, r2, r3)