wumu
2025-03-02 f7dd8173db8f3c2eb5beefa960d86a2bc2144f9f
historydata.cpp
@@ -25,6 +25,8 @@
    // ui相关
    ui->dateEdit->setDate(QDate::currentDate());
    ui->dateEdit_2->setDate(QDate::currentDate());
    m_model = new QStandardItemModel(1,9,this);
    m_customModel = new CustomSortProxyModel; // 可自定义排序的模型
@@ -66,6 +68,22 @@
    // 去请求首页
    m_manager.get(m_request);
    // 股票池相关
    m_poolModel = new QStandardItemModel(1,9,this);
    QStringList poolLabel;
    poolLabel<<"id"<<"股票名字"<<"股票代号"<<"监控买入价格"<<"监控记录时间"<<"当前价格"<<"盈亏百分比"<<"监控卖出价格"<<"监控卖出时间"<<"策略周期";
    m_poolModel->setHorizontalHeaderLabels(poolLabel);
    ui->tableView_2->setModel(m_poolModel);
    // 股票池定时刷新
    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()));
}
@@ -110,9 +128,10 @@
{
    // 把5000支股票挨个获取到,然后进行数据保存,存到表格中
    // 更新的时候,默认支持10年的数据查询
    if(m_days <= 0) return;
    QString time_tar=QString::number(QDateTime::currentMSecsSinceEpoch());
    QString dayCnt=QString::number(3000);
    QString dayCnt=QString::number(m_days);
    QString type = "day"; // day week month
    for(auto code:m_codeNames.keys()){
        QString url = QString("https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=%1&begin=%2&period=%3&type=before&count=-%4&indicator=kline,pe,market_capital,ma").arg(code)
@@ -123,6 +142,15 @@
        m_manager.get(m_request);
        //break; // 测试用
    }
    // 更新时间
    QString curDate = QDate::currentDate().toString("yyyy-MM-dd");
    QString sql = QString("insert into days_info (log_time) values ('%1')").arg(curDate);
    QSqlQuery que(db);
    if(que.exec(sql)){
        qDebug()<<"update log time ok:"<<curDate;
    }else{
        qDebug()<<"update log time fail:"<<curDate;
    }
    
}
@@ -155,6 +183,9 @@
        qDebug()<<"查看个股情况:";
        getOneStock(buffer);
    }else if(reply->url().toString().indexOf("https://hqm.stock.sohu.com/getqjson") != -1){
        qDebug()<<"获取股票池个股价格";
        getPoolStock(buffer);
    }
}
@@ -215,12 +246,71 @@
        QSqlQuery que(db);
        if(que.exec(sql)){
            qDebug()<<"intsert ok";
            qDebug()<<"insert ok";
        }else{
            qDebug()<<"intsert fail"<<que.lastError().text();
            qDebug()<<"insert fail"<<que.lastError().text();
        }
    }
}
/*
 * {"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()
@@ -298,3 +388,130 @@
    //emit sendHistoryModel(m_modelDatas); // 给信息展示发模型
    emit sendHistoryModel(m_model);
}
void HistoryData::on_pushButton_addStock_clicked()
{
    QString name = ui->lineEdit->text();
    QString code = ui->lineEdit_2->text();
    QString price = ui->lineEdit_3->text();
    QString in_date = ui->dateEdit_2->text();
    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,type) values('%1','%2',%3,'%4','%5')").arg(name).arg(code).arg(price).arg(in_date).arg(type);
    qDebug()<<"sql:"<<sql;
    QSqlQuery que(db);
    if(que.exec(sql)){
        qDebug()<<"insert ok";
        poolTimerSlot(); // 立马刷新股票池的模型,刷新视图
    }else{
        qDebug()<<"insert fail";
    }
}
void HistoryData::poolTimerSlot()
{
    QString sql = "select * from stock_pool where state=1";
    QSqlQuery que(db);
    if(que.exec(sql)){
        qDebug()<<"select ok:"<<QDateTime::currentDateTime();
        m_poolModel->setRowCount(0);
        while (que.next()) {
            QList<QStandardItem*> items;
            for(int i=0;i<10;++i){
                QString item = que.value(i).toString();
                QStandardItem *it = new QStandardItem(item);
                if(i==6){
                    if(item.toDouble() > 0){
                        it->setData(QColor("red"),Qt::DecorationRole); // 添加一个装饰的颜色为红色
                        it->setData(QColor("red"),Qt::TextColorRole);  // 将字体颜色设置为红色
                        items.at(1)->setData(QColor("red"),Qt::TextColorRole);  // 将股票名字设置为红色
                    }else if(item.toDouble() < 0){
                        it->setData(QColor("green"),Qt::BackgroundColorRole);
                        items.at(1)->setData(QColor("green"),Qt::TextColorRole);
                    }
                }
                items.append(it);
            }
            m_poolModel->appendRow(items); // 整行添加 效率更高
        }
    }else{
        qDebug()<<"select fail";
    }
    m_poolTimer->setInterval(60*1000);
}
void HistoryData::on_checkBox_clicked()
{
    QString dt = "2025-02-10";
    QString sql = "select log_time from days_info order by log_time desc";
    QSqlQuery que(db);
    if(que.exec(sql)){
        if (que.next()) {
            dt = que.value(0).toString();
        }else{
            qDebug()<<"查询结果集为空";
            return;
        }
    }else{
        qDebug()<<"查询失败";
        return ;
    }
    if(ui->checkBox->isChecked()){
        ui->pushButton_update->setEnabled(true);
    }else{
        ui->pushButton_update->setEnabled(false);
    }
    QDate d1 = QDate::fromString(dt,"yyyy-MM-dd");
    QDate d2 = QDate::currentDate();
    int days = 0; // 经过了多少个周内的天数
    for(QDate begin = d1.addDays(1);begin <= d2;begin = begin.addDays(1)){
        if(begin.dayOfWeek() != Qt::Saturday && begin.dayOfWeek() != Qt::Sunday){
            days++;
        }
    }
    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();
    }
}