Python pandas.ewma() Examples
The following are 23
code examples of pandas.ewma().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pandas
, or try the search function
.
Example #1
Source File: compare_csv.py From tensorflow-litterbox with Apache License 2.0 | 6 votes |
def main(): parser = argparse.ArgumentParser() parser.add_argument('files', metavar='filename', type=str, nargs='*') args = parser.parse_args() args = vars(args) files = args['files'] assert len(files) == 2 targets_df = pd.read_csv(files[0], header=0, index_col=False) predict_df = pd.read_csv(files[1], header=0, index_col=False) column = targets_df.columns[1] targets = targets_df.as_matrix(columns=[column]) #predict_df[column] = pd.ewma(predict_df[column], com=1, adjust=False) predictions = predict_df.as_matrix(columns=[column]) rmse, mse = calc_rmse(predictions, targets) print("RMSE: %f, MSE: %f" % (rmse, mse))
Example #2
Source File: macd_live_test.py From stock with Apache License 2.0 | 6 votes |
def select_Time_MACD(self): #EMA # print self.df_close.tail() ema_close_short = self.df_close[self.COL_EMA_S].get_values() ema_close_long = self.df_close[self.COL_EMA_L].get_values() dif_price = ema_close_short - ema_close_long dea_price = pd.ewma(dif_price, span=self.MA_DEA) macd_price = 2 * (dif_price - dea_price) signal = SIGNAL_DEFAULT if dif_price[-1] > dif_price[-2] and dif_price[-1] > dea_price[-2] \ and dif_price[-2] < dea_price[-2] and dea_price[-1] > 0: signal = SIGNAL_BUY elif dif_price[-1] < dif_price[-2] and dif_price[-1] < dea_price[-1] \ and dif_price[-2] > dea_price[-2] and dif_price[-1] < 0: signal = SIGNAL_SALE return signal # DMA指标择时 (回测)
Example #3
Source File: data_calcute.py From stock with Apache License 2.0 | 6 votes |
def calcute_ma(df, avr_short=12, avr_long=40): """ 计算ma, ema :param df: :return: """ if len(df) == 0: return # print "{} calcute ma".format(df.ix[0,'code']) df['ma_' + str(avr_short)] = pd.rolling_mean(df['close'], avr_short) # 12 df['ma_' + str(avr_long)] = pd.rolling_mean(df['close'], avr_long) # 40 # print "{} calcute ema".format(df.ix[0, 'code']) df['ema_' + str(avr_short)] = pd.ewma(df['close'], span=avr_short) # 12 df['ema_' + str(avr_long)] = pd.ewma(df['close'], span=avr_long) # 40 df = df.replace(np.nan, 0) return df
Example #4
Source File: ma_strategy.py From stock with Apache License 2.0 | 6 votes |
def calcute_ma(self, df, avr_short=12, avr_long=40): """ 计算ma, ema :param df: :return: """ if len(df) == 0: return # print "{} calcute ma".format(df.ix[0,'code']) df['ma_' + str(avr_short)] = pd.rolling_mean(df['close'], avr_short) # 12 df['ma_' + str(avr_long)] = pd.rolling_mean(df['close'], avr_long) # 40 # print "{} calcute ema".format(df.ix[0, 'code']) df['ema_' + str(avr_short)] = pd.ewma(df['close'], span=avr_short) # 12 df['ema_' + str(avr_long)] = pd.ewma(df['close'], span=avr_long) # 40 df = df.replace(np.nan, 0) return df
Example #5
Source File: ma_strategy.py From stock with Apache License 2.0 | 6 votes |
def select_Time_MACD(self): #EMA # print self.df_close.tail() ema_close_short = self.df_close[self.COL_EMA_S].get_values() ema_close_long = self.df_close[self.COL_EMA_L].get_values() dif_price = ema_close_short - ema_close_long dea_price = pd.ewma(dif_price, span=self.MA_DEA) macd_price = 2 * (dif_price - dea_price) signal = SIGNAL_DEFAULT if dif_price[-1] > dif_price[-2] and dif_price[-1] > dea_price[-2] \ and dif_price[-2] < dea_price[-2] and dea_price[-1] > 0: signal = SIGNAL_BUY elif dif_price[-1] < dif_price[-2] and dif_price[-1] < dea_price[-1] \ and dif_price[-2] > dea_price[-2] and dif_price[-1] < 0: signal = SIGNAL_SALE return signal # DMA指标择时 (回测)
Example #6
Source File: utils.py From batchflow with Apache License 2.0 | 6 votes |
def four_losses_draw(losses, names, title): """ Draw two graphs. First - last 100 iterations. Second - all iterations. Parameters ---------- losses : list loss values names : list names of loss title : str title to graph """ _, axis = plt.subplots(1, 2) for loss, name in zip(losses, names): axis[0].plot(loss[-100:], label='%s'%name) axis[0].plot(pd.ewma(np.array(loss[-100:]), span=10, adjust=False), label='%s'%name) axis[1].plot(loss, label='%s'%name) axis[1].plot(pd.ewma(np.array(loss), span=10, adjust=False), label='%s'%name) axis[0].set_title(title) axis[0].legend() axis[1].legend() plt.show()
Example #7
Source File: macd_back_test.py From stock with Apache License 2.0 | 5 votes |
def processEMA(stockCsvPath, stockCsvNewPath): #导入数据,stockCsvPath为在电脑中的路径 stock_data = pd.read_csv(stockCsvPath) # 将数据按照交易日期从远到近排序 stock_data.sort('Date', inplace=True) #=====================计算移动平均线 # 分别计算5日、20日、60日移动平均线 ma_list = [5, 20, 60] # 计算简单算术移动平均线MA - 注意:stock_data['close']为股票每条的收盘价 for ma in ma_list: stock_data['MA_' + str(ma)] = pd.rolling_mean(stock_data['Adj Close'], ma) # 计算指数平滑移动平均线EMA for ma in ma_list: stock_data['EMA_' + str(ma)] = pd.ewma(stock_data['Adj Close'], span=ma) # 将数据按照交易日期从近到远排序 stock_data.sort('Date', ascending=False, inplace=True) stock_data['DIF'] = stock_data['EMA_'+str(ma_list[0])] - stock_data['EMA_'+str(ma_list[-1])] stock_data['DEA_' + str(10)] = pd.ewma(stock_data['DIF'], span=10) # =================================== 将算好的数据输出到csv文件,这里请填写输出文件在您电脑的路径 stock_data.to_csv(stockCsvNewPath, index=False) # 自适应均线
Example #8
Source File: technical_indicators.py From binance-technical-algorithm with MIT License | 5 votes |
def DG_Cross(data,short,long): short_ma = pd.ewma(data["lastprice"], span = short) long_ma = pd.ewma(data["lastprice"], span = long) DGcross = short_ma - long_ma return DGcross #Williams Overbought/Oversold Index #Overbought market condition: 20 or less, #Oversold market condition: 80 to 100
Example #9
Source File: technical_indicators.py From binance-technical-algorithm with MIT License | 5 votes |
def MACDcross(data, span_short, span_long, span_signal): ema_short = pd.ewma(data["lastprice"], span=span_short) ema_long = pd.ewma(data["lastprice"], span=span_long) MACD = ema_short - ema_long MACDsigline = pd.ewma(MACD, span=span_signal) MACDcross = MACD-MACDsigline return MACDcross #Death/Golden Cross (50ma,200ma). Cross up is bullish trend, cross down is bearish trend.
Example #10
Source File: technical_indicators.py From binance-technical-algorithm with MIT License | 5 votes |
def MACD(data, span_short, span_long): ema_short = pd.ewma(data["lastprice"], span=span_short) ema_long = pd.ewma(data["lastprice"], span=span_long) MACD = ema_short - ema_long return MACD #MACD Cross
Example #11
Source File: technical_indicators.py From binance-technical-algorithm with MIT License | 5 votes |
def ewma_ind(data, window): ewma = pd.ewma(data["lastprice"], span = window) ewma_ratio = (data["lastprice"]/ewma)-1 ewma_mean = pd.stats.moments.rolling_mean(ewma_ratio,60) ewma_std = pd.stats.moments.rolling_std(ewma_ratio,60) ewma_ub2 = ewma_mean + (ewma_std*2) ewma_lb2 = ewma_mean - (ewma_std*2) if pd.Series(ewma_ratio).any() < 1: ewma_ind = (abs(ewma_ratio)/abs(ewma_lb2))*-100 else: ewma_ind = (ewma_ratio/ewma_ub2)*100 return ewma_ind #MACD Line and Crossover
Example #12
Source File: technical_indicators.py From binance-technical-algorithm with MIT License | 5 votes |
def ewma(data, window): ewma = pd.ewma(data["lastprice"], span = window) return ewma #Simple Moving Average Volitility
Example #13
Source File: ABuPdHelper.py From abu with GNU General Public License v3.0 | 5 votes |
def _pd_ewm(pd_object, pd_object_cm, how, *args, **kwargs): """ 被_pd_object_covert装饰,对pandas中的ewm操作,根据pandas version版本自动选择调用方式 :param pd_object: 可迭代的序列,pd.Series, pd.DataFrame或者只是Iterable :param pd_object_cm: 与pd_object相同,针对需要两个pandas对象或者序列执行的操作,如corr,cov等 :param how: 代表方法操作名称,eg. mean, std, var :return: """ if g_pandas_has_ewm: """pandas版本高,使用如pd_object.ewm直接调用""" ewm_obj = pd_object.ewm(*args, **kwargs) if hasattr(ewm_obj, how): if pd_object_cm is None: return getattr(ewm_obj, how)() # 需要两个pd_object进行的操作 return getattr(ewm_obj, how)(pd_object_cm) else: """pandas版本低,使用如pd.ewmstd方法调用""" if how == 'mean': # pd.ewma特殊代表加权移动平均,所以使用a替换mean how = 'a' how_func = 'ewm{}'.format(how) if hasattr(pd, how_func): if pd_object_cm is None: return getattr(pd, how_func)(pd_object, *args, **kwargs) # 需要两个pd_object进行的操作 return getattr(pd, how_func)(pd_object, pd_object_cm, *args, **kwargs) raise RuntimeError('_pd_ewm {} getattr error'.format(how))
Example #14
Source File: tdlib.py From Crypto_trading_robot with MIT License | 5 votes |
def stats_MA_only(self, market, exch_use, period = '1h', maperiod = 10, nentries = 100000, tail = 10, short_flag = False, b_test = None, ma_calc = 'simple'): nentries = self.get_nentries(period) if not self.using_data_source: # Added for cases when we have a different reference exchange / market for calculating the TD filename = self.filename_define(market, exch_use) transactions = self.read_transactions(filename, nentries, b_test) if transactions is None: return None # if bars are provided, e.g. for traditional markets else: transactions = self.source_snapshot(b_test.time()).tail(nentries).copy() # Base for starting time self.price_base = self.get_period(period, b_test) bars = self.transaction_resample(transactions, b_test, period, remove_nans = self.is_traditional(exch_use)) del transactions # Calculate the MA values ma_df = bars['close'] # why not working for simple ma for oanda? if ma_calc == 'simple': ma_rolling = ma_df.rolling(window=maperiod, min_periods=maperiod).mean() else: ma_rolling = pd.ewma(ma_df, span=maperiod) # Memory cleaning del bars gc.collect() ### ended cleanup return ma_rolling ### Returning the max or min of last N candles for specific period
Example #15
Source File: indicators.py From technical with GNU General Public License v3.0 | 5 votes |
def rolling_weighted_mean(series, window=200, min_periods=None): min_periods = window if min_periods is None else min_periods try: return series.ewm(span=window, min_periods=min_periods).mean() except Exception as e: # noqa: F841 return pd.ewma(series, span=window, min_periods=min_periods) # ---------------------------------------------
Example #16
Source File: tradingrules.py From systematictradingexamples with GNU General Public License v2.0 | 5 votes |
def calc_ewmac_forecast(price, Lfast, Lslow=None, usescalar=True): """ Calculate the ewmac trading fule forecast, given a price and EWMA speeds Lfast, Lslow and vol_lookback Assumes that 'price' is daily data """ ## price: This is the stitched price series ## We can't use the price of the contract we're trading, or the volatility will be jumpy ## And we'll miss out on the rolldown. See http://qoppac.blogspot.co.uk/2015/05/systems-building-futures-rolling.html if Lslow is None: Lslow=4*Lfast ## We don't need to calculate the decay parameter, just use the span directly fast_ewma=pd.ewma(price, span=Lfast) slow_ewma=pd.ewma(price, span=Lslow) raw_ewmac=fast_ewma - slow_ewma ## volatility adjustment stdev_returns=volatility(price) vol_adj_ewmac=raw_ewmac/stdev_returns ## scaling adjustment if usescalar: f_scalar=ewmac_forecast_scalar(Lfast, Lslow) forecast=vol_adj_ewmac*f_scalar else: forecast=vol_adj_ewmac cap_forecast=cap_series(forecast, capmin=-20.0,capmax=20.0) return cap_forecast
Example #17
Source File: utils.py From batchflow with Apache License 2.0 | 5 votes |
def draw_avgpooling(maps, answers, axis=None, span=350, model=True): """ Draw maps from GAP Parameters ---------- maps : np.array all maps from GAP layers answers : np.array answers to all maps span : float, optional Specify decay in terms of span axis : list, optional sets the min and max of the x and y axes, with ``[xmin, xmax, ymin, ymax]`` model : bool, optional se resnet or simple resnet """ axis = [0, 2060, 0, 1] if axis is None else axis col = sns.color_palette("Set2", 8) + sns.color_palette(["#9b59b6", "#3498db"]) indices = np.array([np.where(answers == i)[0] for i in range(10)]) filters = np.array([np.mean(maps[indices[i]], axis=0).reshape(-1) for i in range(10)]) for i in range(10): plt.plot(pd.ewma(filters[i], span=span, adjust=False), color=col[i], label=str(i)) plt.title("Distribution of average pooling in "+("SE ResNet" if model else 'simple ResNet')) plt.legend(fontsize=16, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.ylabel('Activation value', fontsize=18) plt.xlabel('Future map index', fontsize=18) plt.axis(axis) plt.show()
Example #18
Source File: ma_strategy.py From stock with Apache License 2.0 | 5 votes |
def select_Time_TRIX(self): #EMA ema_close_short = self.df_close[self.COL_EMA_S].get_values() ema_ema_close_short = pd.ewma(ema_close_short, span=self.AVR_SHORT) tr_close = pd.ewma(ema_ema_close_short, span=self.AVR_SHORT) # ma_list = [self.AVR_SHORT, self.AVR_SHORT] #N,M # # if ma_list[0] == self.AVR_SHORT: # ema_close = self.ema_short # else: # ema_close = pd.ewma(self.close_price, span=ma_list[0]) # ema_close = pd.ewma(ema_close, span=ma_list[0]) # tr_close = pd.ewma(ema_close, span=ma_list[0]) trixsList = [0] for i in range(1, len(tr_close)): #print tr_close[i], tr_close[i-1] trix = (tr_close[i]-tr_close[i-1])/tr_close[i-1]*100 trixsList.append(trix) trixs = np.array(trixsList) maxtrix = pd.rolling_mean(trixs, self.AVR_LONG) signal = SIGNAL_DEFAULT if trixs[-1] > trixs[-2] and trixs[-1] > maxtrix[-1] \ and trixs[-2] < maxtrix[-2]: signal = SIGNAL_BUY elif trixs[-1] < trixs[-2] and trixs[-1] < maxtrix[-1] \ and trixs[-2] > maxtrix[-2]: signal = SIGNAL_SALE return signal # AMA指标择时
Example #19
Source File: indicators.py From qtpylib with Apache License 2.0 | 5 votes |
def rolling_weighted_mean(series, window=200, min_periods=None): min_periods = window if min_periods is None else min_periods try: return series.ewm(span=window, min_periods=min_periods).mean() except Exception as e: return pd.ewma(series, span=window, min_periods=min_periods) # ---------------------------------------------
Example #20
Source File: ta.py From tia with BSD 3-Clause "New" or "Revised" License | 5 votes |
def ema(arg, n): if n == 0: return pd.ewma(arg, span=len(arg), min_periods=1) else: return pd.ewma(arg, span=n, min_periods=n)
Example #21
Source File: transforms.py From AlphaPy with Apache License 2.0 | 5 votes |
def ema(f, c, p = 20): r"""Calculate the mean on a rolling basis. Parameters ---------- f : pandas.DataFrame Dataframe containing the column ``c``. c : str Name of the column in the dataframe ``f``. p : int The period over which to calculate the rolling mean. Returns ------- new_column : pandas.Series (float) The array containing the new feature. References ---------- *An exponential moving average (EMA) is a type of moving average that is similar to a simple moving average, except that more weight is given to the latest data* [IP_EMA]_. .. [IP_EMA] http://www.investopedia.com/terms/e/ema.asp """ new_column = pd.ewma(f[c], span=p) return new_column # # Function extract_bizday #
Example #22
Source File: macd_back_test.py From stock with Apache License 2.0 | 4 votes |
def self_adaptive_ma(stock_data): # 将数据按照交易日期从远到近排序 stock_data.sort('Date', inplace=True) close_price = stock_data['Adj Close'].get_values() high_price = stock_data['High'].get_values() low_price = stock_data['Low'].get_values() longDay = 100 print len(close_price) if len(close_price) < 100: return print close_price[-10:-1] direction = abs(close_price[-1] - close_price[-10]) volatility = sum(abs(close_price[i+1]-close_price[i]) for i in range(-9,0)) ER = abs(direction/volatility) fastSC = 2.0/(2.0+1) slowSC = 2.0/(30.0+1) sSC = ER * (fastSC-slowSC) + slowSC constaint = sSC*sSC #EMA 100 ema_close_100 = pd.ewma(close_price, span=longDay) ema_high_100 = pd.ewma(high_price, span=longDay) ema_low_100 = pd.ewma(low_price, span=longDay) amaClose = ema_close_100[-1] + constaint * (close_price[-1] - ema_close_100[-1]) amaHigh = ema_high_100[-1] + constaint * (high_price[-1] - ema_high_100[-1]) amaLow = ema_low_100[-1] + constaint * (low_price[-1] - ema_low_100[-1]) print ema_close_100[-1], ema_high_100[-1], ema_low_100[-1] BKPRICE = 0.0 SKPRICE = float('Inf') status = SIGNAL_DEFAULT print high_price[-1], low_price[-1], close_price[-1] if low_price[-1] > amaHigh: status = SIGNAL_BUY elif close_price[-1] < amaClose or close_price[-1] <= 0.995 * BKPRICE: status = SIGNAL_BUY elif high_price[-1] < amaLow: status = SIGNAL_SALE elif close_price[-1] > amaClose or close_price[-1] >= 1.005 * SKPRICE: status = SIGNAL_SALE return status # MA指标择时 (回测)
Example #23
Source File: utils.py From batchflow with Apache License 2.0 | 4 votes |
def draw(first, first_label, second=None, second_label=None, type_data='loss', window=5, bound=None, axis=None): """ Draw on graph first and second data. The graph shows a comparison of the average values calculated with a 'window'. You can draw one graph or create your oun subplots and one of it in 'axis'. Parameters ---------- first : list or numpy array Have a values to show first_label : str Name of first data second : list or numpy array, optional Have a values to show second_label : str, optional Name of second data type_data : str, optional Type of data. Example 'loss', 'accuracy' window : int, optional window width for calculate average value bound : list or None Bounds to limit graph: [min x, max x, min y, max y] axis : None or element of subplot If you want to draw more subplots give the element of subplot """ firt_ewma = pd.ewma(np.array(first), span=window, adjust=False) second_ewma = pd.ewma(np.array(second), span=window, adjust=False) if second else None plot = axis or matplotlib.pyplot plot.plot(firt_ewma, label='{} {}'.format(first_label, type_data)) if second_label: plot.plot(second_ewma, label='{} {}'.format(second_label, type_data)) if axis is None: plot.xlabel('Iteration', fontsize=16) plot.ylabel(type_data, fontsize=16) else: plot.set_xlabel('Iteration', fontsize=16) plot.set_ylabel(type_data, fontsize=16) plot.legend(fontsize=14) if bound: plot.axis(bound)