Example #1
0
    def calculate(self, stock_code, date, short=12, long=26, mid=9):
        dif = 0.0
        dea = 0.0
        macd = 0.0
        max_time_period = max(short, long, mid)
        data = su.get_basic_data(stock_code, date, max_time_period *
                                 3).sort_index(ascending=False)
        if data.shape[0] >= max_time_period * 3:
            close = data['CLOSE'].values
            ewma_short = pd.ewma(close, span=short)
            ewma_long = pd.ewma(close, span=long)
            difs = (ewma_short - ewma_long)
            deas = pd.ewma(difs, span=mid)
            macds = (difs - deas) * 2

            dif = round(difs[-1], 3)
            dea = round(deas[-1], 3)
            macd = round(macds[-1], 3)

        if np.isnan(dif) or np.isinf(dif) or np.isneginf(dif):
            dif = 0.0
        if np.isnan(dea) or np.isinf(dea) or np.isneginf(dea):
            dea = 0.0
        if np.isnan(macd) or np.isinf(macd) or np.isneginf(macd):
            macd = 0.0

        self.save_tech_data(stock_code, date, {
            'MACD_DIF': dif,
            'MACD_DEA': dea,
            'MACD': macd
        })
        return dif, dea, macd
Example #2
0
    def calculate(self,
                  stock_code,
                  date,
                  time_period=20,
                  nbdev_up=2,
                  nbdev_down=2):
        upper_brand = 0.0
        middle_brand = 0.0
        lower_brand = 0.0
        data = su.get_basic_data(stock_code, date,
                                 time_period + 1).sort_index(ascending=False)

        if data.shape[0] >= time_period:
            upper_brands, middle_brands, lower_brands = ta.BBANDS(
                data['CLOSE'].as_matrix(), time_period, nbdev_up, nbdev_down)
            upper_brand = round(upper_brands[-1], 3)
            middle_brand = round(middle_brands[-1], 3)
            lower_brand = round(lower_brands[-1], 3)
        if np.isinf(upper_brand) or np.isnan(upper_brand) or np.isneginf(
                upper_brand):
            upper_brand = 0.0
        if np.isinf(middle_brand) or np.isnan(middle_brand) or np.isneginf(
                middle_brand):
            middle_brand = 0.0
        if np.isinf(lower_brand) or np.isnan(lower_brand) or np.isneginf(
                lower_brand):
            lower_brand = 0.0

        self.save_tech_data(
            stock_code, date, {
                'BOLL_UP': upper_brand,
                'BOLL_LOW': lower_brand,
                'BOLL_MID': middle_brand
            })
        return upper_brand, middle_brand, lower_brand
Example #3
0
    def calculate(self, stock_code, date, time_period1=6, time_period2=12, time_period3=24):
        bias1 = 0.0
        bias2 = 0.0
        bias3 = 0.0
        max_time_period = max(time_period1, time_period2, time_period3)
        #print max_time_period
        data = su.get_basic_data(stock_code, date, max_time_period + 1).sort_index(ascending=False)

        if data.shape[0] >= max_time_period + 1:
            data['MA1'] = data['CLOSE'].rolling(window=time_period1).mean()
            data['MA2'] = data['CLOSE'].rolling(window=time_period2).mean()
            data['MA3'] = data['CLOSE'].rolling(window=time_period3).mean()
            close = data['CLOSE'].as_matrix()[-1]
            data['BIAS1'] = (close - data['MA1']) * 100 / data['MA1']
            data['BIAS2'] = (close - data['MA2']) * 100 / data['MA2']
            data['BIAS3'] = (close - data['MA3']) * 100 / data['MA3']

            bias1 = round(data['BIAS1'].as_matrix()[-1], 3)
            bias2 = round(data['BIAS2'].as_matrix()[-1], 3)
            bias3 = round(data['BIAS3'].as_matrix()[-1], 3)
        #print data
        #print data
        if np.isinf(bias1) or np.isnan(bias1) or np.isneginf(bias1):
            bias1 = 0.0
        if np.isinf(bias2) or np.isnan(bias2) or np.isneginf(bias2):
            bias2 = 0.0
        if np.isinf(bias3) or np.isnan(bias3) or np.isneginf(bias3):
            bias3 = 0.0
        self.save_tech_data(stock_code, date, {'BIAS_6': bias1, 'BIAS_12':bias2, 'BIAS_24':bias3})
        return bias1, bias2, bias3
Example #4
0
    def calculate(self,
                  stock_code,
                  date,
                  time_period1=3,
                  time_period2=6,
                  time_period3=12,
                  time_period4=24):
        bbi = 0.0
        max_time_period = max(time_period1, time_period2, time_period3,
                              time_period4)
        data = su.get_basic_data(stock_code, date, max_time_period +
                                 1).sort_index(ascending=False)

        if data.shape[0] >= max_time_period + 1:
            data['MA1'] = data['CLOSE'].rolling(window=time_period1).mean()
            data['MA2'] = data['CLOSE'].rolling(window=time_period2).mean()
            data['MA3'] = data['CLOSE'].rolling(window=time_period3).mean()
            data['MA4'] = data['CLOSE'].rolling(window=time_period4).mean()
            data['BBI'] = (data['MA1'] + data['MA2'] + data['MA3'] +
                           data['MA4']) / 4

            bbi = round(data['BBI'].as_matrix()[-1], 3)
        if np.isnan(bbi) or np.isinf(bbi) or np.isneginf(bbi):
            bbi = 0.0
        self.save_tech_data(stock_code, date, {'BBI': bbi})
        return bbi
Example #5
0
    def calculate(self,
                  stock_code,
                  date,
                  time_period1=5,
                  time_period2=10,
                  time_period3=20):
        ma_1 = 0.0
        ma_2 = 0.0
        ma_3 = 0.0

        max_time_period = max(time_period1, time_period2, time_period3)
        data = su.get_basic_data(stock_code, date, max_time_period +
                                 1).sort_index(ascending=False)

        if data.shape[0] >= max_time_period + 1:
            data['MA1'] = data['CLOSE'].rolling(window=time_period1).mean()
            data['MA2'] = data['CLOSE'].rolling(window=time_period2).mean()
            data['MA3'] = data['CLOSE'].rolling(window=time_period3).mean()

            ma_1 = round(data['MA1'].as_matrix()[-1], 3)
            ma_2 = round(data['MA2'].as_matrix()[-1], 3)
            ma_3 = round(data['MA3'].as_matrix()[-1], 3)

        if np.isnan(ma_1) or np.isinf(ma_1) or np.isneginf(ma_1):
            ma_1 = 0.0
        if np.isnan(ma_2) or np.isinf(ma_2) or np.isneginf(ma_2):
            ma_2 = 0.0
        if np.isnan(ma_3) or np.isinf(ma_3) or np.isneginf(ma_3):
            ma_3 = 0.0
        self.save_tech_data(stock_code, date, {
            'MA_5': ma_1,
            'MA_10': ma_2,
            'MA_20': ma_3
        })
        return ma_1, ma_2, ma_3
Example #6
0
    def calculate(self, stock_code, date, time_period1=12, time_period2=6):
        psy_12 = 0.0
        psy_6 = 0.0
        data = su.get_basic_data(stock_code, date,
                                 time_period1 + 1).sort_index(ascending=False)
        if data.shape[0] >= time_period1:
            count = 0.0
            data['P_CLOSE'] = data['CLOSE'].shift(1)
            data['DIFF'] = data['CLOSE'] - data['P_CLOSE']
            data['DIFF'].fillna(0, inplace=True)
            #print data
            for each in data[1:].itertuples():
                if each.DIFF > 0:
                    count += 1.0
            psy_12 = round((count / time_period1 * 100), 3)

            count = 0.0
            for each in data[-6:].itertuples():
                if each.DIFF > 0:
                    count += 1.0
            psy_6 = round((count / time_period2 * 100), 3)
        #print data
        if np.isnan(psy_12) or np.isinf(psy_12) or np.isneginf(psy_12):
            psy_12 = 0.0
        if np.isnan(psy_6) or np.isinf(psy_6) or np.isneginf(psy_6):
            psy_6 = 0.0
        self.save_tech_data(stock_code, date, {
            'PSY_12': psy_12,
            'PSY_6': psy_6
        })
        return psy_12, psy_6
Example #7
0
 def calculate(self,
               stock_code,
               date,
               time_period1=6,
               time_period2=14,
               time_period3=24):
     rsi_6 = 0.0
     rsi_14 = 0.0
     rsi_24 = 0.0
     data = su.get_basic_data(stock_code, date,
                              time_period3 + 1).sort_index(ascending=False)
     if data.shape[0] >= time_period3 + 1:
         rsi_6 = round(
             ta.RSI(data['CLOSE'].as_matrix(), time_period1)[-1], 3)
         rsi_14 = round(
             ta.RSI(data['CLOSE'].as_matrix(), time_period2)[-1], 3)
         rsi_24 = round(
             ta.RSI(data['CLOSE'].as_matrix(), time_period3)[-1], 3)
     if np.isnan(rsi_6) or np.isinf(rsi_6) or np.isneginf(rsi_6):
         rsi_6 = 0.0
     if np.isnan(rsi_14) or np.isinf(rsi_14) or np.isneginf(rsi_14):
         rsi_14 = 0.0
     if np.isnan(rsi_24) or np.isinf(rsi_24) or np.isneginf(rsi_24):
         rsi_24 = 0.0
     self.save_tech_data(stock_code, date, {
         'RSI_6': rsi_6,
         'RSI_14': rsi_14,
         'RSI_24': rsi_24
     })
     return rsi_6, rsi_14, rsi_24
Example #8
0
    def calculate(self,
                  stock_code,
                  date,
                  time_period1=10,
                  time_period2=50,
                  time_period3=6):
        dma = 0.0
        ama = 0.0
        max_time_period = max(time_period1, time_period2, time_period3)
        data = su.get_basic_data(stock_code, date, max_time_period +
                                 time_period3).sort_index(ascending=False)

        if data.shape[0] >= max_time_period + 1:
            data['MA1'] = data['CLOSE'].rolling(window=time_period1).mean()
            data['MA2'] = data['CLOSE'].rolling(window=time_period2).mean()
            data['DMA'] = data['MA1'] - data['MA2']
            data['AMA'] = data['DMA'].rolling(window=time_period3).mean()
            dma = round(data['DMA'].as_matrix()[-1], 3)
            ama = round(data['AMA'].as_matrix()[-1], 3)

        if np.isnan(dma) or np.isinf(dma) or np.isneginf(dma):
            dma = 0.0
        if np.isnan(ama) or np.isinf(ama) or np.isneginf(ama):
            ama = 0.0

        self.save_tech_data(stock_code, date, {'DMA': dma, 'AMA': ama})
        return dma, ama
Example #9
0
    def calculate(self, stock_code, date, time_period=14):
        cci = 0.0
        data = su.get_basic_data(stock_code, date, time_period + 1).sort_index(ascending=False)

        if data.shape[0] >= time_period + 1:
            cci = round(ta.CCI(data['HIGH'].as_matrix(), data['LOW'].as_matrix(), data['CLOSE'].as_matrix(), time_period)[-1], 3)
        if np.isnan(cci) or np.isinf(cci) or np.isneginf(cci):
            cci = 0.0
        self.save_tech_data(stock_code, date, {'CCI':cci})
        return cci
Example #10
0
    def calculate(self, stock_code, date, time_period1=14, time_period2=6):
        roc = 0.0
        maroc = 0.0
        data = su.get_basic_data(stock_code, date,
                                 time_period1 * 2).sort_index(ascending=False)

        if data.shape[0] >= time_period1:
            rocs = ta.ROC(data['CLOSE'].as_matrix(), time_period1)
            roc = round(rocs[-1], 3)
            maroc = round(rocs[-6:].sum() / float(time_period2), 3)

        if np.isnan(roc) or np.isinf(roc) or np.isneginf(roc):
            roc = 0.0
        if np.isnan(maroc) or np.isinf(maroc) or np.isneginf(maroc):
            maroc = 0.0
        self.save_tech_data(stock_code, date, {'ROC': roc, 'MAROC': maroc})
        return roc, maroc
Example #11
0
    def download_basic_data(self, stock_code, start_date, end_date):
        data = su.get_hist_data(stock_code, start_date, end_date)

        if data is None:
            logger.warn(
                "No basic data is downloaded from 163 for stock code " +
                stock_code + ", try from tushare now...")
            print("No basic data is downloaded from 163 for stock code " +
                  stock_code + ", try from tushare now...")
            data = ts.get_hist_data(stock_code, start_date, end_date)
            if not 'turnover' in data.columns:
                logger.warn("Tushare data without turnover data")
                data.insert(loc=2, column="turnover", value=0)

        if data is not None:
            data = data.sort_index(ascending=True)[1:]
            data['code'] = pd.Series(stock_code, index=data.index)
        return data
Example #12
0
    def calculate(self,
                  stock_code,
                  date,
                  time_period1=9,
                  time_period2=3,
                  time_period3=3):
        kdj_k = 0.0
        kdj_d = 0.0
        kdj_j = 0.0
        data = su.get_basic_data(
            stock_code, date,
            max(time_period1, time_period2, time_period3) * 5)

        if data.shape[0] > time_period1:
            data['LOW_N'] = data['LOW'].rolling(window=time_period1).min()
            data['LOW_N'].fillna(value=data['LOW'].expanding().min(),
                                 inplace=True)
            data['HIGH_N'] = data['HIGH'].rolling(window=time_period1).max()
            data['HIGH_N'].fillna(value=data['HIGH'].expanding().max(),
                                  inplace=True)
            data['RSV'] = (data['CLOSE'] - data['LOW_N']) / (
                data['HIGH_N'] - data['LOW_N']) * 100
            data.sort_index(ascending=False, inplace=True)

            data['KDJ_K'] = data['RSV'].ewm(com=(time_period2 - 1)).mean()
            data['KDJ_D'] = data['KDJ_K'].ewm(com=(time_period2 - 1)).mean()
            data['KDJ_J'] = 3 * data['KDJ_K'] - 2 * data['KDJ_D']
            kdj_k = round(data['KDJ_K'].as_matrix()[-1], 3)
            kdj_d = round(data['KDJ_D'].as_matrix()[-1], 3)
            kdj_j = round(data['KDJ_J'].as_matrix()[-1], 3)

        if np.isnan(kdj_d) or np.isinf(kdj_d) or np.isneginf(kdj_d):
            kdj_d = 0.0
        if np.isnan(kdj_j) or np.isinf(kdj_j) or np.isneginf(kdj_j):
            kdj_j = 0.0
        if np.isnan(kdj_k) or np.isinf(kdj_k) or np.isneginf(kdj_k):
            kdj_k = 0.0
        self.save_tech_data(stock_code, date, {
            'KDJ_K': kdj_k,
            'KDJ_D': kdj_d,
            'KDJ_J': kdj_j
        })
        return kdj_k, kdj_d, kdj_j
Example #13
0
    def calculate(self, stock_code, date, time_period1=14, time_period2=6):
        wr_14 = 0.0
        wr_6 = 0.0
        data = su.get_basic_data(stock_code, date,
                                 time_period1 + 1).sort_index(ascending=False)

        if data.shape[0] >= time_period1 + 1:
            wr_14 = round(
                ta.WILLR(data['HIGH'].as_matrix(), data['LOW'].as_matrix(),
                         data['CLOSE'].as_matrix(), time_period1)[-1] * -1, 3)
            wr_6 = round(
                ta.WILLR(data['HIGH'].as_matrix(), data['LOW'].as_matrix(),
                         data['CLOSE'].as_matrix(), time_period2)[-1] * -1, 3)
        if np.isnan(wr_14) or np.isinf(wr_14) or np.isneginf(wr_14):
            wr_14 = 0.0
        if np.isnan(wr_6) or np.isinf(wr_6) or np.isneginf(wr_6):
            wr_6 = 0.0
        self.save_tech_data(stock_code, date, {'WR_14': wr_14, 'WR_6': wr_6})
        return wr_14, wr_6
Example #14
0
    def calculate(self, stock_code, date, time_period=26):
        ar = 0.0
        br = 0.0
        data = su.get_basic_data(stock_code, date,
                                 time_period + 1).sort_index(ascending=False)

        if data.shape[0] >= time_period + 1:
            ar = round((data['HIGH'][1:] - data['OPEN'][1:]).sum() /
                       (data['OPEN'][1:] - data['LOW'][1:]).sum() * 100, 3)
            data['P_CLOSE'] = data['CLOSE'].shift(1)
            data['BR_U'] = data['HIGH'][1:] - data['P_CLOSE'][1:]
            data['BR_D'] = data['P_CLOSE'][1:] - data['LOW'][1:]
            br = round(
                data[data['BR_U'] > 0]['BR_U'].sum() /
                data[data['BR_D'] > 0]['BR_D'].sum() * 100, 3)
        if np.isnan(ar) or np.isinf(ar) or np.isneginf(ar):
            ar = 0.0
        if np.isnan(br) or np.isinf(br) or np.isneginf(br):
            br = 0.0
        self.save_tech_data(stock_code, date, {'BRAR_BR': br, 'BRAR_AR': ar})
        return br, ar
Example #15
0
    def calculate(self, stock_code, date, time_period1=12, time_period2=6):
        max_time_period = max(time_period1, time_period2)
        mtm = 0.0
        mamtm = 0.0
        data = su.get_basic_data(stock_code, date, max_time_period *
                                 2).sort_index(ascending=False)

        if data.shape[0] >= max_time_period * 2:
            data['N_CLOSE'] = data['CLOSE'].shift(time_period1)
            data['MTM'] = data['CLOSE'] - data['N_CLOSE']
            mtm = round(data['MTM'].as_matrix()[-1], 3)
            mamtm = round(
                data['MTM'].as_matrix()[-6:].sum() / float(time_period2), 3)

        if np.isnan(mtm) or np.isinf(mtm) or np.isneginf(mtm):
            mtm = 0.0
        if np.isnan(mamtm) or np.isinf(mamtm) or np.isneginf(mamtm):
            mamtm = 0.0

        self.save_tech_data(stock_code, date, {'MTM': mtm, 'MAMTM': mamtm})
        return mtm, mamtm
Example #16
0
    def calculate(self, stock_code, date, time_period=24):
        vr = 0.0
        data = su.get_basic_data(stock_code, date, time_period + 1).sort_index(ascending=False)

        if data.shape[0] >= time_period:
            p_volume = 0.0
            n_volume = 0.0
            data['P_CLOSE'] = data['CLOSE'].shift(1)
            data['DIFF'] = data['CLOSE']-data['P_CLOSE']
            data['DIFF'].fillna(0, inplace=True)

            for each in data[1:].itertuples():
                if each.DIFF >= 0:
                    p_volume += each.VOLUME
                else:
                    n_volume += each.VOLUME
            vr = round(p_volume / n_volume * 100, 3)
        if np.isnan(vr) or np.isinf(vr) or np.isneginf(vr):
            vr = 0.0
        self.save_tech_data(stock_code, date, {'VR': vr})
        return vr
Example #17
0
            se.export()
            sc = StockCleaner(stock_code, end_date)
            sc.clean()
        else:
            logger.info("Only the first day of the year will do export and clean")
    else:
        logger.warn("No data found for stock code: " + stock_code)

if __name__ == '__main__':
    try:
        now = datetime.now()
        today = du.convertDateToString(now, '%Y-%m-%d')
        logger.debug("Today is: " + today)

        # today = '2017-09-29'
        if su.is_working_day(today):
            sbd = StockBasicData()
            stock_list = sbd.get_stock_list(today)

            pool = mp.Pool(processes=1)
            for stock in stock_list:
                stock_code = stock[0]
                last_updated_date = du.convertDateToString(stock[1], '%Y-%m-%d')
                pool.apply_async(create_process, (stock_code, last_updated_date, today))

            pool.close()
            pool.join()
            logger.info("All stocks information is downloaded")
        else:
            logging.info("Today is holiday, program will terminate...")
    except Exception, e:
Example #18
0
import os
from util import StockUtil, MysqlUtil
import shutil
import sys
import time


def move_directory(source, destination):
    for file_name in os.listdir(source):
        if file_name.endswith(".csv"):
            shutil.move(os.path.join(source, file_name), destination)


if __name__ == "__main__":
    stock_util = StockUtil()
    mysql_util = MysqlUtil(stock_util.conn, stock_util.cursor)

    stock_util.test = 0
    # stock_util.stagnate = 1 if len(sys.argv) >= 2 and sys.argv[1] == '--stagnate' else 0
    stock_util.stagnate = 0
    stock_util.get_new_stocks = 0

    if stock_util.get_new_stocks:
        if os.path.exists('GICS/backup'):
            shutil.rmtree('GICS/backup')
        os.makedirs('GICS/backup')
        move_directory('GICS', 'GICS/backup')
        stock_util.renew_categories_index_volume()
        shutil.rmtree('GICS/finished')

        sys.exit(0)