Ejemplo n.º 1
0
void DbService::batchWriteTicks()
{
    g_sm->checkCurrentOn(ServiceMgr::DB);

    if( tickCount_ == 0 ){
        return;
    }

    leveldb::WriteOptions options;
    leveldb::WriteBatch batch;
    for (int i = 0; i < tickCount_; i++) {
        void* curTick = tickArray[i].curTick;
        void* preTick = tickArray[i].preTick;

        BfTickData bfItem;
        CtpUtils::translateTick(curTick, preTick, &bfItem);

        // tick里面的exchange不一定有=
        QString exchange = bfItem.exchange().c_str();
        if (exchange.trimmed().length() == 0) {
            void* contract = g_sm->ctpMgr()->getContract(bfItem.symbol().c_str());
            exchange = CtpUtils::getExchangeFromContract(contract);
            bfItem.set_exchange(exchange.toStdString());
        }

        // key: tick.exchange.symbol.actiondata.ticktime
        std::string key = QString().sprintf("tick.%s.%s.%s.%s", bfItem.exchange().c_str(), bfItem.symbol().c_str(), bfItem.actiondate().c_str(), bfItem.ticktime().c_str()).toStdString();
        std::string val = bfItem.SerializeAsString();

        batch.Put(key, val);
    }
    db_->Write(options, &batch);

    tickCount_ = 0;
}
Ejemplo n.º 2
0
void DbService::insertTick(const BfTickData& bfItem)
{
    //BfDebug(__FUNCTION__);
    g_sm->checkCurrentOn(ServiceMgr::DB);

    if (bfItem.symbol().length() == 0 || bfItem.exchange().length() == 0 || bfItem.actiondate().length() == 0 || bfItem.ticktime().length() == 0) {
        BfDebug("invalid tick,ignore");
        return;
    }

    leveldb::WriteOptions options;
    leveldb::WriteBatch batch;
    std::string key, val;

    if (1) {
        // key: tick-symbol-exchange-actiondate-ticktime
        key = QString().sprintf("tick-%s-%s-%s-%s", bfItem.symbol().c_str(), bfItem.exchange().c_str(), bfItem.actiondate().c_str(), bfItem.ticktime().c_str()).toStdString();
        bool ok = bfItem.SerializeToString(&val);
        if (!ok) {
            qFatal("SerializeToString fail");
        }
        batch.Put(key, val);
    }

    db_->Write(options, &batch);
}
Ejemplo n.º 3
0
void PushService::onGotTick(void* curTick, void* preTick)
{
    g_sm->checkCurrentOn(ServiceMgr::PUSH);

    BfTickData data;
    CtpUtils::translateTick(curTick, preTick, &data);

    // tick里面的exchange不一定有=
    QString exchange = data.exchange().c_str();
    if (exchange.trimmed().length() == 0) {
        void* contract = g_sm->gatewayMgr()->getContract(data.symbol().c_str());
        exchange = CtpUtils::getExchangeFromContract(contract);
        data.set_exchange(exchange.toStdString());
    }

    for (auto client : clients_) {
        if (client->tickHandler() && client->subscribled(data.symbol(), data.exchange())) {
            client->OnTick(data);
        }
    }
}
Ejemplo n.º 4
0
void TickForm::onGotTick(void* curTick, void* preTick)
{
    BfTickData bfTick;
    CtpUtils::translateTick(curTick, preTick, &bfTick);

    QVariantMap vItem;
    vItem.insert("symbol", bfTick.symbol().c_str());
    // tick里面的exchange不一定有=
    QString exchange = bfTick.exchange().c_str();
    if (exchange.trimmed().length() == 0) {
        void* contract = g_sm->ctpMgr()->getContract(bfTick.symbol().c_str());
        exchange = CtpUtils::getExchangeFromContract(contract);
    }
    vItem.insert("exchange", exchange);
    vItem.insert("actionDate", bfTick.actiondate().c_str());
    vItem.insert("tickTime", bfTick.ticktime().c_str());
    vItem.insert("lastPrice", bfTick.lastprice());
    vItem.insert("volume", bfTick.volume());
    vItem.insert("openInterest", bfTick.openinterest());
    vItem.insert("lastVolume", bfTick.lastvolume());

    vItem.insert("bidPrice1", bfTick.bidprice1());
    vItem.insert("askPrice1", bfTick.askprice1());
    vItem.insert("bidVolume1", bfTick.bidvolume1());
    vItem.insert("askVolume1", bfTick.askvolume1());

    vItem.insert("openPrice", bfTick.openprice());
    vItem.insert("highPrice", bfTick.highprice());
    vItem.insert("lowPrice", bfTick.lowprice());
    vItem.insert("preClosePrice", bfTick.precloseprice());
    vItem.insert("upperLimit", bfTick.upperlimit());
    vItem.insert("lowerLimit", bfTick.lowerlimit());

    //根据id找到对应的行,然后用列的text来在map里面取值设置到item里面=
    QString id = vItem.value("symbol").toString();
    int row = table_row_.value(id);
    for (int i = 0; i < table_col_.count(); i++) {
        QVariant raw_val = vItem.value(table_col_.at(i));
        QString str_val = raw_val.toString();
        if (raw_val.type() == QMetaType::Double || raw_val.type() == QMetaType::Float) {
            str_val = QString().sprintf("%6.3f", raw_val.toDouble());
        }

        QTableWidgetItem* item = new QTableWidgetItem(str_val);
        ui->tableWidget->setItem(row, i, item);
    }
}