Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*副本.*
# C++ objects and libs
*.slo
*.lo
Expand Down
979 changes: 483 additions & 496 deletions candlesticksview.cpp

Large diffs are not rendered by default.

114 changes: 54 additions & 60 deletions candlesticksview.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,99 +4,93 @@
#include <QWidget>

#include "qcustomplot.h"
#include "qcpcandlechart.h"
#include "qcpcandleticker.h"
#include "tradesummaryinfoview.h"
#include "common.h"
#include "datamanager.h"


class CandlesticksView : public QCustomPlot
{
Q_OBJECT
public:
explicit CandlesticksView(QWidget *parent = 0);
~CandlesticksView();

void setDataManager(DataManager *manager);
Stock * currentStock();

//data
signals:
void historicalDataRequested(QString *code, int offset);
void stockChanged(const QString &code);
void escape();


private slots:
void mouseDoubleClickEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
void keyPressEvent(QKeyEvent *event);


//区间统计
void intervalStatistics();

//区间放大
void intervalZoomin();

//右键
void contextMenuRequest(QPoint pos);

//设置坐标轴范围
void setAxisRange2(const QCPRange &newRange, const QCPRange &oldRange);
void setAxisRange();

//绘制蜡烛图
void drawCandlesticks();


//生成交易信息小窗口和十字光标
void createTradeInfoView();

//更新交易信息小窗口内容及十字光标
void updateTradeInfoView(const QPoint &pos, bool updateWhenInvisible = false);
void updateTradeInfoView(double curKey, double curValue, bool updateWhenInvisible = false);
void updateCrossCurvePoint(const QPointF &pos);

//显示及交易信息小窗口和十字光标是否可见
void setInfoViewVisible(bool visible);

//更新成交量柱坐标轴范围
void updateVolumeYAxisRange();

public slots:
void historicalDataRead(Stock *stock);
void showCandlesticks(const QString &code);


void showStock(const QString &code);
private:
bool isEmpty();
private:
DataManager *m_dataManager;
Stock *m_curStock;
QString m_stockCode; //证券代码
QString m_stockName; //证券名称
QString m_stockCodeExpected; //证券代码


// PeriodType m_periodType; //数据周期类型
QCPFinancialDataMap *m_ohlcDataMap; //基本交易数据
QSharedPointer<QCPFinancialDataContainer> m_ohlcData; //基本交易数据
typedef QMap<double, TradeExtraData> TradeExtraDataMap;
TradeExtraDataMap *m_tradeExtraDataMap; //交易数据

QCPPlotTitle *m_plotTitle;
QCPFinancial *m_candlesticks; //绘图类
QCPAxis *m_yAxis; //Y轴
QCPItemLine *m_horizontalLine, *m_verticalLine; //十字光标
//candle chart
private:
void initCandlesticks();
void initTickAndGridStyle(QCPAxis* axis);
void initTracer(QCPItemTracer* tracer);
void initTracerText();

double getFocusKey(const QPointF& point);
QPointF getFocusPoint(const double& key);

double getZoomCenter();

QCPAxisRect *volumeAxisRect;
private slots:
void zoom(double zoomFactor);
void adjustAllAndReplot();
void adjustVolumeYRange();
private:
QCPCandleChart *m_candleChart; //绘图类
QCPAxisRect* m_vAxisRect;
QCPBars *m_volumePos; //成交量柱
QCPBars *m_volumeNeg;
QCPAxis *m_volumeLeftAxis;
QCPAxis *m_vxAxis, *m_vyAxis;

TradeSummaryInfoView *m_infoView; //交易信息小窗口
//control
signals:
void escape();
protected:
void mouseDoubleClickEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
void keyPressEvent(QKeyEvent *event);
void resizeEvent(QResizeEvent *event);
private:
bool m_draggingByMouse;
double m_leftKey, m_focusedKey, m_rightKey; //X轴的左中右坐标

//indicator
//cross and info view
private:
QCPItemTracer* m_tracerCandle, *m_tracerVolume;
bool m_tracerVisible;

QCPItemText* m_leftTracerLabel, *m_rightTracerLabel;
QCPItemText* m_leftVLabel, *m_rightVLabel, *m_bottomTracerLabel;
private slots:
void setTracerAndInfoVisible(bool visible=true);
private:
void updateTracerAndInfo(const QPoint& pos);
void updateTracerAndInfoInner(const double& key, const double& candleValue,\
const double& volumeValue, const bool& bInCandle,\
const bool& bInVolume);
void moveTracer(const int& step);
};

#endif // CANDLESTICKSVIEW_H
54 changes: 39 additions & 15 deletions datamanager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "datamanager.h"
#include "datamanager.h"

#include "qcustomplot.h"
#include "stock.h"
Expand Down Expand Up @@ -156,7 +156,7 @@ bool DataManager::readHistoricalTradeDataFile(const QString &fileName){
qDebug()<<title;

Stock *stock = 0;
QMap<double, QCPFinancialData> *ohlcDataMap = 0;
QSharedPointer<QCPFinancialDataContainer> ohlcDataMap;
QMap<double, TradeExtraData> *tradeExtraDataMap = 0;
QVector<double> *futuresDeliveryDates = 0;

Expand All @@ -177,7 +177,7 @@ bool DataManager::readHistoricalTradeDataFile(const QString &fileName){
m_allStocks->insert(code, stock);
}

ohlcDataMap = stock->ohlcDataMap();
ohlcDataMap = stock->ohlcDataContainer();
tradeExtraDataMap = stock->tradeExtraDataMap();
futuresDeliveryDates = stock->futuresDeliveryDates();
}
Expand Down Expand Up @@ -214,7 +214,8 @@ bool DataManager::readHistoricalTradeDataFile(const QString &fileName){
double turnover = dataList.at(12).toDouble();
double exchangeRatio = dataList.at(10).toDouble();

ohlcDataMap->insert(index, QCPFinancialData(index, open, high, low, close));
ohlcDataMap->add(QCPFinancialData(index, open, high, low, close));
//ohlcDataMap->insert(index, QCPFinancialData(index, open, high, low, close));
tradeExtraDataMap->insert(index, TradeExtraData(time_t, preClose, volume, turnover, exchangeRatio));

//数据文件为倒序。不可使用日期做KEY,日期有空档。
Expand Down Expand Up @@ -639,7 +640,7 @@ bool DataManager::loadHistoricalTradeData(Stock * stock){
return false;
}

QMap<double, QCPFinancialData> *ohlcDataMap = stock->ohlcDataMap();
QSharedPointer<QCPFinancialDataContainer> ohlcDataMap = stock->ohlcDataContainer();
QMap<double, TradeExtraData> *tradeExtraDataMap = stock->tradeExtraDataMap();

uint index = (std::numeric_limits<uint>::max)();
Expand All @@ -656,7 +657,7 @@ bool DataManager::loadHistoricalTradeData(Stock * stock){
double exchangeRatio = query.value(idx++).toDouble();


ohlcDataMap->insert(index, QCPFinancialData(index, open, high, low, close));
ohlcDataMap->add(QCPFinancialData(index, open, high, low, close));
tradeExtraDataMap->insert(index, TradeExtraData(tradeDate, preClose, volume, turnover, exchangeRatio));

index--;
Expand Down Expand Up @@ -692,30 +693,53 @@ bool DataManager::saveHistoricalTradeData(Stock * stock){
return false;
}

QMap<double, QCPFinancialData> *ohlcDataMap = stock->ohlcDataMap();
QSharedPointer<QCPFinancialDataContainer> ohlcDataMap = stock->ohlcDataContainer();
QMap<double, TradeExtraData> *tradeExtraDataMap = stock->tradeExtraDataMap();

foreach (double index, ohlcDataMap->keys()) {
QCPFinancialData ohlcData = ohlcDataMap->value(index);
TradeExtraData tradeExtraDat = tradeExtraDataMap->value(index);
QCPFinancialDataContainer::const_iterator it = ohlcDataMap->constBegin();
while (it != ohlcDataMap->constEnd())
{
TradeExtraData tradeExtraDat = tradeExtraDataMap->value(it->key);
QDateTime dateTime = QDateTime::fromTime_t(tradeExtraDat.time);
dateTime.setTime(QTime(15, 0, 0));
query.prepare("INSERT INTO DailyTradeinfo(Code, TradeDate, Open, High, Low, Close, PreClose, Volume, Turnover, ExchangeRatio)"
"VALUES(:Code, :TradeDate, :Open, :High, :Low, :Close, :PreClose, :Volume, :Turnover, :ExchangeRatio); ");
query.bindValue(":Code", stock->code());
query.bindValue(":TradeDate", dateTime.toString("yyyy-MM-dd hh:mm:ss"));
query.bindValue(":Open", ohlcData.open);
query.bindValue(":High", ohlcData.high);
query.bindValue(":Low", ohlcData.low);
query.bindValue(":Close", ohlcData.close);
query.bindValue(":Open", it->open);
query.bindValue(":High", it->high);
query.bindValue(":Low", it->low);
query.bindValue(":Close", it->close);
query.bindValue(":PreClose", tradeExtraDat.preClose);
query.bindValue(":Volume", tradeExtraDat.volume);
query.bindValue(":Turnover", tradeExtraDat.turnover);
query.bindValue(":ExchangeRatio", tradeExtraDat.exchangeRatio);
query.exec();

qApp->processEvents();
}
it++;
}
// foreach (double index, ohlcDataMap->keys()) {
// QCPFinancialData ohlcData = ohlcDataMap->value(index);
// TradeExtraData tradeExtraDat = tradeExtraDataMap->value(index);
// QDateTime dateTime = QDateTime::fromTime_t(tradeExtraDat.time);
// dateTime.setTime(QTime(15, 0, 0));
// query.prepare("INSERT INTO DailyTradeinfo(Code, TradeDate, Open, High, Low, Close, PreClose, Volume, Turnover, ExchangeRatio)"
// "VALUES(:Code, :TradeDate, :Open, :High, :Low, :Close, :PreClose, :Volume, :Turnover, :ExchangeRatio); ");
// query.bindValue(":Code", stock->code());
// query.bindValue(":TradeDate", dateTime.toString("yyyy-MM-dd hh:mm:ss"));
// query.bindValue(":Open", ohlcData.open);
// query.bindValue(":High", ohlcData.high);
// query.bindValue(":Low", ohlcData.low);
// query.bindValue(":Close", ohlcData.close);
// query.bindValue(":PreClose", tradeExtraDat.preClose);
// query.bindValue(":Volume", tradeExtraDat.volume);
// query.bindValue(":Turnover", tradeExtraDat.turnover);
// query.bindValue(":ExchangeRatio", tradeExtraDat.exchangeRatio);
// query.exec();

// qApp->processEvents();
// }
statement = "Commit Transaction;";
if(!query.exec(statement)){
QSqlError error = query.lastError();
Expand Down
3 changes: 1 addition & 2 deletions datamanager.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef DATAMANAGER_H
#ifndef DATAMANAGER_H
#define DATAMANAGER_H

#include <QThread>
Expand All @@ -13,7 +13,6 @@ class QCPFinancialData;
class TradeExtraData;
class DownloadManager;


class DataManager : public QObject
{
Q_OBJECT
Expand Down
4 changes: 2 additions & 2 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "mainwindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include <QtConcurrent/QtConcurrent>

Expand Down Expand Up @@ -151,7 +151,7 @@ void MainWindow::networkError(const QUrl &url, const QString &errorString){

void MainWindow::stockActivated(Stock *stock){
qDebug()<<"stockActivated:"<<stock->name();
ui->tabCandlestick->showCandlesticks(stock->code());
ui->tabCandlestick->showStock(stock->code());
ui->stackedWidget->setCurrentWidget(ui->pageTradeInfo);
ui->tabCandlestick->setFocus();

Expand Down
22 changes: 20 additions & 2 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<item>
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="pageStocksList">
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -394,6 +394,24 @@
</layout>
</widget>
<widget class="QWidget" name="widgetRealTimeInfo" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>250</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>250</width>
<height>16777215</height>
</size>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<widget class="QLabel" name="labelCode">
Expand Down Expand Up @@ -758,7 +776,7 @@
<x>0</x>
<y>0</y>
<width>941</width>
<height>23</height>
<height>25</height>
</rect>
</property>
<widget class="QMenu" name="menu_System">
Expand Down
Loading