From 8ea6d363df77de2dca288397da8d4f9c3d3a5c4d Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期五, 18 十月 2024 18:41:57 +0800 Subject: [PATCH] '项目完善' --- CBTrade/MainFrame.cpp | 324 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 264 insertions(+), 60 deletions(-) diff --git a/CBTrade/MainFrame.cpp b/CBTrade/MainFrame.cpp index e4259a8..d376d93 100644 --- a/CBTrade/MainFrame.cpp +++ b/CBTrade/MainFrame.cpp @@ -34,7 +34,7 @@ SetIcon(icon); //SetTransparent(225); - + viewManager = new ViewManager(); viewManager->initView(this); @@ -44,7 +44,7 @@ initTask(); Bind(wxEVT_SHOW, [this](wxShowEvent& event) { this->SetWindowStyleFlag(GetWindowStyleFlag() | wxSTAY_ON_TOP); - }); + }); this->SetWindowStyleFlag(GetWindowStyleFlag() | wxSTAY_ON_TOP); } @@ -83,8 +83,7 @@ wxMessageBox("输入格式错误"); return; } - MyConfigUtil::setSellMoney(stoi(value.c_str())); - wxMessageBox("锁定卖出金额成功"); + lockSellVolume(); } @@ -105,8 +104,141 @@ sellBtn->Bind(wxEVT_BUTTON, [this](wxCommandEvent& event) { wxButton* btn = (wxButton*)event.GetEventObject(); this->viewManager->sellWidgets->sellMoney->SetLabelText(btn->GetLabelText()); + // 锁定量 + if (this->viewManager->sellWidgets->sellLock->IsChecked()) { + lockSellVolume(); + } }); } + + viewManager->sellWidgets->openTrade->Bind(wxEVT_BUTTON, [this](wxCommandEvent& event) { + try { + string result = MyNetworkApi::set_trade_state(1); + auto doc = JsonUtil::parseUTF16(result); + if (doc.IsObject()) { + if (doc[L"code"] == 0) { + this->setTradeStateView(1); + } + else { + throw wstring(doc[L"msg"].GetString()); + } + } + else { + throw wstring(L"网络请求失败"); + } + } + catch (wstring st) { + wxMessageBox(st); + } + }); + + + viewManager->sellWidgets->closeTrade->Bind(wxEVT_BUTTON, [this](wxCommandEvent& event) { + try { + string result = MyNetworkApi::set_trade_state(0); + auto doc = JsonUtil::parseUTF16(result); + if (doc.IsObject()) { + if (doc[L"code"] == 0) { + this->setTradeStateView(0); + } + else { + throw wstring(doc[L"msg"].GetString()); + } + } + else { + throw wstring(L"网络请求失败"); + } + } + catch (wstring st) { + wxMessageBox(st); + } + }); + + + + viewManager->sellWidgets->addWantBuy->Bind(wxEVT_BUTTON, [this](wxCommandEvent& event) { + try { + wxString code = this->viewManager->topWidgets->codeEdit->GetValue(); + if (code.Length() != 6) { + throw wstring(L"代码格式错误"); + } + string result = MyNetworkApi::add_want_buy_code(code.ToStdString()); + auto doc = JsonUtil::parseUTF16(result); + if (doc.IsObject()) { + if (doc[L"code"] == 0) { + wxMessageBox("添加成功"); + } + else { + throw wstring(doc[L"msg"].GetString()); + } + } + else { + throw wstring(L"网络请求失败"); + } + } + catch (wstring st) { + wxMessageBox(st); + } + }); + + + viewManager->sellWidgets->removeWantBuy->Bind(wxEVT_BUTTON, [this](wxCommandEvent& event) { + try { + wxString code = this->viewManager->topWidgets->codeEdit->GetValue(); + if (code.Length() != 6) { + throw wstring(L"代码格式错误"); + } + string result = MyNetworkApi::remove_want_buy_code(code.ToStdString()); + auto doc = JsonUtil::parseUTF16(result); + if (doc.IsObject()) { + if (doc[L"code"] == 0) { + wxMessageBox("移除成功"); + } + else { + throw wstring(doc[L"msg"].GetString()); + } + } + else { + throw wstring(L"网络请求失败"); + } + } + catch (wstring st) { + wxMessageBox(st); + } + }); + + viewManager->sellWidgets->wantBuyList->Bind(wxEVT_BUTTON, [this](wxCommandEvent& event) { + try { + string result = MyNetworkApi::list_want_buy_codes(); + auto doc = JsonUtil::parseUTF16(result); + if (doc.IsObject()) { + if (doc[L"code"] == 0) { + wxString st = ""; + auto array = doc[L"data"].GetArray(); + for (int i = 0; i < array.Size(); i++) { + st.Append(array[i].GetArray()[0].GetString()); + st.Append("("); + st.Append(array[i].GetArray()[1].GetString()); + st.Append(")"); + if (i % 2 == 1) { + st.Append("\n"); + } + } + wxMessageBox(st, "想买单"); + } + else { + throw wstring(doc[L"msg"].GetString()); + } + } + else { + throw wstring(L"网络请求失败"); + } + } + catch (wstring st) { + wxMessageBox(st); + } + }); + } @@ -129,32 +261,46 @@ wxAcceleratorEntry(wxACCEL_SHIFT, WXK_F10, 901), wxAcceleratorEntry(wxACCEL_CTRL, 'H', 902) }; - + auto acceleratorTable = wxAcceleratorTable(2, entries); this->SetAcceleratorTable(acceleratorTable); this->Bind(wxEVT_MENU, [this](wxCommandEvent& event) { this->OnBtnSellClick(event); - }, 901); + }, 901); this->Bind(wxEVT_MENU, [this](wxCommandEvent& event) { for (std::list<PositionInfo>::iterator e = this->positionList.begin(); e != this->positionList.end(); e++) { - PositionInfo info = *e; - if (this->selectPositionId == info.id) { - try { - Sleep(200); - CallAfter([info]() { - thread t(Win32Util::addToTHS, info.underlyingMarketInfo.code.ToStdString()); - t.detach(); - }); - - } - catch (...) { + PositionInfo info = *e; + if (this->selectPositionId == info.id) { + try { + Sleep(200); + CallAfter([info]() { + thread t(Win32Util::addToTHS, info.underlyingMarketInfo.code.ToStdString()); + t.detach(); + }); + } + catch (...) { + + } + break; } - break; + } + + }, 902); + + //-----------------------获取当前交易状态-------------------------- + try { + string result = MyNetworkApi::get_trade_state(); + auto doc = JsonUtil::parseUTF8(result); + if (doc.IsObject() && doc["code"].GetInt() == 0) { + tradeState = doc["data"]["state"].GetInt(); + setTradeStateView(tradeState); } } - - }, 902); + catch (...) { + + } + // -----------------------卖出参数初始化-------------------------- @@ -191,26 +337,38 @@ } requestCount += 1; - std::list < PositionInfo> plist = DataParseUtil::parsePositionList(result); + std::list<PositionInfo> plist = DataParseUtil::parsePositionList(result); + if (plist.size() == 0) { + string nowTime = TimeUtil::format(TimeUtil::getNowTimeStamp(), "%H%M%S"); + if (stoi(nowTime) < stoi("093000")) { + MyConfigUtil::clearCodesSellMoney(); + } + } //根据持仓排序, plist.sort([](PositionInfo p1, PositionInfo p2) { - return p1.availablePosition == p2.availablePosition? p1.createTime > p2.createTime: p1.availablePosition > p2.availablePosition; - }); + + int p1Int = p1.currentPosition > 0 ? 1 : 0; + int p2Int = p2.currentPosition > 0 ? 1 : 0; - - if (requestCount>1&&this->orginPositionList.size() != plist.size()) { - CallAfter([this]() { - this->blinkingMessageDialog = new BlinkingMessageDialog(this, "可转债有新下单"); - this->blinkingMessageDialog->show(); + return p1Int == p2Int ? p1.createTime > p2.createTime: p1Int > p2Int; }); + + + + + if (requestCount > 1 && this->orginPositionList.size() != plist.size()) { + CallAfter([this]() { + this->blinkingMessageDialog = new BlinkingMessageDialog(this, "可转债有新下单"); + this->blinkingMessageDialog->show(); + }); } this->orginPositionList = plist; viewManager->positionWidgets->listCtrlReport->DeleteAllItems(); - + // 按条件过滤 std::list < PositionInfo> temList; for (std::list<PositionInfo>::iterator e = plist.begin(); e != plist.end(); ++e) { @@ -232,12 +390,14 @@ PositionInfo p = *e; //{ "名称", "涨幅", "现价", "L1现手", "剩余持仓", "持仓金额", "盈亏", "盈亏比", "正股名称", "更多" }; wxVector<wxVariant> data; - bool hasPosition = p.availablePosition > 0; - wxColor color = !hasPosition ? COLOR_INVALID: COLOR_VALID; - data.push_back(wxVariant(MyColorText(p.securityName, color))); + bool hasPosition = p.currentPosition > 0; + wxColor color = !hasPosition ? COLOR_INVALID : COLOR_VALID; + map<int, wxVariant> dataMap; + + dataMap[0] = wxVariant(MyColorText(p.securityName, color)); if (!hasPosition) { - data.push_back(wxVariant(MyColorText(p.marketInfo.rate,color))); + dataMap[1] = wxVariant(wxVariant(MyColorText(p.marketInfo.rate, color))); } else { wxString rateText = p.marketInfo.rate; @@ -258,17 +418,19 @@ else { tempColor = wxColor(64, 64, 64); } - data.push_back(wxVariant(MyColorText(p.marketInfo.rate, tempColor))); + dataMap[1] = (wxVariant(MyColorText(p.marketInfo.rate, tempColor))); } - data.push_back(wxVariant(MyColorText(wxString(p.marketInfo.price),color))); - data.push_back(wxVariant(MyColorText(p.marketInfo.lastVolume, color))); - data.push_back(wxVariant(MyColorText(p.underlyingMarketInfo.rate, color))); + dataMap[7] = (wxVariant(MyColorText(wxString(p.marketInfo.price), color))); + // 获取卖额 + int sellMoney = MyConfigUtil::getSellMoney(p.securityID.ToStdString()); + + dataMap[6] = (wxVariant(MyColorText(to_string(sellMoney), color))); + dataMap[2] = wxVariant(MyColorText(p.underlyingMarketInfo.rate, color)); if (!hasPosition) { - data.push_back(wxVariant(MyColorText(p.underlyingMarketInfo.buy1Money, color))); - } - else { - data.push_back(wxVariant(MyColorText(p.underlyingMarketInfo.buy1Money, wxColor(204, 78, 232)))); + dataMap[3]=wxVariant(MyColorText(p.underlyingMarketInfo.buy1Money, color)); + } else { + dataMap[3] = wxVariant(MyColorText(p.underlyingMarketInfo.buy1Money, wxColor(204, 78, 232))); } @@ -280,10 +442,10 @@ positionMoney = price * p.currentPosition; } if (!hasPosition) { - data.push_back(wxVariant(MyColorText(StringUtil::to_string(positionMoney, 2),color))); + dataMap[4]=wxVariant(MyColorText(StringUtil::to_string(positionMoney, 2), color)); } else { - data.push_back(wxVariant(MyColorText(StringUtil::to_string(positionMoney, 2), *wxRED))); + dataMap[4]=wxVariant(MyColorText(StringUtil::to_string(positionMoney, 2), *wxRED)); } @@ -301,7 +463,7 @@ p.todayCommission.ToDouble(&todayCommission); totalPosCost += todayCommission; wxString cost_price = StringUtil::to_string(totalPosCost / totalVolume, 3); - data.push_back(wxVariant(MyColorText(cost_price, color))); + dataMap[8] = (wxVariant(MyColorText(cost_price, color))); // 今日盈亏计算: 总卖出金额 + 持仓*现价 - 买入金额 - 手续费 double totalSellMoney = 0; for (std::list<DealInfo>::iterator e = p.sellList.begin(); e != p.sellList.end(); e++) { @@ -315,18 +477,22 @@ double profit_rate = profit_money / totalPosCost; wxString profit_rate_str = ""; profit_rate_str.Append(StringUtil::to_string(profit_rate * 100)).Append("%"); - data.push_back(wxVariant(MyColorText(profit_rate_str, color))); + dataMap[5] = (wxVariant(MyColorText(profit_rate_str, color))); // 正股数据设置 if (hasPosition) { - data.push_back(wxVariant(MyButton( p.underlyingMarketInfo.code,"查看",wxColor(64,64,64), wxColor(225,225,225)))); + dataMap[9] = (wxVariant(MyButton(p.underlyingMarketInfo.code, "查看", wxColor(64, 64, 64), wxColor(225, 225, 225)))); } else { - data.push_back(wxVariant(MyButton(p.underlyingMarketInfo.code, "查看", color, wxColor(255,255,255)))); + dataMap[9] = (wxVariant(MyButton(p.underlyingMarketInfo.code, "查看", color, wxColor(255, 255, 255)))); } - data.push_back(wxVariant(MyColorText(wxString(to_string(p.currentPosition).c_str()),color))); - data.push_back(wxVariant(MyColorText(StringUtil::to_string(profit_money, 2), color))); - data.push_back(wxVariant(MyColorText(p.underlyingMarketInfo.name, color))); + dataMap[10] = (wxVariant(MyColorText(wxString(to_string(p.currentPosition).c_str()), color))); + dataMap[11] = (wxVariant(MyColorText(StringUtil::to_string(profit_money, 2), color))); + dataMap[12] = (wxVariant(MyColorText(p.underlyingMarketInfo.name, color))); + + for (const auto& pair : dataMap) { + data.push_back(pair.second); + } viewManager->positionWidgets->listCtrlReport->AppendItem(data); if (selectPositionId == p.id) { @@ -337,7 +503,7 @@ DealInfo info = *e; double price = stod(info.price.ToStdString().c_str()); double rate = (price - p.marketInfo.preClosePrice) * 100 / p.marketInfo.preClosePrice; - tradePoints.push_back(TickTradeData({ info.tradeTime, (float)rate, info.price.ToStdString(), info.volume, StringUtil::to_string(info.volume * stod(info.price.ToStdString().c_str())) })); + tradePoints.push_back(TickTradeData({ info.tradeTime, (float)rate, info.price.ToStdString(), info.volume, StringUtil::to_string(info.volume * stod(info.price.ToStdString().c_str())), info.type })); } viewManager->tickWidgets->tickChart->SetBuyPoint(tradePoints); tradePoints.clear(); @@ -351,7 +517,7 @@ } index += 1; } - viewManager->positionWidgets->listCtrlReport->Refresh(); + viewManager->positionWidgets->listCtrlReport->HandleDataUpdate(); } void MainFrame::requestMoney() @@ -426,11 +592,11 @@ LRESULT MainFrame::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) { - cout <<"MSWWindowProc:"<< nMsg << endl; + cout << "MSWWindowProc:" << nMsg << endl; if (nMsg == WM_COPYDATA) { COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam; if (pcds != NULL) { - int action_code = pcds->dwData; + int action_code = pcds->dwData; if (action_code == 100) { // 卖 BYTE* pBuf = (BYTE*)(pcds->lpData); @@ -461,6 +627,24 @@ MyConfigUtil::setPositionColumnWidth(positionWidthList); event.Skip(); exit(0); +} + +void MainFrame::lockSellVolume() +{ + string value = viewManager->sellWidgets->sellMoney->GetValue().ToStdString(); + if (selectPositionId.IsEmpty()) { + MyConfigUtil::setSellMoney(stoi(value.c_str())); + //wxMessageBox("锁定卖出金额成功"); + } + else { + for (std::list<PositionInfo>::iterator e = orginPositionList.begin(); e != orginPositionList.end(); ++e) { + if ((*e).id == selectPositionId) { + MyConfigUtil::setSellMoney((*e).securityID.ToStdString(), stoi(value.c_str())); + //wxMessageBox("锁定卖出金额成功"); + break; + } + } + } } void MainFrame::OnBtnRefreshClick(wxCommandEvent& event) @@ -577,7 +761,11 @@ that->viewManager->tickWidgets->tickChart->AddTickData(TimeUtil::format((long)tick->created_at, "%H:%M:%S"), 100 * (tick->price - info.marketInfo.preClosePrice) / info.marketInfo.preClosePrice, to_string(tick->price)); } else if (info.underlyingMarketInfo.code == symbol) { - that->viewManager->tickWidgets->tickChart->AddUnderlyingTickData(TimeUtil::format((long)tick->created_at, "%H:%M:%S"), 100 * (tick->price - info.underlyingMarketInfo.preClosePrice) / info.underlyingMarketInfo.preClosePrice); + list<Quote> quotes; + for (int i = 0; i < 5; i++) { + quotes.push_back(tick->quotes[i]); + } + that->viewManager->tickWidgets->tickChart->AddUnderlyingTickData(TimeUtil::format((long)tick->created_at, "%H:%M:%S"), 100 * (tick->price - info.underlyingMarketInfo.preClosePrice) / info.underlyingMarketInfo.preClosePrice, quotes); } break; } @@ -592,6 +780,19 @@ return info; } } +} + +void MainFrame::setTradeStateView(int state) +{ + if (state > 0) { + viewManager->sellWidgets->openTrade->SetForegroundColour(*wxRED); + viewManager->sellWidgets->closeTrade->SetForegroundColour(*wxBLACK); + } + else { + viewManager->sellWidgets->openTrade->SetForegroundColour(*wxBLACK); + viewManager->sellWidgets->closeTrade->SetForegroundColour(*wxRED); + } + } @@ -668,7 +869,7 @@ DealInfo info = *e; double price = stod(info.price.ToStdString().c_str()); double rate = (price - p.marketInfo.preClosePrice) * 100 / p.marketInfo.preClosePrice; - this->viewManager->tickWidgets->tickChart->AddBuyPoint(info.tradeTime, rate, info.price.ToStdString(), info.volume, StringUtil::to_string(info.volume * stod(info.price.ToStdString().c_str()))); + this->viewManager->tickWidgets->tickChart->AddBuyPoint(info.tradeTime, rate, info.price.ToStdString(), info.volume, StringUtil::to_string(info.volume * stod(info.price.ToStdString().c_str())), info.type); } for (std::list<DealInfo>::iterator e = p.sellList.begin(); e != p.sellList.end(); ++e) { DealInfo info = *e; @@ -679,7 +880,7 @@ wxRichTextAttr style; - wxRichTextCtrl *richText = this->viewManager->tickWidgets->codeInfoLabel; + wxRichTextCtrl* richText = this->viewManager->topWidgets->codeInfoLabel; richText->GetBuffer().Clear(); richText->Clear(); @@ -715,8 +916,8 @@ codeInfoStr = ""; codeInfoStr.Append(",板块:"); - - + + auto blocks = p.underlyingDetailInfo.blocks; i = 0; for (std::list<wxString>::iterator e = blocks.begin(); e != blocks.end(); ++e) { @@ -742,7 +943,7 @@ } codeInfoStr.Append(","); - + codeInfoStr.Append("市值:").Append(p.underlyingDetailInfo.zyltgb).Append(","); codeInfoStr.Append("股价:").Append(p.underlyingDetailInfo.price).Append(","); @@ -751,6 +952,9 @@ richText->WriteText(codeInfoStr); tickDataRequestStrategy->subscribeSymbol(JueJinDataUtil::getSymbol(p.securityID.ToStdString()), JueJinDataUtil::getSymbol(p.underlyingMarketInfo.code.ToStdString())); + // 获取设置的卖出金额 + int sellMoney = MyConfigUtil::getSellMoney(p.securityID.ToStdString()); + viewManager->sellWidgets->sellMoney->SetLabelText(to_wstring(sellMoney)); } else { selectPositionId = ""; -- Gitblit v1.8.0