def gen_features(condition): k = utils.kline(condition["currency"], condition["t"], "", condition["size"]) points = pick(k, condition) box = [] for i in points: p = {} #p["currency"], p["t"], p["since"], p["size"], p["feature_size"] = condition["currency"], condition["t"], i - 1000 * utils.str2sec(condition["t"]) * condition["size"], condition["size"], 30 p["k"], p["point"], p["feature_size"] = k, i, condition["feature_size"] box.append(p) pool = ThreadPool(16) feas = pool.map(mfeature, box) pool.close() pool.join() conn = sqlite3.connect("%s/%s" % (config.db_dir, config.db_file)) c = conn.cursor() feature_items = [] for f in range(len(feas)): content = json.dumps(feas[f]) digest = "".join(hashlib.md5(content).hexdigest()) point = points[f] c.execute("select content from features where digest = ? or point = ?", (digest, point)) r = c.fetchone() if r != None: #print "digest(%s) or point(%s) exists." % (digest, point) #print json.loads(r[0])["v"] #print datetime.datetime.fromtimestamp(point/1000).strftime('%Y-%m-%d %H:%M:%S') continue currency = condition["currency"] size = condition["size"] n = condition["n"] increase = condition["increase"] feature_size = 30 feature_items.append( (digest, content, condition["currency"], condition["t"], condition["size"], condition["n"], condition["increase"], feature_size, point)) c.executemany("insert into features values (?, ?, ?, ?, ?, ?, ?, ?, ?)", feature_items) conn.commit() conn.close() return feature_items
def real_buy(): conn = sqlite3.connect("%s/%s" % (config.db_dir, config.db_file)) c = conn.cursor() k = {} digests = [i[0] for i in rank() if i[1] > 0] c.execute( "select digest, content, currency, t, size, n, increase, feature_size, point from features where digest in (%s)order by point" % (",".join('?' * len(digests)), ), digests) for f in c.fetchall(): digest, content, currency, t, size, n, increase, feature_size, point = f if "%s_%s_%s" % (currency, t, size) not in k: k["%s_%s_%s" % (currency, t, size)] = utils.kline( currency, t, "", size) print "Real comparing to %s @ %s %s %s" % ( digest, datetime.datetime.fromtimestamp( point / 1000).strftime('%Y-%m-%d %H:%M:%S'), increase, t) s = kline_similarity(k["%s_%s_%s" % (currency, t, size)], json.loads(content), feature_size) v = float(json.loads(content)["v"]) if not math.isnan(s) and s <= v: #if True: created_at = int(time.time() * 1000) c.execute( "select * from trade where digest = ? and created_at > ? and updated_at = 0", (digest, created_at - 1000 * n * utils.str2sec(t))) if len(c.fetchall()) > 0: continue print "Real match" r = trade(currency, "b", 1.0) print r if r != False: #ticker = utils.tick(currency) #buy, sell = float(ticker["ticker"]["buy"]), float(ticker["ticker"]["sell"]) print "real buy %s @ %s, amount %s" % (currency, r["price"], r['total_amount']) c.execute( "Insert into trade (created_at, updated_at, digest, buy, sell) values (?, 0, ?, ?, 0.0)", (r["trade_date"], digest, r["price"])) conn.commit() else: #print "Not Match" pass conn.close()
def train_buy(): conn = sqlite3.connect("%s/%s" % (config.db_dir, config.db_file)) c = conn.cursor() k = {} c.execute( "select digest, content, currency, t, size, n, increase, feature_size, point from features order by point" ) for f in c.fetchall(): digest, content, currency, t, size, n, increase, feature_size, point = f if "%s_%s_%s" % (currency, t, size) not in k: k["%s_%s_%s" % (currency, t, size)] = utils.kline( currency, t, "", size) print "Comparing to %s @ %s %s %s" % ( digest, datetime.datetime.fromtimestamp( point / 1000).strftime('%Y-%m-%d %H:%M:%S'), increase, t) s = kline_similarity(k["%s_%s_%s" % (currency, t, size)], json.loads(content), feature_size) v = float(json.loads(content)["v"]) if not math.isnan(s) and s <= v: created_at = int(time.time() * 1000) c.execute( "select * from training where digest = ? and created_at > ? and updated_at = 0", (digest, created_at - 1000 * n * utils.str2sec(t))) if len(c.fetchall()) > 0: continue print "Match" ticker = utils.tick(currency) buy, sell = float(ticker["ticker"]["buy"]), float( ticker["ticker"]["sell"]) print "buy %s @ %s" % (currency, sell) c.execute( "Insert into training (created_at, updated_at, digest, buy, sell) values (?, 0, ?, ?, 0.0)", (created_at, digest, sell)) conn.commit() else: #print "Not Match" pass conn.close()