def get_SMA(self, p):
     s = "SMA_{}".format(str(p))
     if s not in self.ti_df.columns:
         self.ti_df[s] = talib.SMA(self.history_df['Close'], p)
     return self.ti_df[s]
Beispiel #2
0
def SMAWMA(df,count, acmroi, winnum, winfact,revenue_sum,avg_return,total_return_ratio):
    #pd.options.mode.chained_assignment = None #
    
    trade_record = pd.DataFrame(data=None, index=None,columns = ["stockid","bought_date","bought_time","bought_price","sold_date","sold_time","sold_price","QTY","cash_account","LS_type"])
    highest= np.array(df['highest'], dtype=float)
    close = np.array(df['close'], dtype=float)
    vol =df['Quantity'].as_matrix().astype("float64") 
    df['volMA'] = talib.SMA(vol, timeperiod=20)
    SMA = talib.SMA(highest,30) #close 代進SMA方法做計算
    WMA = talib.WMA(close,5) #close 代進WMA方法做計算
    df['SMA'] = SMA
    df['WMA'] = WMA
#    print(df)
    #設定初始值
    
    df["stockid"]= np.nan
    df["bought_date"]= np.nan
    df["bought_time"]= np.nan
    df["bought_price"]= np.nan
    df["sold_date"]= np.nan
    df["sold_time"]= np.nan
    df["sold_price"]= np.nan
    df["QTY"]=1000
    df["cash_account"]= np.nan
    df["LS_type"]=0
    df['XBuy'] = np.nan
    df['YBuy'] = np.nan
    df['XSell'] = np.nan
    df['YSell'] = np.nan
    row = len(df)
    flag1 = False
    flag = False
    change = 0
    buyprice=[]
    sellprice=[] 
    win = 0
    loss = 0
    roi =0
    revenue=0
    total_return=0
    revenue_sum=0
    for i in range(row):
        change =df['WMA'].iloc[i]/ df['SMA'].iloc[i]
       
        if (flag1 == False) & (df['WMA'].iloc[i] <df['SMA'].iloc[i]) & (change <= 0.948)& (float(df['Quantity'].iloc[i]) >=(float(df['volMA'].iloc[i]))):
            df['XSell'].iloc[i] = df['MATCH_TIME'].iloc[i]
            df['YSell'].iloc[i] = df['close'].iloc[i]
            sellprice= df['close'].iloc[i]
            df["stockid"].iloc[i]=df['STOCK_SYMBOL'].iloc[i]
            df["sold_date"].iloc[i]=df['date'].iloc[i]
            df["sold_time"].iloc[i]=df['MATCH_TIME'].iloc[i]
            df["sold_price"].iloc[i]=df['close'].iloc[i]
            #df["cash_account"].iloc[i]= round(float(5000000+float(buyprice*1.001425-sellprice*(1-0.001425*0.6-0.001))*1000),0)
            df["LS_type"].iloc[i]=2
            print("S2賣出價"+str(sellprice))
            flag1 = True
        elif (flag == False) & (df['WMA'].iloc[i] <= df['SMA'].iloc[i]) &(change <= 0.995) & (float(df['Quantity'].iloc[i]) >=(float(df['volMA'].iloc[i]))):
            df['XBuy'].iloc[i] = df['MATCH_TIME'].iloc[i]
            df['YBuy'].iloc[i] = df['close'].iloc[i]
            buyprice =df['close'].iloc[i]
            df["stockid"].iloc[i]=df['STOCK_SYMBOL'].iloc[i]
            df["bought_date"].iloc[i]=df['date'].iloc[i]
            df["bought_time"].iloc[i]=df['MATCH_TIME'].iloc[i]
            df["bought_price"].iloc[i]=df['close'].iloc[i]
            #df["cash_account"].iloc[i]= round(float(5000000-float(buyprice)*1000*(1-0.001425*0.6-0.001)),0)
            df["LS_type"].iloc[i]=1
            flag = True
            print("S1買進價"+str(buyprice))
        
        elif(flag1 == True) & (df['WMA'].iloc[i] >= df['SMA'].iloc[i])& (change >= 1.009) & (float(df['Quantity'].iloc[i]) >=(float(df['volMA'].iloc[i]))):
          try:  
            df['XBuy'].iloc[i] = df['MATCH_TIME'].iloc[i+1]
            df['YBuy'].iloc[i] = df['close'].iloc[i+1]
            buyprice =df['close'].iloc[i+1]
            df["stockid"].iloc[i]=df['STOCK_SYMBOL'].iloc[i+1]
            df["bought_date"].iloc[i]=df['date'].iloc[i+1]
            df["bought_time"].iloc[i]=df['MATCH_TIME'].iloc[i+1]
            df["bought_price"].iloc[i]=df['close'].iloc[i+1]
            #df["cash_account"].iloc[i]= round(float(5000000-float(buyprice)*1000*(1-0.001425*0.6-0.001)),0)
            df["LS_type"].iloc[i]=2
            flag1 = False
            print("S2回補價"+str(buyprice))
            count += 1
            revenue=((sellprice*0.995575)- (buyprice*1.001425))*1000 
            revenue_sum +=revenue
            premium=revenue/((buyprice*1.001425)*1000)
            total_return +=premium
            avg_return=(total_return/count)
            total_return_ratio=round((revenue_sum/5000000),3)*100
            [roi, winnum] =roical(buyprice, sellprice, winnum)
            acmroi += roi
            [loss, win] = winfactor(buyprice, sellprice, loss, win)
            
          except (ValueError): 
            continue 
        elif (flag == True) & (df['WMA'].iloc[i] >df['SMA'].iloc[i]) & (change >= 1.021) & (float(df['Quantity'].iloc[i]) >=(float(df['volMA'].iloc[i]))):  
          try: 
            df['XSell'].iloc[i] = df['MATCH_TIME'].iloc[i+2]
            df['YSell'].iloc[i] = df['close'].iloc[i+2]
            sellprice= df['close'].iloc[i+2]
            count += 1
            df["stockid"].iloc[i]=df['STOCK_SYMBOL'].iloc[i+2]
            df["sold_date"].iloc[i]=df['date'].iloc[i+2]
            df["sold_time"].iloc[i]=df['MATCH_TIME'].iloc[i+2]
            df["sold_price"].iloc[i]=df['close'].iloc[i+2]
            #df["cash_account"].iloc[i]= round(float(5000000+float(buyprice*1.001425-sellprice*(1-0.001425*0.6-0.001))*1000),0)
            df["LS_type"].iloc[i]=1
            flag = False
            print("S1賣出價"+str(sellprice))
            [roi, winnum] =roical(buyprice, sellprice, winnum)
            acmroi += roi
            [loss, win] = winfactor(buyprice, sellprice, loss, win)
            
            revenue=((sellprice*0.995575)- (buyprice*1.001425))*1000 
            revenue_sum +=revenue
            premium=revenue/((buyprice*1.001425)*1000)
            total_return +=premium
            avg_return=(total_return/count)
            total_return_ratio=round((revenue_sum/5000000),3)*100
          except (ValueError): 
            continue
          if (flag == True & i==(row-1)):
              df['XSell'].iloc[i] = df['MATCH_TIME'].iloc[i]
              df['YSell'].iloc[i] = df['close'].iloc[i]
              sellprice=df['close'].iloc[i]
              df["stockid"].iloc[i]=df['STOCK_SYMBOL'].iloc[i]
              df["sold_date"].iloc[i]=df['date'].iloc[i]
              df["sold_time"].iloc[i]=df['MATCH_TIME'].iloc[i]
              df["sold_price"].iloc[i]=df['close'].iloc[i]
              df["QTY"].iloc[i]=1000
              #df["cash_account"].iloc[i]= (5000000+(buyprice*1.001425*0.6-sellprice* (1-0.001425*0.6-0.001))*1000)
              df["LS_type"].iloc[i]=1
              count += 1
              [roi, winnum] = roical(buyprice, sellprice, winnum)
              acmroi += roi
              [loss, win] = winfactor(buyprice, sellprice, loss, win)
              print("S最後被迫賣價"+str(sellprice))
              #revenue=((sellprice*0.995575)- (buyprice*1.001425))*1000 
              #revenue_sum +=revenue
              #premium=revenue/((buyprice*1.001425)*1000)
              #total_return +=premium
              #avg_return=(total_return/count)
              #total_return_ratio=round((revenue_sum/5000000),3)*100
          elif (flag1 == True & i==(row-1)): 
              df['Xbuy'][i] = df['MATCH_TIME'].iloc[i]
              df['Ybuy'][i] = df['close'].iloc[i]
              buyprice=df['close'].iloc[i]
              df["stockid"].iloc[i]=df['STOCK_SYMBOL'].iloc[i]
              df["bought_date"].iloc[i]=df['date'].iloc[i]
              df["bought_time"].iloc[i]=df['MATCH_TIME'].iloc[i]
              df["bought_price"].iloc[i]=df['close'].iloc[i]
              df["QTY"].iloc[i]=1000
              #df["cash_account"].iloc[i]= (5000000+(buyprice*1.001425*0.6-sellprice* (1-0.001425*0.6-0.001))*1000)
              df["LS_type"].iloc[i]=2
              count += 1
              [roi, winnum] = roical(buyprice, sellprice, winnum)
              acmroi += roi
              print("S2最後被迫回補價"+str(buyprice))
              #revenue=((sellprice*0.995575)- (buyprice*1.001425))*1000 
              #revenue_sum +=round(revenue,2)
              #premium=revenue/((buyprice*1.001425)*1000)
              #total_return +=premium
              #avg_return=(total_return/count)
              #total_return_ratio=round((revenue_sum/5000000),3)*100
          elif (flag1 == False or flag == False&  i==(row-10)):
              break
    if (loss == 0):
        loss = 1
    winvar = win / loss
    print(' win = ', win)
    print('loss = ', loss)
    print('winvar = ', winvar)
    print('revenue_sum=',revenue_sum)
    if (count == 0):
        count = 0.01  
    #str1 = 'SMAWMA策略: ' +'交易次數 = '+ str(count) + ' 次; ' + '累計報酬率 = ' + str(round(acmroi*100, 2)) + '%;' + '勝率 = ' + str(round((winnum/count)*100,2)) + '%' + '; 獲利因子 = ' + str(round(winvar, 2) )+ '%' + '; 總收益 = ' + str(round(revenue_sum, 2)+ '; 平均報酬率 = ' + str(round(avg_return*100, 2))+ '%'+ '; 夏普指數 = ' + str(round(sharpe_ratio*100, 2))+ '%'+ '; MDD = ' + str(round(MDD, 2))+ '; 總資產報酬率 = ' + str(round(total_return_ratio*100, 2))+ '%')
    #print(str1)
    trade_record.index.names = ['trade_no']
    trade_record["stockid"]= df["stockid"]
    trade_record["bought_date"]=df["bought_date"]
    trade_record["bought_time"]=df["bought_time"]
    trade_record["bought_price"]=df["bought_price"]
    trade_record["sold_date"]=df["sold_date"]
    trade_record["sold_time"]= df["sold_time"]
    trade_record["sold_price"]=df["sold_price"]
    #trade_record["cash_account"]=df["cash_account"]
    trade_record["LS_type"]=df["LS_type"]
    trade_record["offset_date"] = trade_record[["bought_date","sold_date"]].max(axis=1) #抓出平倉日
    #trade_record =trade_record.sort_values(by=["offset_date",'trade_no'])
    #trade_record = trade_record.drop(["bought_date"],axis=1) #將多餘的紀錄刪除
    #trade_record =trade_record.reset_index(drop=True) #重設index
    #trade_record["Algorithm"] =2
    #trade_record["QTY"]=1000
    trade_record.to_csv(r"./SAM_trade_record.csv", sep = ",")
    #print(str1)
    #print(trade_record)
    #print(df)
    #print(trade_record)
    return (count, acmroi, winnum, winvar,revenue_sum,avg_return,total_return_ratio ) 
def sma(px, lookback):
    sig = ta.SMA(px, timeperiod=lookback)
    return sig[-1]
Beispiel #4
0
def feature_engineering(train_data):
    #SMA
    train_data.insert(0, 'sma20',
                      talib.SMA(train_data['close'].values, timeperiod=20),
                      True)

    #CCI
    train_data.insert(
        0, 'cci14',
        talib.CCI(train_data['high'].values,
                  train_data['low'].values,
                  train_data['close'].values,
                  timeperiod=14), True)

    #RSI
    train_data.insert(0, 'rsi14',
                      talib.RSI(train_data['close'].values, timeperiod=14),
                      True)

    #ADX
    train_data.insert(
        0, 'adx14',
        talib.ADX(train_data['high'].values,
                  train_data['low'].values,
                  train_data['close'].values,
                  timeperiod=14), True)

    #ATR
    train_data.insert(
        0, 'atr14',
        talib.ATR(train_data['high'].values,
                  train_data['low'].values,
                  train_data['close'].values,
                  timeperiod=14), True)

    #Bands
    bb20Upperband, bb20Middleband, bb20Lowerband = talib.BBANDS(
        train_data['close'].values,
        timeperiod=20,
        nbdevup=2,
        nbdevdn=2,
        matype=0)
    train_data.insert(0, 'bb20Upperband', bb20Upperband, True)
    train_data.insert(0, 'bb20Middleband', bb20Middleband, True)
    train_data.insert(0, 'bb20Lowerband', bb20Lowerband, True)

    bb50Upperband, bb50Middleband, bb50Lowerband = talib.BBANDS(
        train_data['close'].values,
        timeperiod=50,
        nbdevup=2,
        nbdevdn=2,
        matype=0)
    train_data.insert(0, 'bb50Upperband', bb50Upperband, True)
    train_data.insert(0, 'bb50Middleband', bb50Middleband, True)
    train_data.insert(0, 'bb50Lowerband', bb50Lowerband, True)

    #MACD
    macd1226, macdSignal1226, macdHist1226 = talib.MACD(
        train_data['close'].values,
        fastperiod=13,
        slowperiod=26,
        signalperiod=9)
    train_data.insert(0, 'macd1226', macd1226, True)
    train_data.insert(0, 'macdSignal1226', macdSignal1226, True)
    train_data.insert(0, 'macdHist1226', macdHist1226, True)

    #Stochastic
    stochasticSlowK335, stochasticSlowD335 = talib.STOCH(
        train_data['high'].values,
        train_data['low'].values,
        train_data['close'].values,
        fastk_period=5,
        slowk_period=3,
        slowk_matype=0,
        slowd_period=3,
        slowd_matype=0)
    train_data.insert(0, 'stochasticSlowK335', stochasticSlowK335, True)
    train_data.insert(0, 'stochasticSlowD335', stochasticSlowD335, True)

    return train_data
Beispiel #5
0
## get the data for all bitcoin pairs and tidy it
bin_products = client.get_products()
bin_data = bin_products['data']
btc_pairs = [pair['s'] for pair in bin_data if pair['s'].endswith("BTC")]

candle_data = [
    client.get_historical_klines(pair, Client.KLINE_INTERVAL_1DAY,
                                 str(active_window)) for pair in btc_pairs
]

close_data = np.zeros(
    (len(btc_pairs), ndays))  #get close data and discard the rest
for token in range(len(candle_data)):
    for day in range(len(candle_data[token])):
        close_data[token][day] = candle_data[token][day][4]

## calculate short and long SMAs
SMA_long_data = np.zeros((len(btc_pairs), ndays))
SMA_short_data = np.zeros((len(btc_pairs), ndays))
for token in range(len(close_data)):
    SMA_long_data[token] = talib.SMA(close_data[token], SMA_long)
    SMA_short_data[token] = talib.SMA(close_data[token], SMA_short)

## run the analysis to find the crossings and add relevant coins to stonk list
stonks = []
for comp in range(len(btc_pairs)):
    if SMA_short_data[comp][cross_window] < SMA_long_data[comp][cross_window]:
        if SMA_short_data[comp][-1] > SMA_long_data[comp][-1]:
            stonks.append(btc_pairs[comp])
def MA(timeperiod):
    MA = tb.SMA(sp.Close, timeperiod)
    return MA
Beispiel #7
0
 def sma(self, n, array=False):
     """简单均线"""
     result = talib.SMA(self.close, n)
     if array:
         return result
     return result[-1]
Beispiel #8
0
    def compute_indicators(self):
        df = deepcopy(self.df)
        open = df.open
        close = df.close
        high = df.high
        low = df.low
        volume = df.volume
        adjclose = close

        nan_offset = 50

        df['beta'] = talib.BETA(high, low, timeperiod=5)
        df['rsi_14'] = talib.RSI(close, timeperiod=14)
        df['rsi_14_thresh'] = df.rsi_14.apply(lambda c: 1 if c > 70 else (-1 if c < 30 else 0))
        df['rsi_21'] = talib.RSI(close, timeperiod=21)
        df['rsi_21_thresh'] = df.rsi_21.apply(lambda c: 1 if c > 70 else (-1 if c < 30 else 0))
        df['rsi_60'] = talib.RSI(close, timeperiod=60)
        df['rsi_60_thresh'] = df.rsi_60.apply(lambda c: 1 if c > 70 else (-1 if c < 30 else 0))
        df['macd_12_26'], df['macdsignal_12_26'], df['macdhist_12_26'] = talib.MACD(close, fastperiod=12, slowperiod=26,
                                                                                    signalperiod=9)
        df['macd_5_12'], df['macdsignal_5_12'], df['macdhist_5_12'] = talib.MACD(close, fastperiod=3, slowperiod=15,
                                                                                 signalperiod=5)
        df['mom'] = talib.MOM(close, timeperiod=10)
        df['roc_20'] = talib.ROC(close, timeperiod=21)
        df['roc_125'] = talib.ROC(close, timeperiod=125)
        df['roc_90'] = talib.ROC(close, timeperiod=90)
        high = deepcopy(df.high)
        low = deepcopy(df.low)
        df['psar_005'] = self.get_sar(high, low, 0.005, 0.2)
        df['psar_02'] = self.get_sar(high, low, 0.02, 0.2)
        df['psar_1'] = self.get_sar(high, low, 0.1, 0.2)
        try:
            df['psar_005_dist'] = df.psar_005 - df.adjclose
            df['psar_02_dist'] = df.psar_02 - df.adjclose
            df['psar_1_dist'] = df.psar_1 - df.adjclose
        except Exception as e:
            print(e)
        try:
            df['psar_005_ind'] = (df.psar_005 < self.df.adjclose).astype(int)
            df['psar_02_ind'] = (df.psar_02 < self.df.adjclose).astype(int)
            df['psar_1_ind'] = (df.psar_1 < self.df.adjclose).astype(int)
        except Exception as e:
            print(e)
        df['sma5'] = talib.SMA(close, timeperiod=5)
        df['sma10'] = talib.SMA(close, timeperiod=10)
        df['sma20'] = talib.SMA(close, timeperiod=20)
        df['sma50'] = talib.SMA(close, timeperiod=50)
        df['sma200'] = talib.SMA(close, timeperiod=200)
        df['midprice_5'] = talib.MIDPRICE(high, low, timeperiod=5)
        df['midprice_14'] = talib.MIDPRICE(high, low, timeperiod=14)
        df['midprice_21'] = talib.MIDPRICE(high, low, timeperiod=21)
        df['upperband'], df['middleband'], df['lowerband'] = talib.BBANDS(close, timeperiod=5, nbdevup=2., nbdevdn=2.,
                                                                          matype=0)
        df['instantaneous_trend'] = talib.HT_TRENDLINE(close)
        df['adx_7'] = talib.ADX(high, low, close, timeperiod=7)
        df['adx_7_pctchg'] = df.adx_7.pct_change()
        df['adx_14'] = talib.ADX(high, low, close, timeperiod=14)
        df['adx_14_pctchg'] = df.adx_14.pct_change()
        df['adx_21'] = talib.ADX(high, low, close, timeperiod=21)
        df['adx_21_pctchg'] = df.adx_21.pct_change()
        df['adx_60'] = talib.ADX(high, low, close, timeperiod=61)
        df['adx_60_pctchg'] = df.adx_60.pct_change()
        df['cci'] = talib.CCI(high, low, close, timeperiod=14)
        df['cci_chg'] = df.cci.pct_change()
        df['cci_thresh'] = df.cci.apply(lambda c: 1 if c > 100 else (-1 if c < 20 else 0))
        df['direction_movement_idx'] = talib.DX(high, low, close, timeperiod=14)
        df['money_flow'] = talib.MFI(high, low, close, volume, timeperiod=14)
        df['aroon_down'], df['aroon_up'] = talib.AROON(high, low)
        df['ppo'] = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0)
        df['trix'] = talib.TRIX(close, timeperiod=30)
        df['stoch_k'], df['stoch_d'] = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0,
                                                   slowd_period=3, slowd_matype=0)
        df['stoch_rsi_k'], df['stoch_rsi_d'] = talib.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3,
                                                              fastd_matype=0)
        df['willR'] = talib.WILLR(high, low, close, timeperiod=14)
        df['natr'] = talib.NATR(high, low, close, timeperiod=14)
        df['trange'] = talib.TRANGE(high, low, close)
        df['obv'] = talib.OBV(close, volume)
        df['adosc'] = talib.ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10)
        df['ad'] = talib.AD(high, low, close, volume)
        try:
            df['log_return'] = df.ta.log_return()
            df['percent_return'] = df.ta.percent_return()
        except Exception as e:
            print(e)
        df['zscore'] = df.ta.zscore()
        df['quantile'] = df.ta.quantile()
        df['ht_dom_per'] = talib.HT_DCPERIOD(close)
        df['ht_dom_cycle'] = talib.HT_DCPHASE(close)
        df['ht_trendmode'] = talib.HT_TRENDMODE(close)
        df['pvt'] = df.ta.pvt()
        df['increasing'] = df.ta.increasing()
        df['decreasing'] = df.ta.decreasing()
        df['cross_sma5'] = df.ta.cross('close', 'sma5')
        df['cross_sma10'] = df.ta.cross('close', 'sma10')
        df['cross_sma20'] = df.ta.cross('close', 'sma20')
        df['cross_sma50'] = df.ta.cross('close', 'sma50')
        df['cross_sma200'] = df.ta.cross('close', 'sma200')
        df['sma20_above'] = (df.sma20 > df.sma200).astype(int)
        df['sma50_above'] = (df.sma50 > df.sma200).astype(int)
        df['cross_psar_rsi14'] = df.ta.cross('psar_02', 'rsi_14')
        df['cross_psar_005_close'] = df.ta.cross('psar_005', 'adjclose')
        df['cross_psar_02_close'] = df.ta.cross('psar_02', 'adjclose')
        df['cross_psar_1_close'] = df.ta.cross('psar_1', 'adjclose')
        df['cross_adx14_psar_02'] = df.ta.cross('psar_02', 'adx_14')
        df['cross_adx7_psar_02'] = df.ta.cross('psar_005', 'adx_7')
        df['cross_adx14_psar_02'] = df.ta.cross('psar_005', 'adx_14')
        df['cross_stoch'] = df.ta.cross('stoch_k', 'stoch_d')
        df['cross_macd_12_26'] = df.ta.cross('macd_12_26', 'macdsignal_12_26')
        df['cross_macd_5_12'] = df.ta.cross('macd_5_12', 'macdsignal_5_12')
        df['roc_20_125'] = df.ta.cross('roc_20', 'roc_125')
        df['roc_20_125_dist'] = df.roc_20 - df.roc_125

        #df = df.iloc[nan_offset:, :].dropna(axis=1).copy()

        self.df = df
        self.indicator_matrix = df.iloc[:, 6:]
Beispiel #9
0
        df = DataReader(stock, 'yahoo', start, end)

        # Get Index Data
        index = 'SPY'
        spy = DataReader(index, 'yahoo', start, end)
        spy['RSI'] = talib.RSI(spy['Adj Close'], timeperiod=5)

        # Technical Indicators
        df['upper_band'], df['middle_band'], df['lower_band'] = talib.BBANDS(
            df['Adj Close'], timeperiod=7)
        df['macd'], df['macdsignal'], df['macdhist'] = talib.MACD(
            df['Adj Close'], fastperiod=12, slowperiod=26, signalperiod=9)
        df['RSI'] = talib.RSI(df['Adj Close'], timeperiod=5)
        df['Momentum'] = talib.MOM(df['Adj Close'], timeperiod=5)
        df['Z-Score'] = zscore(df['Adj Close'])
        df['SMA'] = talib.SMA(df['Adj Close'], timeperiod=7)
        df['EMA'] = talib.EMA(df['Adj Close'], timeperiod=7)
        df['OBV'] = talib.OBV(df['Adj Close'], df['Volume']) / 10**6
        df['OBV'] = df['OBV'].diff()
        df['CCI'] = ta.trend.cci(df['High'],
                                 df['Low'],
                                 df['Adj Close'],
                                 n=7,
                                 c=0.015)

        # Set signal position columns
        df['bbPos'] = None
        df['macdPos'] = None
        df['rsiPos'] = None
        df['spyPos'] = None
        df['zPos'] = None
Beispiel #10
0
 def add_sma(self, df):
     output = talib.SMA(df['Close'].values, timeperiod=25)
     df['SMA'] = output
     return df
def create_features(twii):

    sma = talib.SMA(twii, timeperiod=120)
    wma = talib.WMA(twii, timeperiod=120)
    mom = talib.MOM(twii, timeperiod=120)
    k, d = talib.STOCH(twii,
                       twii,
                       twii,
                       fastk_period=120,
                       slowk_period=60,
                       slowd_period=60)
    k2, d2 = talib.STOCH(twii,
                         twii,
                         twii,
                         fastk_period=240,
                         slowk_period=120,
                         slowd_period=120)
    k3, d3 = talib.STOCH(twii,
                         twii,
                         twii,
                         fastk_period=360,
                         slowk_period=180,
                         slowd_period=180)
    k4, d4 = talib.STOCH(twii,
                         twii,
                         twii,
                         fastk_period=480,
                         slowk_period=240,
                         slowd_period=240)
    k5, d5 = talib.STOCH(twii,
                         twii,
                         twii,
                         fastk_period=640,
                         slowk_period=320,
                         slowd_period=320)
    k6, d6 = talib.STOCH(twii,
                         twii,
                         twii,
                         fastk_period=720,
                         slowk_period=360,
                         slowd_period=360)
    k7, d7 = talib.STOCH(twii,
                         twii,
                         twii,
                         fastk_period=840,
                         slowk_period=420,
                         slowd_period=420)
    k8, d8 = talib.STOCH(twii,
                         twii,
                         twii,
                         fastk_period=960,
                         slowk_period=480,
                         slowd_period=480)

    rsi = talib.RSI(twii, timeperiod=120)
    rsi2 = talib.RSI(twii, timeperiod=240)
    rsi3 = talib.RSI(twii, timeperiod=480)
    rsi4 = talib.RSI(twii, timeperiod=640)
    rsi5 = talib.RSI(twii, timeperiod=720)
    rsi6 = talib.RSI(twii, timeperiod=840)

    macd1, macd2, macd3 = talib.MACD(twii,
                                     fastperiod=120,
                                     slowperiod=60,
                                     signalperiod=60)
    willr = talib.WILLR(twii, twii, twii, timeperiod=120)
    cci = talib.CCI(twii, twii, twii, timeperiod=120)

    return pd.DataFrame({
        'RSIb': rsi / 50,
        'RSIb2': rsi2 / 50,
        'RSIb3': rsi3 / 50,
        'RSIb4': rsi4 / 50,
        'RSIb5': rsi5 / 50,
        'RSIb6': rsi6 / 50,
        'MOMb': mom - 0,
        'KDb': k - d,
        'KDb2': k2 - d2,
        'KDb3': k3 - d3,
        'KDb4': k4 - d4,
        'KDb5': k5 - d5,
        'KDb6': k6 - d6,
        'KDb7': k7 - d7,
        'KDb8': k8 - d8,
        'a5': (twii.rolling(5).mean() / twii),
        'a10': (twii.rolling(10).mean() / twii),
        'a20': (twii.rolling(20).mean() / twii),
        'a40': (twii.rolling(40).mean() / twii),
        'a80': (twii.rolling(80).mean() / twii),
        'a160': (twii.rolling(160).mean() / twii),
        'a320': (twii.rolling(320).mean() / twii),
        'a640': (twii.rolling(640).mean() / twii),
        'a720': (twii.rolling(720).mean() / twii),
        'a840': (twii.rolling(840).mean() / twii),
        'a960': (twii.rolling(960).mean() / twii),
        'a1024': (twii.rolling(1024).mean() / twii),
        'b1': twii / twii.shift(50),
        'b2': twii / twii.shift(100),
        'b3': twii / twii.shift(150),
        'b4': twii / twii.shift(200),
        'b5': twii / twii.shift(250),
        'b6': twii / twii.shift(300),
        'b7': twii / twii.shift(350),
        'LINEARREG_SLOPE0': talib.LINEARREG_SLOPE(twii, 60),
        'LINEARREG_SLOPE1': talib.LINEARREG_SLOPE(twii, 120),
        'ADXR0': talib.ADXR(twii, twii, twii, 60),
        'ADXR1': talib.ADXR(twii, twii, twii, 120),
        'ADXR2': talib.ADXR(twii, twii, twii, 240),
        'ADXR3': talib.ADXR(twii, twii, twii, 360),
        'ADXR4': talib.ADXR(twii, twii, twii, 480),
        'ADXR5': talib.ADXR(twii, twii, twii, 640),
    })
# input
symbol = str(input('Enter a ticker: '))
symbol = symbol.strip()
symbol = symbol.upper()

num_of_years = 1
start = dt.date.today() - dt.timedelta(days=int(365.25 * num_of_years))
end = dt.date.today()

# Read data
data = yf.download(symbol, start, end)

# ## SMA and EMA
#Simple Moving Average
data['SMA'] = talib.SMA(data['Adj Close'], timeperiod=20)

# Exponential Moving Average
data['EMA'] = talib.EMA(data['Adj Close'], timeperiod=20)

# Bollinger Bands
data['upper_band'], data['middle_band'], data['lower_band'] = talib.BBANDS(
    data['Adj Close'], timeperiod=20)

# Plot
rcParams['figure.figsize'] = 15, 10
fig, (ax1, ax2) = plt.subplots(2)
ax1.plot(data[['Adj Close', 'SMA', 'EMA']])
ax1.legend(('Adj Close', 'SMA', 'EMA'))
ax1.set_ylabel('Close Price')
ax1.set_title(f'SMA vs. EMA for {symbol.upper()}')
 def get_SMA_price(self, p):
     s = "SMA_{}_price".format(str(p))
     if s not in self.ti_df.columns:
         self.ti_df[s] = talib.SMA(self.history_df['Low'], p)
     return self.ti_df[s]
 def get_SMA_turnover(self, p):
     s = "SMA_{}_turnover".format(str(p))
     if s not in self.ti_df.columns:
         self.ti_df[s] = talib.SMA(self.history_df['Turnover'], p)
     return self.ti_df[s]
Beispiel #15
0
def get_ma_ouptput(codes):
    """
    get moving average day, week, month for output
    :param codes:
    :return:

    codes = [("code", "name")]
    """
    #codes = [('000636', '风华高科') ,  ('600066', '宇通客车'),  ('000786','北新建材'), ('600817','ST宏盛'),
    #     ('000830', '鲁西化工'), ('510050', '50ETF')]

    #codes2 = ['QQQ','SPY','IWM','DIA', 'GLD']


    today = datetime.datetime.now()
    start_date = today - datetime.timedelta(days = 200)
    start_date = start_date.strftime("%Y-%m-%d")
    end_date = today.strftime("%Y-%m-%d")
    output_m =[]
    output_w =[]
    output_d =[]

    coll_stock_day = DATABASE.stock_day
    stock_list = DATABASE.stock_list
    code_list = coll_stock_day.distinct('code')
    index_day = DATABASE.index_day
    index_list = index_day.distinct('code')


    for (code, symbol) in codes:
        ''''
        if code  == '510050':
            df = QA.QA_fetch_index_day_adv([code], start_date, end_date)
        else:
            df = QA.QA_fetch_stock_day_adv([code], start_date, end_date)
        #df = ts.get_hist_data(code, ktype='D', start=start_date, end=end_date)
        if code != '510050':
            df = df.to_qfq()()
        else:
            df = df()
        '''
        if code in code_list:
            df = QA.QA_fetch_stock_day_adv([code], start_date, end_date)
            df = df.to_qfq()()
        else:
            df = QA.QA_fetch_index_day_adv([code], start_date, end_date)()

        df_w = QA.QAData.data_resample.QA_data_day_resample(df, 'W')

        df_m = QA.QAData.data_resample.QA_data_day_resample(df, 'M')

        df['ma5'] = talib.SMA(df['close'].values, timeperiod=5)
        df_w['ma5'] = talib.SMA(df_w['close'].values, timeperiod=5)
        df_m['ma5'] = talib.SMA(df_m['close'].values, timeperiod=5)
        df = df.reset_index().tail(1)
        df_w = df_w.reset_index().tail(1)
        df_m = df_m.reset_index().tail(1)
        df['indicator'] = 'No'
        df_w['indicator'] = 'No'
        df_m['indicator'] = 'No'
        df.loc[df.close > df.ma5,'indicator'] = "Yes"
        df_w.loc[df_w.close > df_w.ma5,'indicator'] = "Yes"
        df_m.loc[df_m.close > df_m.ma5,'indicator'] = "Yes"

        df_m['symbol'] = symbol
        #df_m['code'] = code
        df_m['type'] = '月5MA'
        #df['code'] = code
        df['type'] = '日5MA'
        df['symbol'] = symbol
        #df_w['code'] = code
        df_w['type'] = '周5MA'
        df_w['symbol'] = symbol
        output_m.append(df_m)
        output_d.append(df)
        output_w.append(df_w)
    result_m = pd.concat(output_m)
    result_d = pd.concat(output_d)
    result_w = pd.concat(output_w)

    cols = ['symbol','code','date','type','close','ma5','indicator']
    result = [result_m[cols], result_d[cols], result_w[cols]]
    combined = pd.concat(result)
    combined = combined.sort_values(['code', 'type'])
    combined['diff'] = round(combined.close - combined.ma5,2)
    combined['close'] = round(combined.close, 2)
    combined['ma5'] = round(combined.ma5, 2)
    combined['date'] = combined.date.map(lambda x: x.strftime("%Y-%m-%d"))
    combined['type'] = pd.Categorical(combined['type'], ['日5MA', '周5MA', '月5MA'])
    combined = combined.sort_values(['symbol', 'type'])

    return combined
Beispiel #16
0
 def getSMA(self):
     output = talib.SMA(self.close, 20)
     a = pd.DataFrame(output)
     a = a.rename(columns={0: "SMA"})
     return a
def get_tech(filepath):
    df = pd.read_csv(filepath, index_col='date')
    # Simple Moving Average SMA 简单移动平均
    df['SMA5'] = talib.MA(df['close'], timeperiod=5)
    df['SMA10'] = talib.MA(df['close'], timeperiod=10)
    df['SMA20'] = talib.MA(df['close'], timeperiod=20)
    # Williams Overbought/Oversold Index WR 威廉指标
    df['WR14'] = talib.WILLR(df['high'], df['low'], df['close'], timeperiod=14)
    df['WR18'] = talib.WILLR(df['high'], df['low'], df['close'], timeperiod=18)
    df['WR22'] = talib.WILLR(df['high'], df['low'], df['close'], timeperiod=22)
    # Moving Average Convergence / Divergence MACD 指数平滑移动平均线
    DIFF1, DEA1, df['MACD9'] = talib.MACD(np.array(df['close']),
                                          fastperiod=12,
                                          slowperiod=26,
                                          signalperiod=9)
    DIFF2, DEA2, df['MACD10'] = talib.MACD(np.array(df['close']),
                                           fastperiod=14,
                                           slowperiod=28,
                                           signalperiod=10)
    DIFF3, DEA3, df['MACD11'] = talib.MACD(np.array(df['close']),
                                           fastperiod=16,
                                           slowperiod=30,
                                           signalperiod=11)
    df['MACD9'] = df['MACD9'] * 2
    df['MACD10'] = df['MACD10'] * 2
    df['MACD11'] = df['MACD11'] * 2
    # Relative Strength Index RSI 相对强弱指数
    df['RSI15'] = talib.RSI(np.array(df['close']), timeperiod=15)
    df['RSI20'] = talib.RSI(np.array(df['close']), timeperiod=20)
    df['RSI25'] = talib.RSI(np.array(df['close']), timeperiod=25)
    df['RSI30'] = talib.RSI(np.array(df['close']), timeperiod=30)
    # Stochastic Oscillator Slow STOCH 常用的KDJ指标中的KD指标
    df['STOCH'] = talib.STOCH(df['high'],
                              df['low'],
                              df['close'],
                              fastk_period=9,
                              slowk_period=3,
                              slowk_matype=0,
                              slowd_period=3,
                              slowd_matype=0)[1]
    # On Balance Volume OBV 能量潮
    df['OBV'] = talib.OBV(np.array(df['close']), df['volume'])
    # Simple moving average SMA 简单移动平均
    df['SMA15'] = talib.SMA(df['close'], timeperiod=15)
    df['SMA20'] = talib.SMA(df['close'], timeperiod=20)
    df['SMA25'] = talib.SMA(df['close'], timeperiod=25)
    df['SMA30'] = talib.SMA(df['close'], timeperiod=30)
    # Money Flow Index MFI MFI指标
    df['MFI14'] = talib.MFI(df['high'],
                            df['low'],
                            df['close'],
                            df['volume'],
                            timeperiod=14)
    df['MFI18'] = talib.MFI(df['high'],
                            df['low'],
                            df['close'],
                            df['volume'],
                            timeperiod=18)
    df['MFI22'] = talib.MFI(df['high'],
                            df['low'],
                            df['close'],
                            df['volume'],
                            timeperiod=22)
    # Ultimate Oscillator UO 终极指标
    df['UO7'] = talib.ULTOSC(df['high'],
                             df['low'],
                             df['close'],
                             timeperiod1=7,
                             timeperiod2=14,
                             timeperiod3=28)
    df['UO8'] = talib.ULTOSC(df['high'],
                             df['low'],
                             df['close'],
                             timeperiod1=8,
                             timeperiod2=16,
                             timeperiod3=22)
    df['UO9'] = talib.ULTOSC(df['high'],
                             df['low'],
                             df['close'],
                             timeperiod1=9,
                             timeperiod2=18,
                             timeperiod3=26)
    # Rate of change Percentage ROCP 价格变化率
    df['ROCP'] = talib.ROCP(df['close'], timeperiod=10)
Beispiel #18
0
fig = plt.figure(figsize=(12, 6))

ax = fig.add_subplot(1, 1, 1)
ax.set_xticks(range(0, len(df_1110.index), 10))
ax.set_xticklabels(df_1110.index[::10])
mpf.candlestick2_ochl(ax,
                      df_1110['Open'],
                      df_1110['Close'],
                      df_1110['High'],
                      df_1110['Low'],
                      width=0.6,
                      colorup='r',
                      colordown='g',
                      alpha=0.75)
import talib
sma_10 = talib.SMA(np.array(df_1110['Close']), 10)
sma_30 = talib.SMA(np.array(df_1110['Close']), 30)

fig = plt.figure(figsize=(10, 4))
ax = fig.add_axes([0, 0.2, 1, 0.5])
ax2 = fig.add_axes([0, 0, 1, 0.2])

ax.set_xticks(range(0, len(df_1110.index), 10))
ax.set_xticklabels(df_1110.index[::10])
mpf.candlestick2_ochl(ax,
                      df_1110['Open'],
                      df_1110['Close'],
                      df_1110['High'],
                      df_1110['Low'],
                      width=0.6,
                      colorup='r',
Beispiel #19
0
from iexfinance import get_historical_data
from datetime import datetime
import pandas as pd
import talib as ta
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('bmh')

start = datetime(2018, 1, 1)
end = datetime(2019, 1, 1)

data = get_historical_data('AAPL',
                           start=start,
                           end=end,
                           output_format='pandas')
data.index = pd.to_datetime(data.index)
data.head()

data['SMA'] = ta.SMA(data.close, timeperiod=20)
data['EMA'] = ta.EMA(data.close, timeperiod=20)

data[['close', 'SMA', 'EMA']].plot(figsize=(10, 5))
plt.show()
Beispiel #20
0
                json.dump(account_info, dump_f)
            print('connect ws error!')
            break
        continue

    retryCount=0

    kline = (pd.DataFrame.from_dict(kline_1min))[['id', 'close', 'high', 'low', 'open', 'amount']]
    id = kline['id'].values
    id = (id[-1] / 60)
    closed = kline['close'].values
    opened = kline['open'].values
    highed = kline['high'].values
    lowed = kline['low'].values
    amounted = kline['amount'].values
    ma5 = talib.SMA(closed, timeperiod=5)
    ma20 = talib.SMA(closed, timeperiod=20)
    ma30 = talib.SMA(closed, timeperiod=30)
    ma60 = talib.SMA(closed, timeperiod=60)
    rsi = talib.RSI(closed, timeperiod=14)
    macd, signal, hist = talib.MACD(closed, fastperiod=12, slowperiod=26, signalperiod=9)

    if (account_info['margin_available'] + account_info['margin_frozen'] + (account_info['price'] - highed[-1]) *
        account_info['volume']) <= 0:
        account_info['margin_available'] = 0
        account_info['margin_frozen'] = 0
        account_info['cost_price'] = 0
        account_info['volume'] =0

    account_info['margin_available'] += (account_info['price'] - closed[-1]) * account_info['volume']
    account_info['price'] = closed[-1]
Beispiel #21
0
def handle_bar(context, bar_dict):
    # 开始编写你的主要的算法逻辑

    # bar_dict[order_book_id] 可以拿到某个证券的bar信息
    # context.portfolio 可以拿到现在的投资组合信息

    # 使用order_shares(id_or_ins, amount)方法进行落单

    # TODO: 开始编写你的算法吧!
    # 因为策略需要用到均线,所以需要读取历史数据
    #做成交量低迷排名
    ranking_list = pd.DataFrame(columns=[
        "code", "volume_division"
    ])  #columes: "code",“volume_division”, rows:每个股票占一行
    for stock in context.fundamental_df.columns.values:
        volumes = history_bars(stock, context.LONGPERIOD + 1, '1d', 'volume')
        volumes = [float(f) for f in volumes]
        volumes = np.array(volumes)
        # 使用talib计算长短两根均线,均线以array的格式表达
        short_avg = talib.SMA(volumes, context.SHORTPERIOD)
        long_avg = talib.SMA(volumes, context.LONGPERIOD)
        plot("short avg", short_avg[-1])
        plot("long avg", long_avg[-1])
        volume_division = long_avg[-1] / short_avg[-1]  #越大越无人问津
        ranking_list = ranking_list.append(
            {
                "code": stock,
                "volume_division": volume_division
            },
            ignore_index=True)
    ranking_list = ranking_list.sort(["volume_division"], ascending=False)
    logger.info(ranking_list)
    for i in range(0, 10):
        stock = ranking_list.iloc[i, 0]
        # 计算现在portfolio中股票的仓位
        cur_position = context.portfolio.positions[stock].quantity
        # 首次买入条件,应该添加只买业绩好的
        if (cur_position == 0 and ranking_list.iloc[i, 1] > 2):
            # 买入
            # 计算现在portfolio中的现金可以购买多少股票
            if (context.portfolio.cash > 10000.0):
                shares = 10000 / bar_dict[stock].close
            else:
                shares = context.portfolio.cash / bar_dict[stock].close
            order_shares(stock, shares)

    for stock in context.portfolio.positions:
        volumes = history_bars(stock, context.LONGPERIOD + 1, '1d', 'volume')
        volumes = [float(f) for f in volumes]
        volumes = np.array(volumes)
        # 使用talib计算长短两根均线,均线以array的格式表达
        short_avg = talib.SMA(volumes, context.SHORTPERIOD)
        long_avg = talib.SMA(volumes, context.LONGPERIOD)
        plot("short avg", short_avg[-1])
        plot("long avg", long_avg[-1])

        # 计算现在portfolio中股票的仓位
        cur_position = context.portfolio.positions[stock].quantity
        # 清仓条件:
        # 成交量太多或者达到止盈条件
        try:
            if ((2 * long_avg[-1] < short_avg[-1]
                 or context.portfolio.positions[stock].market_value /
                 context.portfolio.positions[stock].quantity /
                 context.portfolio.positions[stock].avg_price > 1.06)
                    and cur_position > 0):
                # 进行清仓
                order_target_value(stock, 0)
        except:
            pass
        # 持仓时买入条件,做T
        try:
            if (cur_position > 0
                    and context.portfolio.positions[stock].market_value < 20000
                    and context.portfolio.positions[stock].market_value /
                    context.portfolio.positions[stock].quantity /
                    context.portfolio.positions[stock].avg_price < 0.92):
                # 买入
                if (context.portfolio.cash > 5000.0):
                    shares = 5000 / bar_dict[stock].close
                else:
                    shares = context.portfolio.cash / bar_dict[stock].close
                order_shares(stock, shares)
        except:
            pass
Beispiel #22
0
    # 1.先测试单进程 cpu == 1
    strat_time = np.array([1, 9, 20])
    end_time = np.array([27, 16, 20])
    a = 1
    b = 10
    c = 10

    if True == a:
        df_time_list = [['2019-10-10 09:15:00', '2019-12-10 16:25:00']]
        s_time, e_time = df_time_list[0]
        df = get_local_hsi_csv(s_time, e_time, datapath)  # 获取本地数据
        df000 = df.copy()
        time0 = time.process_time()
        df000 = df.copy()
        df000['ma'] = talib.SMA(df000['close'], 5000)
        # df000['ma2']=talib.MA(df000['close'],5000)
        #[44.000 140.000 30.000 50.000 140.000]

        df = zsy_bollin_01(df000, para=[90, 2])
        print(df.tail())
        huice_hsi(df,
                  bzj=1 / 5,
                  leverage_rate=1,
                  c_rate=20,
                  hycs=50,
                  slip=1,
                  min_margin_rate=(0.8),
                  is_print=True)
        exit()
        res0, resdf = duojincheng_backtesting(
Beispiel #23
0
import numpy as np
import talib

close = np.random.random(100)

output = talib.SMA(close)

Beispiel #24
0
def backtest_KD_pick(mainParament):
    userInfo = get_user_info.data_user_info(mainParament.money_start,mainParament.date_start,mainParament.date_end)
    Temp_result_pick = pd.DataFrame(columns=['date','選股數量'])
    Temp_table = get_stock_history.get_stock_history(2330,"2005-01-01",reGetInfo = False,UpdateInfo = False)
    All_stock_signal = dict()
      
    ROE_record_day = datetime.strptime("2000-01-01","%Y-%m-%d")
    buy_data = pd.DataFrame(columns = ['Date','code']).set_index('Date')
    sell_data = pd.DataFrame(columns = ['Date','code']).set_index('Date')
    ROE_data = {}
    add_one_day = userInfo.add_one_day
    changeDateMonth = tools.changeDateMonth
    get_ROE_range = get_stock_history.get_ROE_range
    MixDataFrames = tools.MixDataFrames
    sell_stock = userInfo.sell_stock
    buy_all_stock = userInfo.buy_all_stock
    for index,row in Temp_table.iterrows():
        if index < userInfo.now_day:
            continue
        while userInfo.now_day != index:
            if add_one_day() == False:
                break
        has_trade = False
        #ROE篩選
        if ROE_record_day <= index:
            ROE_data = {}
            ROE_record_day = changeDateMonth(index,1)
            ROE_data["ROE_data_1"] = pd.DataFrame(get_ROE_range(changeDateMonth(index,-3),0,999))
            ROE_data["ROE_data_1"].rename(columns={'ROE':'ROE_data_1'},inplace=True)
            ROE_data["ROE_data_2"] = pd.DataFrame(get_ROE_range(changeDateMonth(index,-6),0,999))
            ROE_data["ROE_data_2"].rename(columns={'ROE':'ROE_data_2'},inplace=True)
            ROE_data["ROE_data_3"] = pd.DataFrame(get_ROE_range(changeDateMonth(index,-9),0,999))
            ROE_data["ROE_data_3"].rename(columns={'ROE':'ROE_data_3'},inplace=True)
            ROE_data["ROE_data_4"] = pd.DataFrame(get_ROE_range(changeDateMonth(index,-12),0,999))
            ROE_data["ROE_data_4"].rename(columns={'ROE':'ROE_data_4'},inplace=True)
            mask = MixDataFrames(ROE_data)
            
            ROE_data_result =(mask["ROE_data_1"]+mask["ROE_data_2"]+mask["ROE_data_3"]+mask["ROE_data_4"])/4
            ROE_data_result = ROE_data_result.dropna()
            ROE_data_mask = mask["ROE_data_1"]> ROE_data_result
            ROE_data = ROE_data_mask[ROE_data_mask]
            
            for key,value in ROE_data.iteritems():#先算出股票的買賣訊號
                if get_stock_history.check_no_use_stock(key) == True:
                    print('get_stock_price: ' + str(key) + ' in no use')
                    continue
                if All_stock_signal.__contains__(key):
                    continue
                table = get_stock_history.get_stock_history(key,"2005-01-01",reGetInfo = False,UpdateInfo = False)
                table_K,table_D = talib.STOCH(table['High'],table['Low'],table['Close'],fastk_period=50, slowk_period=20, slowk_matype=0, slowd_period=20, slowd_matype=0)
                table_sma10 = talib.SMA(np.array(table['Close']), 10)
                table_sma240 = talib.SMA(np.array(table['Close']), 240)
                signal_buy = (table_K > table_D)
                signal_sell = (table_K < table_D)
                signal_sma10 = table.Close < table_sma10
                signal_sma240 = table.Close > table_sma240
                signal = signal_buy.copy()
                signal = (signal_sma10 & signal_sma240 & signal_buy)
                signal[signal_sell] = -1
                All_stock_signal[key] = signal
        
        
        #找出買入訊號跟賣出訊號-------------------------
        buy_numbers = []
        sell_numbers = []
        for i,value in All_stock_signal.items():
            try:
                if value[index] == True:
                    if ROE_data.index.__contains__(int(i)):
                        if ROE_data[int(i)] == True:
                            buy_numbers.append(i)
                elif value[index] < 0 :
                    sell_numbers.append(i)
            except:
                #print('error:' + str(index) + ' at ' + str(i))
                continue
        #出場訊號篩選--------------------------------------
        if len(userInfo.handle_stock) > 0:
            Temp_data = userInfo.handle_stock
            for key,value in list(Temp_data.items()):
                if sell_numbers.__contains__(int(key)):
                    sell_stock(key,value.amount)
                    has_trade = True
        #入場訊號篩選--------------------------------------
        if len(buy_numbers) > 0 :
            Temp_buy = pd.DataFrame(columns={'code','volume'})
            for number in buy_numbers:
                volume = get_stock_history.get_stock_price(number,tools.DateTime2String(userInfo.now_day),get_stock_history.stock_data_kind.Volume)
                Temp_buy = Temp_buy.append({'code':str(number),'volume':volume},ignore_index = True)
            Temp_buy = Temp_buy.sort_values(by='volume', ascending=False).set_index('code')
            buy_all_stock(Temp_buy)
            has_trade = True
            
        #更新資訊--------------------------------------
        if has_trade:
            if len(buy_numbers) != 0:
                buy_data = buy_data.append({'Date':index,'code':buy_numbers},ignore_index = True)
            if len(sell_numbers) != 0:
                sell_data = sell_data.append({'Date':index,'code':sell_numbers},ignore_index = True)
            userInfo.Record_userInfo()
            userInfo.Recod_tradeInfo()
            Temp_result_pick = Temp_result_pick.append({'date':userInfo.now_day,
                                                '選股數量':len(buy_numbers)},ignore_index = True)
    
    buy_data = buy_data.set_index('Date')
    buy_data.to_csv('buy.csv')
    sell_data = sell_data.set_index('Date')
    sell_data.to_csv('sell.csv')
    #最後總結算----------------------------
    Temp_result_pick.set_index('date',inplace=True)
    Temp_alldata = tools.MixDataFrames({'draw':userInfo.Temp_result_draw,'pick':Temp_result_pick},'date')
    
    Temp_alldata = tools.MixDataFrames({'all':Temp_alldata,'userinfo':userInfo.Temp_result_All},'date')
    
    userInfo.Temp_result_draw.set_index('date').to_csv('backtestdata.csv')
    userInfo.Temp_trade_info.set_index('date').to_csv('backtesttrade.csv')
    Temp_alldata.set_index('date').to_csv('backtestAll.csv')   
    return userInfo.Temp_result_draw.set_index('date')
Beispiel #25
0
DataDict = {}
for file in filelist:
    key = file.split('.')[0]
    DataDict[key] = pd.read_csv(raw_data_dir + file, index_col=0)

format = lambda x: '%.4f' % x

#计算指标
for key in DataDict:
    data = DataDict[key]
    data.sort_index(inplace=True)  #根据日期进行排序
    close = np.array(data['close'])
    high = np.array(data['high'])
    low = np.array(data['low'])
    volume = np.array(data['volume'])
    data['SMA5'] = ta.SMA(close, timeperiod=5)  #5日均线
    data['SMA10'] = ta.SMA(close, timeperiod=10)  #10日均线
    data['SMA30'] = ta.SMA(close, timeperiod=30)  #30日均线
    data['SMA60'] = ta.SMA(close, timeperiod=60)  #60日均线
    #upper, middle, lower = ta.BBANDS(close, matype=ta.MA_Type.T3)
    data['DEMA5'] = ta.DEMA(close, timeperiod=5)  #5日双指数平均
    data['DEMA10'] = ta.DEMA(close, timeperiod=10)  #10日双指数平均
    data['DEMA30'] = ta.DEMA(close, timeperiod=30)  #30日双指数平均
    data['DEMA60'] = ta.DEMA(close, timeperiod=60)  #60日双指数平均
    data['SAR'] = ta.SAR(high, low)
    data['WMA'] = ta.WMA(close, timeperiod=10)
    data['MIDPOINT'] = ta.MIDPOINT(close, timeperiod=10)
    data['MIDPRICE'] = ta.MIDPRICE(high, low, timeperiod=10)
    data['MFI'] = ta.MFI(high, low, close, volume, timeperiod=10)
    data['MOM10'] = ta.MOM(close, timeperiod=10)  #动量指标
    data['MOM5'] = ta.MOM(close, timeperiod=5)  #动量指标
Beispiel #26
0
def find_ma_crossovers(serverSite):
    crossovers = []
    
    bt_tickers = read_from_database("SELECT DISTINCT ticker FROM backtestdata;",serverSite).ticker.tolist()
    
    for ticker in bt_tickers:
        bt_data = read_from_database("SELECT ticker, timestamp, open, high ,low, close, volume FROM backtestdata WHERE ticker ='"+ticker+"';",serverSite)
        
        #Talib need the oldest data to be first 
        bt_data = bt_data.iloc[::-1]
        bt_data["EMA"+str(9)] = talib.EMA(bt_data.close, timeperiod = 9)
        bt_data["EMA"+str(20)] = talib.EMA(bt_data.close, timeperiod = 20)
        bt_data["EMA"+str(50)] = talib.SMA(bt_data.close, timeperiod = 50)
        bt_data["EMA"+str(100)] = talib.SMA(bt_data.close, timeperiod = 100)
        bt_data["EMA"+str(200)] = talib.SMA(bt_data.close, timeperiod = 200)
        bt_data["MACD"],bt_data["MACDsignal"], bt_data["MACDhist"] = talib.MACD(bt_data.close) #Using default params
        bt_data["RSI"] = talib.RSI(bt_data.close) #Using default params
        bt_data["ATR"] = talib.ATR(bt_data.high, bt_data.low,bt_data.close)
        bt_data["TATR"] = talib.TRANGE(bt_data.high, bt_data.low,bt_data.close)
        bt_data["BBUpper"],bt_data["BBmiddle"], bt_data["BBlower"] = talib.BBANDS(bt_data.close, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0) #Using default params
        bt_data["dojiDH"] = talib.CDLDRAGONFLYDOJI(bt_data.open, bt_data.high, bt_data.low, bt_data.close)
        bt_data["hammer"] = talib.CDLHAMMER(bt_data.open, bt_data.high, bt_data.low, bt_data.close)
        bt_data["CDLDOJI"] = talib.CDLDOJI(bt_data.open, bt_data.high, bt_data.low, bt_data.close)
        
        #Dropping NAs
        bt_data.dropna(inplace = True)
        
        #Find the crossovers
        #Extremely bad solution. Im sure there are better ways
        crossAbove = []
        crossBelow = []
        for key,  row in enumerate(bt_data.iterrows()):
            
            if(bt_data.iloc[key,5] > bt_data.iloc[key,8] and bt_data.iloc[key-1,5] < bt_data.iloc[key-1,8]):
                crossAbove.append(True)
            else:
                crossAbove.append(False)
        
        
        
        for key,  row in enumerate(bt_data.iterrows()):                
            
            if(bt_data.iloc[key,5] < bt_data.iloc[key,8] and bt_data.iloc[key-1,5] > bt_data.iloc[key-1,8]):
                crossBelow.append(True)

            else:
                crossBelow.append(False)
                
        bt_data["crossBelow"] = crossBelow
        bt_data["crossAbove"] = crossAbove
        
        
        class Trade:
            def __init__(self,ticker, targetEntry, stoploss, crossrow, active = True):
                self.ticker = ticker
                self.targetEntry = targetEntry                
                self.actualEntry = 0
                self.stoploss = stoploss
                self.target = 0
                self.crossrow = crossrow
                self.searching_for_entry = True
                self.active = active
                
                
            
            def tradeFoundLong(self,actualEntry):
                self.actualEntry = actualEntry
                self.target = self.actualEntry + (self.actualEntry- self.stoploss) * 2
                self.searching_for_entry = False
                
                
                #print(self.entry, self.target, self.stoploss)
            
            def tradeFoundShort(self,actualEntry):
                self.actualEntry = actualEntry
                self.target = self.actualEntry - (self.stoploss - self.actualEntry) * 2
                self.searching_for_entry = False
            
            def deactivateTrade(self):
                self.active = False
                
            
        #Looking at the longs!
        #Check if hit stoploss or target
        count_target = 0
        count_stop = 0
        count_started_trade = 0
        active_trade = Trade("Ticker",1,1,1, active = False) #Dummy trade, not important for result but excecution needs once to start with.
        
        for key, row in enumerate(bt_data.iterrows()):

            if (row[1].crossAbove):
                active_trade = Trade(ticker, targetEntry = row[1].high, stoploss = row[1].EMA20 , crossrow = row[0])
                print("Started search at row",row[0])
              
            #Enter at open if there has been a big gap durin pre and post market, othervise assume we enter at the highs
            if (active_trade.targetEntry < row[1].high and active_trade.searching_for_entry and active_trade.active):
                if (row[1].open > active_trade.targetEntry):      
                    active_trade.tradeFoundLong(actualEntry = row[1].open)  
                else:
                    (active_trade.tradeFoundLong(actualEntry = active_trade.targetEntry))
                    
                print("Started trade at row", row[0], "from", active_trade.crossrow)
                count_started_trade += 1 
            
            #Assume if it the candle hits out target we get out
            if (active_trade.target < row[1].high and active_trade.searching_for_entry == False and active_trade.active):
                active_trade.deactivateTrade()
                crossovers.append([active_trade.ticker, active_trade.crossrow,"Target"])
                print("Target met at row", row[0], " from row ", active_trade.crossrow)
                count_target += 1
            
            #If the candle closes above our stop, stop out
            if (active_trade.stoploss > row[1].close and active_trade.searching_for_entry == False and active_trade.active):
                active_trade.deactivateTrade()
                crossovers.append([active_trade.ticker, active_trade.crossrow,"Stoploss"])
                print("Stoploss met at row", row[0], " from row ", active_trade.crossrow)
                count_stop += 1
            
            
            
        
        print("Trades started:",count_started_trade ,"Stops:", count_stop, "Targets:",count_target,"from total", sum(bt_data.crossAbove))
        
        
        #Shorts!!        
        count_target = 0
        count_stop = 0
        count_started_trade = 0
        active_trade = Trade("Ticker",1,1,1, active = False) #Dummy trade, not important for result but excecution needs once to start with.
        
        for key, row in enumerate(bt_data.iterrows()):

            if (row[1].crossBelow):
                active_trade = Trade(ticker,targetEntry = row[1].low, stoploss = row[1].EMA20 , crossrow = row[0])
                print("Started search at row",row[0])
              
            #Enter at open if there has been a big gap durin pre and post market, othervise assume we enter at the lows        
            if (active_trade.targetEntry < row[1].low and active_trade.searching_for_entry and active_trade.active):
                if (row[1].open < active_trade.targetEntry):      
                    active_trade.tradeFoundShort(actualEntry = row[1].open)  
                else:
                    (active_trade.tradeFoundShort(actualEntry = active_trade.targetEntry))
                    
                print("Started trade at row", row[0], "from", active_trade.crossrow)
                count_started_trade += 1 
            
            #Assume if it the candle hits out target we get out
            if (active_trade.target < row[1].low and active_trade.searching_for_entry == False and active_trade.active):
                active_trade.deactivateTrade()
                crossovers.append([active_trade.ticker, active_trade.crossrow,"Target"])
                print("Target met at row", row[0], " from row ", active_trade.crossrow)
                count_target += 1
            
            #If the candle closes above our stop, stop out
            if (active_trade.stoploss < row[1].close and active_trade.searching_for_entry == False and active_trade.active):
                active_trade.deactivateTrade()
                crossovers.append([active_trade.ticker, active_trade.crossrow,"Stoploss"])
                print("Stoploss met at row", row[0], " from row ", active_trade.crossrow)
                count_stop += 1
            
            
            
        
        print("Trades started:",count_started_trade ,"Stops:", count_stop, "Targets:",count_target,"from total", sum(bt_data.crossAbove))
          
    return crossovers
Beispiel #27
0
def BBandMA(df, count, acmroi, winnum, winfact,revenue_sum,avg_return,total_return_ratio, MDD,sharpe_ratio):
    #設定初始值
  #for stk_no in range(df):
    #total_trade_record=pd.DataFrame(data=None, index=None,columns = ["stockid","bought_date","bought_time","bought_price","sold_date","sold_time","sold_price","QTY","cash_account","LS_type"])
    trade_record = pd.DataFrame(data=None, index=None,columns = ["stockid","bought_date","bought_time","bought_price","sold_date","sold_time","sold_price","QTY","cash_account","LS_type"])
    df["stockid"]= np.nan
    df["bought_date"]= np.nan
    df["bought_time"]= np.nan
    df["bought_price"]= np.nan
    df["sold_date"]= np.nan
    df["sold_time"]= np.nan
    df["sold_price"]= np.nan
    df["QTY"]=1000
    df["cash_account"]= np.nan
    df["LS_type"]=0
    df['BBXBuy'] = np.nan
    df['BBYBuy'] = np.nan
    df['BBXSell'] = np.nan
    df['BBYSell'] = np.nan
    #計算BBand上, 中, 下線
    close = np.array(df['close'], dtype=float)
    upper, middle, lower = talib.BBANDS(close, timeperiod=10, nbdevup=0.03, nbdevdn=2, matype=MA_Type.T3)
    df['BBupper'] = upper
    df['BBlower'] = lower
    vol =df['Quantity'].as_matrix().astype("float64") 
    df['volMA'] = talib.SMA(vol, timeperiod=30)   
    flag = False
    buyprice=[]
    sellprice=[]
    win = 0
    loss = 0
    roi = 0
    revenue=0
    total_return=0
    revenue_sum=0
    for i in range(len(df)):
        if (flag == False) & (float(df['highest'].iloc[i])> float(df['BBupper'].iloc[i])*1.075)   :
        #if (flag == False) & (float(df['highest'].iloc[i]) > float(df['BBupper'].iloc[i])) & (float(df['Quantity'].iloc[i]) > (float(df['volMA'].iloc[i]))) & (float(df['close'].iloc[i])*1.01 <(float(df['BBupper'].iloc[i]))) :
            sellprice= df['close'].iloc[i]
            df['BBXSell'].iloc[i] = df['MATCH_TIME'].iloc[i]
            df['BBYSell'].iloc[i] = sellprice
            df["stockid"].iloc[i]=df['STOCK_SYMBOL'].iloc[i]
            df["sold_date"].iloc[i]=df['date'].iloc[i]
            df["sold_time"].iloc[i]=df['MATCH_TIME'].iloc[i]
            df["sold_price"].iloc[i]=df['close'].iloc[i]  
            #df["cash_account"].iloc[i]= round((5000000-float(buyprice)*1000*1.001425*0.6),0)
            flag = True
            print("BB賣價"+str(sellprice))
        if (flag == True) & (float(df['lowest'].iloc[i])*0.953>=float(df['BBlower'].iloc[i])<float(df['BBupper'].iloc[i])) :
            buyprice=df['close'].iloc[i+6]
            df['BBXBuy'].iloc[i] = df['MATCH_TIME'].iloc[i+6]
            df['BBYBuy'].iloc[i] = buyprice
            print("BB買進價"+str(buyprice))
            df["stockid"].iloc[i]=df['STOCK_SYMBOL'].iloc[i+6]
            df["bought_date"].iloc[i]=df['date'].iloc[i+6]
            df["bought_time"].iloc[i]=df['MATCH_TIME'].iloc[i+6]
            df["bought_price"].iloc[i]=df['close'].iloc[i+6]
            
            #df["cash_account"].iloc[i]= round( (5000000+float(buyprice* 1.001425*0.6-sellprice* (1-0.001425*0.6-0.001))*1000),0)
            df["LS_type"].iloc[i]=2
            count += 1
            flag = False
            [roi, winnum] =roical(buyprice,sellprice, winnum)
            acmroi += roi
            [loss, win] = winfactor(buyprice, sellprice, loss, win)
            revenue=((sellprice*0.995575)- (buyprice*1.001425))*1000 
            revenue_sum +=round((revenue),2)
            premium=revenue/((buyprice*1.001425)*1000)
            total_return +=premium
            avg_return=(total_return/count)
            total_return_ratio=round((revenue_sum/5000000),3)*100
            #return_standard+=premium.std()#這是以半年投資報酬標準差,所以標準差將會半年化算Sharp指數
            #sharpe_ratio=((avg_return - 0.01095/2)/ return_standard)*100
                       
            if (flag == True & i==(len(df)-1)):
                df['BBXBuy'][i] = sellprice
                df['BBYBuy'][i] = df['MATCH_TIME'].iloc[i]
                df['BBXBuy'][i] = buyprice
                df["stockid"].iloc[i]=df['STOCK_SYMBOL'].iloc[i]
                df["bought_date"].iloc[i]=df['date'].iloc[i]
                df["bought_time"].iloc[i]=df['MATCH_TIME'].iloc[i]
                df["bought_price"].iloc[i]=df['close'].iloc[i]
                #df["cash_account"].iloc[i]=round( (5000000+float(buyprice* (1+0.001425*0.6)-sellprice*(1-0.001425*0.6-0.001))*1000),0)
                df["LS_type"].iloc[i]=2
                count += 1
                [roi, winnum] = roical(buyprice, sellprice, winnum)
                acmroi += roi
                [loss, win] = winfactor(buyprice, sellprice, loss, win)
                print("BB最後1天買入價"+str(buyprice))
                revenue=((sellprice*0.995575)- (buyprice*1.001425))*1000 
                revenue_sum +=round((revenue),2)
                premium=revenue/(buyprice*1.001425)
                total_return +=premium
                avg_return=(total_return/count)
                total_return_ratio=round((revenue_sum/5000000),3)*100
                #return_standard+=premium.std()#這是以半年投資報酬標準差,所以將會半年化算Sharp指數
                #sharpe_ratio=((avg_return - 0.01095/2)/ return_standard)*100
            elif ( flag == False& i==(len(df)-10)):
                break   
    
    if (loss == 0):
        loss = 1
    winvar = win / loss
    if (count == 0):
        count = 0.01      
    #str1 = 'BBand策略: ' + '交易次數 = '+ str(count) + ' 次; ' + '累計報酬率 = ' + str(round(acmroi*100, 2)) + '%; ' + '勝率 = ' + str(round((winnum/count)*100,2)) + '%' + '; 獲利因子 = ' + str(round(winvar, 2))+ '%' + '; 總收益 = ' + str(round(revenue_sum, 2)+ '; 平均報酬率 = ' + str(round(avg_return*100, 2))+ '%'+ '; 夏普指數 = ' + str(round(sharpe_ratio*100, 2))+ '%'+ '; MDD = ' + str(round(MDD, 2))+ '; 總資產報酬率 = ' + str(round(total_return_ratio*100, 2))+ '%')
    
   
    trade_record.index.names = ['trade_no']
    trade_record["stockid"]= df["stockid"]
    trade_record["bought_date"]=df["bought_date"]
    trade_record["bought_time"]=df["bought_time"]
    trade_record["bought_price"]=df["bought_price"]
    trade_record["sold_date"]=df["sold_date"]
    trade_record["sold_time"]= df["sold_time"]
    trade_record["sold_price"]=df["sold_price"]
    #trade_record["cash_account"]=df["cash_account"]
    trade_record["offset_date"] = trade_record[["bought_date","sold_date"]].max(axis=1) #抓出平倉日
    #trade_record =trade_record.sort_values(by=["offset_date",'trade_no'])
    #trade_record = trade_record.drop(["bought_date"],axis=1) #將多餘的紀錄刪除
    #trade_record.loc[:,["offset_date"]].dropna(axis=1)
    #trade_record.loc[:,["bought_date"],["bought_time"],["bought_price"]].sort_values(by=["bought_date"])
    trade_record.loc[:,["bought_date"]].dropna(axis=1)
    #trade_record.loc[:,["sell_date"],["sell_time"],["sell_price"]].sort_values(by=["sold_date"])
    #trade_record =trade_record.reset_index(drop=True) #重設index
    #trade_record["LS_type"]=df["LS_type"]=2
    #trade_record["Algorithm"] =3
    #trade_record["QTY"]=1000
    #total_trade_record.append(trade_record)
    trade_record.to_csv(r"./BBands_record.csv", sep = ",")
    #print(str1)

    #print(trade_record)
       
    return (count, acmroi, winnum, winvar,revenue_sum,avg_return,total_return_ratio, MDD,sharpe_ratio) 
                    for cs4 in range(3, 6, 1):
                        # for cs5 in range(1, 2, 1):
                        # for cs6 in range(1, 3, 1):
                        canshu_list.append([cs1, cs2, cs3, cs4, 10])

        df_time_list = [['2016-1-10 09:15:00', '2019-12-28 16:25:00']]
        s_time, e_time = df_time_list[0]
        name = f'{filename}_{s_time[:4]}_{e_time[:4]}_3min'

        canshu_list = canshu_list[:10]
        df = get_local_hsi_csv(s_time, e_time, datapath)  # 获取本地数据
        df = transfer_to_period_data(df, rule_type='3T')
        print('数据大小', df.shape)
        df_day = transfer_to_period_data(df, rule_type='1D')
        df_day['atr_day'] = talib.SMA(
            talib.ATR(df_day['close'], df_day['high'], df_day['low'], 2),
            2) * 0.8
        df_day['candle_begin_time'] = df_day['candle_begin_time'].apply(
            lambda x: x + datetime.timedelta(hours=9, minutes=15))
        df = pd.merge(df,
                      df_day[['candle_begin_time', 'atr_day']],
                      how='outer',
                      on='candle_begin_time')
        df = df[df['volume'] > 0]
        df.fillna(method='ffill', inplace=True)
        print(df.tail(10))
        df000 = df.copy()

        if True == 1:
            print('参数列表个数:', len(canshu_list))
            time0 = time.process_time()
Beispiel #29
0
def get_data(type):

    data_df = pd.read_csv(FILE_PATH, index_col=[0])

    #data_df['date'] = data_df['date'].map(lambda x: dt.strptime(x, '%m/%d/%Y %I:%M:%S %p').date())
    data_df['volume'] = data_df['volume'].astype(float)
    #data_df = data_df.set_index('date')

    # Simple Moving Average
    data_df['sma_5'] = talib.SMA(data_df['close'].values, timeperiod=5)
    data_df['sma_10'] = talib.SMA(data_df['close'].values, timeperiod=10)
    # Exponential Moving Average
    data_df['ema_20'] = talib.EMA(data_df['close'].values, timeperiod=20)
    # Momentum 6 Month / Momentum 12 Month
    data_df['mtm6_mtm12'] = talib.MOM(
        data_df['close'].values, timeperiod=126) / talib.MOM(
            data_df['close'].values, timeperiod=252)
    # Stochastic Relative Strength Index
    data_df['fastk'], data_df['fastd'] = talib.STOCHRSI(
        data_df['close'].values,
        timeperiod=14,
        fastk_period=5,
        fastd_period=3,
        fastd_matype=0)
    # Rate Of Change
    data_df['roc_10'] = talib.ROC(data_df['close'].values, timeperiod=10)
    # Bollinger Bands
    data_df['bband_upper'], data_df['bband_middle'], data_df[
        'bband_lower'] = talib.BBANDS(data_df['close'].values,
                                      timeperiod=5,
                                      nbdevup=2,
                                      nbdevdn=2,
                                      matype=0)
    # Moving Average Convergence Divergence
    data_df['macd'], data_df['macdsignal'], data_df['macdhist'] = talib.MACD(
        data_df['close'].values, fastperiod=12, slowperiod=26, signalperiod=9)
    # Chaikin A/D Oscillator
    data_df['adosc'] = talib.ADOSC(data_df['high'],
                                   data_df['low'],
                                   data_df['close'],
                                   data_df['volume'],
                                   fastperiod=3,
                                   slowperiod=10)
    # Commodity Channel Index
    data_df['cci_14'] = talib.CCI(data_df['high'].values,
                                  data_df['low'].values,
                                  data_df['close'].values,
                                  timeperiod=14)
    # Average True Range
    data_df['atr_14'] = talib.ATR(data_df['high'].values,
                                  data_df['low'].values,
                                  data_df['close'].values,
                                  timeperiod=14)

    # Pivot Points
    dfpp = finta.PIVOT(data_df[["open", "high", "low", "close", "volume"]])
    dfpp.drop(columns=['s3', 's4', 'r3', 'r4'], inplace=True)
    pd.concat([data_df, dfpp], axis=1)

    # Target
    data_df['target'] = np.append(data_df['close'][1:].values, [np.nan])

    # Drop Rows With NA Values In Any Column
    data_df = data_df.dropna(axis=0, how='any')

    if type == "reg":

        # Popping The Target Column
        target = data_df.pop('target').values

    elif type == "class":
        print("type = classification")
        data_df['class'] = np.where((data_df['close'] < data_df['target']), 1,
                                    0)

        data_df.drop('target', axis=1, inplace=True)

        target = data_df.pop('class').values

    return data_df, target
Beispiel #30
0
def get_moving_average(prices):
    arr = numpy.array(prices)
    output = talib.SMA(arr, timeperiod=35)
    return list(output)