wumu
2025-03-02 f7dd8173db8f3c2eb5beefa960d86a2bc2144f9f
historydata.cpp
@@ -72,7 +72,7 @@
    m_poolModel = new QStandardItemModel(1,9,this);
    QStringList poolLabel;
    poolLabel<<"id"<<"股票名字"<<"股票代号"<<"监控记录时间"<<"监控买入价格"<<"当前价格"<<"盈亏百分比"<<"监控卖出价格"<<"监控卖出时间";
    poolLabel<<"id"<<"股票名字"<<"股票代号"<<"监控买入价格"<<"监控记录时间"<<"当前价格"<<"盈亏百分比"<<"监控卖出价格"<<"监控卖出时间"<<"策略周期";
    m_poolModel->setHorizontalHeaderLabels(poolLabel);
    ui->tableView_2->setModel(m_poolModel);
@@ -81,6 +81,10 @@
    m_poolTimer = new QTimer(this);
    connect(m_poolTimer,SIGNAL(timeout()),this,SLOT(poolTimerSlot()));
    m_poolTimer->start(10*1000);
    m_getPoolCloseTimer = new QTimer(this); // 刷新股票池最新价格
    connect(m_getPoolCloseTimer,SIGNAL(timeout()),this,SLOT(getLastCloseSlot()));
}
HistoryData::~HistoryData()
@@ -179,6 +183,9 @@
        qDebug()<<"查看个股情况:";
        getOneStock(buffer);
    }else if(reply->url().toString().indexOf("https://hqm.stock.sohu.com/getqjson") != -1){
        qDebug()<<"获取股票池个股价格";
        getPoolStock(buffer);
    }
}
@@ -245,6 +252,65 @@
        }
    }
}
/*
 * {"cn_000032":["cn_000032","深桑达A","26.33","-9.98%","-2.92","1102599","3473","306282","17.10%","2.38","29.87","26.33","0.00","29.25","29.25","\/cn\/000032\/index.shtml","169.77","2025-02-28 15:00:54","\/t\/cn\/032\/000032.html"],
 * "cn_603729":["cn_603729","龙韵股份","15.50","-4.79%","-0.78","52294","1220","8268","5.60%","0.57","16.27","15.46","407.89","16.28","16.20","\/cn\/603729\/index.shtml","14.47","2025-02-28 15:00:57","\/t\/cn\/729\/603729.html"],
 * "cn_300253":["cn_300253","卫宁健康","11.03","-7.08%","-0.84","1667417","11","189099","8.96%","3.64","11.86","10.97","116.11","11.87","11.70","\/cn\/300253\/index.shtml","205.36","2025-02-28 15:30:57","\/t\/cn\/253\/300253.html"],
 * "cn_300454":["cn_300454","深信服  ","102.06","-7.13%","-7.84","173684","1","181338","6.29%","2.29","109.90","101.00","0.00","109.90","107.14","\/cn\/300454\/index.shtml","281.95","2025-02-28 15:30:57","\/t\/cn\/454\/300454.html"],
 * "cn_688055":["cn_688055","龙腾光电","3.88","-4.67%","-0.19","98688","1418","3898","0.30%","1.01","4.08","3.87","0.00","4.07","4.07","\/cn\/688055\/index.shtml","129.33","2025-02-28 15:30:56","\/t\/cn\/055\/688055.html"],
 * "cn_000818":["cn_000818","航锦科技","31.45","-3.82%","-1.25","982520","16510","316446","14.50%","5.41","33.40","30.98","393.13","32.70","32.30","\/cn\/000818\/index.shtml","213.03","2025-02-28 15:00:54","\/t\/cn\/818\/000818.html"]}
 *
*/
void HistoryData::getPoolStock(QByteArray &buffer){
    // 通过搜狐股票行情中心来批量获取价格情况,https://hqm.stock.sohu.com/getqjson?code=cn_603729,cn_688055,cn_000032,cn_000818,cn_300454,cn_300253
    qDebug()<<buffer;
    qDebug()<<buffer.left(buffer.size()-1);
    QJsonDocument jd = QJsonDocument::fromJson(buffer.left(buffer.size()-1));
    qDebug()<<jd.isObject()<<jd.toJson();
    if(jd.isObject()){
        QJsonObject jObject = jd.object();
        //QJsonArray jArr = jObject.keys(); // 通过键值对取值
        int cnt = jObject.keys().size();
        qDebug()<<"数组size:"<<cnt;
        for(auto it = jObject.begin();it != jObject.end();++it){
            qDebug()<<it.key()<<it.value().toArray().at(1)<<it.value().toArray().at(2);
        }
    }else{
        // 当字符串处理
        QList<QByteArray> tmp = buffer.split('[');
        //qDebug()<<"[ tmp size:"<<tmp.size();
        for(int i=0;i<tmp.size();++i){
            //qDebug()<<i<<tmp.at(i);
            QList<QByteArray> row = tmp.at(i).split(',');
            if(row.size() > 2){
                QString code = QString::fromLocal8Bit(row.at(0).mid(1,row.at(0).size()-2)).trimmed().split('_').at(1);
                QString name = QString::fromLocal8Bit(row.at(1).mid(1,row.at(1).size()-2)).trimmed();
                double close = row.at(2).mid(1,row.at(2).size()-2).toDouble();
                //qDebug()<<name<<close<<code;
                QString sql = QString("update stock_pool set cur_price=%1 where code like '%%2%' and state=1").arg(close).arg(code);
                qDebug()<<sql;
                QSqlQuery que(db);
                if(que.exec(sql)){
                    qDebug()<<"sql update ok";
                }else{
                    qDebug()<<"sql update fail";
                }
            }
        }
        // 最后更新一下表格的百分比情况
        QString sql = "update stock_pool set percent = (cur_price/in_price - 1)*100 where cur_price > 0 and in_price > 0 and state=1";
        QSqlQuery que(db);
        if(que.exec(sql)){
            qDebug()<<"sql update percent ok";
        }else{
            qDebug()<<"sql update percent fail";
        }
    }
}
void HistoryData::on_pushButton_search_clicked()
@@ -329,9 +395,10 @@
    QString code = ui->lineEdit_2->text();
    QString price = ui->lineEdit_3->text();
    QString in_date = ui->dateEdit_2->text();
    qDebug()<<name<<code<<price<<in_date;
    QString type = ui->comboBox->currentText();
    qDebug()<<name<<code<<price<<in_date<<type;
    QString sql = QString("insert into stock_pool (name,code,in_price,in_time) values('%1','%2',%3,'%4')").arg(name).arg(code).arg(price).arg(in_date);
    QString sql = QString("insert into stock_pool (name,code,in_price,in_time,type) values('%1','%2',%3,'%4','%5')").arg(name).arg(code).arg(price).arg(in_date).arg(type);
    qDebug()<<"sql:"<<sql;
    QSqlQuery que(db);
@@ -352,7 +419,7 @@
        m_poolModel->setRowCount(0);
        while (que.next()) {
            QList<QStandardItem*> items;
            for(int i=0;i<9;++i){
            for(int i=0;i<10;++i){
                QString item = que.value(i).toString();
                QStandardItem *it = new QStandardItem(item);
                if(i==6){
@@ -412,6 +479,39 @@
    qDebug()<<"days:"<<days;
    m_days = days; // 刷新时间
   getLastCloseSlot();
}
void HistoryData::getLastCloseSlot()
{
    // 获取股票池每只股票最新价格
    QString url = "https://hqm.stock.sohu.com/getqjson?code=";
    for(int i=0;i<m_poolModel->rowCount();++i){
        if(m_poolModel->item(i,2)){
            QString code="cn_";
            if(i > 0) code = ",cn_";
            code += m_poolModel->item(i,2)->text().mid(2);
            // qDebug()<<"code:"<<code;
            url+=code;
            //qDebug()<<"url:"<<url;
        }
    }
    qDebug()<<QDateTime::currentDateTime()<<"url:"<<url;
    m_request.setUrl(QUrl(url));
    m_manager.get(m_request);
}
void HistoryData::on_checkBox_updatePoolClose_clicked()
{
    if(ui->checkBox_updatePoolClose->isChecked()){
        m_getPoolCloseTimer->start(3*60*1000); // 3分钟更新一次
    }else{
        m_getPoolCloseTimer->stop();
    }
}