Esempio n. 1
12
    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
Esempio n. 2
0
def storage(code):
	try:
		a=ts.get_hist_data(code)
	except:
		print('{} is wrong'.format(code))
	else:
		if a is not None:
			a['code']=str(code)		        	            
			a.sort_index(inplace=True)
			boll(a)
			a['rsi']=rsi(a)
			kdj(a,9,3,3)
			a['macd']=pd.ewma(a.close,12)-pd.ewma(a.close,26)
			a['ma30']=pd.rolling_mean(a.close,30)
			a['ma60']=pd.rolling_mean(a.close,60)
			a['ma90']=pd.rolling_mean(a.close,90)
			a['change30']=pd.rolling_std(np.gradient(a.ma30),30)
			for t in [5,10,20,30,60,90]:
				a['max'+str(t)]=pd.rolling_max(a.close,t)
				a['min'+str(t)]=pd.rolling_min(a.close,t)
			a['macd_a']=pd.ewma(a.close,12)
			a['macd_d']=pd.ewma(a.close,26)
			a['diff5']=100*(a.shift(-5).close-a.close)/a.close
			a['diff10']=100*(a.shift(-10).close-a.close)/a.close
			a['diff5_c']=a.diff5.apply(category)
			a['diff10_c']=a.diff10.apply(category)
			a.dropna()
			return a
Esempio n. 3
0
def calc_ewmac_forecast(price, Lfast, Lslow=None):
    """
    Calculate the ewmac trading fule forecast, given a price and EWMA speeds Lfast, Lslow and vol_lookback

    """
    # 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

    price=price.resample("1B", how="last")

    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

    vol = robust_vol_calc(price.diff())

    return divide_df_single_column(raw_ewmac, vol)
Esempio n. 4
0
def fill_df_MACD_normalratio(df=None,params=None,names=['macd','signal','htg']):
    if df is None: return None
    if len(df.index)<40: 
        #print("index length=%d <40, return None"%len(index))
        return None
    #df=df.sort_index(ascending=True)
    if params is None: params=MACD_STD_PARAMS
    pos_scale=1
    neg_scale=1
    
    close=df.close
    ema1=pd.ewma(close,span=params[0])
    ema2=pd.ewma(close,span=params[1])

    macd=(ema1-ema2)/ema2*100
    signal=pd.ewma(macd,span=params[2])
    htg=macd-signal

    macd_max=macd.max()
    macd_min=macd.min()

    pos_scale=abs(1/macd_max)
    neg_scale=abs(1/macd_min)

    macd=macd.map(lambda x :x*neg_scale if x<0 else x*pos_scale)
    signal=signal.map(lambda x :x*neg_scale if x<0 else x*pos_scale)
    htg=htg.map(lambda x :x*neg_scale if x<0 else x*pos_scale)


    #df['ema'+str(params[0])]=ema1
    #df['ema'+str(params[1])]=ema2
    df[names[0]]=macd
    df[names[1]]=signal
    df[names[2]]=htg
    return df
Esempio n. 5
0
def get_MACD_ratio(se,params=[12,26,9]):
    ema1=pd.ewma(se,span=params[0])
    ema2=pd.ewma(se,span=params[1])
    macd=(ema1-ema2)/ema2*100
    signal=pd.ewma(macd,span=params[2])
    htg=macd-signal
    return macd,signal,htg
Esempio n. 6
0
def get_MACD(se,params=[12,26,9]):
    ema1=pd.ewma(se,span=params[0])
    ema2=pd.ewma(se,span=params[1])
    macd=ema1-ema2
    signal=pd.ewma(macd,span=params[2])
    htg=macd-signal
    return macd,signal,htg
def ADX(df, n, n_ADX):  
    i = 0  
    UpI = []  
    DoI = []  
    while i + 1 <= df.index[-1]:  
        UpMove = df.get_value(i + 1, 'High') - df.get_value(i, 'High')  
        DoMove = df.get_value(i, 'Low') - df.get_value(i + 1, 'Low')  
        if UpMove > DoMove and UpMove > 0:  
            UpD = UpMove  
        else: UpD = 0  
        UpI.append(UpD)  
        if DoMove > UpMove and DoMove > 0:  
            DoD = DoMove  
        else: DoD = 0  
        DoI.append(DoD)  
        i = i + 1  
    i = 0  
    TR_l = [0]  
    while i < df.index[-1]:  
        TR = max(df.get_value(i + 1, 'High'), df.get_value(i, 'Close')) - min(df.get_value(i + 1, 'Low'), df.get_value(i, 'Close'))  
        TR_l.append(TR)  
        i = i + 1  
    TR_s = pd.Series(TR_l)  
    ATR = pd.Series(pd.ewma(TR_s, span = n, min_periods = n))  
    UpI = pd.Series(UpI)  
    DoI = pd.Series(DoI)  
    PosDI = pd.Series(pd.ewma(UpI, span = n, min_periods = n - 1) / ATR)  
    NegDI = pd.Series(pd.ewma(DoI, span = n, min_periods = n - 1) / ATR)  
    ADX = pd.Series(pd.ewma(abs(PosDI - NegDI) / (PosDI + NegDI), span = n_ADX, min_periods = n_ADX - 1), name = 'ADX_' + str(n) + '_' + str(n_ADX))  
    df = df.join(ADX)  
    return df
Esempio n. 8
0
def plot(name):
        name = str(name)
        data_ext = YFHistDataExtr()
        data_ext.set_interval_to_retrieve(200)
        data_ext.set_multiple_stock_list([name])
        data_ext.get_hist_data_of_all_target_stocks()
        # convert the date column to date object
        data_ext.all_stock_df['Date'] =  pandas.to_datetime( data_ext.all_stock_df['Date'])
        temp_data_set = data_ext.all_stock_df.sort('Date', ascending=True)

        temp_data_set['20d_ma'] = pandas.rolling_mean(temp_data_set['Adj Close'], window=20)
        temp_data_set['50d_ma'] = pandas.rolling_mean(temp_data_set['Adj Close'], window=50)
        temp_data_set['Bol_upper'] = pandas.rolling_mean(temp_data_set['Adj Close'], window=20) + 2* pandas.rolling_std(temp_data_set['Adj Close'], 20, min_periods=20)
        temp_data_set['Bol_lower'] = pandas.rolling_mean(temp_data_set['Adj Close'], window=20) - 2* pandas.rolling_std(temp_data_set['Adj Close'], 20, min_periods=20)
        temp_data_set['Bol_BW'] = ((temp_data_set['Bol_upper'] - temp_data_set['Bol_lower'])/temp_data_set['20d_ma'])*100
        temp_data_set['Bol_BW_200MA'] = pandas.rolling_mean(temp_data_set['Bol_BW'], window=50)
        temp_data_set['Bol_BW_200MA'] = temp_data_set['Bol_BW_200MA'].fillna(method='backfill')
        temp_data_set['20d_exma'] = pandas.ewma(temp_data_set['Adj Close'], span=20)
        temp_data_set['50d_exma'] = pandas.ewma(temp_data_set['Adj Close'], span=50)
        data_ext.all_stock_df = temp_data_set.sort('Date', ascending = False)

        data_ext.all_stock_df.plot(x='Date', y=['Adj Close', '20d_ma', '50d_ma', 'Bol_upper', 'Bol_lower'])

        cur_path = os.path.dirname(os.path.abspath(__file__))
        #cur_path = os.path.join(cur_path, "app/analysis")
        data_path = os.path.join(cur_path, "raw_stock_data")
        print cur_path
        img_path = os.path.join(cur_path, "static/img")
        history_img_path = os.path.join(img_path, "history.png")
        dividend_img_path = os.path.join(img_path, "dividend.png")
        plt.savefig(history_img_path)
        data_ext.all_stock_df.plot(x='Date', y=['Bol_BW','Bol_BW_200MA'])
        plt.savefig(dividend_img_path)

        return render_template('analysis.html')
Esempio n. 9
0
def RSI(df, n):
    """
    Relative Strength Index
    """
    i = 0
    UpI = [0]
    DoI = [0]
    while i + 1 <= len(df) - 1: # df.index[-1]
        UpMove = df.get_value(i + 1, 'High') - df.get_value(i, 'High')
        DoMove = df.get_value(i, 'Low') - df.get_value(i + 1, 'Low')
        if UpMove > DoMove and UpMove > 0:
            UpD = UpMove
        else: UpD = 0
        UpI.append(UpD)
        if DoMove > UpMove and DoMove > 0:
            DoD = DoMove
        else: DoD = 0
        DoI.append(DoD)
        i = i + 1
    UpI = pd.Series(UpI)
    DoI = pd.Series(DoI)
    PosDI = pd.Series(pd.ewma(UpI, span = n, min_periods = n - 1))
    NegDI = pd.Series(pd.ewma(DoI, span = n, min_periods = n - 1))
    result = pd.Series(PosDI / (PosDI + NegDI), name = 'RSI_' + str(n))
    return out(SETTINGS, df, result)
Esempio n. 10
0
def Chaikin(df):
    """
    Chaikin Oscillator
    """
    ad = (2 * df['Close'] - df['High'] - df['Low']) / (df['High'] - df['Low']) * df['Volume']
    result = pd.Series(pd.ewma(ad, span = 3, min_periods = 2) - pd.ewma(ad, span = 10, min_periods = 9), name = 'Chaikin')
    return out(SETTINGS, df, result)
Esempio n. 11
0
def TSI(df, r, s,ksgn='close'):   
    
    '''
    def TSI(df, r, s,ksgn='close'):   
    TSI,真实强度指数,True Strength Index
  TSI是相对强弱指数 (RSI) 的变体。
  TSI 使用价格动量的双重平滑指数移动平均线,剔除价格的震荡变化并发现趋势的变化。
  r一般取25,是一般取13
    【输入】
        df, pd.dataframe格式数据源
        r,s,时间长度;  r一般取25,是一般取13
        ksgn,列名,一般是:close收盘价
    【输出】    
        df, pd.dataframe格式数据源,
        增加了一栏:tsi,输出数据
    '''
    xnam='tsi'.format(d=r,d2=s)
    M = pd.Series(df[ksgn].diff(1))  
    aM = abs(M)  
    EMA1 = pd.Series(pd.ewma(M, span = r, min_periods = r - 1))  
    aEMA1 = pd.Series(pd.ewma(aM, span = r, min_periods = r - 1))  
    EMA2 = pd.Series(pd.ewma(EMA1, span = s, min_periods = s - 1))  
    aEMA2 = pd.Series(pd.ewma(aEMA1, span = s, min_periods = s - 1))  
    TSI = pd.Series(EMA2 / aEMA2, name = xnam)  
    df = df.join(TSI)  
    
    return df
Esempio n. 12
0
def ADX(df, n, n_ADX):
    """
    Average Directional Movement Index
    """
    i = 0
    UpI = []
    DoI = []
    while i + 1 <= len(df) - 1: #df.index[-1]:
        UpMove = df.get_value(i + 1, 'High') - df.get_value(i, 'High')
        DoMove = df.get_value(i, 'Low') - df.get_value(i + 1, 'Low')
        if UpMove > DoMove and UpMove > 0:
            UpD = UpMove
        else: UpD = 0
        UpI.append(UpD)
        if DoMove > UpMove and DoMove > 0:
            DoD = DoMove
        else: DoD = 0
        DoI.append(DoD)
        i = i + 1
    i = 0
    TR_l = [0]
    while i < len(df) - 1: #df.index[-1]:
        TR = max(df.get_value(i + 1, 'High'), df.get_value(i, 'Close')) - min(df.get_value(i + 1, 'Low'), df.get_value(i, 'Close'))
        TR_l.append(TR)
        i = i + 1
    TR_s = pd.Series(TR_l)
    ATR = pd.Series(pd.ewma(TR_s, span = n, min_periods = n))
    UpI = pd.Series(UpI)
    DoI = pd.Series(DoI)
    PosDI = pd.Series(pd.ewma(UpI, span = n, min_periods = n - 1) / ATR)
    NegDI = pd.Series(pd.ewma(DoI, span = n, min_periods = n - 1) / ATR)
    result = pd.Series(pd.ewma(abs(PosDI - NegDI) / (PosDI + NegDI), span = n_ADX, min_periods = n_ADX - 1), name = 'ADX_' + str(n) + '_' + str(n_ADX))
    return out(SETTINGS, df, result)
Esempio n. 13
0
def MACD(df, n_fast, n_slow,ksgn='close'): 
    '''
    def MACD(df, n_fast, n_slow):           
      #MACD指标信号和MACD的区别, MACD Signal and MACD difference   
	MACD是查拉尔·阿佩尔(Geral Appel)于1979年提出的,由一快及一慢指数移动平均(EMA)之间的差计算出来。
	“快”指短时期的EMA,而“慢”则指长时期的EMA,最常用的是12及26日EMA:

    【输入】
        df, pd.dataframe格式数据源
        n,时间长度
        ksgn,列名,一般是:close收盘价
    【输出】    
        df, pd.dataframe格式数据源,
        增加了3栏:macd,sign,mdiff
    '''
    xnam='macd'.format(n=n_fast,n2=n_slow)
    xnam2='msign'.format(n=n_fast,n2=n_slow)
    xnam3='mdiff'.format(n=n_fast,n2=n_slow)
    EMAfast = pd.Series(pd.ewma(df[ksgn], span = n_fast, min_periods = n_slow - 1))  
    EMAslow = pd.Series(pd.ewma(df[ksgn], span = n_slow, min_periods = n_slow - 1))  
    MACD = pd.Series(EMAfast - EMAslow, name = xnam)  
    MACDsign = pd.Series(pd.ewma(MACD, span = 9, min_periods = 8), name =xnam2)  
    MACDdiff = pd.Series(MACD - MACDsign, name =xnam3)  
    df = df.join(MACD)  
    df = df.join(MACDsign)  
    df = df.join(MACDdiff)  
    return df
Esempio n. 14
0
def TRIX(df, n,ksgn='close'): 
    '''
    def TRIX(df, n,ksgn='close'): 
    
    【输入】
        df, pd.dataframe格式数据源
        n,时间长度
        ksgn,列名,一般是:close收盘价
    【输出】    
        df, pd.dataframe格式数据源,
        增加了一栏:trix_{n},输出数据
    '''
    xnam='trix_{n}'.format(n=n)
    EX1 = pd.ewma(df[ksgn], span=n, min_periods=n - 1)
    EX2 = pd.ewma(EX1, span=n, min_periods=n - 1)
    EX3 = pd.ewma(EX2, span=n, min_periods=n - 1)
    i = 0
    ROC_l = [0]
    while i + 1 <= len(df) - 1:  # df.index[-1]:
        ROC = (EX3[i + 1] - EX3[i]) / EX3[i]
        ROC_l.append(ROC)
        i = i + 1
    trix  = pd.Series(ROC_l, name=xnam)
    #df = df.join(trix)     
    trix.index=df.index;
    df[xnam]=trix
     
    #print(trix.tail())
    #print('n',len(df))
    return df
Esempio n. 15
0
def MassI(df):
    Range = df['high'] - df['low']
    EX1 = pd.ewma(Range, span = 9, min_periods = 8)
    EX2 = pd.ewma(EX1, span = 9, min_periods = 8)
    Mass = EX1 / EX2
    MassI = pd.Series(pd.rolling_sum(Mass, 25), name = 'MassIndex')
    return MassI
Esempio n. 16
0
def TEMA(ts, n):
    n = int(n)
    ts_ema1 = pd.Series( pd.ewma(ts, span = n, adjust = False), name = 'EMA_' + str(n) )
    ts_ema2 = pd.Series( pd.ewma(ts_ema1, span = n, adjust = False), name = 'EMA2_' + str(n) )
    ts_ema3 = pd.Series( pd.ewma(ts_ema2, span = n, adjust = False), name = 'EMA3_' + str(n) )
    ts_tema = pd.Series( 3 * ts_ema1 - 3 * ts_ema2 + ts_ema3, name = 'TEMA_' + str(n) )
    return ts_tema
Esempio n. 17
0
def indicators(prices, window=20):
    
    # Calculate out the Moving Average and the SD of the prices
    sma = pd.stats.moments.rolling_mean(prices, window) 
    sd = pd.stats.moments.rolling_std(prices, window)
    # Ratio to the bolligner bands (above upper band is > 1; below lower band <-1)    
    bb_diff =  (prices - sma)/(2*sd)
        
    # Ratio to a recent high in a specific window   
    winmax = pd.stats.moments.rolling_max(prices, window)
    high_diff = (prices-winmax)/(2*sd)
    
    # Ratio to a recent low in a specific window         
    winlow = pd.stats.moments.rolling_min(prices, window)
    low_diff = (prices-winlow)/(2*sd)
    
    #Moving Average Convergence Divergence (MACD) using exponentially weighted Moving average
    macd=pd.ewma(prices,span=window) - pd.ewma(prices,window/2) 
    
    # combine all to a dataframe 
    df = pd.DataFrame(pd.concat([bb_diff, high_diff, low_diff, macd], axis=1))
    df.columns = ['bb_diff', 'high_diff', 'low_diff',  'macd']
    df.columns =  [prices.columns.values[0] + '_' + x for x in df.columns]

    # Normalaise the data to between -1 and 1    
    df = df.apply(lambda x: ((x - np.min(x)))*2 / (np.max(x) - np.min(x))-1)    
    return df[[0,1,2,3]]
Esempio n. 18
0
def MACD(df, n_fast, n_slow):
    EMAfast = pd.Series(pd.ewma(df['close'], span = n_fast, min_periods = n_slow - 1))
    EMAslow = pd.Series(pd.ewma(df['close'], span = n_slow, min_periods = n_slow - 1))
    MACD = pd.Series(EMAfast - EMAslow, name = 'MACD_' + str(n_fast) + '_' + str(n_slow))
    MACDsign = pd.Series(pd.ewma(MACD, span = 9, min_periods = 8), name = 'MACDsign_' + str(n_fast) + '_' + str(n_slow))
    MACDdiff = pd.Series(MACD - MACDsign, name = 'MACDdiff_' + str(n_fast) + '_' + str(n_slow))
    return pd.concat([MACD, MACDsign, MACDdiff], join='outer', axis=1)
Esempio n. 19
0
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)   
Esempio n. 20
0
def select_Time_TRIX(stock_data, stockName):
    
    start_money = 100000000
    now_count = 0
    now_money = start_money
    
    # 将数据按照交易日期从远到近排序
    stock_data.sort('Date', inplace=True)
    
    close_price = stock_data['Adj Close'].get_values()
    
    #EMA 
    ma_list = [AVR_SHORT, AVR_SHORT] #N,M
    
    ema_close = pd.ewma(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, ma_list[1])
    
    
    signals = [0]*(ma_list[1]+1)
    tradeTimes = 0
    bBuySignal = True 
    for t in range(ma_list[1]+1, len(close_price)):
        
        signal = SIGNAL_DEFAULT
        
        if trixs[t] > trixs[t-1] and trixs[t] > maxtrix[t] \
                                        and trixs[t-1] < maxtrix[t-1]:
            if bBuySignal:
                signal = SIGNAL_BUY
                now_count = (int)(now_money / close_price[t] /100)*100
                now_money = start_money - now_count * close_price[t]
                #print u"股票价格/持股数/剩余金额:",close_price[t], '/',  now_count, '/', now_money
                bBuySignal = False 
        elif trixs[t] < trixs[t-1] and trixs[t] < maxtrix[t] \
                        and trixs[t-1] > maxtrix[t-1]:
            if bBuySignal == False:
                signal = SIGNAL_SALE
                now_money += now_count * close_price[t]
                now_count = 0
                #print u"股票价格/持股数/剩余金额:",close_price[t], '/',  now_count, '/', now_money
                bBuySignal = True      
        signals.append(signal)
        if signal != 0:
            #print 't:', t, '  signal:', signal
            tradeTimes += 1

    
    
    print stockName, u"收益率:", (now_money+now_count * close_price[-1]-start_money)/start_money*100, '%\t' \
        u"交易次数", tradeTimes, u" 最新市值:", now_money+now_count * close_price[-1]  
    stock_data['SIGNAL_TRIX'] = signals
Esempio n. 21
0
 def select_Time_MACD(self):
     
     #EMA 
     ma_list = [self.AVR_SHORT, self.AVR_LONG]
     ma_dea = 10
     if ma_list[0] == self.AVR_SHORT and ma_list[1] == self.AVR_LONG:
         ema_close_short = self.ema_12
         ema_close_long = self.ema_40
     else:     
         ema_close_short = pd.ewma(self.close_price, span=ma_list[0])
         ema_close_long = pd.ewma(self.close_price, span=ma_list[1])
     
     
     dif_price = ema_close_short - ema_close_long
     dea_price = pd.ewma(dif_price, span=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            
Esempio n. 22
0
 def select_Time_TRIX(self):
     
     #EMA 
     ma_list = [self.AVR_SHORT, self.AVR_SHORT] #N,M
     
     if ma_list[0] == self.AVR_SHORT:
         ema_close = self.ema_12
     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, ma_list[1])
     
     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
def MACD( ohlc, f=12, s=26, m=9):
  fast_ema = pd.ewma( ohlc.close, span=f)
  slow_ema = pd.ewma( ohlc.close, span=s)
  macd = fast_ema - slow_ema
  macd_sig = pd.ewma( macd, span=9)
  hist = macd - macd_sig
  return pd.DataFrame( {"MACD_hist":hist, "MACD":macd_sig}, index=[ohlc.index])
def ELI( ohlc, n=14):
  """ This is the Ehler's Leading Indicator ... it predicts cyclical changes
      in price using a tripple EMA scheme. It first calculates two EMAs on the
      original price: one that's 1/2 n and the other 1/4 n. Then those are
      subtracted, giving us a synthetic price. This is smoothed with an EMA
      of 1/4 n. The ELI is calculated by subtracting the syn from the syn's EMA.
      This leads cyclical changes, assuming they exist in the data.

      Parameters:
      ohlc : OHLC dataframe to take our prices from. Right now we're using close,
             but theoretically this could be anything.
      n : n-period cycles in our data. Tuning this parameter totally depends
          on the market/dataset, but I found 14 to work well for now.
  """
  a = n / 4.0
  _EMA1 = pd.ewma( ohlc.close, span=a)

  a = n / 2.0
  _EMA2 = pd.ewma( ohlc.close, span=a)

  syn = _EMA1 - _EMA2
  _EMAsyn = pd.ewma( syn, span=a)
  _ELI = syn - _EMAsyn

  return pd.DataFrame( { "ELI_%s"%n: _ELI }, index=[ohlc.index])
Esempio n. 25
0
def RSI(df, n):
    i = 0
    UpI = [0]
    DoI = [0]
    while i + 1 <= df.index[-1]:
        UpMove = df.get_value(i + 1, "High") - df.get_value(i, "High")
        DoMove = df.get_value(i, "Low") - df.get_value(i + 1, "Low")
        if UpMove > DoMove and UpMove > 0:
            UpD = UpMove
        else:
            UpD = 0
        UpI.append(UpD)
        if DoMove > UpMove and DoMove > 0:
            DoD = DoMove
        else:
            DoD = 0
        DoI.append(DoD)
        i = i + 1
    UpI = Series(UpI)
    DoI = Series(DoI)
    PosDI = Series(ewma(UpI, span=n, min_periods=n - 1))
    NegDI = Series(ewma(DoI, span=n, min_periods=n - 1))
    RSI = Series(PosDI / (PosDI + NegDI), name="RSI_" + str(n))
    df = df.join(RSI)
    return df
Esempio n. 26
0
def computeBidAskVolumeFeats(df, feats_df):
    df['askbid_vol_ema_200ms'] = df['ask_vol_ema_200ms'] - df['bid_vol_ema_200ms']
    df['askbid_vol_ema_2s'] = df['ask_vol_ema_2s'] - df['bid_vol_ema_2s']
    df['askbid_vol_ema_10s'] = df['ask_vol_ema_10s'] - df['bid_vol_ema_10s']
    feats_df['diff_200ms_2s_askbid_vol_ema'] = df['askbid_vol_ema_200ms'] - df['askbid_vol_ema_2s']  #1
    feats_df['diff_2s_10s_askbid_vol_ema'] = df['askbid_vol_ema_2s'] - df['askbid_vol_ema_10s']  #1
    feats_df['d_diff_200ms_2s_askbid_vol_ema'] = feats_df['diff_200ms_2s_askbid_vol_ema'] - feats_df['diff_200ms_2s_askbid_vol_ema'].shift(1)  #1
    feats_df['d_diff_2s_10s_askbid_vol_ema'] = feats_df['diff_2s_10s_askbid_vol_ema'] - feats_df['diff_2s_10s_askbid_vol_ema'].shift(2)  #1

    feats_df['norm_askbid_vol_ema_200ms'] = (df['ask_vol_ema_200ms'] - df['bid_vol_ema_200ms']) / (df['ask_vol_ema_200ms'] + df['bid_vol_ema_200ms'])  #1
    feats_df['norm_askbid_vol_ema_2s'] = (df['ask_vol_ema_2s'] - df['bid_vol_ema_2s']) / (df['ask_vol_ema_2s'] + df['bid_vol_ema_2s'])  #1
    feats_df['norm_askbid_vol_ema_10s'] = (df['ask_vol_ema_10s'] - df['bid_vol_ema_10s']) / (df['ask_vol_ema_10s'] + df['bid_vol_ema_10s'])  #1
    feats_df['diff_norm_200ms_2s_askbid_vol_ema'] = feats_df['norm_askbid_vol_ema_200ms'] - feats_df['norm_askbid_vol_ema_2s']  #1
    feats_df['diff_norm_2s_10s_askbid_vol_ema'] = feats_df['norm_askbid_vol_ema_2s'] - feats_df['norm_askbid_vol_ema_10s']  #1
    
    df['smooth_norm_askbid_vol_ema_200ms'] = pd.ewma(feats_df['norm_askbid_vol_ema_200ms'], span=25)
    df['smooth_norm_askbid_vol_ema_2s'] = pd.ewma(feats_df['norm_askbid_vol_ema_2s'], span=25)
    df['smooth_norm_askbid_vol_ema_10s'] = pd.ewma(feats_df['norm_askbid_vol_ema_10s'], span=25)
    feats_df['smooth_diff_norm_200ms_2s_askbid_vol_ema'] = pd.ewma(feats_df['diff_norm_200ms_2s_askbid_vol_ema'], span=25)  #1
    feats_df['smooth_diff_norm_2s_10s_askbid_vol_ema'] = pd.ewma(feats_df['diff_norm_2s_10s_askbid_vol_ema'], span=25)  #1

    feats_df['d_smooth_diff_norm_200ms_2s_askbid_vol_ema'] = feats_df['smooth_diff_norm_200ms_2s_askbid_vol_ema'] - feats_df['smooth_diff_norm_200ms_2s_askbid_vol_ema'].shift(1)  #1
    feats_df['d_smooth_diff_norm_2s_10s_askbid_vol_ema'] = feats_df['smooth_diff_norm_2s_10s_askbid_vol_ema'] - feats_df['smooth_diff_norm_2s_10s_askbid_vol_ema'].shift(5)  #1

    return feats_df
Esempio n. 27
0
def MACD(df, n_fast, n_slow, price='Close'):
    """
    MACD, MACD Signal and MACD difference
    """
    EMAfast = pd.Series(
        pd.ewma(
            df[price],
            span=n_fast,
            min_periods=n_slow - 1
        )
    )
    EMAslow = pd.Series(
        pd.ewma(
            df[price],
            span=n_slow,
            min_periods=n_slow - 1
        )
    )
    MACD = pd.Series(EMAfast - EMAslow, name='MACD_%d_%d' % (n_fast, n_slow))
    MACDsign = pd.Series(
        pd.ewma(
            MACD,
            span=9,
            min_periods=8
        ),
        name='MACDsign_%d_%d' % (n_fast, n_slow)
    )
    MACDdiff = pd.Series(
        MACD - MACDsign,
        name='MACDdiff_%d_%d' % (n_fast, n_slow)
    )
    result = pd.DataFrame([MACD, MACDsign, MACDdiff]).transpose()
    return out(SETTINGS, df, result)
Esempio n. 28
0
def get_signals(data, long_window, short_window, confirmation_window):
    returns = data['alpha'].cumsum()
    _short = pd.ewma(returns, span=short_window)
    _long = pd.ewma(returns, span=long_window)
    result = _short > _long
    result = _adjust_series_of_signals(result, confirmation_window)
    return remove_consecutive_values(result)
Esempio n. 29
0
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
Esempio n. 30
0
def calc_ewmac_forecast(price, Lfast, Lslow):
    price=price.resample("1B", how="last")
    fast_ewma = pd.ewma(price, span=Lfast)
    slow_ewma = pd.ewma(price, span=Lslow)
    raw_ewmac = fast_ewma - slow_ewma
    vol = robust_vol_calc(price.diff())
    return raw_ewmac /  vol
Esempio n. 31
0
def plot_agg_comparison(LD,
                        value,
                        groupby,
                        name_legend_map,
                        sm=5,
                        n_quantiles=10):
    '''Make Bokeh time series plot.
    INPUTS:
        LD: pandas dataframe with loan data
        value: name of column to plot as response variable.
        groupby: name of column to use for grouping data.
        sm: number of months to use for EWMA smoothing window (default 5).
        n_quantiles: number of quantiles to use if plotting response variable by quantiles.
    OUTPUTS:
        (script, div) html components of Bokeh plot.'''

    min_counts = 500  #min number of loans for each group (across all time points) for inclusion in plot
    if value == 'counts':  #treat response variable 'counts' by setting that to the agg fnx.
        agg = 'count'
        value = 'ROI'
    else:
        agg = 'mean'

    if groupby == 'loan_status':  #specify order and pallete if plotting by loan-status
        group_set = [
            'Fully Paid', 'Current', 'In Grace Period', 'Late (16-30 days)',
            'Late (31-120 days)', 'Default', 'Charged Off'
        ]
        pal = sns.cubehelix_palette(n_colors=len(group_set)).as_hex()
    elif groupby == 'quantiles':  #same for if plotting by quantiles
        quantiles = np.linspace(n_quantiles, 100 - n_quantiles,
                                n_quantiles).astype(float) / 100.
        pal = sns.cubehelix_palette(n_colors=len(quantiles)).as_hex()
    else:  #otherwise, assume categorical grouping vars with no specified plotting order
        group_set = np.sort(LD[groupby].unique())
        pal = sns.color_palette("muted", n_colors=len(group_set)).as_hex()

    start_date = np.datetime64(dt.datetime(
        2009, 01, 01))  #only plot data for loans issued after this time
    if groupby == 'quantiles':
        time_avgs = LD[LD.issue_d > start_date].groupby(
            ['issue_d'])[value].quantile(quantiles)
        time_avgs = time_avgs.swaplevel(0, 1)  # swap quantile and time levels
        group_set = time_avgs.index.levels[0]
    else:
        time_avgs = LD[LD.issue_d > start_date].groupby([groupby, 'issue_d'
                                                         ])[value].agg(agg)
        time_counts = LD[LD.issue_d > start_date].groupby([groupby, 'issue_d'
                                                           ])[value].count()

    time_avgs.index.names = [groupby, 'issue_d']

    ylabel = name_legend_map[value]

    if sm > 0:  #apply EWMA smoothing to time-grouped avgs
        time_avgs = pd.ewma(time_avgs, span=sm)

    s1 = figure(x_axis_type="datetime",
                plot_width=800,
                plot_height=500,
                tools=tools,
                x_axis_label='Issue Date',
                y_axis_label=ylabel)

    for idx, group in enumerate(group_set):
        if groupby != 'quantiles':
            if time_counts.ix[group].sum() >= min_counts:
                group_avg = time_avgs.ix[group]
                s1.line(group_avg.index,
                        group_avg,
                        line_color=pal[idx],
                        line_width=4,
                        legend=str(group))
        else:
            group_avg = time_avgs.ix[group]
            s1.line(group_avg.index,
                    group_avg,
                    line_color=pal[idx],
                    line_width=4,
                    legend=str(group))

    s1.legend.label_text_font_size = '13'
    s1.legend.orientation = "bottom_right"
    return components(s1)
Esempio n. 32
0
    df = df.set_index('DATE')
    PRICE0[prod] = df
PRICE = PRICE0

# calculate price series momentum (10-day), RSI (14-day), and MFI (14-days)
print('Calculate secondary series')
for prod in PRICE.keys():
    previous = PRICE[prod].shift(periods=1)
    # RSI
    PRICE[prod]['UPMOVE'] = \
        (PRICE[prod]['CLOSE'] > previous['CLOSE']) * \
        (PRICE[prod]['CLOSE'] - previous['CLOSE'])
    PRICE[prod]['DOWNMOVE'] = \
        (PRICE[prod]['CLOSE'] < previous['CLOSE']) * \
        (previous['CLOSE'] - PRICE[prod]['CLOSE'])
    U = pd.ewma(PRICE[prod]['UPMOVE'], com=6.5)
    D = pd.ewma(PRICE[prod]['DOWNMOVE'], com=6.5)
    RS = U / D
    PRICE[prod]['RSI'] = 100 - 100. / (1 + RS)
    PRICE[prod].drop('UPMOVE', axis=1, inplace=True)
    PRICE[prod].drop('DOWNMOVE', axis=1, inplace=True)
    # MFI
    PRICE[prod]['TP'] = (PRICE[prod]['HIGH'] + PRICE[prod]['LOW'] +
                         PRICE[prod]['CLOSE']) / 3.
    PRICE[prod]['POSFLOW'] = \
        (PRICE[prod]['CLOSE'] > previous['CLOSE']) * \
        PRICE[prod]['TP'] * PRICE[prod]['VOLUME']
    PRICE[prod]['NEGFLOW'] = \
        (PRICE[prod]['CLOSE'] < previous['CLOSE']) * \
        PRICE[prod]['TP'] * PRICE[prod]['VOLUME']
    POS = pd.ewma(PRICE[prod]['POSFLOW'], com=6.5)
Esempio n. 33
0
 def ewma_metric(cls, n, df_price, name, adjust=True):
     ewma_df = pd.ewma(df_price[[name]], n, adjust=adjust)
     ewma_mt = list(ewma_df[name])
     return ewma_mt
Esempio n. 34
0
                         ])
    for key, value in dftest[4].items():
        dfoutput['Critical value (%s)' % key] = value

    print(dfoutput)


test_stationarity(data_label)

#处理时间序列:
#让时序数据变得稳定,去除趋势:平滑: 以一个滑动窗口内的均值代替原来的值,为了使值之间的差距缩小
#而在许多情况下,可以认为越近的时刻越重要。所以引入指数加权移动平均--
# Exponentially-weighted moving average.(pandas中通过ewma()函数提供了此功能。)
data_label_log = np.log(data_label)
# halflife的值决定了衰减因子alpha:  alpha = 1 - exp(log(0.5) / halflife)
expmoving_avg = pd.ewma(data_label_log, 200)
data_label_log_moving_diff = data_label_log - expmoving_avg

print('aaaaaa')
print('data_label_log_moving_diff.type', data_label_log_moving_diff.dtype)

print('data_label_log_moving_diff:', data_label_log_moving_diff)
data_label_log_moving_diff.dropna(inplace=True)
test_stationarity(data_label_log_moving_diff)

#第四步 对时序数据进行预测
#一阶差分后数据已经稳定,所以d=1。所以用一阶差分化的data_label_log_moving_diff = data_label_log-expmoving_avg 作为输入
#先画出ACF,PACF的图像
from statsmodels.tsa.stattools import acf, pacf
#from statsmodels.graphics.tsaplots import acf, pacf
lag_acf = acf(data_label_log_moving_diff, nlags=20)
Esempio n. 35
0
def ewma(df_in, periods):
    if abs(periods) < 2:
        return df_in
    else:
        return pd.ewma(df_in, abs(periods), min_periods=abs(periods)) 
Esempio n. 36
0
for m in mkts.keys():
    try:
        data_index[m] = quandl.get(mkts[m], authtoken=token).Last
    except:
        try:
            data_index[m] = quandl.get(mkts[m], authtoken=token).Settle
        except:
            try:
                data_index[m] = quandl.get(mkts[m], authtoken=token).Value
            except:
                try:
                    data_index[m] = quandl.get(mkts[m], authtoken=token).value
                except:
                    try:
                        data_index[m] = quandl.get(mkts[m],
                                                   authtoken=token).Rate
                    except:
                        print(m)
data_pct = data_index.pct_change()

mu = pd.ewma(data_pct, 60)
sd = pd.ewmstd(data_pct, 60)
zscores = (data_pct - mu) / sd
last = zscores.iloc[-2].dropna().sort_values()
last.plot(kind='barh', colormap='jet',
          ylim=[-3, 3]).get_figure().savefig('zscore.png', bbox_inches='tight')

e = Email(to='*****@*****.**',
          subject='Morning Update: Macro Dashboard')
e.add_attachment('zscore.png')
e.send()
def EMA(df, n):
    EMA = pd.Series(pd.ewma(df['Close'], span=n, min_periods=n - 1),
                    name='EMA_' + str(n))
    df = df.join(EMA)
    return df
Esempio n. 38
0
def feature_engineering(df, complete_dates):

    df = df.groupby( ["Ciclo_Estacion", "day_counter", "ITERATION", "iteration_start", "iteration_end"])["hora"].count()
    df = df.sort_index()
    df = df.reset_index()
    df = df.rename( columns = {"hora": "flow"})

    df_append = pd.DataFrame()
    for station in df.Ciclo_Estacion.unique():
        df_station = df[df.Ciclo_Estacion == station]
        df_merge = complete_dates.merge( df_station, on= ["day_counter", "ITERATION", "iteration_start", "iteration_end"], how ="left" )
        df_merge["Ciclo_Estacion"] = station
        df_merge.loc[pd.isnull(df_merge.flow), "flow"] =0 

        if len(df_append) ==0 :
            df_append = df_merge
        else:
            df_append = df_append.append(df_merge)

    df = df_append

    #ITERATION (15 minutes) LAG VALUES
    df["flow_lag1"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(1)
    df["flow_lag2"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(2)
    df["flow_lag3"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(3)
    df["flow_lag4"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(4)
    df["flow_lag5"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(5)
    df["flow_lag6"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(6)
    df["flow_lag7"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(7)
    df["flow_lag8"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(8)

    df["flow_rollingmean_lag1_4"]  = pd.rolling_mean( df["flow_lag1"], 4)
    df["flow_rollingmean_lag1_8"]  = pd.rolling_mean( df["flow_lag1"], 8)
    df["flow_rollingmean_lag1_12"] = pd.rolling_mean( df["flow_lag1"], 12)
    df["flow_rollingmean_lag1_16"] = pd.rolling_mean( df["flow_lag1"], 16)

    df["flow_rollingmean_lag4_4"]  = pd.rolling_mean( df["flow_lag4"], 4)
    df["flow_rollingmean_lag4_8"]  = pd.rolling_mean( df["flow_lag4"], 8)
    df["flow_rollingmean_lag4_12"] = pd.rolling_mean( df["flow_lag4"], 12)
    df["flow_rollingmean_lag4_16"] = pd.rolling_mean( df["flow_lag4"], 16)

    df["flow_rollingmean_lag8_4"]  = pd.rolling_mean( df["flow_lag8"], 4)
    df["flow_rollingmean_lag8_8"]  = pd.rolling_mean( df["flow_lag8"], 8)
    df["flow_rollingmean_lag8_12"] = pd.rolling_mean( df["flow_lag8"], 12)
    df["flow_rollingmean_lag8_16"] = pd.rolling_mean( df["flow_lag8"], 16)

    df["flow_ewma_lag1_4"]  = pd.ewma( df["flow_lag1"], 4)
    df["flow_ewma_lag1_8"]  = pd.ewma( df["flow_lag1"], 8)
    df["flow_ewma_lag1_12"] = pd.ewma( df["flow_lag1"], 12)    
    df["flow_ewma_lag1_16"] = pd.ewma( df["flow_lag1"], 16)

    df["flow_ewma_lag4_4"]  = pd.ewma( df["flow_lag4"], 4)
    df["flow_ewma_lag4_8"]  = pd.ewma( df["flow_lag4"], 8)
    df["flow_ewma_lag4_12"] = pd.ewma( df["flow_lag4"], 12)    
    df["flow_ewma_lag4_16"] = pd.ewma( df["flow_lag4"], 16)

    df["flow_ewma_lag8_4"]  = pd.ewma( df["flow_lag4"], 4)
    df["flow_ewma_lag8_8"]  = pd.ewma( df["flow_lag8"], 8)
    df["flow_ewma_lag8_12"] = pd.ewma( df["flow_lag8"], 12)    
    df["flow_ewma_lag8_16"] = pd.ewma( df["flow_lag8"], 16)

    #DAYs LAG VALUES
    df["flow_lag1day"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(94)
    df["flow_lag2day"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(95)
    df["flow_lag3day"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(96)
    df["flow_lag4day"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(97)
    df["flow_lag5day"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(98)
    df["flow_lag6day"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(99)
    df["flow_lag7day"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(100)
    df["flow_lag8day"] = df.groupby(["Ciclo_Estacion"])["flow"].shift(101)

    df["flow_rollingmean_lag1day_4"]  = pd.rolling_mean( df["flow_lag1day"], 4)
    df["flow_rollingmean_lag1day_8"]  = pd.rolling_mean( df["flow_lag1day"], 8)
    df["flow_rollingmean_lag1day_12"] = pd.rolling_mean( df["flow_lag1day"], 12)
    df["flow_rollingmean_lag1day_16"] = pd.rolling_mean( df["flow_lag1day"], 16)


    df["flow_rollingmean_lag4day_4"]  = pd.rolling_mean( df["flow_lag4day"], 4)
    df["flow_rollingmean_lag4day_8"]  = pd.rolling_mean( df["flow_lag4day"], 8)
    df["flow_rollingmean_lag4day_12"] = pd.rolling_mean( df["flow_lag4day"], 12)
    df["flow_rollingmean_lag4day_16"] = pd.rolling_mean( df["flow_lag4day"], 16)


    df["flow_rollingmean_lag8day_4"]  = pd.rolling_mean( df["flow_lag8day"], 4)
    df["flow_rollingmean_lag8day_8"]  = pd.rolling_mean( df["flow_lag8day"], 8)
    df["flow_rollingmean_lag8day_12"] = pd.rolling_mean( df["flow_lag8day"], 12)
    df["flow_rollingmean_lag8day_16"] = pd.rolling_mean( df["flow_lag8day"], 16)

    df["flow_ewma_lag1day_4"]  = pd.ewma( df["flow_lag1day"], 4)
    df["flow_ewma_lag1day_8"]  = pd.ewma( df["flow_lag1day"], 8)
    df["flow_ewma_lag1day_12"] = pd.ewma( df["flow_lag1day"], 12)    
    df["flow_ewma_lag1day_16"] = pd.ewma( df["flow_lag1day"], 16)

    df["flow_ewma_lag4day_4"]  = pd.ewma( df["flow_lag4day"], 4)
    df["flow_ewma_lag4day_8"]  = pd.ewma( df["flow_lag4day"], 8)
    df["flow_ewma_lag4day_12"] = pd.ewma( df["flow_lag4day"], 12)    
    df["flow_ewma_lag4day_16"] = pd.ewma( df["flow_lag4day"], 16)

    df["flow_ewma_lag8day_4"]  = pd.ewma( df["flow_lag8day"], 4)
    df["flow_ewma_lag8day_8"]  = pd.ewma( df["flow_lag8day"], 8)
    df["flow_ewma_lag8day_12"] = pd.ewma( df["flow_lag8day"], 12)    
    df["flow_ewma_lag8day_16"] = pd.ewma( df["flow_lag8day"], 16)

    #WEEK LAG VALUES
    df["month"]     = df.iteration_start.apply(lambda x: x.date().month)
    df["day_month"] = df.iteration_start.apply(lambda x: x.date().day)

    return df
Esempio n. 39
0

ts_log = np.log(ts)
plt.plot(ts_log)

moving_avg = pd.rolling_mean(ts_log, 12)
plt.plot(ts_log)
plt.plot(moving_avg, color='red')

ts_log_moving_avg_diff = ts_log - moving_avg
ts_log_moving_avg_diff.head(12)

ts_log_moving_avg_diff.dropna(inplace=True)
test_stationarity(ts_log_moving_avg_diff)

expwighted_avg = pd.ewma(ts_log, halflife=12)
plt.plot(ts_log)
plt.plot(expwighted_avg, color='red')

ts_log_ewma_diff = ts_log - expwighted_avg
test_stationarity(ts_log_ewma_diff)

ts_log_diff = ts_log - ts_log.shift()
plt.plot(ts_log_diff)

ts_log_diff.dropna(inplace=True)
test_stationarity(ts_log_diff)

from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(ts_log)
Esempio n. 40
0
					data = {"Close": before_close, "High" : high, "Low" : low}

					df = pandas.DataFrame(data)

					# print(df)
					# print(close_dataframe)

					# MACD = MACD(close_dataframe, 13, 26)


					n_fast = 13
					n_slow = 26



					EMAfast = pd.Series(pd.ewma(df['Close'], span = n_fast))  
					EMAslow = pd.Series(pd.ewma(df['Close'], span = n_slow))  
					MACD = pd.Series(EMAfast - EMAslow, name = 'MACD_' + str(n_fast) + '_' + str(n_slow))  
					MACDsign = pd.Series(pd.ewma(MACD, span = 9, min_periods = 8), name = 'MACDsign_' + str(n_fast) + '_' + str(n_slow))  
					MACDdiff = pd.Series(MACD - MACDsign, name = 'MACDdiff_' + str(n_fast) + '_' + str(n_slow))  
					df = df.join(MACD)  
					df = df.join(MACDsign)  
					df = df.join(MACDdiff)  

					print(df["MACDdiff_13_26"])


					MACD_signal = df["MACDdiff_13_26"].dropna().tolist()[-1] > 0

					MACD_1.append(df["MACDdiff_13_26"].dropna().tolist()[-1])
					MACD_2.append( df["MACDdiff_13_26"].dropna().tolist()[-2])
Esempio n. 41
0
print('生成日期范围:\n', pd.date_range('2012/1/4', '2012/4/6', freq='BM'))
print('生成日期范围:\n', pd.date_range('2012/3/4', '2012/4/6', freq='W-FRI'))

ts = pd.Series(np.random.randn(4),
               index=pd.date_range('1/1/2000', periods=4, freq='M'))
print('时间位移:', ts.shift(2))
print('时间位移:', ts.shift(-2))
print('时间位移:', ts.shift(1, freq='3D'))

s = pd.Series(df.trade_vol.values, index=df.time)
s2 = pd.Series(df.trade_pr.values, index=df.time)
ticks = pd.DataFrame(
    {
        'open': df.trade_pr.values,
        'high': df.s1pr.values,
        'low': df.b1pr.values,
        'close': df.trade_pr.values
    },
    index=df.time)
ms = s.resample('5min', how=sum)
print('降采样:', ms[:5])
mt = s2.resample('1min', how='ohlc', fill_method="ffill")
print('open high low close降采样:', mt[:10])

s15t = mt.resample('15s', fill_method='ffill')
print('升采样:', s15t[:10])

mean_s = pd.rolling_mean(s, 5, min_periods=1)
print('求移动均值:', mean_s[:10])
ema_s = pd.ewma(s, 60)
print('求指数移动均值:', mean_s[:10])
     df = pd.read_csv('../../data/5_minutes_dump.csv') # ШОРТЫ

    df.columns = ['open', 'close','low', 'high', 'volume', 'date_time', 'ex', 'typeBlockchain']
    df.index = df.date_time
    df = df.sort_index()
    
    x_scaler = MinMaxScaler()
    y_scaler = MinMaxScaler()

    all_df = df.copy()

    x = all_df[['open', 'low', 'high', 'volume']].copy()
    
    y = all_df['close'].copy()
    
    x = pd.ewma(x,2)
    y = pd.ewma(y,2)
    
    x[['open', 'low', 'high', 'volume']] = x_scaler.fit_transform(x)

    y = y_scaler.fit_transform(y.values.reshape(-1, 1))

    
    shape = x.shape[1]
    #X_train, y_train = load.load_data(x, WINDOW, TrainTest = False) # не удалять, чтобы переключить на сбор данных только трейна
    X_train, y_train, X_test, y_test = load.load_data(x, WINDOW, train_size= 0.96, TrainTest = True)
    
    model = build_model(input_shape=(WINDOW, shape))
    
    print('START FIT MODEL...')
    
Esempio n. 43
0
def ewma(series, span):
    ema = pd.ewma(series, span=span)
    return ema
Esempio n. 44
0
    yo = y_obs[0:i]
    mu, var = gu.gp_solve(xo, yo, x_pred, kernel_used, sig_noise, **params)

    gu.gp_plot(xo, yo, x_pred, mu, var)
    plt.plot(x, fx, 'orange', alpha=0.5)
    plt.pause(0.1)

# ----------------------
# Real Exmaple

#tmp = pd.read_csv('shop.csv')
tmp = pd.read_csv('auto.csv')
tmp = tmp.sort_index(ascending=False)

dt_raw = pd.Series(tmp['Close'].values, index=pd.to_datetime(tmp['Date']))
dt_smth = pd.ewma(dt_raw, halflife=15)
dt = dt_smth.resample('W-MON', how='last')
#dt = dt_smth.resample('M', how='last')
plt.figure()
plt.plot(dt, color='orange')
plt.grid(alpha=0.2)

t = np.arange(1, len(dt) + 1)
no_sample = 2
sig_noise = np.sqrt(0.01)

# periodicic
kernel_used = kf.kfunc_per
params = {'l': 5, 'p': 12 * 4}
t0 = t[0:no_sample]
y0 = dt[0:no_sample]
Esempio n. 45
0
    df = df.reset_index()
    df['30 mavg'] = pd.rolling_mean(df['Close'], 30)
    df['26 ema'] = pd.ewma(df['Close'], span=26)
    df['12 ema'] = pd.ewma(df['Close'], span=12)
    df['MACD'] = (df['12 ema'] - df['26 ema'])
    df['Signal'] = pd.ewma(df['MACD'], span=9)
    df['Crossover'] = df['MACD'] - df['Signal']
    return stock, df['Date'][-1:].to_string(),df['Crossover'][-1:].mean()


directory = 'C:/Users/Richard Hardis/.spyder-py3/'
df = pd.read_csv(directory+'SPY.csv')


# MACD 3,6,2
df['stock_df_3_ema'] = pd.ewma(df['Close'], span=3)
df['stock_df_6_ema'] = pd.ewma(df['Close'], span=6)
df['stock_df_macd_3_6'] = df['stock_df_3_ema'] - df['stock_df_6_ema']
df['stock_df_signal_3_6'] = pd.ewma(df['stock_df_macd_3_6'], span=2)
df['stock_df_crossover_3_6'] = df['stock_df_macd_3_6'] - df['stock_df_signal_3_6'] # means, if this is > 0, or stock_df['Crossover'] =  stock_df['MACD'] - stock_df['Signal'] > 0, there is a buy signal
                                                                               # means, if this is < 0, or stock_df['Crossover'] =  stock_df['MACD'] - stock_df['Signal'] < 0, there is a sell signal

# MACD 5,15,3
df['stock_df_5_ema'] = pd.ewma(df['Close'], span=5)
df['stock_df_15_ema'] = pd.ewma(df['Close'], span=15)
df['stock_df_macd_5_15'] = df['stock_df_5_ema'] - df['stock_df_15_ema']
df['stock_df_signal_5_15'] = pd.ewma(df['stock_df_macd_5_15'], span=3)
df['stock_df_crossover_5_15'] = df['stock_df_macd_5_15'] - df['stock_df_signal_5_15']  # means, if this is > 0, or stock_df['Crossover'] =  stock_df['MACD'] - stock_df['Signal'] > 0, there is a buy signal
                                                                                # means, if this is < 0, or stock_df['Crossover'] =  stock_df['MACD'] - stock_df['Signal'] < 0, there is a sell signal

    def __init__(self,
                 symbol='SPY',
                 sd=dt.datetime(2008, 1, 1),
                 ed=dt.datetime(2009, 1, 1),
                 sv=10000,
                 verbose=False):

        self.symbol = symbol
        self.sv = sv
        self.verbose = verbose
        self.cash = sv
        self.shares = 0
        self.position = 0

        # Read data
        dates = pd.date_range(sd - dt.timedelta(100), ed)
        df = get_data([symbol], dates)[symbol]

        # Normalize close to starting date
        norm_val = get_data([symbol],
                            pd.date_range(sd, sd + dt.timedelta(10)))[symbol]
        normed = df / norm_val.ix[0]

        # Determine features
        # Daily Returns
        rets = pd.DataFrame(df)
        daily_ret = rets[symbol].copy()
        daily_ret[1:] = (rets[symbol].ix[1:] / rets[symbol].ix[:-1].values) - 1
        daily_ret.ix[0] = 0

        # Relative Strnength Index
        up, down = daily_ret.copy(), daily_ret.copy()
        up[up < 0] = 0
        down[down > 0] = 0
        roll_up = up.rolling(14).mean()
        roll_down = down.rolling(14).mean().abs()
        rsi = 100.0 - (100.0 / (1.0 + roll_up / roll_down))

        # Simple moving average
        sma5 = normed.rolling(5).mean()
        sma10 = normed.rolling(10).mean()
        sma15 = normed.rolling(15).mean()
        sma20 = normed.rolling(20).mean()
        sma25 = normed.rolling(25).mean()
        sma30 = normed.rolling(30).mean()
        sma40 = normed.rolling(40).mean()

        # Volatility
        vol5 = normed.rolling(5).std()
        vol10 = normed.rolling(10).std()
        vol20 = normed.rolling(20).std()
        vol30 = normed.rolling(30).std()

        # Bollinger bands
        sma_bb = normed.rolling(5).mean()
        sma_bb_std = normed.rolling(5).std()
        bb = (normed -
              (sma_bb - 2 * sma_bb_std)) / ((sma_bb + 2 * sma_bb_std) -
                                            (sma_bb - 2 * sma_bb_std))

        # Moving average convergence/divergence
        ema12 = pd.ewma(np.asarray(normed), span=12)
        ema26 = pd.ewma(np.asarray(normed), span=26)
        macd = ema12 - ema26

        # Momentum
        momentum2 = normed / normed.shift(2) - 1
        momentum5 = normed / normed.shift(5) - 1
        momentum10 = normed / normed.shift(10) - 1

        # Combine into new dataframe
        df = pd.DataFrame(df).assign(sma15=normed / sma15 - 1).assign(
            sma5=normed / sma5 - 1).assign(bb=bb).assign(rsi=rsi).assign(
                momentum2=momentum2).assign(normed=normed).assign(
                    macd=macd).assign(vol10=vol10).assign(vol20=vol20).assign(
                        vol30=vol30).assign(vol10=vol10).assign(
                            sma10=normed / sma10 -
                            1).assign(sma20=normed / sma20 - 1).assign(
                                sma25=normed / sma25 -
                                1).assign(sma30=normed / sma30 - 1).assign(
                                    sma40=normed / sma40 -
                                    1).assign(vol5=vol5).assign(
                                        momentum5=momentum5).assign(
                                            momentum10=momentum10)[sd:]
        daily_ret.ix[0] = 0
        df = df.assign(dr=daily_ret)

        # Determine optimal features for states
        corr_df = df.corr().abs()
        # Plot results
        if verbose:
            fig, ax = plt.subplots(figsize=(len(corr.columns),
                                            len(corr.columns)))
            ax.matshow(corr)
            cmap = cm.get_cmap('coolwarm', 30)
            cax = ax.imshow(df.corr(), interpolation="nearest", cmap=cmap)
            plt.xticks(range(len(corr.columns)), corr.columns)
            plt.yticks(range(len(corr.columns)), corr.columns)
            fig.colorbar(cax, ticks=[0, .25, .5, .75, 1])
            plt.show()

        corr = corr_df['dr'][:]
        icorr = np.asarray(corr)
        scorr = icorr.argsort(
        )[-4:][::-1]  # select top 3 features and daily returns
        scorr = scorr[1:]  # remove daily returns from possible features

        optimal_ftrs = []
        for i in scorr:
            optimal_ftrs.append(corr_df.columns[i])

        self.optimal_ftrs = optimal_ftrs
        self.df = df
        self.market = df.iterrows()
        self.current = self.market.next()
        self.action = self.Action()
Esempio n. 47
0
def calcAllInstructor(stockCode):
    #MACD params is 5,10,7
    pa = os.getcwd()
    if os.path.exists(pa + '/' + stockCode + '_week.csv') == False:
        print stockCode, " week data not exists"
        return
    stock_data = pandas.read_csv(stockCode + '_week.csv')
    if stock_data['close'].count() == 0:
        print stockCode, " data empty, maybe exist stock market"
        return

    stock_data['amplitude'] = numpy.round(stock_data['close'].diff(), 3)
    stock_data['percent'] = numpy.round(stock_data['close'].pct_change(), 4)
    stock_data['MA3'] = numpy.round(
        pandas.rolling_mean(stock_data['close'], 3), 3)
    stock_data['EMA5'] = numpy.round(pandas.ewma(stock_data['close'], span=5),
                                     3)
    stock_data['EMA10'] = numpy.round(
        pandas.ewma(stock_data['close'], span=10), 3)
    stock_data['EMA20'] = numpy.round(
        pandas.ewma(stock_data['close'], span=20), 3)
    stock_data['EMA22'] = numpy.round(
        pandas.ewma(stock_data['close'], span=22), 3)
    stock_data['DIFF'] = map(lambda x, y: x - y, stock_data['EMA10'],
                             stock_data['EMA22'])
    stock_data['DEA'] = numpy.round(pandas.rolling_mean(stock_data['DIFF'], 7),
                                    3)

    stock_data['abspercent'] = map(lambda x: x * 100, stock_data['percent'])
    stock_data['EMA15PER'] = numpy.round(
        pandas.ewma(stock_data['abspercent'], span=15), 3)

    #now KDJ params is 9,3,3
    stock_data['low9'] = pandas.rolling_min(stock_data['close'], 9)
    stock_data['high9'] = pandas.rolling_max(stock_data['close'], 9)
    stock_data['quick_k'] = map(
        lambda x, y, z: numpy.round((x - y) / (z - y) * 100, 2)
        if z > y else 0, stock_data['close'], stock_data['low9'],
        stock_data['high9'])
    stock_data['quick_d'] = numpy.round(
        pandas.rolling_mean(stock_data['quick_k'], 3), 2)
    stock_data['slow_d'] = numpy.round(
        pandas.rolling_mean(stock_data['quick_d'], 3), 2)

    #calc low open diff
    stock_data['maxclosediff'] = map(lambda x: x
                                     if x > 0 else 0, stock_data['amplitude'])
    stock_data['absclosediff'] = map(lambda x: x
                                     if x > 0 else -x, stock_data['amplitude'])
    stock_data['fmrsi'] = numpy.round(
        pandas.ewma(stock_data['maxclosediff'], span=5), 3)
    stock_data['fzrsi'] = numpy.round(
        pandas.ewma(stock_data['absclosediff'], span=5), 3)
    stock_data['rsi5'] = numpy.round(
        map(lambda x, y: x / y * 100, stock_data['fmrsi'],
            stock_data['fzrsi']), 2)

    stock_data['bollstd'] = numpy.round(
        pandas.rolling_std(stock_data['close'], 20), 3)

    stock_data['jd'] = map(lambda x, y: x * y, stock_data['amplitude'],
                           stock_data['volume'])
    stock_data['ejd'] = pandas.ewma(stock_data['jd'], span=3)

    stock_data.to_csv(stockCode + '_weekewma.csv')
Esempio n. 48
0
def exponential_moving_average(df, windowsize=60):
    # df['ewma'] = pd.ewma(df["avg"], span=60, freq="D")
    pd.ewma(df["Adj Close"], span=windowsize, freq="D")
# to remove the presence of noise from the TS we can use some smoothing techniques


moving_avg = pd.rolling_mean(ts_log,8)
plt.plot(ts_log)
plt.plot(moving_avg, color='red')

ts_log_moving_avg_diff = ts_log - moving_avg
ts_log_moving_avg_diff.head(8)

ts_log_moving_avg_diff.dropna(inplace=True)
test_stationarity(ts_log_moving_avg_diff)


# exponentially moving average
expwighted_avg = pd.ewma(ts_log, halflife=8) # halflife is used to define amount of exponential decay
plt.plot(ts_log)
plt.plot(expwighted_avg, color='red')    

ts_log_ewma_diff = ts_log - expwighted_avg
test_stationarity(ts_log_ewma_diff)

## here t-stat val is lesser than 1% critical value hence this gives a better result


## ELIMINATING TREND and SEASONALITY
#differencing

ts_log_diff = ts_log - ts_log.shift()  #  first order differencing 
plt.plot(ts_log_diff)
    for key, value in dftest[4].items():
        dfoutput['Critical Value (%s)' % key] = value
    print dfoutput


test_stationarity(ts)
plt.show()
plt.plot(np.log(ts))
moving_avg = pd.rolling_mean(np.log(ts), 12)
plt.plot(moving_avg)
ts_log_moving_avg_diff = np.log(ts) - moving_avg
plt.show()
plt.plot(ts_log_moving_avg_diff)
ts_log_moving_avg_diff.dropna(inplace=True)
test_stationarity(ts_log_moving_avg_diff)
expwmavg = pd.ewma(np.log(ts), halflife=12)
ts_log_expwmavg = np.log(ts) - expwmavg
plt.show()
test_stationarity(ts_log_expwmavg)
plt.plot(ts_log_expwmavg)
plt.show()
plt.plot(np.log(ts) - np.log(ts).shift())

from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(np.log(ts))
trend = decomposition.trend
seasonal = decomposition.seasonal
resid = decomposition.resid

plt.show()
plt.plot(trend)
def ComputeEMA(data):
    #Exponential moving average
    D = PD.Series(data[:, 1])
    return PD.ewma(D, 0.99)
Esempio n. 52
0
@contact: QQ:2089973054 email:[email protected]
"""
import pandas as pd

# ========== 从原始csv文件中导入股票数据,以浦发银行sh600000为例

# 导入数据 - 注意:这里请填写数据文件在您电脑中的路径
stock_data = pd.read_csv('stock data/sh600000.csv', parse_dates=[1])

# 将数据按照交易日期从远到近排序
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['close'], ma)

# 计算指数平滑移动平均线EMA
for ma in ma_list:
    stock_data['EMA_' + str(ma)] = pd.ewma(stock_data['close'], span=ma)

# 将数据按照交易日期从近到远排序
stock_data.sort('date', ascending=False, inplace=True)

# ========== 将算好的数据输出到csv文件 - 注意:这里请填写输出文件在您电脑中的路径
stock_data.to_csv('sh600000_ma_ema.csv', index=False)
Esempio n. 53
0
    except:
        try:
            data_index[m] = quandl.get(mkts[m], authtoken=token).Settle
        except:
            try:
                data_index[m] = quandl.get(mkts[m], authtoken=token).Value
            except:
                try:
                    data_index[m] = quandl.get(mkts[m], authtoken=token).value
                except:
                    try:
                        data_index[m] = quandl.get(mkts[m],
                                                   authtoken=token).Rate
                    except:
                        print(m)
data_pct = data_index.pct_change()

factors = pd.DataFrame()
for f in factor_map.keys():
    factors[f] = data_pct[factor_map[f]].mean(axis=1)

mu = pd.ewma(factors, 260)
sd = pd.ewmstd(factors, 260)
zscores = (factors - mu) / sd
last = zscores.iloc[-2].dropna().sort_values()
last.plot(kind='barh', colormap='jet',
          ylim=[-3, 3]).get_figure().savefig('zscore.png', bbox_inches='tight')

e = Email(subject='Morning Update: Factor Dashboard')
e.add_attachment('zscore.png')
e.send()
Esempio n. 54
0
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)
Esempio n. 55
0
def EWMA(data, ndays):
    EMA = pd.Series(pd.ewma(data['Close'], span=ndays, min_periods=ndays - 1),
                    name='EWMA_' + str(ndays))
    data = data.join(EMA)
    return data
Esempio n. 56
0
def do_something():
    dateparse = lambda dates: pd.datetime.strptime(dates, '%Y-%m')
    data = pd.read_csv('AirPassengers.csv',
                       parse_dates=['date'],
                       index_col='date',
                       date_parser=dateparse)

    print data.head()
    ts = data['Passengers']

    ts_log = np.log(ts)

    # 平滑处理
    moving_avg = pd.Series.rolling(ts_log, 12).mean()
    # plt.plot(ts_log)
    # plt.plot(moving_avg, color='red')
    # plt.show()

    # 做差,均值和原始值的差
    ts_log_moving_avg_diff = ts_log - moving_avg
    adf_test(ts_log_moving_avg_diff.dropna(), 'ts_log_moving_avg_diff')
    plt.close()

    # 指数加权移动平均
    # 前面移动平均数需要指定window, 并且对所有的数一视同仁;
    # 这里采用指数加权移动平均方法,会对当前的数据加大权重,对过去的数据减小权重。
    # halflife半衰期,用来定义衰减量。其他参数, 如跨度span和质心com也可以用来定义衰减。
    expwighted_avg = pd.ewma(ts_log, halflife=12)
    plt.plot(ts_log)
    plt.plot(expwighted_avg, color='red')
    plt.savefig('pic/expwighted_avg.png')
    ts_log_ewma_diff = ts_log - expwighted_avg

    adf_test(ts_log_ewma_diff, 'ts_log_ewma_diff')

    # Test Statistic - 3.601262
    # p-value                          0.005737
    # #Lags Used                      13.000000
    # Number of Observations Used    130.000000
    # Critical Value (5%)             -2.884042
    # Critical Value (1%)             -3.481682
    # Critical Value (10%)            -2.578770
    # 可以发现,经过指数移动平均后,再做差的结果,已经能够通过1%显著性水平检验了。

    # 步长为1的一阶差分
    plt.close()
    ts_log_diff = ts_log - ts_log.shift(periods=1)
    ts_log_diff.dropna(inplace=True)
    # adf_test(ts_log_diff, 'ts_log_diff')

    # 只通过了10%的显著性检验

    # 我们继续进行二阶差分
    # 一阶差分:Y(k)=X(k+1)-X(k)
    # 二阶差分:Y(k)的一阶差分Z(k)=Y(k+1)-Y(k)=X(k+2)-2*X(k+1)+X(k)为此函数的二阶差分
    ts_log_diff = ts_log - ts_log.shift(periods=1)
    ts_log_diff2 = ts_log_diff - ts_log_diff.shift(periods=1)
    plt.plot(ts_log_diff2)
    ts_log_diff2.dropna(inplace=True)
    adf_test(ts_log_diff2, 'ts_log_diff2')
    # 可以看到,二阶差分,p值非常小,小于1%,检验统计量也明显小于%1的临界值。因此认定为很平稳

    decompostion = seasonal_decompose(ts_log)
    trend = decompostion.trend
    seasonal = decompostion.seasonal
    residual = decompostion.resid

    plt.subplot(411)
    plt.plot(ts_log, label='Original')
    plt.legend(loc='best')
    plt.subplot(412)
    plt.plot(trend, label='Trend')
    plt.legend(loc='best')
    plt.subplot(413)
    plt.plot(seasonal, label='Seasonality')
    plt.legend(loc='best')
    plt.subplot(414)
    plt.plot(residual, label='Residuals')
    plt.legend(loc='best')
    plt.tight_layout()
    plt.savefig('pic/decompostion.png')

    # 对残差进行ADF检验
    # 可以发现序列非常平稳
    plt.close()
    ts_log_decompose = residual
    ts_log_decompose.dropna(inplace=True)
    adf_test(ts_log_decompose, 'ts_log_decompose')
    # 对残差进行ADF检验,可以发现序列非常平稳。

    # 前面我们对数据进行ADF检验,判断序列是否平稳,这里我们使用自相关图和偏自相关图对数据平稳性再次进行验证,一阶差分如下图:
    acf_pacf_plot(ts_log_diff)  # 调用一阶差分
Esempio n. 57
0
    """
    base_capital = DEFAULT_CAPITAL
    daily_risk_capital = DEFAULT_CAPITAL * DEFAULT_ANN_RISK_TARGET / ROOT_BDAYS_INYEAR
    ts_capital = pd.Series(np.ones(len(price)) * DEFAULT_CAPITAL,
                           index=price.index)
    ann_risk = ts_capital * DEFAULT_ANN_RISK_TARGET
    daily_returns_volatility = robust_vol_calc(price.diff())
    multiplier = daily_risk_capital * 1.0 * 1.0 / 10.0
    numerator = forecast * multiplier
    positions = numerator.ffill() / daily_returns_volatility.ffill()
    cum_trades = positions.shift(1).ffill()
    price_returns = price.diff()
    instr_ccy_returns = cum_trades.shift(1) * price_returns
    instr_ccy_returns = instr_ccy_returns.cumsum().ffill().reindex(
        price.index).diff()
    mean_return = instr_ccy_returns.mean() * BUSINESS_DAYS_IN_YEAR
    vol = instr_ccy_returns.std() * ROOT_BDAYS_INYEAR
    return mean_return / vol


if __name__ == "__main__":

    f = '../sysdata/legacycsv/EDOLLAR_price.csv'
    df = pd.read_csv(f, index_col=0, parse_dates=True)
    fast_ewma = pd.ewma(df.PRICE, span=32)
    slow_ewma = pd.ewma(df.PRICE, span=128)
    raw_ewmac = fast_ewma - slow_ewma
    vol = robust_vol_calc(df['PRICE'].diff())
    forecast = raw_ewmac / vol
    print(sharpe(df.PRICE, forecast))
Esempio n. 58
0
RS1 = roll_up1 / roll_down1
RSI1 = 100.0 - (100.0 / (1.0 + RS1))
# Calculate the SMA
roll_up2 = pd.rolling_mean(up, window_length)
roll_down2 = pd.rolling_mean(down.abs(), window_length)
RS2 = roll_up2 / roll_down2
RSI2 = 100.0 - (100.0 / (1.0 + RS2))

#12-EMA Calculation
ema12 = pd.stats.moments.ewma(close, 12)

#26-EMA Calculation
ema26 = pd.stats.moments.ewma(close, 26)

#Signal
signal = pd.ewma(close, span=9)

#MACD
macd = ema12 - ema26

#10-SMA Calculation
sma10 = pd.rolling_mean(close, window=10)

#50-SMA Calculation
sma50 = pd.rolling_mean(close, window=50)

#Crossover = macd - signal
crossover = macd - signal

#Calculate  CCI
tp = ((df['High'] + df['Close'] + df['Low']) / 3).dropna()
Esempio n. 59
0
def ema(y, alpha=0.20):
    '''EXPONENTIAL MOVING AVERAGE using traditional weight arg.'''
    #  y could be a dataframe.
    s = (2 / float(alpha)) - 1
    #  Thus default alpha has span of 9, i.e. "9-period EWMA."
    return pd.ewma(y, span=s)
def gen_from_begin_to_today_stock_macd_data(stock_base_csv_path,
                                            ts_stock_code_file_name,
                                            stock_everyday_csv_path,
                                            stock_everyday_csv_name):
    #加载每天的数据
    every_days_stock = get_every_days_stock_data(stock_everyday_csv_path,
                                                 stock_everyday_csv_name)
    #old stock code list
    stock_code_list = get_all_stock_code_list(stock_base_csv_path)
    # ========== 根据上一步得到的代码列表,遍历所有股票,将这些股票合并到一张表格 all_stock 中
    all_stock = pd.DataFrame()
    i = 0
    # 遍历每个股票
    for code in stock_code_list:
        # 测试 5 次跳过
        i += 1
        if i >= 5:
            #break
            pass
        #print code
        # 从csv文件中读取该股票数据
        # 注意:这里请填写数据文件在您电脑中的路径
        stock_data = pd.read_csv(stock_base_csv_path + code + '.csv',
                                 parse_dates=[2],
                                 encoding='gbk')
        # print stock_data.columns
        # 选取需要的字段,去除其他不需要的字段
        # 股票代码,股票名称,交易日期,新浪行业,新浪概念,新浪地域,开盘价,最高价,最低价,收盘价,后复权价,前复权价,涨跌幅,成交量,成交额,换手率,流通市值,总市值,是否涨停,是否跌停,市盈率TTM,市销率TTM,市现率TTM,市净率,MA_5,MA_10,MA_20,MA_30,MA_60,MA金叉死叉,MACD_DIF,MACD_DEA,MACD_MACD,MACD_金叉死叉,KDJ_K,KDJ_D,KDJ_J,KDJ_金叉死叉,布林线中轨,布林线上轨,布林线下轨,psy,psyma,rsi1,rsi2,rsi3
        stock_data = stock_data[[
            u'交易日期', u'股票代码', u'收盘价', 'MACD_DIF', 'MACD_DEA', 'MACD_MACD'
        ]]
        stock_data.columns = [
            'date', 'code', 'close', 'macd_dif', 'macd_dea', 'macd_macd'
        ]
        # 去掉 stock 代表市场的前两个字符
        trim_stock_code = stock_data['code'].map(lambda x: x[2:])
        stock_data['code'] = trim_stock_code
        #加入 every day 的数据
        cur_code_every_data = every_days_stock[every_days_stock['code'] ==
                                               code[2:]]
        stock_data = stock_data.append(cur_code_every_data, ignore_index=True)
        #排序
        stock_data.sort_values(by='date', ascending=True, inplace=True)
        #
        ema_12 = pd.ewma(stock_data['close'], span=12)
        ema_26 = pd.ewma(stock_data['close'], span=26)
        stock_data['ema_12'] = ema_12
        stock_data['ema_26'] = ema_26
        stock_data['diff'] = ema_12 - ema_26
        stock_data['dea'] = pd.ewma(stock_data['diff'], span=9)
        stock_data['macd'] = 2 * (stock_data['diff'] - stock_data['dea'])
        #
        # 将该股票的合并到output中
        all_stock = all_stock.append(stock_data.tail(1), ignore_index=True)
    #新的stock
    new_stock_data = get_new_stock(every_days_stock, stock_code_list)
    all_new_stock = gen_new_stock_macd_data(new_stock_data)
    #
    # 将 新股票的合并到output中
    all_stock = all_stock.append(all_new_stock, ignore_index=True)
    # 去除已经退市的股票
    ts_stock_code = pd.read_csv(ts_stock_code_file_name,
                                names=['code'],
                                dtype={'code': str})
    all_stock = all_stock[all_stock['code'].isin(ts_stock_code['code']) ==
                          False]

    return all_stock