-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistwindow.cpp.autosave
110 lines (84 loc) · 3.29 KB
/
listwindow.cpp.autosave
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
#include "listwindow.h"
#include "ui_listwindow.h"
const int dayJumps[] = {1, 5, 30, 90, 180, 365, 1825}; // 1d, 1w, 1m, 3m, 6m, 1y, 5y
const int maxJumps = sizeof(dayJumps)/sizeof(dayJumps[0]);
ListWindow::ListWindow(QWidget *parent, QTableWidget *stockListTableWidget, qint64 fromDate, qint64 toDate) :
QWidget(parent),
ui(new Ui::ListWindow),
stockListTableWidget(stockListTableWidget),
fromDate(fromDate),
toDate(toDate)
{
ui->setupUi(this);
// Set up the table
clearTable();
// Set the limit
ui->limitComboBox->addItems({"1D", "1W", "1M", "3M", "6M", "1Y", "5Y"});
// Connect to yahoo finance
connect(&yahooFinance, &YahooFinance::transmitDownloadResult, this, &ListWindow::receiveResult);
}
ListWindow::~ListWindow()
{
delete ui;
}
void ListWindow::receiveResult(QByteArray data){
// Split the data
QString text = QString(data);
QStringList textRows = text.split('\n');
// Check if we have more than two lines = available data
int rowLength = textRows.length();
if(rowLength > 1){
// Prepare rows
QString stockName = stockListTableWidget->item(receivedResults, 0)->text();
ui->tableWidget->setRowCount(receivedResults+1);
// Read all lines
int dayIndex = 0;
QList<float> closePrices;
for(int i = rowLength; i >= 1; i--){
// Jump to 1d, 1w, 1m, 3m, 6m, 1y, 5y
i -= dayJumps[dayIndex++];
// Add the close price
QStringList row = textRows.at(i).split(',');
float closePrice = row.at(4).toFloat();
closePrices.append(closePrice);
// This is important so we don't over indexing
if(dayIndex >= maxJumps || dayIndex > ui->limitComboBox->currentIndex())
break;
}
// Convert to percent
for(int i = 0; i < closePrices.length(); i++){
float lastClose
}
// Set the numbers
ui->tableWidget->setItem(receivedResults, 0, new QTableWidgetItem(stockName));
ui->tableWidget->setItem(receivedResults, dayIndex, new QTableWidgetItem(QString::number(closePrice)));
}
// Count
receivedResults++;
// Check
int rows = stockListTableWidget->rowCount();
if(rows > receivedResults){
QString stockName = stockListTableWidget->item(receivedResults, 0)->text();
yahooFinance.stockCSVDownload(stockName, fromDate, toDate); // When this is called, then this function receiveResult will be called too
}else{
qDebug() << "done";
/* Analysstrategi:
* Visa alla tillfällen: 1d, 1w, 1m, 3m, 6m, 1y, 5y
* Omplacera grundat på de som har störst procentuella förändring, högst upp på toppen
* */
}
}
void ListWindow::on_sortPushButton_clicked()
{
// Reset
clearTable();
receivedResults = 0;
// This will trigger the download
QString stockName = stockListTableWidget->item(0, 0)->text();
yahooFinance.stockCSVDownload(stockName, fromDate, toDate);
}
void ListWindow::clearTable(){
ui->tableWidget->clear();
ui->tableWidget->setColumnCount(maxJumps + 1); // + 1 because the stock name column
ui->tableWidget->setHorizontalHeaderLabels({"Stock name", "1D", "1W", "1M", "3M", "6M", "1Y", "5Y"});
}