Esempio n. 1
0
def history(host, db, symbols, api, trade_days, freqs, max=1000):
    assert isinstance(api, DataApi)
    storage = MongodbBarAppender.config(
        host,
        db, [
            env.get_table_name(name, freq)
            for name, freq in product(symbols, freqs)
        ], ["datetime"],
        size=2**25,
        max=max)

    for symbol, freq in product(symbols, freqs):
        table = env.get_table_name(symbol, freq)
        last = storage.last(table)
        if last:
            dt = datetime.strptime(last["datetime"], "%Y%m%d %H:%M:%S")
            dates = find(trade_days, date2int(datetime.now()), date2int(dt))
            data = create(api, symbol, dates, freq, max)
        else:
            dates = find(trade_days, date2int(datetime.now()))
            data = create(api, symbol, dates, freq, max)
        for doc in data.to_dict("record"):
            doc["datetime"] = doc["datetime"].strftime("%Y%m%d %H:%M:%S")
            for key in ["volume", "init_volume", "last_volume", "flag"]:
                doc[key] = int(doc[key])
            storage.put(table, doc)

        logging.warning("reload | %s | %s", symbol, freq)
Esempio n. 2
0
 def handle(self, req):
     logging.warning("received bar req | %s", req)
     with self as api:
         try:
             data = get_bar(api, **req)
         except Exception as e:
             logging.error("require bar error | %s", e)
         else:
             self.send(env.get_table_name(req["symbol"], req["freq"]), data)
Esempio n. 3
0
    def handle_new(self, symbol, gran, data, time):
        name = env.get_table_name(symbol, gran)
        self.storage.finish(name)
        if env.is_trade_time(symbol, time):
            doc = transform(data)
            doc["flag"] = 0
            self.storage.put(name, doc)
            logging.warning("write | new bar | %s | %s | %s", symbol, gran,
                            data)

        end = data["datetime"]
        start = end - self.gran_diff[gran]
        self.send_bar_req(symbol,
                          start.hour * 10000 + start.minute * 100,
                          end.hour * 10000 + end.minute * 100,
                          freq=gran)
Esempio n. 4
0
def run():
    from itertools import product

    qp = TickPublisher(env.tick_subscribe, env.mapper)
    appender = MongodbBarAppender.config(
        env.mongodb_uri,
        env.mongodb_db, [
            env.get_table_name(symbol, gran)
            for symbol, gran in product(env.listen_symbol, env.listen_freq)
        ], ["datetime"],
        size=2**25,
        max=1000)

    bars = BarsInstance(
        {freq: GRANULARITIES[freq]
         for freq in env.listen_freq})
    handler = BarsStorage(appender, env.tick_publish, bars)
    core = CoreEngine()
    core.register_publisher("qp", qp)
    core.register_handler(TICK, handler)
    core.start()
Esempio n. 5
0
 def handle_upd(self, symbol, gran, data, time):
     name = env.get_table_name(symbol, gran)
     if env.is_trade_time(symbol, time):
         doc = transform(data)
         self.storage.put(name, doc)