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 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 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(N, data): # from the RSI indicator variations = np.diff(data) #variations = np.diff(data) / 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 = MM_n(N, h) # MMexp_n(N, h) bn = MM_n(N, b) # MMexp_n(N, b # prefer to return in normalized 0..1 rsi = hn / (hn + bn) return rsi
def SMA_n_sf(N, data, step=1, filtering=False): """ Calcule une SMA 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) sma = MM_n(N, data) return sma
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 RSI_n(N, data): """ 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. """ variations = np.diff(data) #variations = np.diff(data) / 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 = MM_n(N, h) # MMexp_n(N, h) bn = MM_n(N, b) # MMexp_n(N, b) # prefer to return in normalized 0..1 rsi = hn/(hn+bn) return rsi
def BB(N, data): mm = MM_n(N, data) up_bol = copy.deepcopy(mm) bottom_bol = copy.deepcopy(mm) for (j, d) in enumerate(data): i = min(j, N) sample = 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 (bottom_bol, mm, up_bol)
def StochRSI(N, rsi, N_D=3): # from the Stochastic indicator K = np.zeros(len(rsi)) for (j, d) in enumerate(rsi): i = min(j, N) highest = max(rsi[j - i:j + 1]) lowest = min(rsi[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 K, D
def Stochastic(N, data, N_D=3): K = np.zeros(len(data)) for (j, d) in enumerate(data): i = min(j, N) highest = max(data[j - i:j + 1]) lowest = min(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 (K, D)
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 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 WMA_n(N, prices): weights = np.array([x for x in range(1, len(prices) + 1)]) wma = MM_n(N, prices * weights) / weights return wma