策略出现在非交易时间委托导致错误与盘中也会出现错误的现象
[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);
}
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 = "20"; t.Explain = "均线周期参数,默认20"; tend(t);
t.Name = "单笔风险"; t.Value = "1"; t.Explain = "交易手数,默认1手"; 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 = "5"; t.Explain = "委托后多少秒检查一下是否有未成交,小于0 不撤单"; tend(t);
t.Name = "策略说明"; t.Value = "0"; t.Explain = "价格超越均线入场开多,跌破均线入场做空,收盘价反向穿越均线出场,"; 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;
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());
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);
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(dbfx) + " 滑点 " + to_string(hd) + " 优先开平 " + to_string(yxpc) + " 撤单时间 "
+ to_string(jg) + " 持仓方向 " + to_string(fx) + " 数量 " + to_string(ss);
InsertLog(s1);
}
void test::OnStop() //点击停止按钮动作模块
{
OnState("stop");
sound("sound\\stop.wav");
xieruzhuangtai();
InsertLog(" 单均线交易系统停止运行,方向 " + to_string(fx) + " 手数 " + to_string(ss));
}
void test::OnMarketData(CThostFtdcDepthMarketDataField* t) //监测有新的tick价格传入,即是否处于交易时间
{
if (state != "run")return;
}
/********************************************委托与成交反馈相关开始***********************************************/
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::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 数量不够 ");
}
/************************************************************************bar2bar模块(开始)*******************************************************************/
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)最新价小于均线价格,无持仓进行卖出开仓模块(sellshort model)
if (mapMd[sInst].LastPrice < ma && fx == 0)
{
fx = -1;
ss = dbfx;
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, mapMd[sInst].AskPrice1 - hd * mapInstrument[sInst].PriceTick, "", "");//卖出开仓语句
string s = it->first + " 突破入场价格空单达到入场条件卖出开仓 " + to_string(sl) + " 手,价格 " + 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();
}
//(2)最新价大于均线价格,无持仓进行卖入开仓模块(buy model)
if (mapMd[sInst].LastPrice > ma && fx == 0)
{
fx = 1;
ss = dbfx;
map<string, double>::iterator it;
for (it = mapSub.begin(); it != mapSub.end(); it++)
{
int sl = (int)(ss * it->second); //获取根据单笔风险计算的交易数量并赋值给(sl)数量
OrderInsert(it->first, sInst, '0', '0', sl, mapMd[sInst].BidPrice1 + hd * mapInstrument[sInst].PriceTick, "", "");//买入开仓语句
//输出至交易界面的log里面
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();
}
//(3)最新价小于均线价格,持有多单进行平仓模块(sell model)
if (mapMd[sInst].LastPrice < ma && fx == 1)
{
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, mapMd[sInst].AskPrice1 - hd * mapInstrument[sInst].PriceTick);//卖出平仓,优先平昨仓
}
else if (yxpc == 1)
{
closesell2(it->first, sInst, sl, mapMd[sInst].AskPrice1 - 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();
}
//(4)最新价大于均线价格,持有空单进行平仓模块(buytocover model)
if (mapMd[sInst].LastPrice > ma && fx == -1)
{
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, mapMd[sInst].BidPrice1 + hd * mapInstrument[sInst].PriceTick); //买入平仓,优先平昨仓
}
else if (yxpc == 1)
{
closebuy2(it->first, sInst, sl, mapMd[sInst].BidPrice1 + 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();
}
}
/************************************************************************bar2bar模块(结束)*******************************************************************/
/************************************************************************委托与交易模块(开始)****************************************************************/
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);
}
}
/************************************************************************委托与交易模块(结束)************************************************************************/[/code] 出现错误的模块:[code]/************************************************************************bar2bar模块(开始)*******************************************************************/
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)最新价小于均线价格,无持仓进行卖出开仓模块(sellshort model)
if (mapMd[sInst].LastPrice < ma && fx == 0)
{
fx = -1;
ss = dbfx;
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, mapMd[sInst].AskPrice1 - hd * mapInstrument[sInst].PriceTick, "", "");//卖出开仓语句
string s = it->first + " 突破入场价格空单达到入场条件卖出开仓 " + to_string(sl) + " 手,价格 " + 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();
}
//(2)最新价大于均线价格,无持仓进行卖入开仓模块(buy model)
if (mapMd[sInst].LastPrice > ma && fx == 0)
{
fx = 1;
ss = dbfx;
map<string, double>::iterator it;
for (it = mapSub.begin(); it != mapSub.end(); it++)
{
int sl = (int)(ss * it->second); //获取根据单笔风险计算的交易数量并赋值给(sl)数量
OrderInsert(it->first, sInst, '0', '0', sl, mapMd[sInst].BidPrice1 + hd * mapInstrument[sInst].PriceTick, "", "");//买入开仓语句
//输出至交易界面的log里面
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();
}
//(3)最新价小于均线价格,持有多单进行平仓模块(sell model)
if (mapMd[sInst].LastPrice < ma && fx == 1)
{
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, mapMd[sInst].AskPrice1 - hd * mapInstrument[sInst].PriceTick);//卖出平仓,优先平昨仓
}
else if (yxpc == 1)
{
closesell2(it->first, sInst, sl, mapMd[sInst].AskPrice1 - 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();
}
//(4)最新价大于均线价格,持有空单进行平仓模块(buytocover model)
if (mapMd[sInst].LastPrice > ma && fx == -1)
{
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, mapMd[sInst].BidPrice1 + hd * mapInstrument[sInst].PriceTick); //买入平仓,优先平昨仓
}
else if (yxpc == 1)
{
closebuy2(it->first, sInst, sl, mapMd[sInst].BidPrice1 + 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();
}
}
/************************************************************************bar2bar模块(结束)*******************************************************************/
[/code] 找到问题所在,出在这里:[code]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();
}[/code]这是传回订单结果的语句,在里面有一个语句:[code] if (t.OrderStatus == '0' && s != "0" && t.MacAddress == sName) OnStop();[/code]问题就出在这里,之前的策略是在一个出场策略上面修改的,所以订单结束后策略就停止……只要把这一个语句注释掉或删除掉就正常了。注释的方式是 :[code] // if (t.OrderStatus == '0' && s != "0" && t.MacAddress == sName) OnStop();[/code]即可。
页:
[1]