- UID
- 2
- 积分
- 2874604
- 威望
- 1387331 布
- 龙e币
- 1487273 刀
- 在线时间
- 13155 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2024-11-24
|
第二部分:主体文件(test.cpp)
1、配置模块部分,勿动- #include "pch.h"
- #include "test.h"
- #include <vector>
- #include <windows.h>
- #include <fstream>
- #include <stdio.h>
- #include <mmsystem.h>
- #include <thread>
- #pragma comment(lib,"winmm.lib")
- using namespace std;
- extern "C" __declspec(dllexport)Interface * create(HWND hw, string sName, string sPeriod, string sInst)
- {
- return new test(hw, sName, sPeriod, sInst);
- }
- extern "C" __declspec(dllexport)void destory(Interface * p)
- {
- delete p;
- }
- vector<std::string> split(std::string str, std::string pattern)
- {
- std::string::size_type pos;
- std::vector<std::string> result;
- str += pattern;
- int size = str.size();
- for (int i = 0; i < size; i++)
- {
- pos = str.find(pattern, i);
- if (pos < size)
- {
- std::string s = str.substr(i, pos - i);
- result.push_back(s);
- i = pos + pattern.size() - 1;
- }
- }
- return result;
- }
- string GetSystemTime()
- {
- SYSTEMTIME m_time;
- GetLocalTime(&m_time);
- char szDateTime[100] = { 0 };
- sprintf_s(szDateTime, "%02d:%02d:%02d.:%03d", m_time.wHour, m_time.wMinute, m_time.wSecond, m_time.wMilliseconds);
- string time(szDateTime);
- return time;
- }
- test::test(HWND hw, string sn, string sp, string si)
- {
- hwnd = hw;
- sName = sn;
- sPeriod = sp;
- sInst = si;
- // SetTimer(NULL, 0, 1000, (TIMERPROC)TimerProc);
- }
- test::~test()
- {
- }
- void test::OnState(string s)
- {
- state = s;
- TDLLDATA* t = new TDLLDATA();
- t->sName = sName;
- t->sType = "state";
- t->sData = s;
- if (hwnd)SendMessage(hwnd, WM_DLLDATA, (WPARAM)t, 10);
- }
- void test::UpdateParm(string s)
- {
- vector<std::string> v = split(s, "@");
- for (int i = 0; i < v.size(); i++)
- {
- string ss = v.at(i);
- vector<std::string> vv = split(ss, "_");
- if (vv.size() >= 3)
- {
- TDLLPARM t;
- t.Name = vv.at(0); t.Value = vv.at(1); t.Explain = vv.at(2); tend(t);
- parm[t.Name] = t;
- }
- }
- }
- void test::UpdateSub(string sub, double ratio)
- {
- mapSub[sub] = ratio;
- }
- void test::end()
- {
- string s;
- list<string>::iterator it;
- for (it = lst.begin(); it != lst.end(); ++it)
- {
- string key = *it;
- TDLLPARM t = parm[key];
- string ss = t.Name + "_" + t.Value + "_" + t.Explain;
- s = s + ss + "@";
- }
- TDLLDATA* t = new TDLLDATA();
- t->sName = sName;
- t->sType = "InitParm";
- t->sData = s;
- if (hwnd)SendMessage(hwnd, WM_DLLDATA, (WPARAM)t, 10);
- }
- void test::tend(TDLLPARM t)
- {
- lst.push_back(t.Name);
- parm[t.Name] = t;
- }
- void test::SubscribeMarketData(string s)
- {
- TDLLDATA* t = new TDLLDATA();
- t->sName = sName;
- t->sType = "SubMd";
- t->sData = s;
- if (hwnd)SendMessage(hwnd, WM_SUBMD, (WPARAM)t, 11);
- }
- bool test::IsbeigenOrdrSysID(string orderid)
- {
- list<string>::iterator it;
- for (it = lstbeigenOrdrSysID.begin(); it != lstbeigenOrdrSysID.end(); it++)
- {
- string temp = *it;
- if (temp == orderid)return true;
- }
- return false;
- }
- bool test::IsActionOrdrSysID(string orderid)
- {
- list<string>::iterator it;
- for (it = lstActionOrdrSysID.begin(); it != lstActionOrdrSysID.end(); it++)
- {
- string temp = *it;
- if (temp == orderid)return true;
- }
- return false;
- }
- void test::OrderInsert(string acc, string inst, char bs, char oc, int vol, double price, string forfok, string ref2)
- {
- TORDERINSERT* t = new TORDERINSERT();
- t->sName = sName;
- t->InvestorID = acc;
- t->InstrumentID = inst;
- t->BuySell = bs;
- t->OpenClose = oc;
- t->Volume = vol;
- t->LimitPrice = price;
- t->FakFok = forfok;
- t->Ref1 = sName;
- t->Ref2 = ref2;
- if (hwnd)SendMessage(hwnd, WM_INSERT, (WPARAM)t, 12);
- }
- void test::OrderAction(CThostFtdcOrderField t)
- {
- TACTION* ta = new TACTION();
- ta->Name = sName;
- ta->InvestorID = t.InvestorID;
- ta->OrderSysID = t.OrderSysID;
- ta->BrokerID = t.BrokerID;
- ta->ExchangeID = t.ExchangeID;
- if (hwnd)SendMessage(hwnd, WM_ACTION, (WPARAM)ta, 13);
- }
- void test::RsqBar(string period, string inst)
- {
- TRSQBAR* t = new TRSQBAR();
- t->Name = sName;
- t->Type = "RsqBar";
- t->Period = period;
- t->Inst = inst;
- if (hwnd)SendMessage(hwnd, WM_RSQBAR, (WPARAM)t, (LPARAM)36);
- }
- void test::InsertLog(string msg)
- {
- TMSG* t = new TMSG();
- t->Name = sName;
- t->Msg = msg;
- if (hwnd)SendMessage(hwnd, WM_MSG, (WPARAM)t, 14);
- }
- void test::RsqInstrument(string inst)
- {
- TRSQ* t = new TRSQ();
- t->Name = sName;
- t->Type = "RsqInstrument";
- t->Inst = inst;
- if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
- }
- void test::RsqRspQryOrder()
- {
- TRSQ* t = new TRSQ();
- t->Name = sName;
- t->Type = "RspQryOrder";
- if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
- }
- void test::RsqRspQryTrade()
- {
- TRSQ* t = new TRSQ();
- t->Name = sName;
- t->Type = "RspQryTrade";
- if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
- }
- void test::RsqPosition()
- {
- TRSQ* t = new TRSQ();
- t->Name = sName;
- t->Type = "RsqPosition";
- if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
- }
- void test::RsqPositionDetail(string acc)
- {
- TRSQ* t = new TRSQ();
- t->Name = sName;
- t->Type = "RsqPositionDetail";
- t->Account = acc;
- if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
- }
- void test::RsqCommissionRate()
- {
- TRSQ* t = new TRSQ();
- t->Name = sName;
- t->Type = "RsqCommissionRate";
- if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
- }
- void test::RsqAccount()
- {
- TRSQ* t = new TRSQ();
- t->Name = sName;
- t->Type = "RsqAccount";
- if (hwnd)SendMessage(hwnd, WM_RSQ, (WPARAM)t, (LPARAM)33);
- }
- void test::sound(string s)
- {
- size_t size = s.length();
- wchar_t* buffer = new wchar_t[size + 1];
- MultiByteToWideChar(CP_ACP, 0, s.c_str(), size, buffer, size * sizeof(wchar_t));
- buffer[size] = 0;
- PlaySound(buffer, NULL, SND_FILENAME | SND_ASYNC);
- delete buffer;
- buffer = NULL;
- }
复制代码 2、指标公式计算模块:勿动- //公式函数计算部分...........................................................................................................................................
- double test::avg(string period, string inst, int num)
- {
- double d = 0;
- int n = 0;
- map<string, TKVALUE>::reverse_iterator it;
- // InsertLog(to_string(mapK[period][inst].size()));
- for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
- {
- d += it->second.dClose;
- n++;
- if (n >= num)break;
- }
- return d / n;
- }
- double test::avg1(string period, string inst, int num, int ref)
- {
- double d = 0;
- int n = 0;
- int r = 0;
- map<string, TKVALUE>::reverse_iterator it;
- // InsertLog(to_string(mapK[period][inst].size()));
- for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
- {
- r++;
- if (r <= ref)continue;
- d += it->second.dClose;
- n++;
- if (n >= num)break;
- }
- return d / n;
- }
- double test::highest(string period, string inst, int num)
- {
- double d = 0;
- int n = 0;
- map<string, TKVALUE>::reverse_iterator it;
- for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
- {
- if (d < it->second.dHigh)d = it->second.dHigh;
- n++;
- if (n >= num)break;
- }
- return d;
- }
- double test::highest1(string period, string inst, int num, int ref)
- {
- double d = 0;
- int n = 0;
- int r = 0;
- map<string, TKVALUE>::reverse_iterator it;
- for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
- {
- r++;
- if (r <= ref)continue;
- if (d < it->second.dHigh)d = it->second.dHigh;
- n++;
- if (n >= num)break;
- }
- return d;
- }
- double test::lowerest(string period, string inst, int num)
- {
- double d = 0;
- int n = 0;
- map<string, TKVALUE>::reverse_iterator it;
- for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
- {
- if (d == 0)d = it->second.dLow;
- if (d > it->second.dLow)d = it->second.dLow;
- n++;
- if (n >= num)break;
- }
- return d;
- }
- double test::lowerest1(string period, string inst, int num, int ref)
- {
- double d = 0;
- int n = 0;
- int r = 0;
- map<string, TKVALUE>::reverse_iterator it;
- for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
- {
- r++;
- if (r <= ref)continue;
- if (d == 0)d = it->second.dLow;
- if (d > it->second.dLow)d = it->second.dLow;
- n++;
- if (n >= num)break;
- }
- return d;
- }
- //公式函数计算结束........................................................................................................................................
复制代码 3、主策略模块:
(1)、定义策略开始结束时间模块:- /// <summary>
- /// 以下开始
- /// </summary>
- //strategy begin and end time set
- string timeStart = "08:50:00";
- string timePause = "15:32:00";
- string timeResume = "20:50:00";
- string timeStop = "02:32:00";
- string state = "stop";
复制代码 (2)、设计策略参数模块:- //strategy parameter set
- void test::InitParm()
- {
- TDLLPARM t;
- //Name 参数名, Value 默认值, Explain 参数说明, tend 表示一个参数结束
- t.Name = "入场价格"; t.Value = "5000"; t.Explain = "入场价格"; tend(t);
- t.Name = "交易方向"; t.Value = "1"; t.Explain = "交易方向,1大于入场价做多,-1小于入场价做空"; tend(t);
- t.Name = "出场价格"; t.Value = "4000"; t.Explain = "1),盘中止损价,2,方向1下穿止损,方向-1上穿止损"; tend(t);
- t.Name = "均线周期"; t.Value = "120"; t.Explain = "均线周期参数"; tend(t);
- t.Name = "单笔风险"; t.Value = "1000"; t.Explain = "单笔风险值根据出场和均线的差值计算手数,小于200位固定手数"; tend(t);
- t.Name = "滑点值"; t.Value = "1"; t.Explain = "每次交易滑点值"; tend(t);
- t.Name = "优先平仓"; t.Value = "0"; t.Explain = "优先平仓,1优先平今,0优先平昨"; tend(t);
- t.Name = "撤单时间"; t.Value = "3"; t.Explain = "委托后多少秒检查一下是否有未成交,小于0 不撤单"; tend(t);
- t.Name = "策略说明"; t.Value = "0"; t.Explain = "策略思路:1、开始按入场价格入场开仓 2、按出场价格盘中出场 3、如果不能触发出场价格后穿越均线周期均线出场,"; tend(t);
- end();
- }
复制代码 对应于界面的这部分:,需要设置哪些参数就在上面的程式码中增减,会在策略加载后显示出来。
(3)、点击运行按钮后的动作模块:- //点击运行按钮动作
- void test::OnRun()
- {
- OnState("run");
- if (state != "run")return;
- mapPosDeta.clear();
- mapPos.clear();
- mapOrder.clear();
- mapTrade.clear();
- sound("sound\\run.wav");
- RsqInstrument(sInst);
- RsqRspQryOrder();
- RsqPosition();
- // RsqRspQryTrade();
- //RsqPositionDetail();
- //RsqAccount();
- SubscribeMarketData(sInst);//订阅在界面选择特定周期,特定品种行情数据
- // 调用参数对策略变量进行配置
- num = 0;
- jxzq = atoi(parm["均线周期"].Value.c_str());
- rcjg = atof(parm["入场价格"].Value.c_str());
- ccjg = atof(parm["出场价格"].Value.c_str());
- jyfx = atoi(parm["交易方向"].Value.c_str());
- dbfx = atof(parm["单笔风险"].Value.c_str());
- hd = atoi(parm["滑点值"].Value.c_str());
- yxpc = atoi(parm["优先平仓"].Value.c_str());
- jg = atoi(parm["撤单时间"].Value.c_str());
- RsqBar(sPeriod, sInst); //调用界面特定品种、特定周期K线数据
- ma = avg(sPeriod, sInst, jxzq); //均线调用
- string s2 = " 均线 " + to_string(ma);
- InsertLog(s2);
- string wj[2];
- string s = "c:/" + sName + sInst + "均线出场.txt";
- fstream ofs;
- ofs.open(s, ios::in);
- if (!ofs.is_open())
- {
- fx = 0;
- ss = 0;
- }
- else
- {
- for (int i = 0; i < 2; i++)
- {
- getline(ofs, wj[i]);
- // InsertLog(wj[i]);
- }
- }
- ofs.close();
- fx = atoi(wj[0].c_str());
- ss = atoi(wj[1].c_str());
- string s1 = " 均线出场策略启动,均线周期 " + to_string(jxzq) + " 入场价格 " + to_string(rcjg) + " 交易方向 " + to_string(jyfx) + " 出场价格 " + to_string(ccjg) + " 单笔风险 "
- + to_string(dbfx) + " 滑点 " + to_string(hd) + " 优先开平 " + to_string(yxpc) + " 撤单时间 " + to_string(jg) + " 方向 " + to_string(fx) + " 数量 " + to_string(ss);
- InsertLog(s1);
- }
复制代码 按钮如图:
说明:点击运行按钮后动作主要包括三部分,1,将上面策略界面的参数设定传到在head头文件里面设置的变量里面。2、在工作界面的LOG里面输出策略的一些加载信息及策略的大体介绍。3、点运行按钮后获得最新时间。- void test::OnTimer(string ts)
- {
- if (state != "run")return;
- SYSTEMTIME m_time;
- GetLocalTime(&m_time);
- t1 = to_string(m_time.wHour);
- t2 = to_string(m_time.wMinute);
- t3 = to_string(m_time.wSecond);
- t4 = to_string(m_time.wMilliseconds);
- if (t3 != t0 && jg > 0)
- {
- if ((tm == jg || tm == jg + 1)) chedan();
- tm++;
- }
- t0 = t3;
- }
复制代码 (4)、点击停止按钮后动作模块:- void test::OnStop()
- {
- OnState("stop");
- sound("sound\\stop.wav");
- xieruzhuangtai();
- InsertLog(" 均线出场策略停止运行,方向 " + to_string(fx) + " 手数 " + to_string(ss));
- }
复制代码 说明:1、运行状态变为“stop” ,2,发出声音。3、输出停止LOG。
如图:
(5)、有最新数据传入后的进出主策略部分,即最新价进出场模块,策略核心模块一- //有数据传入后开始策略
- void test::OnMarketData(CThostFtdcDepthMarketDataField* t)
- {
- if (state != "run")return;
- if (t->InstrumentID != sInst)return;
- mapMd[t->InstrumentID] = *t;
- //最新价大于设定入场价开仓做多操作(开仓条件:1,最新价大于设定价 2,交易方向做多 3,当前没有持仓,fx==0)
- if (t->LastPrice >= rcjg && jyfx == 1 && fx == 0)
- {
- fx = 1;
- if (dbfx < 200)ss = dbfx;
- if (dbfx >= 200)ss = (int)floor(dbfx / (rcjg - ma) / mapInstrument[sInst].VolumeMultiple);
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
- OrderInsert(it->first, sInst, '0', '0', sl, t->AskPrice1 + hd * mapInstrument[sInst].PriceTick, "", "");
- string s = it->first + " 突破入场价格多单达到入场条件买入开仓 " + to_string(sl) + " 手,价格 " + to_string(t->AskPrice1 + hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- //最新价小于设定入场价开仓做空操作(开仓条件:1 最新价小于设定价 2 交易方向做空 3 当前没有持仓,fx==0)
- if (t->LastPrice <= rcjg && jyfx == -1 && fx == 0)
- {
- fx = -1;
- if (dbfx < 200)ss = dbfx;
- if (dbfx >= 200)ss = (int)floor(dbfx / (ma - rcjg) / mapInstrument[sInst].VolumeMultiple);
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
- OrderInsert(it->first, sInst, '1', '0', sl, t->BidPrice1 - hd * mapInstrument[sInst].PriceTick, "", "");
- string s = it->first + " 突破入场价格空单达到入场条件卖出开仓 " + to_string(sl) + " 手,价格 " + to_string(t->BidPrice1 - hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- //多单出场:1, 多仓 2, 交易方向fx==1 3, 最新价小于出场价
- if (jyfx == 1 && fx == 1 && t->LastPrice <= ccjg)
- {
- fx = 2;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
- if (yxpc == 0)
- {
- closesell1(it->first, sInst, sl, t->BidPrice1 - hd * mapInstrument[sInst].PriceTick);
- }
- else if (yxpc == 1)
- {
- closesell2(it->first, sInst, sl, t->BidPrice1 - hd * mapInstrument[sInst].PriceTick);
- }
- string s = it->first + " 多单达到出场条件卖出平仓 " + to_string(sl) + " 手,价格 " + to_string(t->BidPrice1 - hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- //空单出场:1,空单 2,交易方向fx==-1 3,最新价大于等于出场价
- if (jyfx == -1 && fx == -1 && t->LastPrice >= ccjg)
- {
- fx = -2;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
-
- if (yxpc == 0)
- {
- closebuy1(it->first, sInst, sl, t->AskPrice1 + hd * mapInstrument[sInst].PriceTick);
- }
- else if (yxpc == 1)
- {
- closebuy2(it->first, sInst, sl, t->AskPrice1 + hd * mapInstrument[sInst].PriceTick);
- }
- string s = it->first + " 空单达到出场条件买入平仓 " + to_string(ss) + " 手,价格 " + to_string(t->AskPrice1 + hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- }
复制代码 (6)、、有新K线(BAR)生成后的主策略部分,即按K线进出场设计模块,策略的核心模块二- //有数据传入后开始策略
- void test::OnMarketData(CThostFtdcDepthMarketDataField* t)
- {
- if (state != "run")return;
- if (t->InstrumentID != sInst)return;
- mapMd[t->InstrumentID] = *t;
- //最新价大于设定入场价开仓做多操作(开仓条件:1,最新价大于设定价 2,交易方向做多 3,当前没有持仓,fx==0)
- if (t->LastPrice >= rcjg && jyfx == 1 && fx == 0)
- {
- fx = 1;
- if (dbfx < 200)ss = dbfx;
- if (dbfx >= 200)ss = (int)floor(dbfx / (rcjg - ma) / mapInstrument[sInst].VolumeMultiple);
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
- OrderInsert(it->first, sInst, '0', '0', sl, t->AskPrice1 + hd * mapInstrument[sInst].PriceTick, "", "");
- string s = it->first + " 突破入场价格多单达到入场条件买入开仓 " + to_string(sl) + " 手,价格 " + to_string(t->AskPrice1 + hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- //最新价小于设定入场价开仓做空操作(开仓条件:1 最新价小于设定价 2 交易方向做空 3 当前没有持仓,fx==0)
- if (t->LastPrice <= rcjg && jyfx == -1 && fx == 0)
- {
- fx = -1;
- if (dbfx < 200)ss = dbfx;
- if (dbfx >= 200)ss = (int)floor(dbfx / (ma - rcjg) / mapInstrument[sInst].VolumeMultiple);
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
- OrderInsert(it->first, sInst, '1', '0', sl, t->BidPrice1 - hd * mapInstrument[sInst].PriceTick, "", "");
- string s = it->first + " 突破入场价格空单达到入场条件卖出开仓 " + to_string(sl) + " 手,价格 " + to_string(t->BidPrice1 - hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- //多单出场:1, 多仓 2, 交易方向fx==1 3, 最新价小于出场价
- if (jyfx == 1 && fx == 1 && t->LastPrice <= ccjg)
- {
- fx = 2;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
- if (yxpc == 0)
- {
- closesell1(it->first, sInst, sl, t->BidPrice1 - hd * mapInstrument[sInst].PriceTick);
- }
- else if (yxpc == 1)
- {
- closesell2(it->first, sInst, sl, t->BidPrice1 - hd * mapInstrument[sInst].PriceTick);
- }
- string s = it->first + " 多单达到出场条件卖出平仓 " + to_string(sl) + " 手,价格 " + to_string(t->BidPrice1 - hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- //空单出场:1,空单 2,交易方向fx==-1 3,最新价大于等于出场价
- if (jyfx == -1 && fx == -1 && t->LastPrice >= ccjg)
- {
- fx = -2;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
-
- if (yxpc == 0)
- {
- closebuy1(it->first, sInst, sl, t->AskPrice1 + hd * mapInstrument[sInst].PriceTick);
- }
- else if (yxpc == 1)
- {
- closebuy2(it->first, sInst, sl, t->AskPrice1 + hd * mapInstrument[sInst].PriceTick);
- }
- string s = it->first + " 空单达到出场条件买入平仓 " + to_string(ss) + " 手,价格 " + to_string(t->AskPrice1 + hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- }
- void test::OnBarOpen(TKVALUE t)
- {
- if (state != "run")return;
- if (t.InstrumentID != sInst)return;
- mapK[sPeriod][sInst][t.sDate + t.sDayNight + t.sTime] = t;
- ma = avg(sPeriod, sInst, jxzq);
- string s2 = " 合约 " + sInst + " 均线 " + to_string(ma);
- InsertLog(s2);
- //多单均线出场:1 最新价小于等与均线价格 2 持仓方向为多
- if (mapMd[sInst].LastPrice <= ma && fx == 1)
- {
- fx = 2;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
- if (yxpc == 0)
- {
- closesell1(it->first, sInst, sl, mapMd[sInst].BidPrice1 - hd * mapInstrument[sInst].PriceTick);
- }
- else if (yxpc == 1)
- {
- closesell2(it->first, sInst, sl, mapMd[sInst].BidPrice1 - hd * mapInstrument[sInst].PriceTick);
- }
- string s = it->first + " 多单跌破均线达到出场条件卖出平仓 " + to_string(sl) + " 手,价格 " + to_string(mapMd[sInst].BidPrice1 - hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- if (mapMd[sInst].LastPrice >= ma && fx == -1)
- {
- fx = -2;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
- if (yxpc == 0)
- {
- closebuy1(it->first, sInst, sl, mapMd[sInst].AskPrice1 + hd * mapInstrument[sInst].PriceTick);
- }
- else if (yxpc == 1)
- {
- closebuy2(it->first, sInst, sl, mapMd[sInst].AskPrice1 + hd * mapInstrument[sInst].PriceTick);
- }
- string s = it->first + " 空单涨破均线达到出场条件买入平仓 " + to_string(ss) + " 手,价格 " + to_string(mapMd[sInst].AskPrice1 + hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- }
复制代码 (7)、有关委托、撤单、重发等交易相关模块,一般勿动- void test::OnRsqBar(string sPeriod, string sInst, map<string, TKVALUE> mp)
- {
- if (state != "run")return;
- mapK[sPeriod][sInst] = mp;
- int n = mp.size();
- if (n < jxzq)InsertLog(sInst + " bar 数量不够 ");
- }
- void test::OnRtnOrder(CThostFtdcOrderField t)
- {
- mapOrder[t.OrderLocalID] = t;
- if (!mapMd.count(t.InstrumentID))SubscribeMarketData(t.InstrumentID);
- if (state != "run")return;
- if (t.OrderStatus == '5' && t.MacAddress == sName &&IsActionOrdrSysID(t.OrderLocalID)) chongfa(t);
- string s = t.CombOffsetFlag;
- // if (t.OrderStatus == '0' && s != "0" && t.MacAddress == sName) OnStop();
- }
- void test::OnRtnTrade(CThostFtdcTradeField t)
- {
- mapTrade[t.OrderSysID] = t;
- if (state != "run")return;
- RsqPosition();
- RsqPositionDetail(t.InvestorID);
- }
- void test::OnInstrument(CThostFtdcInstrumentField t)
- {
- mapInstrument[t.InstrumentID] = t;
- }
- void test::OnRspQryOrder(map<string, CThostFtdcOrderField>* m)
- {
- mapOrder = *m;
- }
- void test::OnRspQryTrade(map<string, CThostFtdcTradeField>* m)
- {
- mapTrade = *m;
- }
- void test::OnPosition(map<string, map<string, TPOSITION>>* m)
- {
- mapPos = *m;
- }
- void test::OnPositionDetail(map<string, TDETAIL>* m)
- {
- mapPosDeta = *m;
- /*map<string, TDETAIL>::iterator it;
- for (it = mapPosDeta.begin(); it != mapPosDeta.end(); it++)
- {
- 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;
- if (it->second.nVol - it->second.nCloseVol != 0)InsertLog(s1);
- }*/
- }
- void test::OnCommissionRate(map<string, map<string, TCOMMISSION>>* m)
- {
- mapCom = *m;
- }
- void test::OnAccount(map<string, CThostFtdcTradingAccountField>* m)
- {
- mapAcc = *m;
- }
- void test::OnInstrumentStatus(CThostFtdcInstrumentStatusField* t)
- {
- }
- void test::OnInstrumentAll(map<string, CThostFtdcInstrumentField>* m)
- {
- mapInstrument = *m;
- }
- void test::chedan()
- {
- map<string, CThostFtdcOrderField>::iterator it;
- for (it = mapOrder.begin(); it != mapOrder.end(); it++)
- {
- if (it->second.MacAddress == sName)
- {
- if (it->second.OrderStatus == '3' || it->second.OrderStatus == '1')
- {
- string bs;
- string soc;
- if (it->second.Direction == '0')bs = "买入";
- if (it->second.Direction == '1')bs = "卖出";
- if (it->second.CombOffsetFlag == "0")soc = "开仓";
- if (it->second.CombOffsetFlag == "1")soc = "平仓";
- if (it->second.CombOffsetFlag == "3")soc = "平今仓";
- if (it->second.CombOffsetFlag == "4")soc = "平昨仓";
- OrderAction(it->second);
- 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;
- maps[num] = s1;
- num++;
- lstActionOrdrSysID.push_back(it->second.OrderSysID);
- }
- }
- }
- if (num != 0)shuchurizhi();
- }
- void test::shuchurizhi()
- {
- map<int, string>::iterator it;
- for (it = maps.begin(); it != maps.end(); it++)
- {
- InsertLog(it->second);
- }
- maps.clear();
- num = 0;
- }
- void test::xieruzhuangtai()
- {
- string s = "c:/" + sName + sInst + "均线出场.txt";
- fstream ofs;
- ofs.open(s, ios::out, ios::_Noreplace);
- if (ofs.is_open())
- {
- ofs << to_string(fx) << endl;
- ofs << to_string(ss) << endl;
- }
- ofs.close();
- }
- void test::chongfa(CThostFtdcOrderField t)
- {
- string bs;
- string soc;
- if (t.Direction == '0')bs = "买入";
- if (t.Direction == '1')bs = "卖出";
- if (t.CombOffsetFlag == "0")soc = "开仓";
- if (t.CombOffsetFlag == "1")soc = "平仓";
- if (t.CombOffsetFlag == "3")soc = "平今仓";
- if (t.CombOffsetFlag == "4")soc = "平昨仓";
- char oc = t.CombOffsetFlag[0];
- double dsj;
- if (t.Direction == '0')
- {
- dsj = mapMd[t.InstrumentID].AskPrice1;
- }
- else if (t.Direction == '1')
- {
- dsj = mapMd[t.InstrumentID].BidPrice1;
- }
- OrderInsert(t.InvestorID, t.InstrumentID, t.Direction, oc, (t.VolumeTotal - t.VolumeTraded), dsj, "", "");
- string s = t.InvestorID;
- string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + s + " 有撤单成功重发,重发委托买卖方向 " + bs + soc + " 未成交数量委托数量 " + to_string((t.VolumeTotal - t.VolumeTraded)) + " 重发价格 " + to_string(dsj) + " " + t.InstrumentID;
- InsertLog(s1);
- }
- // 卖出平仓 优先平昨仓
- void test::closesell1(string sInvestorID, string sName, int sl, double jg)
- {
- //if (mapInstrument[sName].ExchangeID != "SHFE")
- //{
- // OrderInsert(sInvestorID, sName, '1', '1', sl, jg, "", "");
- //}
- //else
- //{
- if (mapPos[sInvestorID][sName].nLongPosYd >= sl)
- {
- OrderInsert(sInvestorID, sName, '1', '4', sl, jg, "", "");
- string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 卖出平昨 " + to_string(sl) + " 手,账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
- InsertLog(s1);
- }
- else if (mapPos[sInvestorID][sName].nLongPosYd > 0)
- {
- OrderInsert(sInvestorID, sName, '1', '4', mapPos[sInvestorID][sName].nLongPosYd, jg, "", "");
- int jys2 = min(mapPos[sInvestorID][sName].nLongPosTd, sl - mapPos[sInvestorID][sName].nLongPosYd);
- if (mapPos[sInvestorID][sName].nLongPosTd > 0)
- {
- OrderInsert(sInvestorID, sName, '1', '3', jys2, jg, "", "");
- }
- 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);
- InsertLog(s1);
- }
- else if (mapPos[sInvestorID][sName].nLongPosTd > 0)
- {
- int jys3 = min(sl, mapPos[sInvestorID][sName].nLongPosTd);
- OrderInsert(sInvestorID, sName, '1', '3', jys3, jg, "", "");
- string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 卖出平今仓 " + to_string(jys3) + " 手,账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
- InsertLog(s1);
- }
- else
- {
- 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);;
- InsertLog(s1);
- }
- // }
- }
- // 买入平仓 优先平昨仓
- void test::closebuy1(string sInvestorID, string sName, int sl, double jg)
- {
- //if (mapInstrument[sName].ExchangeID != "SHFE")
- //{
- // OrderInsert(sInvestorID, sName, '0', '1', sl, jg, "", "");
- //}
- //else
- //{
- if (mapPos[sInvestorID][sName].nShortPosYd >= sl)
- {
- OrderInsert(sInvestorID, sName, '0', '4', sl, jg, "", "");
- string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 买入平昨 " + to_string(sl) + " 手,账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
- InsertLog(s1);
- }
- else if (mapPos[sInvestorID][sName].nShortPosYd > 0)
- {
- OrderInsert(sInvestorID, sName, '0', '4', mapPos[sInvestorID][sName].nShortPosYd, jg, "", "");
- int jys2 = min(mapPos[sInvestorID][sName].nShortPosTd, sl - mapPos[sInvestorID][sName].nShortPosYd);
- if (mapPos[sInvestorID][sName].nShortPosTd > 0)
- {
- OrderInsert(sInvestorID, sName, '0', '3', jys2, jg, "", "");
- }
- 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);
- InsertLog(s1);
- }
- else if (mapPos[sInvestorID][sName].nShortPosTd > 0)
- {
- int jys3 = min(mapPos[sInvestorID][sName].nShortPosTd, sl);
- OrderInsert(sInvestorID, sName, '0', '3', jys3, jg, "", "");
- string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 买入平昨仓 " + to_string(jys3) + " 手,账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
- InsertLog(s1);
- }
- else
- {
- 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);;
- InsertLog(s1);
- }
- // }
- }
- // 卖出平仓 优先平今仓
- void test::closesell2(string sInvestorID, string sName, int sl, double jg)
- {
- //if (mapInstrument[sName].ExchangeID != "SHFE")
- //{
- // OrderInsert(sInvestorID, sName, '1', '1', sl, jg, "", "");
- //}
- //else
- //{
- if (mapPos[sInvestorID][sName].nLongPosTd >= sl)
- {
- OrderInsert(sInvestorID, sName, '1', '3', sl, jg, "", "");
- string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 卖出平今 " + to_string(sl) + " 手,账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
- InsertLog(s1);
- }
- else if (mapPos[sInvestorID][sName].nLongPosTd > 0)
- {
- OrderInsert(sInvestorID, sName, '1', '3', mapPos[sInvestorID][sName].nLongPosTd, jg, "", "");
- int jys2 = min(mapPos[sInvestorID][sName].nLongPosYd, sl - mapPos[sInvestorID][sName].nLongPosTd);
- if (mapPos[sInvestorID][sName].nLongPosYd > 0)
- {
- OrderInsert(sInvestorID, sName, '1', '4', jys2, jg, "", "");
- }
- 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);
- InsertLog(s1);
- }
- else if (mapPos[sInvestorID][sName].nLongPosYd > 0)
- {
- int jys3 = min(mapPos[sInvestorID][sName].nLongPosYd, sl);
- OrderInsert(sInvestorID, sName, '1', '4', jys3, jg, "", "");
- string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 卖出平昨仓 " + to_string(jys3) + " 手,账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
- InsertLog(s1);
- }
- else
- {
- 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);;
- InsertLog(s1);
- }
- }
- //
- //}
- // 买入平仓 优先平今仓
- void test::closebuy2(string sInvestorID, string sName, int sl, double jg)
- {
- //if (mapInstrument[sName].ExchangeID != "SHFE")
- //{
- // OrderInsert(sInvestorID, sName, '0', '1', sl, jg, "", "");
- //}
- //else
- //{
- if (mapPos[sInvestorID][sName].nShortPosTd >= sl)
- {
- OrderInsert(sInvestorID, sName, '0', '3', sl, jg, "", "");
- string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 买入平今 " + to_string(sl) + " 手,账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
- InsertLog(s1);
- }
- else if (mapPos[sInvestorID][sName].nShortPosTd > 0)
- {
- OrderInsert(sInvestorID, sName, '0', '3', mapPos[sInvestorID][sName].nShortPosTd, jg, "", "");
- int jys2 = min(mapPos[sInvestorID][sName].nShortPosYd, sl - mapPos[sInvestorID][sName].nShortPosTd);
- if (mapPos[sInvestorID][sName].nShortPosYd > 0)
- {
- OrderInsert(sInvestorID, sName, '0', '4', jys2, jg, "", "");
- }
- 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);
- InsertLog(s1);
- }
- else if (mapPos[sInvestorID][sName].nShortPosYd > 0)
- {
- int jys2 = min(mapPos[sInvestorID][sName].nShortPosYd, sl);
- OrderInsert(sInvestorID, sName, '0', '4', jys2, jg, "", "");
- string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 买入平昨仓 " + to_string(jys2) + " 手,账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
- InsertLog(s1);
- }
- else
- {
- 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);;
- InsertLog(s1);
- }
- }
复制代码 |
|