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); } }
// 如果不是上期所,平今仓可用close或closeToday,平昨仓可用close或closeYesterday= // 如果是上期所,平今仓只可用closeToday,平昨仓可用close或closeYesterday= // 那成交回报的流水回来的时侯: // CloseYesterday=> insert =>CloseYesterday // CloseToday=> insert =>CloseToday // 也存在:closeToday =>close或closeYesterday =>close的情况= // 参考:http://www.cnblogs.com/xiaouisme/p/4654750.html void PositionForm::on_pushButtonCloseAll_clicked() { for (auto pos : positions_) { // 没有持仓的pass if (pos.position() == 0) { continue; } QString symbol = pos.symbol().c_str(); QString exchange = pos.exchange().c_str(); if (exchange.length() == 0) { void* contract = g_sm->gatewayMgr()->getContract(symbol); exchange = CtpUtils::getExchangeFromContract(contract); } // 没有订阅的pass if (!symbols_my_.contains(symbol)) { BfInfo(symbol + " not subscrible,please close byhand"); continue; } // 取不到tick的pass,比如晚上if不开盘= void* tick = g_sm->gatewayMgr()->getLatestTick(symbol); if (!tick) { BfInfo(symbol + " has no tick,please close byhand"); continue; } BfTickData bfTick; CtpUtils::translateTick(tick, nullptr, &bfTick); // 平昨= if (pos.ydposition() > 0) { // 限价单= BfOffset offset = OFFSET_CLOSE; BfPriceType priceType = PRICETYPE_LIMITPRICE; //TODO(hege):这里要判断是否超过了contract->maxLimit //TODO(hege):这里要判断昨持仓和今持仓,先平昨再平今= int volume = pos.ydposition(); //空单-->最高价+多+平= //多单-->最低价+空+平= double price = bfTick.upperlimit(); BfDirection direction = DIRECTION_LONG; if (pos.direction() == DIRECTION_NET || pos.direction() == DIRECTION_LONG) { direction = DIRECTION_SHORT; price = bfTick.lowerlimit(); } BfSendOrderReq req; req.set_symbol(symbol.toStdString()); req.set_exchange(exchange.toStdString()); req.set_price(price); req.set_volume(volume); req.set_direction(direction); req.set_offset(offset); req.set_pricetype(priceType); QMetaObject::invokeMethod(g_sm->gatewayMgr(), "sendOrder", Qt::QueuedConnection, Q_ARG(BfSendOrderReq, req)); } // 平今= if (pos.position() - pos.ydposition() > 0) { // 限价单= BfOffset offset = OFFSET_CLOSETODAY; BfPriceType priceType = PRICETYPE_LIMITPRICE; //TODO(hege):这里要判断是否超过了contract->maxLimit int volume = pos.position() - pos.ydposition(); //空单-->最高价+多+平= //多单-->最低价+空+平= double price = bfTick.upperlimit(); BfDirection direction = DIRECTION_LONG; if (pos.direction() == DIRECTION_NET || pos.direction() == DIRECTION_LONG) { direction = DIRECTION_SHORT; price = bfTick.lowerlimit(); } BfSendOrderReq req; req.set_symbol(symbol.toStdString()); req.set_exchange(exchange.toStdString()); req.set_price(price); req.set_volume(volume); req.set_direction(direction); req.set_offset(offset); req.set_pricetype(priceType); QMetaObject::invokeMethod(g_sm->gatewayMgr(), "sendOrder", Qt::QueuedConnection, Q_ARG(BfSendOrderReq, req)); } } }