コード例 #1
0
ファイル: select_stock_center.py プロジェクト: aopore/stock
def evaluation_stock(code):
    # 以上一交易日的收盘价为判断点
    
    try:
        #print 'evaluation', getSixDigitalStockCode(code), 'begin'
        date_end = datetime.date.today() + datetime.timedelta(-1)
        df = get_stock_k_line(code)
        if df is None:
            return
        else:
            nearest_date = df.head(1)['date']
            #print nearest_date
            if str(nearest_date.values[0]) != str(date_end):
                return
        if len(df.index) > 30:
        
            maStrategy = MACD_LIVE_TEST.MAStrategy(df=df)
            signal = maStrategy.select_Time_Mix()
            if signal > 0:
                #print stock.name, stock.current, (float(stock.current)-float(stock.close))/float(stock.close)*100, '%'
                print '>' * 5, 'Buy now!', getSixDigitalStockCode(code)
                stock_buy_list.append(getSixDigitalStockCode(code))
            elif signal < 0:     
                #print stock.name, stock.current, (float(stock.current)-float(stock.close))/float(stock.close)*100, '%'
                print '>' * 5, 'Sale now!', getSixDigitalStockCode(code)
                stock_sale_list.append(getSixDigitalStockCode(code))
    except Exception as e:
        print str(e)
コード例 #2
0
def evaluation_stock(code):
    # 以上一交易日的收盘价为判断点

    try:
        #print 'evaluation', getSixDigitalStockCode(code), 'begin'
        date_end = datetime.date.today() + datetime.timedelta(-1)
        df = get_stock_k_line(code)
        if df is None:
            return
        else:
            nearest_date = df.head(1)['date']
            #print nearest_date
            if str(nearest_date.values[0]) != str(date_end):
                return
        if len(df.index) > 30:

            maStrategy = MACD_LIVE_TEST.MAStrategy(df=df)
            signal = maStrategy.select_Time_Mix()
            if signal > 0:
                #print stock.name, stock.current, (float(stock.current)-float(stock.close))/float(stock.close)*100, '%'
                print '>' * 5, 'Buy now!', getSixDigitalStockCode(code)
                stock_buy_list.append(getSixDigitalStockCode(code))
            elif signal < 0:
                #print stock.name, stock.current, (float(stock.current)-float(stock.close))/float(stock.close)*100, '%'
                print '>' * 5, 'Sale now!', getSixDigitalStockCode(code)
                stock_sale_list.append(getSixDigitalStockCode(code))
    except Exception as e:
        print str(e)
コード例 #3
0
ファイル: get_data_center.py プロジェクト: hbwzhsh/stock
def get_stock_k_line(code, date_start='', date_end=datetime.date.today()):
    code = getSixDigitalStockCode(code)
    fileName = 'h_kline_' + str(code) + '.csv'
    
    df = None
    #如果存在则直接取
    if cm.DB_WAY == 'csv':
    
        if os.path.exists(cm.DownloadDir+fileName):
            df = pd.read_csv(cm.DownloadDir+fileName)
        # 不存在则立即下载
        #     else:
        #         df = download_stock_kline(code, date_start, date_end)
    elif cm.DB_WAY == 'redis':
        r = redis.Redis(host='127.0.0.1', port=6379)
        if len(date_start) == 0:
            date_start = r.hget(cm.PRE_STOCK_BASIC+code, cm.KEY_TimeToMarket)
        #date_start = '20150101'    
            date_start = datetime.datetime.strptime(str(date_start), "%Y%m%d")
        date_start = date_start.strftime("%Y-%m-%d") 
        date_end = date_end.strftime("%Y-%m-%d")
        
        keys = r.lrange(cm.INDEX_STOCK_KLINE+code, 0, -1)
        
        listSeries = []
        for key in keys:
            if key > date_start and key < date_end:
                dict = r.hgetall(cm.PRE_STOCK_KLINE+code +':'+ key)
                #_se = pd.Series(dict, index=[cm.KEY_DATE, cm.KEY_OPEN, cm.KEY_HIGH, cm.KEY_CLOSE, cm.KEY_LOW, cm.KEY_VOLUME,cm.KEY_AMOUNT]) 
                _se = pd.Series(dict, index=[cm.KEY_DATE, cm.KEY_CLOSE]) 
                listSeries.append(_se)
        df = pd.DataFrame(listSeries)       
    elif cm.DB_WAY == 'sqlite':
        
        #engine = create_engine('sqlite:///:memory:')
        engine = create_engine('sqlite:///..\stocks.db3')
        
        if len(date_start) == 0:
#             date_start = r.hget(cm.PRE_STOCK_BASIC+code, cm.KEY_TimeToMarket)
#             date_start = datetime.datetime.strptime(str(date_start), "%Y%m%d")
#             date_start = date_start.strftime("%Y-%m-%d") 
            date_start = '2015-01-01'
        date_end = date_end.strftime("%Y-%m-%d")
        
        try:
                   
            sql = "select %s, %s from %s where %s > '%s' and %s < '%s' order by %s asc" % \
                (cm.KEY_DATE, cm.KEY_CLOSE, cm.PRE_STOCK_KLINE_Sqlite+code, cm.KEY_DATE, date_start, \
                cm.KEY_DATE, date_end, cm.KEY_DATE)
            df = pd.read_sql_query(sql, engine)
            
        except Exception as e:
            print str(e)    
    return df     
コード例 #4
0
ファイル: data_to_sql.py プロジェクト: hbwzhsh/stock
def download_stock_kline_to_redis(code, date_start="", date_end=datetime.date.today()):
    code = util.getSixDigitalStockCode(code)

    try:

        if date_start == "":
            dates = r.lrange(cm.INDEX_STOCK_KLINE + code, 0, -1)
            if len(dates) > 0:
                nearstDate = dates[0]
                date = datetime.datetime.strptime(str(nearstDate), "%Y-%m-%d") + datetime.timedelta(1)
                date_start = date.strftime("%Y-%m-%d")
            else:
                se = get_stock_info(code)
                date_start = se["timeToMarket"]
                date = datetime.datetime.strptime(str(date_start), "%Y%m%d")
                date_start = date.strftime("%Y-%m-%d")
        date_end = date_end.strftime("%Y-%m-%d")

        print "download " + str(code) + " k-line >>>begin (", date_start + u" 到 " + date_end + ")"
        df_qfq = ts.get_h_data(str(code), start=date_start, end=date_end)  # 前复权

        if df_qfq is None:
            return None

        print "choose redis"

        # 查数据库大小
        print "\ndbsize:%s" % r.dbsize()
        # 看连接
        print "ping %s" % r.ping()

        for idx, row in df_qfq.iterrows():
            strDate = str(idx)[0:10]
            mapStock = {
                cm.KEY_DATE: strDate,
                cm.KEY_OPEN: float(row["open"]),
                cm.KEY_HIGH: float(row["high"]),
                cm.KEY_CLOSE: float(row["close"]),
                cm.KEY_VOLUME: float(row["volume"]),
                cm.KEY_AMOUNT: float(row["amount"]),
            }
            # 写入hash表
            r.hmset(cm.PRE_STOCK_KLINE + code + ":" + strDate, mapStock)

            # 索引
            r.rpush(cm.INDEX_STOCK_KLINE + code, strDate)

        print "\ndownload " + str(code) + " k-line to redis finish"

    except Exception as e:
        print str(e)

    return None
コード例 #5
0
ファイル: data_download.py プロジェクト: ongbe/stock
def download_stock_kline_to_redis(code, date_start='', date_end=datetime.date.today()):
    code = util.getSixDigitalStockCode(code)
    
    try:
        
        if date_start == '':
            dates = r.lrange(INDEX_STOCK_KLINE+code, 0, -1)
            if len(dates) > 0:
                nearstDate = dates[0]
                date = datetime.datetime.strptime(str(nearstDate), "%Y-%m-%d") + datetime.timedelta(1)
                date_start = date.strftime('%Y-%m-%d')
            else:
                se = get_stock_info(code) 
                date_start = se['timeToMarket']
                date = datetime.datetime.strptime(str(date_start), "%Y%m%d")
                date_start = date.strftime('%Y-%m-%d')
        date_end = date_end.strftime('%Y-%m-%d')  
        
        print 'download ' + str(code) + ' k-line >>>begin (', date_start+u' 到 '+date_end+')'
        df_qfq = ts.get_h_data(str(code), start=date_start, end=date_end) # 前复权
        
        if df_qfq is None:
            return None
        
        print 'choose redis'
        
        # 查数据库大小
        print '\ndbsize:%s' %r.dbsize()
        # 看连接
        print 'ping %s' %r.ping()
        
        for idx, row in df_qfq.iterrows():
            strDate = str(idx)[0:10]
            mapStock =  {KEY_DATE:strDate,KEY_OPEN:float(row['open']),KEY_HIGH:float(row['high']),\
                             KEY_CLOSE:float(row['close']), KEY_VOLUME:float(row['volume']),\
                              KEY_AMOUNT:float(row['amount'])}
            # 写入hash表
            r.hmset(PRE_STOCK_KLINE+code + ':' + strDate, mapStock)
            
            #索引
            r.rpush(INDEX_STOCK_KLINE+code, strDate)
             
        
       
        
        print '\ndownload ' + str(code) +  ' k-line to redis finish'
            
        
    except Exception as e:
        print str(e)        
    
        
    return None
コード例 #6
0
ファイル: data_download.py プロジェクト: xyicheng/stock
def download_stock_quotes(code, date_start='', date_end=str(datetime.date.today())):
    code = util.getSixDigitalStockCode(code)
    try:
        if date_start == '':
            date = datetime.datetime.today().date() + datetime.timedelta(-365*3) 
            date_start = str(date)
          
        dateStart = datetime.datetime.strptime(str(date_start), "%Y-%m-%d")   
                
        for i in range(du.diff_day(date_start, date_end)):
            date = dateStart + datetime.timedelta(i)
            strDate = date.strftime("%Y-%m-%d")
            df = ts.get_tick_data(code, strDate)
            print df
    except Exception as e:
        print str(e)        
コード例 #7
0
def download_stock_quotes(code, date_start='', date_end=str(datetime.date.today())):
    code = util.getSixDigitalStockCode(code)
    try:
        if date_start == '':
            date = datetime.datetime.today().date() + datetime.timedelta(-365*3) 
            date_start = str(date)
          
        dateStart = datetime.datetime.strptime(str(date_start), "%Y-%m-%d")   
                
        for i in range(du.diff_day(date_start, date_end)):
            date = dateStart + datetime.timedelta(i)
            strDate = date.strftime("%Y-%m-%d")
            df = ts.get_tick_data(code, strDate)
            print df
    except Exception as e:
        print str(e)        
コード例 #8
0
ファイル: data_download.py プロジェクト: ongbe/stock
def download_stock_kline_to_sql(code, date_start='', date_end=datetime.date.today()):
    code = util.getSixDigitalStockCode(code)
    
    try:
        
        if date_start == '':
            try:
                sql = 'select %s from %s order by %s desc limit 1' % (KEY_DATE, PRE_STOCK_KLINE+code, KEY_DATE)

                df = pd.read_sql_query(sql, engine)
                dates = df[KEY_DATE].get_values()
            except Exception, e:
                print str(e)
                dates = ''
            
            if len(dates) > 0:
                #print dates
                nearstDate = str(dates[0])[:10]
                date = datetime.datetime.strptime(str(nearstDate), "%Y-%m-%d") + datetime.timedelta(1)
                date_start = date.strftime('%Y-%m-%d')
            else:
                se = get_stock_info(code) 
                date_start = se['timeToMarket']
                date = datetime.datetime.strptime(str(date_start), "%Y%m%d")
                date_start = date.strftime('%Y-%m-%d')
        date_end = date_end.strftime('%Y-%m-%d')

        if date_start >= date_end:
            return
                
        print 'download ' + str(code) + ' k-line >>>begin (', date_start+u' 到 '+date_end+')'
        
        df_qfq = download_kline_source_select(code, date_start, date_end)
        
        if df_qfq is None:
            return None
        print df_qfq[-10:]
        
        print 'choose  mysql'
        df_qfq.to_sql(PRE_STOCK_KLINE+code, engine,if_exists='append')
        
        print '\ndownload ' + str(code) +  ' k-line to mysql finish'
コード例 #9
0
ファイル: data_get.py プロジェクト: ongbe/stock
def get_stock_k_line(code, date_start='', date_end='', all_columns = False):
    code = getSixDigitalStockCode(code)

    if len(date_end) == 0:
        date_end=datetime.date.today().strftime("%Y-%m-%d")


    #DB_WAY == 'mysql':
    try:
        if len(date_start) == 0:
            sql = "select * from %s where %s <= '%s' order by %s asc" % \
                (PRE_STOCK_KLINE+code, KEY_DATE, date_end, KEY_DATE)
        else:
            sql = "select * from %s where %s >= '%s' and %s <= '%s' order by %s asc" % \
                (PRE_STOCK_KLINE+code, KEY_DATE, date_start, KEY_DATE, date_end, KEY_DATE)

        df = pd.read_sql_query(sql, engine)
        return df
    except Exception as e:
        print str(e)
        return None
コード例 #10
0
ファイル: data_to_sql.py プロジェクト: hbwzhsh/stock
def download_stock_kline_to_sqlite(code, date_start="", date_end=datetime.date.today()):
    code = util.getSixDigitalStockCode(code)

    try:

        if date_start == "":
            sql = "select %s from %s order by %s desc" % (cm.KEY_DATE, cm.PRE_STOCK_KLINE_Sqlite + code, cm.KEY_DATE)
            df = pd.read_sql_query(sql, engine)
            dates = df[cm.KEY_DATE].get_values()

            if len(dates) > 0:
                nearstDate = dates[0][:10]
                date = datetime.datetime.strptime(str(nearstDate), "%Y-%m-%d") + datetime.timedelta(1)
                date_start = date.strftime("%Y-%m-%d")
            else:
                se = get_stock_info(code)
                date_start = se["timeToMarket"]
                date = datetime.datetime.strptime(str(date_start), "%Y%m%d")
                date_start = date.strftime("%Y-%m-%d")
        date_end = date_end.strftime("%Y-%m-%d")

        #         if date_start == '2015-07-11':
        #             return;

        print "download " + str(code) + " k-line >>>begin (", date_start + u" 到 " + date_end + ")"

        df_qfq = download_kline_source_select(False, code, date_start, date_end)

        if df_qfq is None:
            return None

        print "choose sqlite"
        df_qfq.to_sql(cm.PRE_STOCK_KLINE_Sqlite + code, engine, index=False, if_exists="append")

        print "\ndownload " + str(code) + " k-line to sqlite finish"

    except Exception as e:
        print str(e)

    return None
コード例 #11
0
ファイル: data_to_sql.py プロジェクト: hbwzhsh/stock
def download_stock_basic_info():

    try:
        df = ts.get_stock_basics()
        # 直接保存
        if cm.DB_WAY == "csv":
            print "choose csv"
            df.to_csv(cm.DownloadDir + cm.TABLE_STOCKS_BASIC + ".csv")
            print "download csv finish"

        elif cm.DB_WAY == "mysql":
            print "choose mysql"
            engineString = "mysql://" + cm.DB_USER + ":" + cm.DB_PWD + "@127.0.0.1/" + cm.DB_NAME + "?charset=utf8"
            print engineString
            engine = create_engine(engineString)

            df.to_sql(cm.TABLE_STOCKS_BASIC, engine, if_exists="replace")

            sql = "select * from " + cm.TABLE_STOCKS_BASIC + ' where code = "600000"'
            dfRead = pd.read_sql(sql, engine)
            print dfRead

        elif cm.DB_WAY == "redis":
            print "choose redis"

            # 查数据库大小
            print "\ndbsize:%s" % r.dbsize()

            # 看连接
            print "ping %s" % r.ping()

            for idx, row in df.iterrows():
                print idx, row
                mapStock = {
                    cm.KEY_CODE: idx,
                    cm.KEY_NAME: row["name"],
                    cm.KEY_INDUSTRY: row["industry"],
                    cm.KEY_AREA: row["area"],
                    cm.KEY_TimeToMarket: row["timeToMarket"],
                }
                # 写入hash表
                r.hmset(cm.PRE_STOCK_BASIC + idx, mapStock)

                # 索引
                r.sadd(cm.INDEX_STOCK_BASIC, idx)
                # r.rpush(cm.INDEX_STOCK_BASIC, idx)
                # print r.hgetall(PRE_BASIC+idx)

        elif cm.DB_WAY == "sqlite":
            con = sqlite3.connect("..\stocks.db3")
            con.text_factory = str
            cur = con.cursor()
            # 创建基本信息表
            sql_create_basic = (
                "create table if not exists %s ( %s TEXT PRIMARY KEY, %s TEXT, %s TEXT \
                    , %s TEXT, %s REAL, %s REAL, %s REAL, %s TEXT)"
                % (
                    cm.INDEX_STOCK_BASIC,
                    cm.KEY_CODE,
                    cm.KEY_NAME,
                    cm.KEY_INDUSTRY,
                    cm.KEY_AREA,
                    cm.KEY_PE,
                    cm.KEY_EPS,
                    cm.KEY_PB,
                    cm.KEY_TimeToMarket,
                )
            )
            cur.execute(sql_create_basic)

            # 清空表
            cur.execute("delete from %s" % (cm.INDEX_STOCK_BASIC))

            # 转换基本信息表
            df = ts.get_stock_basics()
            for idx, row in df.iterrows():
                # print idx, row
                code = util.getSixDigitalStockCode(idx)
                sql_insert = "insert into %s (%s, %s, %s, %s, %s, %s, %s, %s) values(?,?,?,?,?,?,?,?)" % (
                    cm.INDEX_STOCK_BASIC,
                    cm.KEY_CODE,
                    cm.KEY_NAME,
                    cm.KEY_INDUSTRY,
                    cm.KEY_AREA,
                    cm.KEY_PE,
                    cm.KEY_EPS,
                    cm.KEY_PB,
                    cm.KEY_TimeToMarket,
                )
                cur.execute(
                    sql_insert,
                    (
                        code,
                        row["name"],
                        row["industry"],
                        row["area"],
                        float(row["pe"]),
                        float(row["esp"]),
                        float(row["pb"]),
                        row["timeToMarket"],
                    ),
                )

            con.commit()
            # 关闭数据库
            con.close()
    except Exception as e:
        print str(e)
コード例 #12
0
ファイル: data_to_sql.py プロジェクト: hbwzhsh/stock
def download_stock_kline(code, date_start="", date_end=datetime.date.today()):
    code = util.getSixDigitalStockCode(code)

    try:
        fileName = "h_kline_" + str(code) + ".csv"

        writeMode = "w"
        if os.path.exists(cm.DownloadDir + fileName):
            # print (">>exist:" + code)
            df = pd.DataFrame.from_csv(path=cm.DownloadDir + fileName)

            se = df.head(1).index  # 取已有文件的最近日期
            dateNew = se[0] + datetime.timedelta(1)
            date_start = dateNew.strftime("%Y-%m-%d")
            # print date_start
            writeMode = "a"

        if date_start == "":
            se = get_stock_info(code)
            date_start = se["timeToMarket"]
            date = datetime.datetime.strptime(str(date_start), "%Y%m%d")
            date_start = date.strftime("%Y-%m-%d")
        date_end = date_end.strftime("%Y-%m-%d")
        date_end = "2015-06-30"
        # 已经是最新的数据
        if date_start >= date_end:
            df = pd.read_csv(cm.DownloadDir + fileName)
            return df

        print "download " + str(code) + " k-line >>>begin (", date_start + u" 到 " + date_end + ")"
        df_qfq = ts.get_h_data(str(code), start=date_start, end=date_end)  # 前复权
        df_nfq = ts.get_h_data(str(code), start=date_start, end=date_end, autype=None)  # 不复权
        df_hfq = ts.get_h_data(str(code), start=date_start, end=date_end, autype="hfq")  # 后复权

        if df_qfq is None or df_nfq is None or df_hfq is None:
            return None

        df_qfq["open_no_fq"] = df_nfq["open"]
        df_qfq["high_no_fq"] = df_nfq["high"]
        df_qfq["close_no_fq"] = df_nfq["close"]
        df_qfq["low_no_fq"] = df_nfq["low"]
        df_qfq["open_hfq"] = df_hfq["open"]
        df_qfq["high_hfq"] = df_hfq["high"]
        df_qfq["close_hfq"] = df_hfq["close"]
        df_qfq["low_hfq"] = df_hfq["low"]

        if writeMode == "w":
            df_qfq.to_csv(cm.DownloadDir + fileName)
        else:

            df_old = pd.DataFrame.from_csv(cm.DownloadDir + fileName)

            # 按日期由远及近
            df_old = df_old.reindex(df_old.index[::-1])
            df_qfq = df_qfq.reindex(df_qfq.index[::-1])

            df_new = df_old.append(df_qfq)
            # print df_new

            # 按日期由近及远
            df_new = df_new.reindex(df_new.index[::-1])
            df_new.to_csv(cm.DownloadDir + fileName)
            # df_qfq = df_new

            print "\ndownload " + str(code) + " k-line to csv finish"
            return pd.read_csv(cm.DownloadDir + fileName)

    except Exception as e:
        print str(e)

    return None
コード例 #13
0
ファイル: data_download.py プロジェクト: ongbe/stock
def download_stock_kline_csv(code, date_start='', date_end=datetime.date.today()):
    code = util.getSixDigitalStockCode(code)
    
    try:
        fileName = 'h_kline_' + str(code) + '.csv'
        
        writeMode = 'w'
        if os.path.exists(DownloadDir+fileName):
            #print (">>exist:" + code)
            df = pd.DataFrame.from_csv(path=DownloadDir+fileName)
            
            se = df.head(1).index #取已有文件的最近日期
            dateNew = se[0] + datetime.timedelta(1)
            date_start = dateNew.strftime("%Y-%m-%d")
            #print date_start
            writeMode = 'a'
        
        if date_start == '':
            se = get_stock_info(code)
            date_start = se['timeToMarket'] 
            date = datetime.datetime.strptime(str(date_start), "%Y%m%d")
            date_start = date.strftime('%Y-%m-%d')
        date_end = date_end.strftime('%Y-%m-%d')  
        date_end = '2015-06-30'
        # 已经是最新的数据
        if date_start >= date_end:
            df = pd.read_csv(DownloadDir+fileName)
            return df
        
        print 'download ' + str(code) + ' k-line >>>begin (', date_start+u' 到 '+date_end+')'
        df_qfq = ts.get_h_data(str(code), start=date_start, end=date_end) # 前复权
        df_nfq = ts.get_h_data(str(code), start=date_start, end=date_end, autype=None) # 不复权
        df_hfq = ts.get_h_data(str(code), start=date_start, end=date_end, autype='hfq') # 后复权
        
        if df_qfq is None or df_nfq is None or df_hfq is None:
            return None
        
        df_qfq['open_no_fq'] = df_nfq['open']
        df_qfq['high_no_fq'] = df_nfq['high']
        df_qfq['close_no_fq'] = df_nfq['close']
        df_qfq['low_no_fq'] = df_nfq['low']
        df_qfq['open_hfq']=df_hfq['open']
        df_qfq['high_hfq']=df_hfq['high']
        df_qfq['close_hfq']=df_hfq['close']
        df_qfq['low_hfq']=df_hfq['low']
             
        if writeMode == 'w':
            df_qfq.to_csv(DownloadDir+fileName)
        else:
            
            df_old = pd.DataFrame.from_csv(DownloadDir + fileName)
            
            # 按日期由远及近
            df_old = df_old.reindex(df_old.index[::-1])
            df_qfq = df_qfq.reindex(df_qfq.index[::-1])
            
            df_new = df_old.append(df_qfq)
            #print df_new
            
            # 按日期由近及远
            df_new = df_new.reindex(df_new.index[::-1])
            df_new.to_csv(DownloadDir+fileName)
            #df_qfq = df_new
        
            print '\ndownload ' + str(code) +  ' k-line to csv finish'
            return pd.read_csv(DownloadDir+fileName)
        
            
        
    except Exception as e:
        print str(e)

    return None