Hi,
for my little QT-project i want to write a function, which detect the current rate of stock.
Something like "double FinanceAPI::GetStockRate(QString stock_symbol)". The committed stock symbol represents the stock.
For these job i want to use the yahoo-finance-api. With a HTTP-request like...
http://finance.yahoo.com/d/quotes.cs...sn&ignore=.cvs
...in my webbrowser, i get a right answer. It is a CSV-file with the rate of stock "847402". My problem is how get these information with a qt-function.
This is what i try:
#include "financeapi.h"
FinanceAPI
::FinanceAPI(QObject *parent
) :{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
}
FinanceAPI::~FinanceAPI(){
delete manager;
}
void FinanceAPI::replyFinished(QNetworkReply *reply)
{
if(reply->error()!=QNetworkReply::NoError){
qDebug()<<"Fehlerhafte Anfrage\n";
}
else{
if(!file.
open(QFile::WriteOnly)) {
qDebug()<<"test";
}
//file.write(*answer);
out<<answer;
file.flush();
file.close();
}
}
double FinanceAPI
::GetStockRate(QString stock_symbol
){
QString tmpQuery
="http://finance.yahoo.com/d/quotes.csv?s="+stock_symbol
+"&f=l1sn&ignore=.cvs";
//"http://ichart.finance.yahoo.com/table.csv?s="+stock_symbol+"&f=l1sn&ignore=.cvs";// manager
->get
(QNetworkRequest
(QUrl(tmpQuery
)));
QNetworkRequest request;
request.
setUrl(QUrl(tmpQuery
));
manager->get(request);
return 0.0;
}
#include "financeapi.h"
FinanceAPI::FinanceAPI(QObject *parent) :
QObject(parent)
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
}
FinanceAPI::~FinanceAPI(){
delete manager;
}
void FinanceAPI::replyFinished(QNetworkReply *reply)
{
if(reply->error()!=QNetworkReply::NoError){
qDebug()<<"Fehlerhafte Anfrage\n";
}
else{
QByteArray bytes = reply->readAll();
QString answer = QString::fromUtf8(bytes);
QFile file("test.csv");
if(!file.open(QFile::WriteOnly))
{
qDebug()<<"test";
}
//file.write(*answer);
QTextStream out(&file);
out<<answer;
file.flush();
file.close();
}
}
double FinanceAPI::GetStockRate(QString stock_symbol){
QString tmpQuery="http://finance.yahoo.com/d/quotes.csv?s="+stock_symbol+"&f=l1sn&ignore=.cvs";//"http://ichart.finance.yahoo.com/table.csv?s="+stock_symbol+"&f=l1sn&ignore=.cvs";//
manager->get(QNetworkRequest(QUrl(tmpQuery)));
QNetworkRequest request;
request.setUrl(QUrl(tmpQuery));
manager->get(request);
return 0.0;
}
To copy to clipboard, switch view to plain text mode
My call of the function looks like:
FinanceAPI *tmpFinance = new FinanceAPI();
double rate =tmpFinance->GetStockRate("847402.DE"));
FinanceAPI *tmpFinance = new FinanceAPI();
double rate =tmpFinance->GetStockRate("847402.DE"));
To copy to clipboard, switch view to plain text mode
Somethings goes wrong respectively i don't understand the working of the...
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
To copy to clipboard, switch view to plain text mode
Because I think the answer from the request (-> the rate) come not in the "GetStockRate()", but rather "replyFinished()". But I needed at the end of the "GetStockRate()"-function to use it as return value.
Thanks for helping
Bookmarks