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); }
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); } }
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; }
void StatForm::statTick(QString symbol, QString exchange, QString name) { QString startDate, startTime, endDate, endTime; if (1) { leveldb::DB* db = g_sm->dbService()->getDb(); leveldb::ReadOptions options; options.fill_cache = false; leveldb::Iterator* it = db->NewIterator(options); if (!it) { qFatal("NewIterator == nullptr"); } //第一个是tick-symbol-exchange+ //最后一个是tick-symbol-exchange= QString key = QString().sprintf("tick-%s-%s+", qPrintable(symbol), qPrintable(exchange)); it->Seek(leveldb::Slice(key.toStdString())); if (it->Valid()) { it->Next(); } if (it->Valid()) { //遇到了前后两个结束item const char* buf = it->value().data(); int len = it->value().size(); BfTickData bfItem; if (bfItem.ParseFromArray(buf, len) && bfItem.symbol().length() != 0) { startDate = bfItem.actiondate().c_str(); startTime = bfItem.ticktime().c_str(); } } delete it; } if (startDate.length() != 0) { leveldb::DB* db = g_sm->dbService()->getDb(); leveldb::ReadOptions options; options.fill_cache = false; leveldb::Iterator* it = db->NewIterator(options); if (!it) { qFatal("NewIterator == nullptr"); } //第一个是tick-symbol-exchange+ //最后一个是tick-symbol-exchange= QString key = QString().sprintf("tick-%s-%s=", qPrintable(symbol), qPrintable(exchange)); it->Seek(leveldb::Slice(key.toStdString())); if (it->Valid()) { it->Prev(); } if (it->Valid()) { //遇到了前后两个结束item const char* buf = it->value().data(); int len = it->value().size(); BfTickData bfItem; if (bfItem.ParseFromArray(buf, len) && bfItem.symbol().length() != 0) { endDate = bfItem.actiondate().c_str(); endTime = bfItem.ticktime().c_str(); } } delete it; } if (startDate.length() != 0 && endDate.length() != 0) { onGotData(symbol, exchange, name, "tick", startDate, startTime, endDate, endTime); } }