- UID
- 2
- 积分
- 2892487
- 威望
- 1396275 布
- 龙e币
- 1496212 刀
- 在线时间
- 13313 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2024-12-25
|
[code][hide=1][code]//****************************************************************外部引用文件开始(谨慎修改)
#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;
}
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);
InsertLog(s);
}
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;
}
//****************************************************************主程序功能性模块结束(谨慎修改)
//****************************************************************主程序指标公式function计算模块开始(编辑与设计区)
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::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::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::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::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::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;
}
//****************************************************************主程序指标公式function计算模块结束(编辑与设计区)
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;
}
//****************************************************************主程序程式码核心策略模块开始
/// <summary>
/// 以下开始
/// </summary>
//设置程序化交易开始、暂时、重置、停止时间模块开始
string timeStart = "08:50:00";
string timePause = "15:32:00";
string timeResume = "20:50:00";
string timeStop = "02:32:00";
string state = "stop";
//设置程序化交易开始、暂时、重置、停止时间模块结束
//****************************************************************交易界面参数编辑与设计模块开始
void test::InitParm()
{
TDLLPARM t;
//Name 参数名, Value 默认值, Explain 参数说明, tend 表示一个参数结束
t.Name = "均线周期"; t.Value = "120"; t.Explain = "均线周期参数"; tend(t);
t.Name = "入场周期"; t.Value = "20"; t.Explain = "入场最高最低的周期参数"; tend(t);
t.Name = "出场周期"; t.Value = "10"; 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 = "1"; t.Explain = "是否输出行情,1是0否"; tend(t);
end();
}
//****************************************************************交易界面参数编辑与设计模块结束
//****************************************************************时间计算模块,用于未成交撤单时间的计算(开始)
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;
}
//****************************************************************时间计算模块,用于未成交撤单时间的计算(结束)
//****************************************************************点击交易界面“运行”按钮后动作模块,用来加载参数与开始启动交易(开始)
void test::OnRun()
{
OnState("run");
if (state != "run")return;
sound("sound\\run.wav");
RsqInstrument(sInst);
RsqRspQryOrder();
RsqPosition();
RsqRspQryTrade();
RsqAccount();
SubscribeMarketData(sInst);
RsqBar(sPeriod, sInst);
num = 0;
jxzq = atoi(parm["均线周期"].Value.c_str());
rczq = atoi(parm["入场周期"].Value.c_str());
cczq = 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());
sc = atoi(parm["输出行情"].Value.c_str());
ma = avg(sPeriod, sInst, jxzq);
ma1 = avg1(sPeriod, sInst, jxzq, 1);
hi = highest1(sPeriod, sInst, rczq, 0);
lo = lowerest1(sPeriod, sInst, rczq, 0);
hi1 = highest1(sPeriod, sInst, cczq, 0);
lo1 = lowerest1(sPeriod, sInst, cczq, 0);
string s2 = " 均线 " + to_string(ma) + " 前根均线 " + to_string(ma1) + " 入场最高 " + to_string(hi) + " 入场最低 " + to_string(lo) + " 出场最高 " + to_string(hi1) + " 出场最低 " + to_string(lo1);
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(rczq) + " 出场周期 " + to_string(cczq) + " 单笔风险 " + to_string(dbfx) + " 滑点 " + to_string(hd) + " 优先开平 " + to_string(yxpc) + " 撤单时间 " + to_string(jg) + " 方向 " + to_string(fx) + " 数量 " + to_string(ss) + " 输出行情 " + to_string(sc);
InsertLog(s1);
}
//****************************************************************点击交易界面“运行”按钮后动作模块,用来加载参数与开始启动交易(结束)
//****************************************************************点击交易界面“停止”按钮后动作模块,用来停止量化交易(开始)
void test::OnStop()
{
if (state == "stop")return;
OnState("stop");
sound("sound\\stop.wav");
xieruzhuangtai();
InsertLog(" 周期交易策略停止运行,方向 " + to_string(fx) + " 手数 " + to_string(ss));
}
//****************************************************************点击交易界面“停止”按钮后动作模块,用来停止量化交易(结束)
//****************************************************************有tick数据传入后动作模块设计(用tick数据设计程式化交易核心模块)(开始)
void test::OnMarketData(CThostFtdcDepthMarketDataField* t)
{
return;
if (state != "run")return;
if (t->InstrumentID != sInst)return;
mapMd[t->InstrumentID] = *t;
string sss = t->UpdateTime;
if (sss == "20:59:00" || sss == "08:59:00")return;
if (fx == 0)
{
if (t->LastPrice >= hi && ma > ma1)
{
fx = 1;
if (dbfx < 200)ss = dbfx;
if (dbfx >= 200)ss = (int)floor(dbfx / (hi - lo) / 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->LastPrice + hd * mapInstrument[sInst].PriceTick, "", "");
string s = it->first + " 突破最高多单达到入场条件买入开仓 " + to_string(sl) + " 手,价格 " + to_string(t->LastPrice + hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 入场最高 " + to_string(hi) + " 入场最低 " + to_string(lo);
maps[num] = s;
num++;
}
if (num != 0)shuchurizhi();
tm = 0;
xieruzhuangtai();
}
if (t->LastPrice <= lo && ma < ma1)
{
fx = -1;
if (dbfx < 200)ss = dbfx;
if (dbfx >= 200)ss = (int)floor(dbfx / (hi - lo) / 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->LastPrice - hd * mapInstrument[sInst].PriceTick, "", "");
string s = it->first + " 突破最低空单达到入场条件卖出开仓 " + to_string(sl) + " 手,价格 " + to_string(t->LastPrice - hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 入场最高 " + to_string(hi) + " 入场最低 " + to_string(lo);
maps[num] = s;
num++;
}
if (num != 0)shuchurizhi();
tm = 0;
xieruzhuangtai();
}
}
if (fx == 1)
{
if (t->LastPrice <= lo1)
{
fx = 0;
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->LastPrice - hd * mapInstrument[sInst].PriceTick);
}
else if (yxpc == 1)
{
closesell2(it->first, sInst, sl, t->LastPrice - hd * mapInstrument[sInst].PriceTick);
}
string s = it->first + " 多单达到出场条件卖出平仓 " + to_string(sl) + " 手,价格 " + to_string(t->LastPrice - hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 出场最高 " + to_string(hi1) + " 出场最低 " + to_string(lo1);
maps[num] = s;
num++;
}
if (num != 0)shuchurizhi();
tm = 0;
xieruzhuangtai();
}
if (t->LastPrice <= lo && ma < ma1)
{
fx = -1;
if (dbfx < 200)ss = dbfx;
if (dbfx >= 200)ss = (int)floor(dbfx / (hi - lo) / 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->LastPrice - hd * mapInstrument[sInst].PriceTick, "", "");
string s = it->first + " 突破最低空单达到入场条件卖出开仓 " + to_string(sl) + " 手,价格 " + to_string(t->LastPrice - hd * mapInstrument[sInst].PriceTick) + " " + to_string(ss) + " 基础手数 " + to_string(ss) + " 入场最高 " + to_string(hi) + " 入场最低 " + to_string(lo);
maps[num] = s;
num++;
}
if (num != 0)shuchurizhi();
tm = 0;
xieruzhuangtai();
}
}
if (fx == -1)
{
if (t->LastPrice >= hi1)
{
fx = 0;
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->LastPrice + hd * mapInstrument[sInst].PriceTick);
}
else if (yxpc == 1)
{
closebuy2(it->first, sInst, sl, t->LastPrice + hd * mapInstrument[sInst].PriceTick);
}
string s = it->first + " 空单达到出场条件买入平仓 " + to_string(ss) + " 手,价格 " + to_string(t->LastPrice + hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 出场最高 " + to_string(hi1) + " 出场最低 " + to_string(lo1);
maps[num] = s;
num++;
}
if (num != 0)shuchurizhi();
tm = 0;
xieruzhuangtai();
}
if (t->LastPrice >= hi && ma > ma1)
{
fx = 1;
if (dbfx < 200)ss = dbfx;
if (dbfx >= 200)ss = (int)floor(dbfx / (hi - lo) / 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->LastPrice + hd * mapInstrument[sInst].PriceTick, "", "");
string s = it->first + " 突破最高多单达到入场条件买入开仓 " + to_string(sl) + " 手,价格 " + to_string(t->LastPrice + hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 入场最高 " + to_string(hi) + " 入场最低 " + to_string(lo);
maps[num] = s;
num++;
}
if (num != 0)shuchurizhi();
tm = 0;
xieruzhuangtai();
}
}
}
//****************************************************************有tick数据传入后动作模块设计(用tick数据设计程式化交易核心模块)(结束)
//**************************************************************K线bar回报相关模块
void test::OnRsqBar(string sPeriod, string sInst, map<string, TKVALUE> mp)
{
InsertLog(" 历史数据 " + to_string(mapK.size()));
//if (state != "run")return;
mapK[sPeriod][sInst] = mp;
// int n = mp.size();
// //int m = 0;
//
// if (n < jxzq || n < rczq || n < cczq)InsertLog(sInst + " bar 数量不够 ");
/*map<string, TKVALUE>::reverse_iterator it;
for (it = mapK[sPeriod][sInst].rbegin(); it != mapK[sPeriod][sInst].rend(); ++it)
{
m++;
TKVALUE t = it->second;
string s = t.sDate + "--.>" + t.sTime + "," + t.InstrumentID + "," + to_string(t.dOpen) + "," + to_string(t.dHigh)
+ "," + to_string(t.dLow) + "," + to_string(t.dClose);
InsertLog(s);
if (m > 200)return;
}*/
}
//**************************************************************K线bar回报相关模块
//**************************************************************有新K线bar产生相关模块(以bar为程序化设计思路核心模块)【开始】
void test::OnBarOpen(TKVALUE t)
{
//InsertLog(" 新数据 ");
string s = t.sDate + "--.>" + t.sTime + "," + t.InstrumentID + "," + to_string(t.dOpen) + "," + to_string(t.dHigh)
+ "," + to_string(t.dLow) + "," + to_string(t.dClose);
InsertLog(s);
/*if (state != "run")return;
map<string, double>::iterator it;
for (it = mapSub.begin(); it != mapSub.end(); it++)
{
RsqPositionDetail(it->first);
}
InsertLog("================输出委托===============");
shuchuweituo();
InsertLog("================输出成交===============");
shuchuchengjiao();
InsertLog("================输出持仓===============");
shuchuchicang();
InsertLog("================输出持仓明细===============");
shuchuchicangmingxi();*/
//if (t.InstrumentID != sInst)return;
// mapK[sPeriod][t.InstrumentID][t.sDate + t.sDayNight + t.sTime] = t;
//
//
// ma = avg(sPeriod, sInst, jxzq);
// ma1 = avg1(sPeriod, sInst, jxzq, 1);
// hi = highest1(sPeriod, sInst, rczq, 0);
// lo = lowerest1(sPeriod, sInst, rczq, 0);
// hi1 = highest1(sPeriod, sInst, cczq, 0);
// lo1 = lowerest1(sPeriod, sInst, cczq, 0);
// string s2 = " 均线 " + to_string(ma) + " 前根均线 " + to_string(ma1) + " 入场最高 " + to_string(hi) + " 入场最低 " + to_string(lo) + " 出场最高 " + to_string(hi1) + " 出场最低 " + to_string(lo1);
// if (sc == 1)InsertLog(s2);
//int m = 0;
//map<string, TKVALUE>::iterator it;
//for (it = mapK[sPeriod][sInst].begin(); it != mapK[sPeriod][sInst].end(); ++it)
//{
// m++;
// TKVALUE t = it->second;
// string s = t.sDate + "--.>" + t.sTime + "," + t.InstrumentID + "," + to_string(t.dOpen) + "," + to_string(t.dHigh)
// + "," + to_string(t.dLow) + "," + to_string(t.dClose);
// InsertLog(s);
// if (m > 10)return;
//}
//map<string, TKVALUE>::reverse_iterator it;
//for (it = mapK[sPeriod][sInst].rbegin(); it != mapK[sPeriod][sInst].rend(); ++it)
//{
// m++;
// TKVALUE t = it->second;
// string s = t.sDate + "--.>" + t.sTime + "," + t.InstrumentID + "," + to_string(t.dOpen) + "," + to_string(t.dHigh)
// + "," + to_string(t.dLow) + "," + to_string(t.dClose);
// InsertLog(s);
// if (m > 10)return;
//}
}
//**************************************************************有新K线bar产生相关模块(以bar为程序化设计思路核心模块)【结束】
//****************************************************************主程序程式码核心策略模块结束
//****************************************************************主程序订单委托与交易回报模块开始(谨慎修改)
//**************************************************************订单回报相关模块
void test::OnRtnOrder(CThostFtdcOrderField t)
{
InsertLog(" 委托回报 " + to_string(mapOrder.size()));
mapOrder[t.OrderLocalID] = t;
/*if (!mapMd.count(t.InstrumentID))SubscribeMarketData(t.InstrumentID);
if (state != "run")return;
if (t.OrderStatus == '5' && t.MacAddress == sName) chongfa(t);*/
/*string s1 = t.InstrumentID;
string s2 = " 合约 " + s1 + " 账号 " + t.InvestorID + " 方向 " + t.Direction + " 开平 " + t.CombOffsetFlag + " 状态 " + t.OrderStatus + " 信息 " + t.StatusMsg + " 未成交手数 " + to_string(t.VolumeTotal) + " 成交手数 " + to_string(t.VolumeTraded);
InsertLog(s2);*/
}
//**************************************************************订单回报相关模块
//**************************************************************成交回报相关模块
void test::OnRtnTrade(CThostFtdcTradeField t)
{
InsertLog(" 成交回报 " + to_string(mapTrade.size()));
mapTrade[t.TradeID] = t;
if (state != "run")return;
RsqPosition();
RsqPositionDetail(t.InvestorID);
}
//**************************************************************成交回报相关模块
void test::OnInstrument(CThostFtdcInstrumentField t)
{
// string s = t.InstrumentID;
// InsertLog(" 查询合约 " + s);
mapInstrument[t.InstrumentID] = t;
/* int i = 0;
map<string, CThostFtdcInstrumentField>::iterator it;
for (it = m->begin(); it != m->end(); ++it)
{
i++;
}
InsertLog(to_string(i));*/
}
void test::OnRspQryOrder(map<string, CThostFtdcOrderField>* m)
{
InsertLog(" 历史委托回报 " + to_string(mapOrder.size()));
mapOrder = *m;
//InsertLog("RspQryOrder");
}
void test::OnRspQryTrade(map<string, CThostFtdcTradeField>* m)
{
InsertLog(" 历史成交回报 " + to_string(mapTrade.size()));
mapTrade = *m;
/*map<string, CThostFtdcTradeField>::iterator it;
for (it = m->begin(); it != m->end(); ++it)
{
InsertLog(it->first);
}*/
}
void test::OnPosition(map<string, map<string, TPOSITION>>* m)
{
InsertLog(" 持仓回报 ");
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)
{
//InsertLog(" 手续费回报 ");
mapCom = *m;
}
void test::OnAccount(map<string, CThostFtdcTradingAccountField>* m)
{
//InsertLog(" 资金回报 ");
mapAcc = *m;
}
void test::OnInstrumentStatus(CThostFtdcInstrumentStatusField* t)
{
//InsertLog(" 状态改变汇报 ");
/* if (state != "run")return;
if (t.InstrumentID != sMainInst)return;
string sInst = t.InstrumentID;
string sTime = t.EnterTime;
string status;
if (t.InstrumentStatus == '0')status = "开盘前";
if (t.InstrumentStatus == '1')status = "非交易";
if (t.InstrumentStatus == '2')status = "连续交易";
if (t.InstrumentStatus == '3')status = "集合竞价报单";
if (t.InstrumentStatus == '4')status = "集合竞价价格平衡";
if (t.InstrumentStatus == '5')status = "集合竞价撮合";
if (t.InstrumentStatus == '6')status = "收盘";
InsertLog(sTime + "--->>>" + sInst + ":" + status);*/
}
void test::OnInstrumentAll(map<string, CThostFtdcInstrumentField>* m)
{
mapInstrument = *m;
}
void test::shuchuchicang()
{
num;
map<string, map<string, TPOSITION>>::iterator it;
for (it = mapPos.begin(); it != mapPos.end(); it++)
{
map<string, TPOSITION>::iterator itt;
for (itt = it->second.begin(); itt != it->second.end(); itt++)
{
num++;
string s2 = " 账户 " + it->first + " 合约 " + itt->first + " 持仓有空单今仓数量 " + to_string(itt->second.nShortPosTd) + " 手持有昨仓空单数量 " + to_string(itt->second.nShortPosYd)
+ " 持仓有多单今仓数量 " + to_string(itt->second.nLongPosTd) + " 手持有昨仓多单数量 " + to_string(itt->second.nLongPosYd);
InsertLog(s2);
}
}
InsertLog(" 持仓数量 " + to_string(num));
num = 0;
}
void test::shuchuweituo()
{
InsertLog(" 委托数量 " + to_string(mapOrder.size()));
map<string, CThostFtdcOrderField>::iterator it;
for (it = mapOrder.begin(); it != mapOrder.end(); it++)
{
/*if (it->second.OrderStatus == '3' || it->second.OrderStatus == '1')
{*/
string s1 = " " + it->first + " " + it->second.InvestorID + " " + it->second.InstrumentID + " 买卖 " + it->second.Direction + " 开平 " + it->second.CombOffsetFlag + " 数量 " + to_string(it->second.VolumeTotal);
InsertLog(s1);
//}
}
}
void test::shuchuchengjiao()
{
InsertLog(" 成交数量 " + to_string(mapTrade.size()));
map<string, CThostFtdcTradeField>::iterator it;
for (it = mapTrade.begin(); it != mapTrade.end(); it++)
{
string s1 = " " + it->first + " " + it->second.InvestorID + " 方向 " + it->second.Direction + " 开平 " + it->second.OffsetFlag + " 数量 " + to_string(it->second.Volume);
InsertLog(s1);
}
}
void test::shuchuchicangmingxi()
{
InsertLog(" 持仓明细数量 " + to_string(mapPosDeta.size()));
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::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 = 0;
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, "", t.OrderLocalID);
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);
}
}
//****************************************************************主程序订单委托与交易回报模块结束(谨慎修改)[/code][/hide][/code] |
|