def get_BollingerBand (timeSeries, L = 20): ## Get the Bollinger Bands !! MA = indl.get_SMA(timeSeries, L = L) diff = np.power(timeSeries - MA, 2);# Get the difference to the square # print MA.shape # print price.shape # print diff.shape diff_SMA = indl.get_SMA(diff, L) # Get the avera STD over L periods diff_SMA = 2 * np.sqrt(diff_SMA) # Now we apply a MA over this shit return diff_SMA
def get_MACD (time_series, Ls = 12, Ll = 26, Lsmoth = 9, alpha = -1): """ Moving Average Convergence/Divergence (MACD) indicates the correlation between two price moving averages. Usually 26-period and 12-period Exponential Moving Average (EMA). In order to clearly show buy/sell opportunities, a so-called signal line (9-period indicators` moving average) is plotted on the MACD chart. The MACD proves most effective in wide-swinging trading markets. There are three popular ways to use the Moving Average Convergence/Divergence: crossovers, overbought/oversold conditions, and divergences. The MACD is calculated by subtracting the value of a 26-period exponential moving average from a 12-period exponential moving average. A 9-period dotted simple moving average of the MACD (the signal line) is then plotted on top of the MACD. """ eMlong = indl.get_EMA(time_series, Ll, alpha) eMshort = indl.get_EMA(time_series, Ls, alpha) MACD = indl.get_SMA(eMshort - eMlong, Lsmoth) return eMlong, eMshort, MACD
right=.90, top=.95, wspace=.20, hspace=0) gl.savefig(folder_images + 'lagsMAs.png', dpi=100, sizeInches=[16, 9]) if (viewing_SEW_windows): # Some basic indicators. price = timeData.get_timeSeries(["Close"]) dates = timeData.get_dates() nMA1 = 10 nMA2 = 20 SMAw = indl.get_SMA(bMA.delta(nMA1), nMA1, cval=1) EMAw = indl.get_EMA(bMA.delta(nMA1), nMA1, cval=1) WMAw = indl.get_WMA(bMA.delta(nMA1), nMA1, cval=1) SMAw2 = indl.get_SMA(bMA.delta(nMA2), nMA2, cval=1) EMAw2 = indl.get_EMA(bMA.delta(nMA2), nMA2, cval=1) WMAw2 = indl.get_WMA(bMA.delta(nMA2), nMA2, cval=1) SMA = timeData.SMA(n=nMA1) """ 1st GRAPH """ ############## Average and Window ################ ax1 = gl.subplot2grid((1, 5), (0, 0), rowspan=1, colspan=3) title = "Price and SMA. " + str( symbols[0]) + "(" + ul5.period_dic[timeData.period] + ")" gl.plot(dates, [price, SMA],
indx = 1 lag = 35 ## MACD Parameters !! n_fast = 12 n_slow = 26 n_smooth = 9 EMAfast = Cartera.EMA(n=n_fast) EMAslow = Cartera.EMA(n=n_slow) MACD = EMAfast - EMAslow MACD = pd.ewma(MACD, span=n_smooth, min_periods=n_smooth - 1) # Smoothin of velocity, we want to enter when it is falling MACD_vel = bMA.diff(MACD, n=1) nsmooth_vel = 4 MACD_vel = indl.get_SMA(MACD_vel, L=nsmooth_vel) ## RSI Parameter ! RSI = Cartera.RSI(n=14) RSI_vel = bMA.diff(RSI, n=1) #RSI_vel = indl.get_SMA(RSI_vel, L = nsmooth_vel) ## ATR Parameter ! ATR = Cartera.ATR(n=14) ATR_vel = bMA.diff(ATR, n=1) RSI_vel = indl.get_SMA(ATR_vel, L=nsmooth_vel) ########################################################### ################# PREPARE THE DATA ######################## ########################################################### X_data = np.concatenate((MACD[:, [indx]], MACD_vel[:, [indx]]), axis=1)
def get_SMA(self, L): timeSeries = self.get_timeSeries() SMA = indl.get_SMA(timeSeries, L) return SMA
ndiff1 = 1; ndiff2 = 3; ndiff3 = 7 # Lag of different amplitudes. # delta = bMA.delta(15) diffw1 = bMA.diffw(10,ndiff1) diffw2 = bMA.diffw(10,ndiff2) diffw3 = bMA.diffw(10,ndiff3) diffws = [diffw1,diffw2,diffw3] sel_diff = 0 diff_final = diffws[sel_diff] SMAw = indl.get_SMA(delta, nHMA, cval = 1) dSMAw = bMA.convolve(SMAw, diff_final) # SMASMA = bMA.diff(SMASMA, cval = np.nan) # SMASMA = indl.get_SMA(diffw1, nHMA, cval = 1) WMAw = indl.get_WMA(delta, nHMA, cval = 1) dWMAw = bMA.convolve(WMAw, diff_final) # WMAWMA = bMA.diff(WMAWMA, cval = np.nan) # WMAWMA = indl.get_WMA(diffw1, nHMA, cval = 1) EMAw = indl.get_EMA(delta, nHMA, cval = 1) dEMAw = bMA.convolve(EMAw, diff_final) # EMAEMA = bMA.diff(EMAEMA, cval = np.nan) # EMAEMA = indl.get_EMA(diffw1, nHMA, cval = 1)
region_to_optimize = df_list[ir] different_contries = np.unique( region_to_optimize["LocationCountryName"]) days = np.unique(region_to_optimize["Date"]) Country_day.append([]) days_list.append([]) for soID in different_contries: Country_day[-1].append([]) days_list[-1].append([]) rows_trailer = region_to_optimize[ region_to_optimize["LocationCountryName"] == soID] # Now we find the collection Ns, Nd = rows_trailer.shape # print rows_trailer.shape for day in days: Country_day[-1][-1].append(0) days_list[-1][-1].append(day) # print rows_trailer.shape rows_trailer2 = rows_trailer[rows_trailer["Date"] == day] # print rows_trailer.shape rows_trailer2 = rows_trailer2[rows_trailer2["TrackingAction"] == "Delivery"] Country_day[-1][-1][-1] += rows_trailer2.shape[0] for k in range(len(days_list[0])): week_mean = intl.get_SMA(ul.fnp(Country_day[0][k]), 7) gl.plot(days_list[0][k], week_mean, nf=0, legend=[different_contries[k]])
def get_SMA(self, L): if (self.timeSeries == []): # Check existence of timeSeries self.get_timeSeries() SMA = indl.get_SMA(self.timeSeries, L) return SMA
# We get now the moving averages indx = 1 lag = 35 ## MACD Parameters !! n_fast = 12; n_slow = 26; n_smooth = 9 EMAfast = Cartera.EMA(n = n_fast) EMAslow = Cartera.EMA(n = n_slow) MACD = EMAfast - EMAslow MACD = pd.ewma(MACD, span = n_smooth, min_periods = n_smooth - 1) # Smoothin of velocity, we want to enter when it is falling MACD_vel = bMA.diff(MACD,n = 1) nsmooth_vel = 4 MACD_vel = indl.get_SMA(MACD_vel, L = nsmooth_vel) ## RSI Parameter ! RSI = Cartera.RSI(n = 14) RSI_vel = bMA.diff(RSI,n = 1) #RSI_vel = indl.get_SMA(RSI_vel, L = nsmooth_vel) ## ATR Parameter ! ATR = Cartera.ATR(n = 14) ATR_vel = bMA.diff(ATR,n = 1) RSI_vel = indl.get_SMA(ATR_vel, L = nsmooth_vel) ########################################################### ################# PREPARE THE DATA ######################## ########################################################### X_data = np.concatenate((MACD[:,[indx]],MACD_vel[:,[indx]]), axis = 1)
def get_SMA(self, L ): timeSeries = self.get_timeSeries() SMA = indl.get_SMA(timeSeries, L) return SMA
legend = legend, AxesStyle = "Normal") gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.20, hspace=0) gl.savefig(folder_images +'lagsMAs.png', dpi = 100, sizeInches = [16, 9]) if (viewing_SEW_windows): # Some basic indicators. price = timeData.get_timeSeries(["Close"]); dates = timeData.get_dates() nMA1 = 10 nMA2 = 20 SMAw = indl.get_SMA(bMA.delta(nMA1), nMA1, cval = 1) EMAw = indl.get_EMA(bMA.delta(nMA1), nMA1, cval = 1) WMAw = indl.get_WMA(bMA.delta(nMA1), nMA1, cval = 1) SMAw2 = indl.get_SMA(bMA.delta(nMA2), nMA2, cval = 1) EMAw2 = indl.get_EMA(bMA.delta(nMA2), nMA2, cval = 1) WMAw2 = indl.get_WMA(bMA.delta(nMA2), nMA2, cval = 1) SMA = timeData.SMA(n = nMA1) """ 1st GRAPH """ ############## Average and Window ################ ax1 = gl.subplot2grid((1,5), (0,0), rowspan=1, colspan=3) title = "Price and SMA. " + str(symbols[0]) + "(" + ul.period_dic[timeData.period]+ ")" gl.plot(dates, [price, SMA] ,AxesStyle = "Normal",