wumu
2025-09-11 abc5b592843b5482ddbeb576ee7fba46f2fc5e39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#include "analysisbyrediscache.h"
#include "ui_analysisbyrediscache.h"
#include <QDateTime>
#include <QDebug>
#include <QElapsedTimer>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
 
AnalysisByRedisCache::AnalysisByRedisCache(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::AnalysisByRedisCache)
{
    ui->setupUi(this);
 
    ui->dateEdit->setDate(QDate::currentDate().addMonths(-1)); // 初始化开始查询时间
    ui->dateEdit_end->setDate(QDate::currentDate()); // 初始化结束时间
 
    if(m_dbCache.initMySQL("127.0.0.1",3306,"root","root","stock_plan")){
        qDebug()<<"mysql conn ok";
    }else{
        qDebug()<<"mysql conn fail";
    }
 
    if(m_dbCache.initRedis("127.0.0.1",6379)){
        qDebug()<<"redis conn ok";
    }else{
        qDebug()<<"redis conn fail";
    }
 
    m_model = new QStandardItemModel(this);
    ui->tableView->setModel(m_model);
 
    m_modelResult = new QStandardItemModel(this);
    ui->tableView_2->setModel(m_modelResult);
 
    QStringList labels;
    labels<<"股票名字"<<"市值"<<"收盘价"<<"涨跌幅"<<"排  名"<<"交易时间"<<"排名变化"<<"交易额"<<"底部反转时间"<<"一阳穿四线时间";
    m_model->setHorizontalHeaderLabels(labels);
    m_modelResult->setHorizontalHeaderLabels(labels);
 
    //ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableView->resizeColumnToContents(0);
    ui->tableView->resizeColumnToContents(4);
    // 初始化爬虫相关的Http
    loadHttpInit(QUrl("https://xueqiu.com/S/SZ002466"));
}
 
AnalysisByRedisCache::~AnalysisByRedisCache()
{
    delete ui;
}
 
void AnalysisByRedisCache::on_pushButton_loadDataSQLRedis_clicked()
{
    ui->label_load->setText("缓存加载中...");
    QString begin_date = ui->dateEdit->text();
    QString end_date = ui->dateEdit_end->text();
    QString limit_str = ui->comboBox->currentText();
    QString sql = QString("select * from stock_day_info where time_trade > '%1' and time_trade <= '%3' limit %2;").arg(begin_date).arg(limit_str).arg(end_date);
    qDebug()<<"sql:"<<sql;
    QElapsedTimer timer;
    timer.start();
 
    auto result = m_dbCache.query(sql);
 
    qDebug() << "Query executed in" << timer.elapsed() << "ms";
    qDebug() << "Retrieved" << result.size() << "rows";
 
    // 第二次查询应该从缓存获取
    timer.restart();
    result = m_dbCache.query(sql);
    qint64 resTime = timer.elapsed();
    qDebug() << "Cached query executed in" << resTime << "ms";
    m_result = result;
    ui->label_load->setText(QString("缓存加载%2条,耗时%1ms").arg(QString::number(resTime)).arg(QString::number(result.size())));
 
}
 
// 滑动窗口计算排名变化
void AnalysisByRedisCache::calcWindowRank(int start)
{
 
    // 滑动窗口排名,6个要2+的前100
    QVector<int> ranks;
    for(int i=start;i<m_model->rowCount();++i){
        ranks.append(m_model->item(i,4)->text().toInt());
        if(i >= 6+start){
            // 先统计排名计数
            int cntBig=0, cntSmall=0;
            for(int j=0;j<ranks.size();++j){
                if(ranks.at(j) > 100){
                    cntBig++;
                }else{
                    cntSmall++;
                }
            }
            // 统计对比
            if(cntSmall == 2 && ranks.back() <= 100){
                // 将当前6个元素定为符合的窗口,背景色设置为紫色
                for(int k=0;k<6;++k){
                    m_model->item(i-k,4)->setData(QColor("red"),Qt::BackgroundColorRole);
                }
            }
 
            // 清除窗口第一个元素
            ranks.pop_front();
        }
 
    }
 
}
 
void AnalysisByRedisCache::loadHttpInit(QUrl url)
{
    // 处理数据爬取的操作
    // 模拟浏览器的参数
    QString userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36";
    m_cookie = "__utma=1.731742638.1647403301.1699341909.1700229030.32; device_id=196eef62baf016c7d95a22752d9bdbab; smidV2=20240414233939e95389ecf7ecd2f4d08524ce770aacd500753aa68e9640320; s=c611de27gr; cookiesu=651726298794778; xq_a_token=220b0abef0fac476d076c9f7a3938b7edac35f48; xqat=220b0abef0fac476d076c9f7a3938b7edac35f48; xq_r_token=1d46f0ed628506486164e5055a4993f9b54b2f4c; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTcyOTIxMjc4NCwiY3RtIjoxNzI3NDkwOTU3MjA0LCJjaWQiOiJkOWQwbjRBWnVwIn0.C_GmKEhTaaioDMLWkgZyMXDl4duYEVmsdJHsTi7gbcNz0Tohc-uxHsaw0yBT5k-qmbrJ_RaLMCSxy06v14-R3dwL-MsiKRHxHa5qvQZN4BjEgvPRkqqvPgE_fkPLte8qQOEgd5iVkhr-4mjip0-9WCeXYiH7DygxFOBXGlgoPtpPzAtOTm5TWJmXh0ipDsIZxfNOl8jipXYaIdkv_kqLul5gqiBi5qqnwONDa24Zx-Kvpm8ySWiPFBLzZBqTuRBs4oKAMpSdOiYGLVL7dcSDDZyWqAexmrN4f19hkmd6gBHL4dCczRMDGYc1e98sQtlbZ5lgeEuuM24jjcuwCxsxXQ; u=651726298794778; Hm_lvt_1db88642e346389874251b5a1eded6e3=1727099939,1727251802,1727488707,1727491020; HMACCOUNT=1628106D67895387; acw_tc=2760828017275244258732552e9880f861be6db0c40facbdd5f223490decc2; acw_sc__v2=66f7ee8290dc3f63112948801ef331b8c97ccb35; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1727524485; .thumbcache_f24b8bbe5a5934237bbc0eda20c1b6e7=t00N841S/BEpxTGOoJrbm0blWik12om0ew/whcq/V2DNtyEA8um7J+yzeGOli+6iP/TrvrH0YKH2kHlsmDb5EQ%3D%3D; ssxmod_itna=YqAOBKYve+x0ODfxBcDB4DKM7RtAA4454DkDIEC+GAqGNK3DZDiqAPGhDC8RzeL4Ko7+e2aeQvSeopd5pYDk0Ge5TB0PIjaIDB3DEx06TCCQxiiSDCeDIDWeDiDG4Gm4qGtDpxG=DjDytZ9TtDm4GWGqDmDGYBWqDgDYQDGwIXD7QDIqtW07tQDDNQKpAKDiYeHlL5uMRMtOrK7DtDjdTD/3+kZCbCcPwVFeFO=nPcDB6wxBjZRq00Un+g4mNqLYf4IDAxQuYKtgheYYoAfDhTKhhLSGx4tGY4+GDKSiMS2DDAIvdKeD; ssxmod_itna2=YqAOBKYve+x0ODfxBcDB4DKM7RtAA4454DkDIEC+GDA69mqD/YttDFhxMltFKApT7bCMH+bytp7GQQyCbBWukeAF3+jxeN2wLjbQAFcdwbeNeoohjQ4qtEnLg4TKyX2LqVL=CGaV=GqZZqbDby8DFnFbgWiZEH8zoBzBCbzqWeqwoPq7/TpL=gIRY8sVhEpnWWHQU8sz+S8=+8oRerHq0wtQnSzyQDK7Dmvr3a+VY7fezRmxKF=bDeqexYUbuuzw2eR3In9evW6tzHbQY6vp=AIOUc9l6vc0vOl9plD6D07zGGQ41uxpii2Y5s7KvYQDWGYExD7=DYKKeD==";
    QByteArray cookieByte = m_cookie.toUtf8();
    QList<QNetworkCookie> list;
    list.push_back(QNetworkCookie(cookieByte));
 
    QVariant var;
    var.setValue(list);
    // 设置要访问的网址
    //m_request.setUrl(QUrl("https://xueqiu.com/S/SZ002466"));
    m_request.setUrl(url);
    // 设置请求头,用户代理,用来模拟浏览器
    m_request.setHeader(QNetworkRequest::UserAgentHeader,userAgent);
    // 设置cookie
    //m_request.setHeader(QNetworkRequest::CookieHeader, var);
 
    // 查看manager都支持哪些协议
    qDebug()<<"缓存分析---支持的协议:"<<m_manager.supportedSchemes();
    // 关联信号槽,当请求管理对象完全打开网页之后,会将数据给槽函数处理
    connect(&m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(showAplyData(QNetworkReply*)));
 
}
 
void AnalysisByRedisCache::showAplyData(QNetworkReply *reply)
{
    qDebug()<<"缓存分析--收到响应";
    // 将响应的数据,一把读取完,放到字节数组里面来处理
    QByteArray buffer = reply->readAll();
 
    // 将数据写到文件中,方便观察数据内容
//    QFile file("data.txt");
//    file.open(QIODevice::ReadWrite | QIODevice::Text);
//    QTextStream out(&file);
//    out << buffer << endl;
//    file.close();
 
    // 下面使用JSON进行数据处理
    if(reply->url() == QUrl("https://xueqiu.com/")){
        qDebug()<<"发现首页url";
        qDebug()<<buffer;
 
    }else if(reply->url().toString().indexOf("https://stock.xueqiu.com/v5/stock/chart/kline.json") != -1){
        qDebug()<<"查看个股情况:";
        getStockOne(buffer);
 
    }else if(reply->url().toString().indexOf("https://stock.xueqiu.com/v5/stock/screener/quote/list.json?page=1&size=100&order=desc&order_by=percent&market=CN&ind_code") != -1){
        qDebug()<<"根据行业获取股票信息"<<reply->url();
        QString industryCode = reply->url().toString().split("=").back();
        qDebug()<<"行业信息:"<<industryCode;
        //getStockByIndustry(buffer,industryCode);
 
    }else if(reply->url().toString().indexOf("https://stock.xueqiu.com/v5/stock/screener/quote/list.json") != -1){
        qDebug()<<"获取所有沪深股票 可以干活了";
        //getStockAllCode(buffer);
    }else if(reply->url().toString().indexOf("https://stock.xueqiu.com/v5/stock/screener/industries.json") != -1){
        qDebug()<<"获取行业信息";
        //getIndustryInfo(buffer);
    }
 
    reply->deleteLater();
}
 
void AnalysisByRedisCache::buttomToUpPolicySlot(QString symbol)
{
    /*
    复盘之前的月线突破情况,放量上涨,5倍左右,并且在60日均线上冲高,不回落,大概率可以继续冲
    关注好冲的形态,加上很好的换手率80%+,甚至100%+,说明充分换手了,突破会更好,有就会赚多倍
    并且多日查看,比例值一直在放大,说明很靠谱,就是有一个数值比例的爬升过程,那就可以重仓买入等收益了,可以绘制一条曲线来观察情况
    */
 
    qDebug()<<"执行底部反转策略";
    //emit sendInfoMess("<font size=12 color=pink>执行底部反转策略 </font>");
    int curRow = ui->tableView->currentIndex().row();
//    QString symbol = m_model->item(curRow,1)->text();
 
    //QString symbol = m_customModel->data(m_customModel->index(curRow,1)).toString();
    qint64 begin = QDateTime::currentMSecsSinceEpoch(); // 取当前时间戳
    int numsCnt = 300;
    QString urlStr = QString("https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=%1&begin=%2&period=%4&type=before&count=-%3&indicator=kline,macd").arg(symbol).arg(begin).arg(numsCnt).arg(m_dateType);
    qDebug()<<urlStr;
    //qDebug()<<"当前股票:"<<symbol<<m_customModel->data(m_customModel->index(curRow,0)).toString();
    // 下一步就是根据url请求到的数据,进行分析
    m_menuIndex = 4; // 第五个菜单项
    m_request.setUrl(QUrl(urlStr));
    m_manager.get(m_request);
}
 
void AnalysisByRedisCache::oneUpCrossFourSlot(QString symbol)
{
    /*  一阳穿四线
    K线出现了大阳线并且贯穿4条均线,说明在猛涨了,并且量能也跟上的话,就说明强劲
 
    日K、周K、月K就很明显。
    大趋势以周、月为主,适合中长线处理
    ---------------------------
    也非常适合超短线的处理,30分、60分、120分的大穿阳线也很明显
    ---------------------------
    就是开盘价和收盘价包含了一个区间,这个区间内都把Ma5 Ma10 Ma20 Ma30给包住了,说明触发了一阳穿四线
    */
    qDebug()<<"一阳穿四线策略";
    //emit sendInfoMess("<font size=12 color=pink>执行一阳穿四线策略 </font>");
    int curRow = ui->tableView->currentIndex().row();
//    QString symbol = m_model->item(curRow,1)->text();
 
    //QString symbol = m_customModel->data(m_customModel->index(curRow,1)).toString();
    qint64 begin = QDateTime::currentMSecsSinceEpoch(); // 取当前时间戳
    int numsCnt = 200;
    QString urlStr = QString("https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=%1&begin=%2&period=%4&type=before&count=-%3&indicator=kline,macd,ma,boll,market_capital").arg(symbol).arg(begin).arg(numsCnt).arg(m_dateType);
    qDebug()<<urlStr;
    //qDebug()<<"当前股票:"<<symbol<<m_customModel->data(m_customModel->index(curRow,0)).toString();
    // 下一步就是根据url请求到的数据,进行分析
    m_menuIndex = 6; // 第七个菜单项
    m_request.setUrl(QUrl(urlStr));
    m_manager.get(m_request);
 
}
 
void AnalysisByRedisCache::updateAllFindTimeSlot()
{
    qDebug()<<"触发了更新策略时间操作";
    for(auto kv:m_codes.keys()){
        qDebug()<<"code:"<<kv<<m_codes[kv];
        //buttomToUpPolicySlot(kv);
        oneUpCrossFourSlot(kv);
    }
}
 
// 单个股票的 JSON格式处理
void AnalysisByRedisCache::getStockOne(QByteArray &buffer)
{
    QJsonDocument jd = QJsonDocument::fromJson(buffer);
    if(jd.isObject()){
        QJsonObject jObject = jd.object();
        QJsonArray jArr = jObject.value("data").toObject().value("item").toArray(); // 通过键值对取值
        int cnt = jArr.count();
        qDebug()<<"数组size:"<<cnt;
        QString symbol = jObject.value("data").toObject().value("symbol").toString();
        QString code = symbol; // 股票代号
//        QLineSeries *series = new QLineSeries;  // QLineSeries 折线的类,QSplineSeries 为圆滑曲线类
//        series->setName(symbol);  // 设置折线名字
//        //series->setColor(QColor(255,0,0)); // 设置颜色 r,g,b 红绿蓝三原色,随意组合
//        QSplineSeries *sp = new QSplineSeries;
//        sp->setName(symbol);
//        QSplineSeries *spVolume = new QSplineSeries;
//        spVolume->setName(symbol.append("--volume"));
//        QSplineSeries *spAmount = new QSplineSeries;
//        spVolume->setName(symbol.append("--amount"));
 
        int ma10_20_flag = 0; // 标记MA金叉、死叉的情况 0:初始值,1:金叉,-1:死叉
        long long oldVolume = 0; // MACD: 老的成交量
        int macd_buy = 0; // 买入标志
        double buy_price = 0; // 买入价格
        double rate_all = 0; // 收益百分比
        for(int i=0;i<cnt;++i){
            qint64 timestamp = jArr.at(i).toArray().at(0).toVariant().toLongLong();
            double close = jArr.at(i).toArray().at(5).toVariant().toDouble();
            long long volume = jArr.at(i).toArray().at(1).toVariant().toLongLong()/100;
 
            long long amount = jArr.at(i).toArray().at(9).toVariant().toLongLong()/100000000;
            QString curDateTime = QDateTime::fromMSecsSinceEpoch(timestamp).toString("yyyy-MM-dd");
            //qDebug()<<curDateTime<<close<<volume<<amount;
//            series->append(timestamp,close); // 折线数据添加
//            sp->append(timestamp,close);  // 曲线数据添加
//            spVolume->append(timestamp,volume);
//            spAmount->append(timestamp,amount);
            if(m_menuIndex == 2){
                // MA  ma10交ma20 金叉,ma10交ma60 死叉
                double ma5 = jArr.at(i).toArray().at(12).toVariant().toDouble();
                double ma10 = jArr.at(i).toArray().at(13).toVariant().toDouble();
                double ma20 = jArr.at(i).toArray().at(14).toVariant().toDouble();
                double ma30 = jArr.at(i).toArray().at(15).toVariant().toDouble();
                //qDebug()<<"MA5-10-20-30"<<ma5<<ma10<<ma20<<ma30;
                if(i>0){
                    double ma5_old = jArr.at(i-1).toArray().at(12).toVariant().toDouble();
                    double ma10_old = jArr.at(i-1).toArray().at(13).toVariant().toDouble();
                    double ma20_old = jArr.at(i-1).toArray().at(14).toVariant().toDouble();
                    double ma30_old = jArr.at(i-1).toArray().at(15).toVariant().toDouble();
 
//                    if(ma5_old > ma20_old && ma5 <= ma20){
//                        // 死叉
//                        qDebug()<<"MA5-20死叉:"<<curDateTime<<" 价格:"<<close;
//                    }else if(ma5_old < ma20_old && ma5 >= ma20){
//                        // 金叉
//                        qDebug()<<"MA5-20金叉:"<<curDateTime<<" 价格:"<<close;
//                    }
 
                    if(ma10_old > ma20_old && ma10 <= ma20){
                        // 死叉
                        qDebug()<<"MA10-20死叉:"<<curDateTime<<" 价格:"<<close;
                        if(buy_price == 0) buy_price = close;
                        double tmp_rate = (close-buy_price)/buy_price;
                        rate_all += tmp_rate;
 
//                        QString info = QString("<font size=8 color=green> %3 %6 MA10-20死叉:%1 价格:%2   盈利比:%4  总盈利:%5</font>").arg(curDateTime).arg(close).arg(code)
//                                .arg(QString::number(tmp_rate*100)).arg(QString::number(rate_all*100)).arg(m_codeNames[code]);
//                        emit sendInfoMess(info);
                        //buy_price = close; // 死叉买入的情况
                    }else if(ma10_old < ma20_old && ma10 >= ma20){
                        // 金叉
                        buy_price = close; // 金叉买入的情况
 
                        qDebug()<<"MA10-20金叉:"<<curDateTime<<" 价格:"<<close;
//                        if(buy_price == 0) buy_price = close;
//                        double tmp_rate = (close-buy_price)/buy_price;
//                        rate_all += tmp_rate;
//                        QString info = QString("<font size=8 color=red> %3 %4 MA10-20金叉:%1 价格:%2 </font>").arg(curDateTime).arg(close).arg(code).arg(m_codeNames[code]);
                                //.arg(QString::number(tmp_rate*100)).arg(QString::number(rate_all*100));
                        //emit sendInfoMess(info);
 
                    }
                }
            }else if(m_menuIndex == 3){
                // MACD策略 结合成交量来判断会更好:买入点成交量继续放大则持有,缩小则离场
                double dea = jArr.at(i).toArray().at(12).toVariant().toDouble();
                double dif = jArr.at(i).toArray().at(13).toVariant().toDouble();
                double macd = jArr.at(i).toArray().at(14).toVariant().toDouble();
 
                if(macd_buy == 1 && oldVolume > volume){
                    QString info = QString("<font size=8 color=green> %4 MACD 成交量初次见顶 离场1:%1 价格:%2 MACD=%3  </font>").arg(curDateTime).arg(close).arg(macd).arg(code);
                    //emit sendInfoMess(info);
                    macd_buy = 0;
                    oldVolume = 0;
 
                }
                if(i > 0){
                    double dea_old = jArr.at(i-1).toArray().at(12).toVariant().toDouble();
                    double dif_old = jArr.at(i-1).toArray().at(13).toVariant().toDouble();
                    double macd_old = jArr.at(i-1).toArray().at(14).toVariant().toDouble();
 
                    if(dif_old > dea_old && dif <= dea){
                        // 死叉
                        qDebug()<<"MACD死叉:"<<curDateTime<<" 价格:"<<close <<"MACD="<<macd ;
                        QString info=QString("<font size=8 color=green> %4 MACD死叉:%1 价格:%2 MACD=%3  </font>").arg(curDateTime).arg(close).arg(macd).arg(code);
                        //emit sendInfoMess(info);
                    }else if(dif_old < dea_old && dif >= dea){
                        // 金叉
                        qDebug()<<"MACD金叉:"<<curDateTime<<" 价格:"<<close <<"MACD="<<macd <<"建议记录当前金叉MACD的值,若后面有低于此值,关注离场提示";
                        QString info=QString("<font size=9 color=red> %4 MACD金叉:%1 价格:%2 MACD=%3 建议记录当前金叉MACD的值,若后面有低于此值,关注离场提示 </font>").arg(curDateTime).arg(close).arg(macd).arg(code);
                        //emit sendInfoMess(info);
 
                        macd_buy = 1;
                        oldVolume = volume;
                    }
                }
            }else if(m_menuIndex == 4 || m_menuIndex == 6){
                // 底部反转策略,提前的感知的时间会更早,得到更好的预判趋势
               // qDebug()<<"底部反转策略";
                if(i > 0){
                    {
                        double volume_cur = jArr.at(i).toArray().at(1).toDouble();
                        double volume_old = jArr.at(i-1).toArray().at(1).toDouble();
                        double volume_rate = volume_cur / volume_old;
                        if(volume_rate > 2){
                            qDebug()<<"底部反转策略:量能比"<<volume_rate<<code <<curDateTime<<"价格:" <<close;
                            QString info = QString("<font size=8 color=red> %4 底部反转策略:量能 时间:%1 价格:%2 量能比值=%3  </font>").arg(curDateTime).arg(close).arg(volume_rate).arg(code);
                            //emit sendInfoMess(info);
 
                            //修改发现的最新时间
                            updateFindTime(m_codes[code],curDateTime,7); // 下标为7
                        }
                    }
 
 
                    {
                        // 一阳穿四线
 
                        double ma5 = jArr.at(i).toArray().at(12).toVariant().toDouble();
                        double ma10 = jArr.at(i).toArray().at(13).toVariant().toDouble();
                        double ma20 = jArr.at(i).toArray().at(14).toVariant().toDouble();
                        double ma30 = jArr.at(i).toArray().at(15).toVariant().toDouble();
 
                        double open = jArr.at(i).toArray().at(2).toVariant().toDouble();
                        long long market_cap = jArr.at(i).toArray().at(22).toVariant().toLongLong();
 
                        double volume_rate = 0;
                        if(i > 0){ // 计算量能比
                            double volume_cur = jArr.at(i).toArray().at(1).toDouble();
                            double volume_old = jArr.at(i-1).toArray().at(1).toDouble();
                            volume_rate = volume_cur / volume_old;
                        }
 
                        if(ma5 >= open && ma10 >= open && ma20 >= open && ma30 >= open
                                && ma5 <= close && ma10 <= close && ma20 <= close && ma30 <= close){
                            qDebug()<<"一阳穿四线:"<< code <<curDateTime<<" 价格:"<<close << "量能比:"<<volume_rate;
                            if(volume_rate > 4){
        //                        QString info = QString("<font size=8 color=blue> %3 %4 一阳穿四线:%1 价格:%2 量能比:%5 周期:%6  市值:%7亿</font>").arg(curDateTime).arg(close).arg(code).arg(m_codeNames[code]).arg(volume_rate)
        //                                .arg(m_dateType).arg(market_cap/100000000.0);
                                //emit sendInfoMess(info);
                            }else{
        //                        QString info = QString("<font size=8 color=red> %3 %4 一阳穿四线:%1 价格:%2 量能比:%5 周期:%6  市值:%7亿</font>").arg(curDateTime).arg(close).arg(code).arg(m_codeNames[code]).arg(volume_rate)
        //                                .arg(m_dateType).arg(market_cap/100000000.0);
                                //emit sendInfoMess(info);
                            }
 
                            //修改发现的最新时间
                            updateFindTime(m_codes[code],curDateTime,8); // 下标为8
 
                        }
                    }
                }
 
            }else if(m_menuIndex == 5){
                // 多策略共振:量能、MACD、MA、BOLL
 
                int cnt = 0;
                // 量能
                if(i > 0){
                    double volume_cur = jArr.at(i).toArray().at(1).toDouble();
                    double volume_old = jArr.at(i-1).toArray().at(1).toDouble();
                    double volume_rate = volume_cur / volume_old;
                    if(volume_rate > 2){
                        cnt++;
                        qDebug()<<"多策略共振:量能比"<<volume_rate<<code <<curDateTime<<"价格:" <<close;
                        QString info = QString("<font size=8 color=red> %4 多策略共振:量能策略 时间:%1 价格:%2 量能比值=%3  共振值:%5</font>").arg(curDateTime).arg(close).arg(volume_rate).arg(code).arg(cnt);
                        //emit sendInfoMess(info);
                    }
                }
 
                // MACD策略 结合成交量来判断会更好:买入点成交量继续放大则持有,缩小则离场
                double dea = jArr.at(i).toArray().at(16).toVariant().toDouble();
                double dif = jArr.at(i).toArray().at(17).toVariant().toDouble();
                double macd = jArr.at(i).toArray().at(18).toVariant().toDouble();
 
                if(macd_buy == 1 && oldVolume > volume){
                    QString info = QString("<font size=8 color=green> %4 MACD 成交量初次见顶 离场1:%1 价格:%2 MACD=%3  </font>").arg(curDateTime).arg(close).arg(macd).arg(code);
                    //emit sendInfoMess(info);
                    macd_buy = 0;
                    oldVolume = 0;
 
                }
                if(i > 0){
                    double dea_old = jArr.at(i-1).toArray().at(16).toVariant().toDouble();
                    double dif_old = jArr.at(i-1).toArray().at(17).toVariant().toDouble();
                    double macd_old = jArr.at(i-1).toArray().at(18).toVariant().toDouble();
 
                    if(dif_old > dea_old && dif <= dea){
                        // 死叉
                        qDebug()<<"MACD死叉:"<<curDateTime<<" 价格:"<<close <<"MACD="<<macd ;
                        QString info=QString("<font size=8 color=green> %4 MACD死叉:%1 价格:%2 MACD=%3  </font>").arg(curDateTime).arg(close).arg(macd).arg(code);
                        //emit sendInfoMess(info);
                    }else if(dif_old < dea_old && dif >= dea){
                        // 金叉
                        cnt++;
                        qDebug()<<"MACD金叉:"<<curDateTime<<" 价格:"<<close <<"MACD="<<macd <<"建议记录当前金叉MACD的值,若后面有低于此值,关注离场提示";
                        QString info=QString("<font size=9 color=red> %4 MACD金叉:%1 价格:%2 MACD=%3 共振值:%5  </font>").arg(curDateTime).arg(close).arg(macd).arg(code).arg(cnt);
                        //emit sendInfoMess(info);
 
                        macd_buy = 1;
                        oldVolume = volume;
                    }
                }
 
 
                // MA  ma10交ma20 金叉,ma10交ma60 死叉
                double ma5 = jArr.at(i).toArray().at(12).toVariant().toDouble();
                double ma10 = jArr.at(i).toArray().at(13).toVariant().toDouble();
                double ma20 = jArr.at(i).toArray().at(14).toVariant().toDouble();
                double ma30 = jArr.at(i).toArray().at(15).toVariant().toDouble();
                //qDebug()<<"MA5-10-20-30"<<ma5<<ma10<<ma20<<ma30;
                if(i>0){
                    double ma5_old = jArr.at(i-1).toArray().at(12).toVariant().toDouble();
                    double ma10_old = jArr.at(i-1).toArray().at(13).toVariant().toDouble();
                    double ma20_old = jArr.at(i-1).toArray().at(14).toVariant().toDouble();
                    double ma30_old = jArr.at(i-1).toArray().at(15).toVariant().toDouble();
 
//                    if(ma5_old > ma20_old && ma5 <= ma20){
//                        // 死叉
//                        qDebug()<<"MA5-20死叉:"<<curDateTime<<" 价格:"<<close;
//                    }else if(ma5_old < ma20_old && ma5 >= ma20){
//                        // 金叉
//                        qDebug()<<"MA5-20金叉:"<<curDateTime<<" 价格:"<<close;
//                    }
 
                    if(ma10_old > ma20_old && ma10 <= ma20){
                        // 死叉
                        qDebug()<<"MA10-20死叉:"<<curDateTime<<" 价格:"<<close;
                        if(buy_price == 0) buy_price = close;
                        double tmp_rate = (close-buy_price)/buy_price;
                        rate_all += tmp_rate;
 
//                        QString info = QString("<font size=8 color=green> %3 %6 MA10-20死叉:%1 价格:%2   盈利比:%4  总盈利:%5</font>").arg(curDateTime).arg(close).arg(code)
//                                .arg(QString::number(tmp_rate*100)).arg(QString::number(rate_all*100)).arg(m_codeNames[code]);
                        //emit sendInfoMess(info);
                        //buy_price = close; // 死叉买入的情况
                    }else if(ma10_old < ma20_old && ma10 >= ma20){
                        // 金叉
                        cnt++;
                        buy_price = close; // 金叉买入的情况
 
                        qDebug()<<"MA10-20金叉:"<<curDateTime<<" 价格:"<<close;
//                        QString info = QString("<font size=8 color=red> %3 %4 MA10-20金叉:%1 价格:%2 共振值:%5 </font>").arg(curDateTime).arg(close).arg(code).arg(m_codeNames[code]).arg(cnt);
                                //.arg(QString::number(tmp_rate*100)).arg(QString::number(rate_all*100));
                        //emit sendInfoMess(info);
 
                    }
                }
 
            }else if(m_menuIndex == 6){
                // 一阳穿四线
 
                double ma5 = jArr.at(i).toArray().at(12).toVariant().toDouble();
                double ma10 = jArr.at(i).toArray().at(13).toVariant().toDouble();
                double ma20 = jArr.at(i).toArray().at(14).toVariant().toDouble();
                double ma30 = jArr.at(i).toArray().at(15).toVariant().toDouble();
 
                double open = jArr.at(i).toArray().at(2).toVariant().toDouble();
                long long market_cap = jArr.at(i).toArray().at(22).toVariant().toLongLong();
 
                double volume_rate = 0;
                if(i > 0){ // 计算量能比
                    double volume_cur = jArr.at(i).toArray().at(1).toDouble();
                    double volume_old = jArr.at(i-1).toArray().at(1).toDouble();
                    volume_rate = volume_cur / volume_old;
                }
 
                if(ma5 >= open && ma10 >= open && ma20 >= open && ma30 >= open
                        && ma5 <= close && ma10 <= close && ma20 <= close && ma30 <= close){
                    qDebug()<<"一阳穿四线:"<< code <<curDateTime<<" 价格:"<<close << "量能比:"<<volume_rate;
                    if(volume_rate > 4){
//                        QString info = QString("<font size=8 color=blue> %3 %4 一阳穿四线:%1 价格:%2 量能比:%5 周期:%6  市值:%7亿</font>").arg(curDateTime).arg(close).arg(code).arg(m_codeNames[code]).arg(volume_rate)
//                                .arg(m_dateType).arg(market_cap/100000000.0);
                        //emit sendInfoMess(info);
                    }else{
//                        QString info = QString("<font size=8 color=red> %3 %4 一阳穿四线:%1 价格:%2 量能比:%5 周期:%6  市值:%7亿</font>").arg(curDateTime).arg(close).arg(code).arg(m_codeNames[code]).arg(volume_rate)
//                                .arg(m_dateType).arg(market_cap/100000000.0);
                        //emit sendInfoMess(info);
                    }
 
                }
            }
 
 
        }
        if(m_menuIndex == 0){
            // 策略分析  只能选一种线形数据
            //emit sendChartData(series); // 发送折线数据
//            emit sendChartData(sp);   // 发送曲线数据 收盘价
//            emit sendChartData(spVolume); // 发送交易量曲线
//            emit sendChartData(spAmount); // 交易额曲线
        }else if(m_menuIndex == 1){
            // 最新价格
        }else if(m_menuIndex == 2){
            // MA策略
        }else if(m_menuIndex == 3){
            // MACD策略
        }
    }
}
 
void AnalysisByRedisCache::updateFindTime(QString name, QString trade_time, int index)
{
    for(int i=0;i<m_modelResult->rowCount();++i){
        if(m_modelResult->item(i,0)->text() == name){ // 匹配到名字
            m_modelResult->setItem(i,index,new QStandardItem(trade_time)); // 更新时间
        }
    }
}
 
void AnalysisByRedisCache::searchAndAnalysisData(QString name)
{
    QElapsedTimer timer;
    timer.start();
    qDebug()<<"m_result size:"<<m_result.size()<<name;
 
    int rank_val = 0;
    double amount_old = 0;
    int cnt = 0;
    int bigFlag = false;
    int bigRankUp = false; // 突然从很低的排名上升到大排名
    bool big_3000 = false;
    bool big_2000 = false;
    for(auto it:m_result){
        if(it["name"].toString() == name){
//            qDebug()<<it["name"].toString()<<it["close"].toString()<<it["amount_rank"].toString()<<it["time_trade"].toString();
            cnt++;
            QList<QStandardItem*> items;
            items.append(new QStandardItem(it["name"].toString()));
            double market_capital = it["market_capital"].toDouble() / 100000000;
            items.append(new QStandardItem(QString::number(market_capital)));
            items.append(new QStandardItem(it["close"].toString()));
 
            QStandardItem *percentItem = new QStandardItem(it["percent"].toString());
            if(percentItem->text().toDouble() > 0){
                percentItem->setData(QColor("red"),Qt::DecorationRole); // 添加装饰颜色为红色
                percentItem->setData(QColor("red"),Qt::TextColorRole); // 添加文本颜色为红色
                items.at(0)->setData(QColor("red"),Qt::TextColorRole); // 设置股票名字为红色
 
            }else if(percentItem->text().toDouble() < 0){
                percentItem->setData(QColor("green"),Qt::DecorationRole); // 添加装饰颜色为红色
                percentItem->setData(QColor("green"),Qt::TextColorRole); // 设置背景颜色为绿色
                items.at(0)->setData(QColor("green"),Qt::TextColorRole); // 设置股票名字为绿色
            }
            items.append(percentItem);
            //items.append(new QStandardItem(it["percent"].toString()));
            items.append(new QStandardItem(it["amount_rank"].toString()));
            items.append(new QStandardItem(it["time_trade"].toString()));
            QString amount_rank = it["amount_rank"].toString();
            if(rank_val != 0){
                double rank_rate = 1 - (amount_rank.toDouble()/rank_val);
                //items.append(new QStandardItem(QString::number(rank_rate)));
 
                QStandardItem * amount_rank_item = new QStandardItem(QString::number(rank_rate));
                if(rank_rate > 0.9){
                    amount_rank_item->setData(QColor("red"),Qt::BackgroundColorRole);
                    bigFlag = true;
                }
                if(rank_rate > 0.7 && rank_val >= 1000 && amount_rank.toInt() < 1000){
                    bigRankUp = true;
                    bigFlag = true;
                    if(rank_rate > 0.9){
                        // 紫色
                        amount_rank_item->setData(QColor(170, 85, 255),Qt::BackgroundColorRole);
                    }else{
                        // 粉色
                        amount_rank_item->setData(QColor("pink"),Qt::BackgroundColorRole);
                    }
 
 
 
                    if(rank_val >= 3000){
                        amount_rank_item->setData(QColor(85,170,255),Qt::BackgroundColorRole);
                        big_3000 = true;
                    }
                    else if(rank_val >= 2000){
                        amount_rank_item->setData(QColor(85,255,255),Qt::BackgroundColorRole);
                        big_2000 = true;
                    }
 
 
 
 
                }
                items.append(amount_rank_item);
                rank_val = amount_rank.toInt(); // 到后面再赋值
 
            }
            if(rank_val == 0 && amount_rank.toInt() != 0){
                rank_val = amount_rank.toInt();
            }
 
            double amount = it["amount"].toDouble();
            QStandardItem *amountItem = new QStandardItem(QString::number(amount/100000000));
            if(amount_old > 0){
                if(amount / amount_old >= 3){
                    amountItem->setData(QColor("red"),Qt::BackgroundColorRole);
                    amountItem->setData(QString::number(amount / amount_old),Qt::ToolTipRole);
                }
            }
            items.append(amountItem);
            amount_old = amount;
 
 
            m_model->appendRow(items);
            if(bigFlag){
                QList<QStandardItem*> items_2;
                for(int i=0;i<items.size();++i){
                    items_2.append(new QStandardItem(items.at(i)->text()));
                    if(i==6 && items.at(i)->text().toDouble() > 0.95){
                        items_2.back()->setData(QColor("red"),Qt::BackgroundColorRole);
                    }else if(i==6 && bigRankUp){
                        if(items.at(i)->text().toDouble() > 0.9){
                            items_2.back()->setData(QColor(170, 85, 255),Qt::BackgroundColorRole);
                        }else{
                            items_2.back()->setData(QColor("pink"),Qt::BackgroundColorRole);
                        }
 
                        if(big_3000){
                            items_2.back()->setData(QColor(85,170,255),Qt::BackgroundColorRole);
                        }
                        if(big_2000){
                            items_2.back()->setData(QColor(85,255,255),Qt::BackgroundColorRole);
                        }
 
                    }else if(i==7){
                        // 看交易额变化
                    }
                }
                m_modelResult->appendRow(items_2);
                bigFlag = false;
                bigRankUp = false;
                big_3000 = false;
                big_2000 = false;
 
                // 保存名字信息:code-name
                m_codes.insert(it["code"].toString(),name);
            }
        }
 
    }
    qDebug()<<"cnt="<<cnt;
    qDebug() << "Query executed in" << timer.elapsed() << "ms";
    qDebug()<<"滑动窗口排名-cnt:"<<m_model->rowCount()<<cnt<<"start:"<<m_model->rowCount() - cnt + 1;
    calcWindowRank(m_model->rowCount() - cnt + 1);
 
    // 更新时间
 
}
void AnalysisByRedisCache::on_pushButton_analysisByRedis_clicked()
{
    // 单个分析
    QString name = ui->lineEdit_name->text();
    searchAndAnalysisData(name);
 
}
 
void AnalysisByRedisCache::on_pushButton_batchAnalysis_clicked()
{
 
    // 批量分析
    // 重设模型的行数为0,清空一下数据
    m_model->setRowCount(0);
    m_modelResult->setRowCount(0);
 
    int batch_num = ui->comboBox_batchNum->currentText().toInt(); // 批量分析个股的数量
    // 拿出个股交易额排名的前N名,进行缓存分析
    emit getStockNamesSignal(batch_num);
    qDebug()<<"批量分析完成";
 
}
 
void AnalysisByRedisCache::on_pushButton_updateStrategyTime_clicked()
{
    // 更新策略的时间
    updateAllFindTimeSlot();
}