Example #1
0
def save_or_update(k, symbol='TJCU'):
    data = models.KLineData()
    data.time = datetime.datetime.strptime(k['Date'], '%Y-%m-%d %H:%M:00')

    hour = data.time.hour
    min = data.time.minute

    if data.time.second > 0:
        print data.time

    if hour == 1 and min > 0:
        return

    if hour > 1 and hour < 9:
        return

    if hour == 11 and min > 30:
        return

    if hour > 11 and hour < 13:
        return

    if hour == 13 and min < 30:
        return

    if hour == 15 and min > 0:
        return

    if hour > 15 and hour < 21:
        return

    if hour == 21 and min == 0:
        return

    data.close = float(k['Close'])
    data.open = float(k['Open'])
    data.low = float(k['Low'])
    data.high = float(k['High'])

    table = 'lme_cu_1min'

    if symbol == config.FUTURES:
        table = 'ctp_cu_1min'

    last_data = count(k['Date'], table)
    if last_data:
        update(last_data, data, table)
    else:
        kline = models.KLineData(data.time,
                                 open=data.open,
                                 close=data.close,
                                 low=data.low,
                                 high=data.high)

        save(kline, table)
Example #2
0
def read_multi():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('121.41.58.181', 22536))
    conn_str = 'm=TJ;u=qq1277;p=abc898;EX_SIZE=1;'
    send_bytes = struct.pack('%ss' % len(conn_str), conn_str)
    print send_bytes

    sock.send(conn_str)
    kline = models.KLineData(datetime.datetime.fromtimestamp(0))
    while True:
        # print 'try to recv package head'
        total_length = utils.read_int(sock)
        # print 'package length is %s' % total_length
        if total_length == 16:
            #print utils.read_raw_data(sock, 16)
            break
        read_size = 0

        while read_size < total_length:
            zip_length = utils.read_int(sock)
            read_size += 4

            if zip_length > 16777216:
                # print 'Recv a text package, length is %s' % zip_length
                # data = utils.read_raw_data(sock, zip_length)
                # read_size += zip_length
                # print data
                break

            if zip_length <= 0:
                # print "invalid package length"
                continue

            ori_length = utils.read_int(sock)

            # print 'to recv zip pacakge, length is %s, ori length is %s' %  (zip_length, ori_length)
            data = utils.read_raw_data(sock, zip_length - 4)

            parsed_data = parse_zlib(data)
            if parsed_data.label.startswith('TJCU'):
                if (is_same_min(kline.time, parsed_data.time)):
                    update(kline, parsed_data)
                else:
                    kline = models.KLineData(parsed_data.time, open = parsed_data.new_price, close = parsed_data.new_price)
                    save(kline)
                #print 'parsed data is %s, name is %s' % (parsed_data, parsed_data.name)

            read_size += zip_length
Example #3
0
def read_data(queue):
    while True:
        tmp_data = []
        value = queue.get(True)
        if value.min != 0 or value.min != 5:
            time.sleep(1)
            continue
        tmp_data.append(value)
        while True:
            value = queue.get()
            if value.min < tmp_data[-1].min + 5:
                tmp_data.append(value)
                continue

            kline = models.KLineData(
                datetime.datetime.fromtimestamp(tmp_data[0].time).timetuple())
            kline.open = tmp_data[0].data
            kline.high = tmp_data[0].data
            kline.low = tmp_data[0].data
            kline.close = tmp_data[0].data
            for d in tmp_data:
                if d.data > kline.high:
                    kline.high = d.data
                if d.data < kline.low:
                    kline.low = d.data

            tmp_data = []
            tmp_data.append(value)
            print kline
            time.sleep(1)
Example #4
0
def save_or_update(k, symbol):
    data = models.RealTimeData()

    if 'Date' in k:
        data.time = datetime.datetime.strptime(k['Date'], '%Y-%m-%d %H:%M:%S')
    else:
        print k
        raise Exception("Can not parse kline save_or_update")

    hour = data.time.hour
    min = data.time.minute

    if hour == 1 and min > 0:
        return

    if hour > 1 and hour < 9:
        return

    if hour == 11 and min > 30:
        return

    if hour > 11 and hour < 13:
        return

    if hour == 13 and min < 30:
        return

    if hour == 15 and min > 0:
        return

    if hour > 15 and hour < 21:
        return

    if hour == 21 and min == 0:
        return

    data.new_price = float(k['Close'])
    data.open = float(k['Open'])
    data.low = float(k['Low'])
    data.high = float(k['High'])

    table = 'lme_cu_1min'

    if symbol == config.FUTURES:
        table = 'ctp_cu_1min'

    last_data = count(k['Date'], table)
    if last_data:
        update(last_data, data, table)
    else:
        kline = models.KLineData(data.time,
                                 open=data.open,
                                 close=data.new_price,
                                 low=data.low,
                                 high=data.high)
        save(kline, table)
Example #5
0
def count(date, table):
    sql = "select open, high, low, close from %s where date = '%s'" % (table,
                                                                       date)
    cur = conn.cursor()
    cur.execute(sql)
    result = cur.fetchone()
    if result:
        data = models.KLineData()
        data.time = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:00')
        data.open = result[0]
        data.high = result[1]
        data.low = result[2]
        data.close = result[3]
        return data
    return None