: | : | :期货量化学习 | :期货量化 |
返回列表 发帖

鼎元全自动C++软件【策略设计:源文件test.cpp】说明

鼎元全自动C++软件【策略设计:源文件test.cpp】说明

test.cpp策略设计主程序文档,下面对其进行解构:

1、外部引用文件:(勿动,保持不变即可)
  1. //***********************************************************************************外部引用项(开始)
  2. #include "pch.h"
  3. #include "test.h"
  4. #include <vector>
  5. #include <windows.h>
  6. #include <fstream>
  7. #include <stdio.h>
  8. #include <mmsystem.h>
  9. #include <thread>
  10. #pragma comment(lib,"winmm.lib")
  11. using namespace std;
  12. extern "C" __declspec(dllexport)Interface * create(HWND hw, string sName, string sPeriod, string sInst)
  13. {
  14.         return new test(hw, sName, sPeriod, sInst);
  15. }
  16. extern "C" __declspec(dllexport)void destory(Interface * p)
  17. {
  18.         delete p;
  19. }
  20. //***********************************************************************************外部引用项(结束)
复制代码
2、一系列功能性文档(勿动,保持不变即可)
  1. //***********************************************************************************TEST主文件功能性模块(开始)
  2. vector<std::string> split(std::string str, std::string pattern)
  3. {
  4.         std::string::size_type pos;
  5.         std::vector<std::string> result;
  6.         str += pattern;
  7.         int size = str.size();
  8.         for (int i = 0; i < size; i++)
  9.         {
  10.                 pos = str.find(pattern, i);
  11.                 if (pos < size)
  12.                 {
  13.                         std::string s = str.substr(i, pos - i);
  14.                         result.push_back(s);
  15.                         i = pos + pattern.size() - 1;
  16.                 }
  17.         }
  18.         return result;
  19. }
  20. test::test(HWND hw, string sn, string sp, string si)
  21. {
  22.         hwnd = hw;
  23.         sName = sn;
  24.         sPeriod = sp;
  25.         sInst = si;
  26.         //  SetTimer(NULL, 0, 1000, (TIMERPROC)TimerProc);
  27. }

  28. test::~test()
  29. {
  30. }

  31. void test::OnState(string s)
  32. {
  33.         state = s;
  34.         TDLLDATA* t = new TDLLDATA();
  35.         t->sName = sName;
  36.         t->sType = "state";
  37.         t->sData = s;
  38.         if (hwnd)SendMessage(hwnd, WM_DLLDATA, (WPARAM)t, 10);
  39. }
  40. void test::UpdateParm(string s)
  41. {
  42.         vector<std::string> v = split(s, "@");
  43.         for (int i = 0; i < v.size(); i++)
  44.         {
  45.                 string ss = v.at(i);
  46.                 vector<std::string> vv = split(ss, "_");
  47.                 if (vv.size() >= 3)
  48.                 {
  49.                         TDLLPARM t;
  50.                         t.Name = vv.at(0); t.Value = vv.at(1); t.Explain = vv.at(2); tend(t);
  51.                         parm[t.Name] = t;
  52.                 }
  53.         }
  54. }

  55. void test::UpdateSub(string sub, double ratio)
  56. {
  57.         mapSub[sub] = ratio;
  58. }

  59. void test::end()
  60. {
  61.         string s;
  62.         list<string>::iterator it;
  63.         for (it = lst.begin(); it != lst.end(); ++it)
  64.         {
  65.                 string key = *it;
  66.                 TDLLPARM t = parm[key];
  67.                 string ss = t.Name + "_" + t.Value + "_" + t.Explain;
  68.                 s = s + ss + "@";
  69.         }
  70.         TDLLDATA* t = new TDLLDATA();
  71.         t->sName = sName;
  72.         t->sType = "InitParm";
  73.         t->sData = s;
  74.         if (hwnd)SendMessage(hwnd, WM_DLLDATA, (WPARAM)t, 10);
  75. }
  76. void test::tend(TDLLPARM t)
  77. {
  78.         lst.push_back(t.Name);
  79.         parm[t.Name] = t;
  80. }

  81. void test::SubscribeMarketData(string s)
  82. {
  83.         TDLLDATA* t = new TDLLDATA();
  84.         t->sName = sName;
  85.         t->sType = "SubMd";
  86.         t->sData = s;
  87.         if (hwnd)SendMessage(hwnd, WM_SUBMD, (WPARAM)t, 11);
  88. }

  89. bool test::IsbeigenOrdrSysID(string orderid)
  90. {
  91.         list<string>::iterator it;
  92.         for (it = lstbeigenOrdrSysID.begin(); it != lstbeigenOrdrSysID.end(); it++)
  93.         {
  94.                 string temp = *it;
  95.                 if (temp == orderid)return true;
  96.         }
  97.         return false;
  98. }

  99. bool test::IsActionOrdrSysID(string orderid)
  100. {
  101.         list<string>::iterator it;
  102.         for (it = lstActionOrdrSysID.begin(); it != lstActionOrdrSysID.end(); it++)
  103.         {
  104.                 string temp = *it;
  105.                 if (temp == orderid)return true;
  106.         }
  107.         return false;
  108. }

  109. void test::OrderInsert(string acc, string inst, char bs, char  oc, int vol, double price, string forfok, string ref2)
  110. {
  111.         TORDERINSERT* t = new TORDERINSERT();
  112.         t->sName = sName;
  113.         t->InvestorID = acc;
  114.         t->InstrumentID = inst;
  115.         t->BuySell = bs;
  116.         t->OpenClose = oc;
  117.         t->Volume = vol;
  118.         t->LimitPrice = price;
  119.         t->FakFok = forfok;
  120.         t->Ref1 = sName;
  121.         t->Ref2 = ref2;
  122.         if (hwnd)SendMessage(hwnd, WM_INSERT, (WPARAM)t, 12);
  123. }

  124. void test::OrderAction(CThostFtdcOrderField t)
  125. {
  126.         TACTION* ta = new TACTION();
  127.         ta->Name = sName;
  128.         ta->InvestorID = t.InvestorID;
  129.         ta->OrderSysID = t.OrderSysID;
  130.         ta->BrokerID = t.BrokerID;
  131.         ta->ExchangeID = t.ExchangeID;
  132.         if (hwnd)SendMessage(hwnd, WM_ACTION, (WPARAM)ta, 13);
  133. }

  134. void test::RsqBar(string period, string inst)
  135. {
  136.         TRSQBAR* t = new TRSQBAR();
  137.         t->Name = sName;
  138.         t->Type = "RsqBar";
  139.         t->Period = period;
  140.         t->Inst = inst;
  141.         if (hwnd)SendMessage(hwnd, WM_RSQBAR, (WPARAM)t, (LPARAM)36);
  142. }

  143. void test::InsertLog(string msg)
  144. {
  145.         TMSG* t = new TMSG();
  146.         t->Name = sName;
  147.         t->Msg = msg;
  148.         if (hwnd)SendMessage(hwnd, WM_MSG, (WPARAM)t, 14);
  149. }

  150. void test::RsqInstrument(string inst)
  151. {
  152.         TRSQ* t = new TRSQ();
  153.         t->Name = sName;
  154.         t->Type = "RsqInstrument";
  155.         t->Inst = inst;
  156.         if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
  157. }

  158. void test::RsqRspQryOrder()
  159. {
  160.         TRSQ* t = new TRSQ();
  161.         t->Name = sName;
  162.         t->Type = "RspQryOrder";
  163.         if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
  164. }

  165. void test::RsqRspQryTrade()
  166. {
  167.         TRSQ* t = new TRSQ();
  168.         t->Name = sName;
  169.         t->Type = "RspQryTrade";
  170.         if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
  171. }

  172. void test::RsqPosition()
  173. {
  174.         TRSQ* t = new TRSQ();
  175.         t->Name = sName;
  176.         t->Type = "RsqPosition";
  177.         if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
  178. }

  179. void test::RsqPositionDetail(string acc)
  180. {
  181.         TRSQ* t = new TRSQ();
  182.         t->Name = sName;
  183.         t->Type = "RsqPositionDetail";
  184.         t->Account = acc;
  185.         if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
  186. }

  187. void test::RsqCommissionRate()
  188. {
  189.         TRSQ* t = new TRSQ();
  190.         t->Name = sName;
  191.         t->Type = "RsqCommissionRate";
  192.         if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
  193. }

  194. void test::RsqAccount()
  195. {
  196.         TRSQ* t = new TRSQ();
  197.         t->Name = sName;
  198.         t->Type = "RsqAccount";
  199.         if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
  200. }

  201. void test::sound(string s)
  202. {
  203.         size_t size = s.length();
  204.         wchar_t* buffer = new wchar_t[size + 1];
  205.         MultiByteToWideChar(CP_ACP, 0, s.c_str(), size, buffer, size * sizeof(wchar_t));
  206.         buffer[size] = 0;
  207.         PlaySound(buffer, NULL, SND_FILENAME | SND_ASYNC);
  208.         delete buffer;
  209.         buffer = NULL;
  210. }
  211. //***********************************************************************************TEST主文件功能性模块(结束)
复制代码
3、获得本地时间文件。(勿动即可,保持不变)
  1. //获得系统时间
  2. string GetSystemTime()
  3. {
  4.         SYSTEMTIME m_time;
  5.         GetLocalTime(&m_time);
  6.         char szDateTime[100] = { 0 };
  7.         sprintf_s(szDateTime, "%02d:%02d:%02d.:%03d", m_time.wHour, m_time.wMinute, m_time.wSecond, m_time.wMilliseconds);
  8.         string time(szDateTime);
  9.         return time;
  10. }
复制代码
4、策略重要时间参数(勿动,保持不变即可,除非自己已经很熟悉)
  1. string timeStart = "08:50:00";
  2. string timePause = "15:32:00";
  3. string timeResume = "20:50:00";
  4. string timeStop = "02:32:00";
  5. string state = "stop";
复制代码
上面为不动部分,一般情况下除非自己已经很熟悉,谨慎修改。

论坛官方微信、群(期货热点、量化探讨、开户与绑定实盘)
 
期货论坛 - 版权/免责声明   1.本站发布源码(包括函数、指标、策略等)均属开放源码,用意在于让使用者学习程序化语法撰写,使用者可以任意修改语法內容并调整参数。仅限用于个人学习使用,请勿转载、滥用,严禁私自连接实盘账户交易
  2.本站发布资讯(包括文章、视频、历史记录、教材、评论、资讯、交易方案等)均系转载自网络主流媒体,内容仅为作者当日个人观点,本网转载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。本网不对该类信息或数据做任何保证。不对您构成任何投资建议,不能依靠信息而取代自身独立判断,不对因使用本篇文章所诉信息或观点等导致的损失承担任何责任。
  3.本站发布资源(包括书籍、杂志、文档、软件等)均从互联网搜索而来,仅供个人免费交流学习,不可用作商业用途,本站不对显示的内容承担任何责任。请在下载后24小时内删除。如果喜欢,请购买正版,谢谢合作!
  4.龙听期货论坛原创文章属本网版权作品,转载须注明来源“龙听期货论坛”,违者本网将保留追究其相关法律责任的权力。本论坛除发布原创文章外,亦致力于优秀财经文章的交流分享,部分文章推送时若未能及时与原作者取得联系并涉及版权问题时,请及时联系删除。联系方式:http://www.qhlt.cn/thread-262-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

下面是策略设计部分,经常需要变动的

5、初始参数设计模块(软件左下角参数变量部分显示界面设计)
  1. void test::InitParm()
  2. {
  3.         TDLLPARM t;
  4.         //交易系统参数设置开始*******************************************************************************************************
  5.         //Name 参数名, Value 默认值, Explain 参数说明, tend 表示一个参数结束
  6.         t.Name = "指标周期"; t.Value = "20"; t.Explain = "指标周期参数,默认20"; tend(t);
  7.         t.Name = "滑点值"; t.Value = "1"; t.Explain = "每次交易滑点值"; tend(t);
  8.         t.Name = "优先平仓"; t.Value = "0"; t.Explain = "优先平仓,1优先平今,0优先平昨"; tend(t);
  9.         t.Name = "撤单时间"; t.Value = "5"; t.Explain = "委托后多少秒检查一下是否有未成交,小于0 不撤单"; tend(t);
  10.         //交易系统参数设置结束*******************************************************************************************************
  11.         end();
  12. }
复制代码
6、时间检测与功能模块(跟时间有关的变量在这里可以设置,比方说定时发邮件或通知)
  1. //时间监测(主要应用于委托后监测多久没有成交撤单用的,主要是计算委托后间隔)
  2. void test::OnTimer(string ts)  //时间监测
  3. {
  4.         if (state != "run")return;
  5.         SYSTEMTIME m_time;
  6.         GetLocalTime(&m_time);
  7.         t1 = to_string(m_time.wHour);
  8.         t2 = to_string(m_time.wMinute);
  9.         t3 = to_string(m_time.wSecond);
  10.         t4 = to_string(m_time.wMilliseconds);
  11.         if (t3 != t0 && jg > 0)
  12.         {
  13.                 if ((tm == jg || tm == jg + 1))  chedan();
  14.                 tm++;
  15.         }
  16.         t0 = t3;
  17. }
  18. //时间监测(主要应用于委托后监测多久没有成交撤单用的,及与时间有关的功能模块使用)
复制代码
7、策略运行面板中,点击“运行”按钮时模块功能设计(1、是将参数模块的设置赋给变量以下面使用 2、是点运行时加载的必要函数或计算。3、可以将重要的参考或监控变量数值通过insertlog输出到软件右下侧的日志面板。本模块学习方法:建议先熟悉每一个函数或变量的意思,然后小幅修改,通过输出查看效果,慢慢熟悉后再自己创作!)
  1. //点击运行按钮动作模块,用来加载交易界面的配置与策略参数配置(开始)
  2. void test::OnRun()
  3. {
  4.         OnState("run");
  5.         if (state != "run")return;
  6.         mapPosDeta.clear();
  7.         mapPos.clear();
  8.         mapOrder.clear();
  9.         mapTrade.clear();
  10.         sound("sound\\run.wav");
  11.         RsqInstrument(sInst);
  12.         //RsqRspQryOrder();
  13.         //RsqPosition();
  14.         //RsqRspQryTrade();
  15.     //RsqPositionDetail();
  16.     //RsqAccount();
  17.         SubscribeMarketData(sInst);

  18.         //交易系统参数变量传递开始*******************************************************************************************************
  19.         num = 0;
  20.         length = atoi(parm["指标周期"].Value.c_str());
  21.         hd = atoi(parm["滑点值"].Value.c_str());
  22.         yxpc = atoi(parm["优先平仓"].Value.c_str());
  23.         jg = atoi(parm["撤单时间"].Value.c_str());
  24.         //交易系统参数变量传递结束*******************************************************************************************************

  25.         //调用bar数据,要包括品种合约,运行周期
  26.         RsqBar(sPeriod, sInst);
  27.         //迭代
  28.         map<string, TKVALUE>::iterator it;//正向迭代
  29.         for (it = mapK[sPeriod][sInst].begin(); it != mapK[sPeriod][sInst].end(); it++)
  30.         {
  31.                 pc.push_back(it->second.dHigh);
  32.         }
  33.         for (size_t i = 0; i < length; i++)
  34.         {
  35.                 InsertLog("第 " + to_string(i) + " 个bar收盘价: " + to_string(pc[pc.size() - 1 - i]));
  36.         }

  37.         //map<string, TKVALUE>::reverse_iterator it;//逆向迭代
  38.         //for (it = mapK[sPeriod][sInst].rbegin(); it != mapK[sPeriod][sInst].rend(); it++) //逆向遍历所有K线
  39.         //{
  40.         //}
  41.        
  42.         //map<string, TKVALUE>::iterator it;//迭代
  43.         //for (it = mapK[sPeriod][sInst].begin(); it != mapK[sPeriod][sInst].end(); it++) //遍历所有K线
  44.         //{
  45.         //}

  46.         //在交易界面面板输出log信息
  47.         string wj[2];
  48.         string s = "c:/" + sName + sInst + "均线计算.txt";
  49.         fstream ofs;
  50.         ofs.open(s, ios::in);
  51.         if (!ofs.is_open())
  52.         {
  53.                 fx = 0;
  54.                 ss = 0;
  55.         }
  56.         else
  57.         {
  58.                 for (int i = 0; i < 2; i++)
  59.                 {
  60.                         getline(ofs, wj[i]);
  61.                         InsertLog(wj[i]);
  62.                 }
  63.         }
  64.         ofs.close();

  65.         fx = atoi(wj[0].c_str());
  66.         ss = atoi(wj[1].c_str());

  67.         //string s1 = "  单均线交易策略启动,均线周期    " + to_string(jxzq) + "   单笔风险   "
  68.         //        + to_string(dbfx) + "   滑点   " + to_string(hd) + "  优先开平    " + to_string(yxpc) + "   撤单时间   "
  69.         //        + to_string(jg) + "   持仓方向   " + to_string(fx) + "   数量   " + to_string(ss) + "  进场模式  " + entrymodel;
  70.         //InsertLog(s1);

  71. }
  72. //点击运行按钮动作模块,用来加载交易界面的配置与策略参数配置(结束)
复制代码
8、运行面板中“停止”按钮相关动作与功能模块(点击停止后的动作与功能)
  1. //点击停止按钮动作模块
  2. void test::OnStop()
  3. {
  4.         OnState("stop");
  5.         sound("sound\\stop.wav");
  6.         xieruzhuangtai();
  7.         //InsertLog(" 指标计算停止运行,周期 " + to_string(length) + "  数值  " + to_string(indvalue));
  8. }
  9. //点击停止按钮动作模块
复制代码
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

9、tick数据模式下策略主核心编辑区(策略主运行模式一)
  1. /********************************************tick数据传入(开始)***********************************************/
  2. void test::OnMarketData(CThostFtdcDepthMarketDataField* t) //监测有新的tick价格传入,即是否处于交易时间
  3. {
  4.         if (state != "run")return;
  5.         if (t->OpenInterest < 1)return;
  6.         if (t->InstrumentID != sInst)return;
  7.         mapMd[t->InstrumentID] = *t;
  8.         string sss = t->UpdateTime;
  9.         if ((sss >= "08:54:59" && sss <= "08:59:59") || (sss >= "20:54:59" && sss <= "20:59:59") || (sss >= "10:14:59" && sss <= "10:29:59"))return; //集合竞价时段与休市阶段返回,不委托下单

  10. }
  11. //********************************************tick数据传入(结束)***********************************************/
复制代码
10、bar(K线)数据模式下策略主核心编辑区(策略主运行模式二)
  1. //************************************************************************bar2bar模块(开始)*******************************************************************/

  2. void test::OnBarOpen(TKVALUE t)
  3. {
  4.         if (state != "run")return;
  5.         if (t.InstrumentID != sInst)return;
  6.         mapK[sPeriod][sInst][t.sDate + t.sDayNight + t.sTime] = t;
  7.        
  8.         string s2 = "  合约  " + sInst + "运行的周期 " + sPeriod + "  参考周期  " + to_string(length) ;
  9.         InsertLog(s2);
  10. }

  11. //************************************************************************bar2bar模块(结束)*******************************************************************/
复制代码
11、跟策略设计无关的交易、委托、撤单等功能模块(勿动即可,除非你了解它)
  1. /********************************************委托与成交反馈相关开始***********************************************/
  2. void test::OnRtnOrder(CThostFtdcOrderField t)
  3. {
  4.         mapOrder[t.OrderLocalID] = t;
  5.         if (!mapMd.count(t.InstrumentID))SubscribeMarketData(t.InstrumentID);
  6.         if (state != "run")return;
  7.         if (t.OrderStatus == '5' && t.MacAddress == sName && IsActionOrdrSysID(t.OrderLocalID)) chongfa(t);

  8.         string s = t.CombOffsetFlag;
  9.         //if (t.OrderStatus == '0' && s != "0" && t.MacAddress == sName) OnStop();
  10. }

  11. void test::OnRtnTrade(CThostFtdcTradeField t)
  12. {
  13.         mapTrade[t.OrderSysID] = t;
  14.         if (state != "run")return;

  15.         RsqPosition();
  16.         RsqPositionDetail(t.InvestorID);
  17. }
  18. /********************************************委托与成交反馈相关结束***********************************************/

  19. void test::OnRsqBar(string sPeriod, string sInst, map<string, TKVALUE> mp)
  20. {
  21.         if (state != "run")return;
  22.         mapK[sPeriod][sInst] = mp;
  23.         int n = mp.size();
  24.         if (n<length)InsertLog(sInst + "  bar 数量不够 ");
  25. }

  26. /************************************************************************委托与交易模块(开始)****************************************************************/
  27. void test::OnInstrument(CThostFtdcInstrumentField t)
  28. {
  29.         mapInstrument[t.InstrumentID] = t;
  30. }

  31. void test::OnRspQryOrder(map<string, CThostFtdcOrderField>* m)
  32. {
  33.         mapOrder = *m;
  34. }

  35. void test::OnRspQryTrade(map<string, CThostFtdcTradeField>* m)
  36. {
  37.         mapTrade = *m;
  38. }

  39. void test::OnPosition(map<string, map<string, TPOSITION>>* m)
  40. {
  41.         mapPos = *m;
  42. }

  43. void test::OnPositionDetail(map<string, TDETAIL>* m)
  44. {
  45.         mapPosDeta = *m;
  46.         /*map<string, TDETAIL>::iterator it;
  47.         for (it = mapPosDeta.begin(); it != mapPosDeta.end(); it++)
  48.         {
  49.                 string s1 = "  编号  " + it->first + "  账户  " + it->second.sAcc + "   合约   " + it->second.sInst + "  方向  " + it->second.sBuySell + "  数量  " + to_string(it->second.nVol) + "    平仓数量    " + to_string(it->second.nCloseVol) + "  价格  " + to_string(it->second.dOpenPrice) + "  时间  " + it->second.sOpenDate;
  50.                 if (it->second.nVol - it->second.nCloseVol != 0)InsertLog(s1);
  51.         }*/
  52. }

  53. void test::OnCommissionRate(map<string, map<string, TCOMMISSION>>* m)
  54. {
  55.         mapCom = *m;
  56. }

  57. void test::OnAccount(map<string, CThostFtdcTradingAccountField>* m)
  58. {
  59.         mapAcc = *m;
  60. }

  61. void test::OnInstrumentStatus(CThostFtdcInstrumentStatusField* t)
  62. {

  63. }

  64. void test::OnInstrumentAll(map<string, CThostFtdcInstrumentField>* m)
  65. {
  66.         mapInstrument = *m;
  67. }

  68. void test::chedan()
  69. {
  70.         map<string, CThostFtdcOrderField>::iterator it;
  71.         for (it = mapOrder.begin(); it != mapOrder.end(); it++)
  72.         {
  73.                 if (it->second.MacAddress == sName)
  74.                 {
  75.                         if (it->second.OrderStatus == '3' || it->second.OrderStatus == '1')
  76.                         {
  77.                                 string bs;
  78.                                 string soc;
  79.                                 if (it->second.Direction == '0')bs = "买入";
  80.                                 if (it->second.Direction == '1')bs = "卖出";
  81.                                 if (it->second.CombOffsetFlag == "0")soc = "开仓";
  82.                                 if (it->second.CombOffsetFlag == "1")soc = "平仓";
  83.                                 if (it->second.CombOffsetFlag == "3")soc = "平今仓";
  84.                                 if (it->second.CombOffsetFlag == "4")soc = "平昨仓";
  85.                                 OrderAction(it->second);
  86.                                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + it->second.InvestorID + "   有未成交委托,委托买卖方向   " + bs + soc + "    未成交数量委托数量    " + to_string((it->second.VolumeTotal - it->second.VolumeTraded)) + "    委托价格    " + to_string(it->second.LimitPrice) + "    " + it->second.InstrumentID;
  87.                                 maps[num] = s1;
  88.                                 num++;
  89.                                 lstActionOrdrSysID.push_back(it->second.OrderSysID);
  90.                         }
  91.                 }
  92.         }
  93.         if (num != 0)shuchurizhi();
  94. }

  95. void test::shuchurizhi()
  96. {
  97.         map<int, string>::iterator it;
  98.         for (it = maps.begin(); it != maps.end(); it++)
  99.         {
  100.                 InsertLog(it->second);
  101.         }
  102.         maps.clear();
  103.         num = 0;
  104. }

  105. void test::xieruzhuangtai()
  106. {
  107.         string s = "c:/" + sName + sInst + "均线出场.txt";
  108.         fstream ofs;
  109.         ofs.open(s, ios::out, ios::_Noreplace);
  110.         if (ofs.is_open())
  111.         {
  112.                 ofs << to_string(fx) << endl;
  113.                 ofs << to_string(ss) << endl;
  114.         }
  115.         ofs.close();
  116. }

  117. void test::chongfa(CThostFtdcOrderField t)
  118. {
  119.         string bs;
  120.         string soc;
  121.         if (t.Direction == '0')bs = "买入";
  122.         if (t.Direction == '1')bs = "卖出";
  123.         if (t.CombOffsetFlag == "0")soc = "开仓";
  124.         if (t.CombOffsetFlag == "1")soc = "平仓";
  125.         if (t.CombOffsetFlag == "3")soc = "平今仓";
  126.         if (t.CombOffsetFlag == "4")soc = "平昨仓";
  127.         char oc = t.CombOffsetFlag[0];
  128.         double dsj;
  129.         if (t.Direction == '0')
  130.         {
  131.                 dsj = mapMd[t.InstrumentID].AskPrice1;
  132.         }
  133.         else if (t.Direction == '1')
  134.         {
  135.                 dsj = mapMd[t.InstrumentID].BidPrice1;
  136.         }
  137.         OrderInsert(t.InvestorID, t.InstrumentID, t.Direction, oc, (t.VolumeTotal - t.VolumeTraded), dsj, "", "");
  138.         string s = t.InvestorID;
  139.         string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + s + "   有撤单成功重发,重发委托买卖方向   " + bs + soc + "    未成交数量委托数量    " + to_string((t.VolumeTotal - t.VolumeTraded)) + "    重发价格    " + to_string(dsj) + "    " + t.InstrumentID;
  140.         InsertLog(s1);
  141. }

  142. //   卖出平仓功能模块不用改动(优先平昨仓)
  143. void test::closesell1(string sInvestorID, string sName, int sl, double jg)
  144. {
  145.         //if (mapInstrument[sName].ExchangeID != "SHFE")
  146.         //{
  147.         //        OrderInsert(sInvestorID, sName, '1', '1', sl, jg, "", "");
  148.         //}
  149.         //else
  150.         //{
  151.         if (mapPos[sInvestorID][sName].nLongPosYd >= sl)
  152.         {
  153.                 OrderInsert(sInvestorID, sName, '1', '4', sl, jg, "", "");
  154.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  卖出平昨  " + to_string(sl) + "  手,账户持仓有多单今仓数量  " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + "   手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
  155.                 InsertLog(s1);
  156.         }
  157.         else if (mapPos[sInvestorID][sName].nLongPosYd > 0)
  158.         {
  159.                 OrderInsert(sInvestorID, sName, '1', '4', mapPos[sInvestorID][sName].nLongPosYd, jg, "", "");
  160.                 int jys2 = min(mapPos[sInvestorID][sName].nLongPosTd, sl - mapPos[sInvestorID][sName].nLongPosYd);
  161.                 if (mapPos[sInvestorID][sName].nLongPosTd > 0)
  162.                 {
  163.                         OrderInsert(sInvestorID, sName, '1', '3', jys2, jg, "", "");
  164.                 }
  165.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  卖出平昨  " + to_string(mapPos[sInvestorID][sName].nLongPosYd) + "  卖出平今仓  " + to_string(jys2) + "  手,账户持仓有多单今仓数量  " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + "   手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
  166.                 InsertLog(s1);
  167.         }
  168.         else if (mapPos[sInvestorID][sName].nLongPosTd > 0)
  169.         {
  170.                 int jys3 = min(sl, mapPos[sInvestorID][sName].nLongPosTd);
  171.                 OrderInsert(sInvestorID, sName, '1', '3', jys3, jg, "", "");
  172.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  卖出平今仓  " + to_string(jys3) + "  手,账户持仓有多单今仓数量  " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + "   手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
  173.                 InsertLog(s1);
  174.         }
  175.         else
  176.         {
  177.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  账户持仓有多单今仓数量  " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + "   手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd) + "  账户持仓有空单今仓数量  " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + "   手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);;
  178.                 InsertLog(s1);
  179.         }
  180.         //        }
  181. }

  182. //  买入平仓功能模块不用改动(优先平昨仓)
  183. void test::closebuy1(string sInvestorID, string sName, int sl, double jg)
  184. {
  185.         //if (mapInstrument[sName].ExchangeID != "SHFE")
  186.         //{
  187.         //        OrderInsert(sInvestorID, sName, '0', '1', sl, jg, "", "");
  188.         //}
  189.         //else
  190.         //{
  191.         if (mapPos[sInvestorID][sName].nShortPosYd >= sl)
  192.         {
  193.                 OrderInsert(sInvestorID, sName, '0', '4', sl, jg, "", "");
  194.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  买入平昨  " + to_string(sl) + "  手,账户持仓有空单今仓数量  " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + "   手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
  195.                 InsertLog(s1);
  196.         }
  197.         else if (mapPos[sInvestorID][sName].nShortPosYd > 0)
  198.         {
  199.                 OrderInsert(sInvestorID, sName, '0', '4', mapPos[sInvestorID][sName].nShortPosYd, jg, "", "");
  200.                 int jys2 = min(mapPos[sInvestorID][sName].nShortPosTd, sl - mapPos[sInvestorID][sName].nShortPosYd);
  201.                 if (mapPos[sInvestorID][sName].nShortPosTd > 0)
  202.                 {

  203.                         OrderInsert(sInvestorID, sName, '0', '3', jys2, jg, "", "");
  204.                 }
  205.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  买入平昨  " + to_string(mapPos[sInvestorID][sName].nShortPosYd) + "  买入平今仓  " + to_string(jys2) + "  手,账户持仓有空单今仓数量  " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + "   手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
  206.                 InsertLog(s1);
  207.         }
  208.         else if (mapPos[sInvestorID][sName].nShortPosTd > 0)
  209.         {
  210.                 int jys3 = min(mapPos[sInvestorID][sName].nShortPosTd, sl);
  211.                 OrderInsert(sInvestorID, sName, '0', '3', jys3, jg, "", "");
  212.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  买入平昨仓  " + to_string(jys3) + "  手,账户持仓有空单今仓数量  " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + "   手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
  213.                 InsertLog(s1);
  214.         }
  215.         else
  216.         {
  217.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  账户持仓有多单今仓数量  " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + "   手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd) + "  账户持仓有空单今仓数量  " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + "   手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);;
  218.                 InsertLog(s1);
  219.         }
  220.         //        }
  221. }

  222. //   卖出平仓功能模块不用改动(优先平今仓)
  223. void test::closesell2(string sInvestorID, string sName, int sl, double jg)
  224. {
  225.         //if (mapInstrument[sName].ExchangeID != "SHFE")
  226.         //{
  227.         //        OrderInsert(sInvestorID, sName, '1', '1', sl, jg, "", "");
  228.         //}
  229.         //else
  230.         //{
  231.         if (mapPos[sInvestorID][sName].nLongPosTd >= sl)
  232.         {
  233.                 OrderInsert(sInvestorID, sName, '1', '3', sl, jg, "", "");
  234.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  卖出平今  " + to_string(sl) + "  手,账户持仓有多单今仓数量  " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + "   手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
  235.                 InsertLog(s1);
  236.         }
  237.         else if (mapPos[sInvestorID][sName].nLongPosTd > 0)
  238.         {
  239.                 OrderInsert(sInvestorID, sName, '1', '3', mapPos[sInvestorID][sName].nLongPosTd, jg, "", "");
  240.                 int jys2 = min(mapPos[sInvestorID][sName].nLongPosYd, sl - mapPos[sInvestorID][sName].nLongPosTd);
  241.                 if (mapPos[sInvestorID][sName].nLongPosYd > 0)
  242.                 {
  243.                         OrderInsert(sInvestorID, sName, '1', '4', jys2, jg, "", "");
  244.                 }
  245.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  卖出平今  " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + "  卖出平昨仓  " + to_string(jys2) + "  手,账户持仓有多单今仓数量  " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + "   手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
  246.                 InsertLog(s1);
  247.         }
  248.         else if (mapPos[sInvestorID][sName].nLongPosYd > 0)
  249.         {
  250.                 int jys3 = min(mapPos[sInvestorID][sName].nLongPosYd, sl);
  251.                 OrderInsert(sInvestorID, sName, '1', '4', jys3, jg, "", "");
  252.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  卖出平昨仓  " + to_string(jys3) + "  手,账户持仓有多单今仓数量  " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + "   手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
  253.                 InsertLog(s1);
  254.         }
  255.         else
  256.         {
  257.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  账户持仓有多单今仓数量  " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + "   手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd) + "  账户持仓有空单今仓数量  " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + "   手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);;
  258.                 InsertLog(s1);
  259.         }
  260. }

  261. //}C++程序化/量化学习视频教程系列C++程序化/量化学习视频教程系列vC++程序化/量化学习视频教程系列
  262. //  买入平仓功能模块不用改动(优先平今仓)
  263. void test::closebuy2(string sInvestorID, string sName, int sl, double jg)
  264. {
  265.         //if (mapInstrument[sName].ExchangeID != "SHFE")
  266.         //{
  267.         //        OrderInsert(sInvestorID, sName, '0', '1', sl, jg, "", "");
  268.         //}
  269.         //else
  270.         //{
  271.         if (mapPos[sInvestorID][sName].nShortPosTd >= sl)
  272.         {
  273.                 OrderInsert(sInvestorID, sName, '0', '3', sl, jg, "", "");
  274.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  买入平今  " + to_string(sl) + "  手,账户持仓有空单今仓数量  " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + "   手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
  275.                 InsertLog(s1);
  276.         }
  277.         else if (mapPos[sInvestorID][sName].nShortPosTd > 0)
  278.         {
  279.                 OrderInsert(sInvestorID, sName, '0', '3', mapPos[sInvestorID][sName].nShortPosTd, jg, "", "");
  280.                 int jys2 = min(mapPos[sInvestorID][sName].nShortPosYd, sl - mapPos[sInvestorID][sName].nShortPosTd);
  281.                 if (mapPos[sInvestorID][sName].nShortPosYd > 0)
  282.                 {
  283.                         OrderInsert(sInvestorID, sName, '0', '4', jys2, jg, "", "");
  284.                 }
  285.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  买入平今  " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + "  买入平昨仓  " + to_string(jys2) + "  手,账户持仓有空单今仓数量  " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + "   手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
  286.                 InsertLog(s1);
  287.         }
  288.         else if (mapPos[sInvestorID][sName].nShortPosYd > 0)
  289.         {
  290.                 int jys2 = min(mapPos[sInvestorID][sName].nShortPosYd, sl);
  291.                 OrderInsert(sInvestorID, sName, '0', '4', jys2, jg, "", "");
  292.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  买入平昨仓  " + to_string(jys2) + "  手,账户持仓有空单今仓数量  " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + "   手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
  293.                 InsertLog(s1);
  294.         }
  295.         else
  296.         {
  297.                 string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + "    " + "  账户  " + sInvestorID + " 合约   " + sName + "  账户持仓有多单今仓数量  " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + "   手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd) + "  账户持仓有空单今仓数量  " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + "   手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);;
  298.                 InsertLog(s1);
  299.         }
  300. }
  301. //************************************************************************委托与交易模块(结束)************************************************************************/
复制代码
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

返回列表