Beispiel #1
0
def update_trade_db(db_cursor, cx, symbol, tag):
    """ 更新交易数据库 """
    # 新建数据库
    if tag == "new":
        current_time = (datetime.datetime.now() -
                        datetime.timedelta(days=1)).strftime("%Y%m%d")
        one_year_ago = (datetime.datetime.now() -
                        datetime.timedelta(days=300)).strftime("%Y%m%d")
        data = stock.get_historical_prices(symbol, one_year_ago, current_time)
        for item in data:
            s_date = item[0]
            s_open = item[1]
            s_high = item[2]
            s_low = item[3]
            s_close = item[4]
            s_volume = item[5]
            if not s_volume.isdigit(): continue
            sql_cmd = 'insert into stock values(NULL,"%s","%s","%s","%s","%s",%s)' % (
                s_date, s_open, s_high, s_low, s_close, s_volume)
            #print "sql_cmd = " + sql_cmd
            db_cursor.execute(sql_cmd)
            cx.commit()
    elif tag == "old":
        # 判断最大日期数
        sql_cmd = "select max(s_date) from stock"
        db_cursor.execute(sql_cmd)
        rs = db_cursor.fetchone()
        max_date = rs[0]
        if max_date:
            current_time = (datetime.datetime.now() -
                            datetime.timedelta(days=1)).strftime("%Y%m%d")
            last_update_time = datetime.datetime.strptime(max_date, "%Y-%m-%d")
            want_update_time = (last_update_time +
                                datetime.timedelta(days=1)).strftime("%Y%m%d")
        else:
            current_time = (datetime.datetime.now() -
                            datetime.timedelta(days=1)).strftime("%Y%m%d")
            want_update_time = (
                datetime.datetime.now() -
                datetime.timedelta(days=300)).strftime("%Y%m%d")

        #want_update_time = last_update_time.strftime("%Y%m%d")
        if want_update_time <= current_time:
            data = stock.get_historical_prices(symbol, want_update_time,
                                               current_time)
            if data:
                for item in data:
                    s_date = item[0]
                    s_open = item[1]
                    s_high = item[2]
                    s_low = item[3]
                    s_close = item[4]
                    s_volume = item[5]
                    if not s_volume.isdigit(): continue
                    sql_cmd = 'insert into stock values(NULL,"%s","%s","%s","%s","%s",%s)' % (
                        s_date, s_open, s_high, s_low, s_close, s_volume)
                    #print "sql_cmd = " + sql_cmd
                    db_cursor.execute(sql_cmd)
                    cx.commit()
Beispiel #2
0
def update_trade_db(db_cursor,cx,symbol,tag):
    """ 更新历史交易数据库 """
    # 新建数据库
    if tag =="new":
        #current_time = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y%m%d")
        current_time = datetime.datetime.now().strftime("%Y%m%d")
        one_year_ago = (datetime.datetime.now() - datetime.timedelta(days=300)).strftime("%Y%m%d")
        data = stock.get_historical_prices(symbol,one_year_ago,current_time)
        if not data:
            return 
        for item in data:
            s_date = item[0]
            s_open = item[1]
            s_high = item[2]
            s_low = item[3]
            s_close = item[4]
            s_volume = item[5]
            if not s_volume.isdigit(): continue
            sql_cmd = 'insert into stock values(NULL,"%s","%s","%s","%s","%s",%s)' % (s_date,s_open,s_high,s_low,s_close,s_volume)
            #print "sql_cmd = " + sql_cmd
            db_cursor.execute(sql_cmd)
            cx.commit()
    elif tag =="old":
        # 判断最大日期数
        sql_cmd = "select max(s_date) from stock" 
        db_cursor.execute(sql_cmd)
        rs = db_cursor.fetchone()
        max_date = rs[0]
        if max_date:
            #current_time = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y%m%d")
            current_time = datetime.datetime.now().strftime("%Y%m%d")
            last_update_time = datetime.datetime.strptime(max_date,"%Y-%m-%d")
            want_update_time = (last_update_time + datetime.timedelta(days=1)).strftime("%Y%m%d")
        else:
            #current_time = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y%m%d")
            current_time = datetime.datetime.now().strftime("%Y%m%d")
            want_update_time = (datetime.datetime.now() - datetime.timedelta(days=300)).strftime("%Y%m%d")
    
        #want_update_time = last_update_time.strftime("%Y%m%d")
        if datetime.datetime.now().weekday() in [0,6]: return  
        #if want_update_time <= current_time:
        if want_update_time < current_time:
            data = stock.get_historical_prices(symbol,want_update_time,current_time)
            if data:
                for item in data:
                    s_date = item[0]
                    s_open = item[1]
                    s_high = item[2]
                    s_low = item[3]
                    s_close = item[4]
                    s_volume = item[5]
                    if not s_volume.isdigit(): continue
                    sql_cmd = 'insert into stock values(NULL,"%s","%s","%s","%s","%s",%s)' % (s_date,s_open,s_high,s_low,s_close,s_volume)
                    #print "sql_cmd = " + sql_cmd
                    db_cursor.execute(sql_cmd)
                    cx.commit()
def update_trade_db(db_cursor, cx, symbol, tag):
    """ 更新股票历史交易数据 """
    # 新建数据库
    if tag == "new":
        current_time = datetime.datetime.now().strftime("%Y%m%d")
        one_year_ago = (datetime.datetime.now() - datetime.timedelta(days=3000)).strftime("%Y%m%d")
        data = stock.get_historical_prices(symbol, one_year_ago, current_time)
        last_close_price = 0
        if not data:
            print "update_trade_db Error : NO HISTORY DATA,PLEASE CHECK,SYMBOL = %s" % (symbol)
            return
        for item in data:
            s_date = item[0]
            s_open = item[1]
            s_high = item[2]
            s_low = item[3]
            s_close = item[4]
            # s_close = item[6]   # adj close 拆股或者合股
            s_volume = item[5]
            if not s_volume.isdigit():
                continue
            if float(s_close) == 0:
                if symbol.find(".") != -1:
                    (real_symbol, suffix) = symbol.split(".")
                    if suffix.lower() in ["ss", "sz"]:
                        s_open = last_close_price
                        s_close = last_close_price
                        s_high = last_close_price
                        s_low = last_close_price
            else:
                last_close_price = s_close

            sql_cmd = 'insert into stock values(NULL,"%s","%s","%s","%s","%s",%s)' % (
                s_date,
                s_open,
                s_high,
                s_low,
                s_close,
                s_volume,
            )
            db_cursor.execute(sql_cmd)
        cx.commit()
    elif tag == "old":
        # 判断最大日期数
        sql_cmd = "select max(s_date),s_close from stock"
        db_cursor.execute(sql_cmd)
        rs = db_cursor.fetchone()
        max_date = rs[0]
        last_close_price = rs[1]
        if max_date:
            # current_time = (datetime.datetime.now() - datetime.timedelta(days=2)).strftime("%Y%m%d")
            current_time = datetime.datetime.now().strftime("%Y%m%d")
            last_update_time = datetime.datetime.strptime(max_date, "%Y-%m-%d")
            want_update_time = (last_update_time + datetime.timedelta(days=1)).strftime("%Y%m%d")
        else:
            # current_time = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y%m%d")
            current_time = datetime.datetime.now().strftime("%Y%m%d")
            want_update_time = (datetime.datetime.now() - datetime.timedelta(days=3000)).strftime("%Y%m%d")

        # want_update_time = last_update_time.strftime("%Y%m%d")
        # if datetime.datetime.now().weekday() in [0,6]: return
        # if want_update_time <= current_time:
        if want_update_time < current_time:
            data = stock.get_historical_prices(symbol, want_update_time, current_time)
            if data:
                for item in data:
                    s_date = item[0]
                    s_open = item[1]
                    s_high = item[2]
                    s_low = item[3]
                    s_close = item[4]
                    # s_close = item[6]   # adj close 拆股或者合股
                    s_volume = item[5]
                    if not s_volume.isdigit():
                        continue
                    if float(s_close) == 0:
                        if symbol.find(".") != -1:
                            (real_symbol, suffix) = symbol.split(".")
                            if suffix.lower() in ["ss", "sz"]:
                                s_open = last_close_price
                                s_close = last_close_price
                                s_high = last_close_price
                                s_low = last_close_price
                    sql_cmd = 'insert into stock values(NULL,"%s","%s","%s","%s","%s",%s)' % (
                        s_date,
                        s_open,
                        s_high,
                        s_low,
                        s_close,
                        s_volume,
                    )
                    # print "sql_cmd = " + sql_cmd
                    db_cursor.execute(sql_cmd)
                cx.commit()
Beispiel #4
0
def update_trade_db(db_cursor,cx,symbol,tag):
    """ 更新股票历史交易数据 """
    # 新建数据库
    if tag =="new":
        current_time = datetime.datetime.now().strftime("%Y%m%d")
        one_year_ago = (datetime.datetime.now() - datetime.timedelta(days=3000)).strftime("%Y%m%d")
        data = stock.get_historical_prices(symbol,one_year_ago,current_time)
        last_close_price = 0
        if not data:
            print "update_trade_db Error : NO HISTORY DATA,PLEASE CHECK,SYMBOL = %s" % (symbol)
            return 
        for item in data:
            s_date = item[0]
            s_open = item[1]
            s_high = item[2]
            s_low = item[3]
            s_close = item[4]
            #s_close = item[6]   # adj close 拆股或者合股
            s_volume = item[5]
            if not s_volume.isdigit(): continue
            if float(s_close) == 0:
                if symbol.find(".") != -1:
                    (real_symbol,suffix) = symbol.split(".")
                    if suffix.lower() in ['ss','sz']:
                        s_open = last_close_price
                        s_close = last_close_price
                        s_high = last_close_price
                        s_low = last_close_price
            else:
                last_close_price = s_close

            sql_cmd = 'insert into stock values(NULL,"%s","%s","%s","%s","%s",%s)' % (s_date,s_open,s_high,s_low,s_close,s_volume)
            db_cursor.execute(sql_cmd)
        cx.commit()
    elif tag =="old":
        # 判断最大日期数
        sql_cmd = "select max(s_date),s_close from stock" 
        db_cursor.execute(sql_cmd)
        rs = db_cursor.fetchone()
        max_date = rs[0]
        last_close_price = rs[1]
        if max_date:
            #current_time = (datetime.datetime.now() - datetime.timedelta(days=2)).strftime("%Y%m%d")
            current_time = datetime.datetime.now().strftime("%Y%m%d")
            last_update_time = datetime.datetime.strptime(max_date,"%Y-%m-%d")
            want_update_time = (last_update_time + datetime.timedelta(days=1)).strftime("%Y%m%d")
        else:
            #current_time = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y%m%d")
            current_time = datetime.datetime.now().strftime("%Y%m%d")
            want_update_time = (datetime.datetime.now() - datetime.timedelta(days=3000)).strftime("%Y%m%d")
    
        #want_update_time = last_update_time.strftime("%Y%m%d")
        #if datetime.datetime.now().weekday() in [0,6]: return  
        #if want_update_time <= current_time:
        if want_update_time < current_time:
            data = stock.get_historical_prices(symbol,want_update_time,current_time)
            if data:
                for item in data:
                    s_date = item[0]
                    s_open = item[1]
                    s_high = item[2]
                    s_low = item[3]
                    s_close = item[4]
                    #s_close = item[6]   # adj close 拆股或者合股
                    s_volume = item[5]
                    if not s_volume.isdigit(): continue
                    if float(s_close) == 0:
                        if symbol.find(".") != -1:
                            (real_symbol,suffix) = symbol.split(".")
                            if suffix.lower() in ['ss','sz']:
                                s_open = last_close_price
                                s_close = last_close_price
                                s_high = last_close_price
                                s_low = last_close_price
                    sql_cmd = 'insert into stock values(NULL,"%s","%s","%s","%s","%s",%s)' % (s_date,s_open,s_high,s_low,s_close,s_volume)
                    #print "sql_cmd = " + sql_cmd
                    db_cursor.execute(sql_cmd)
                cx.commit()