CTP示例程序下载地址【百度网盘】
[size=12px]百度网盘链接:[/size][color=blue][url=https://pan.baidu.com/s/16FC5BCnCkho24YiYQvozUg]https://pan.baidu.com/s/16FC5BCnCkho24YiYQvozUg[/url] [/color][size=12px]提取码:[/size][color=blue][size=12px]8t1s[/size][/color] 此C++实例程序的核心策略为网格策略,近期会增加策略说明及程序核心代码,希望大家来讨论一下,集思广益,提出您宝贵的意见。程序版本会不断更新,详见网盘中说明文档。 策略:(多方向)1、快均线与慢均线金叉即做多方向。
2、首次开仓间隔15秒左右,即时价格小于15秒之前价格,开多仓,开仓头寸为配置文件中参数设置的一个基本持仓单元。
3、按照首次开仓价格向下铺设网格,网格大小固定,可以在配置文件中设定。首次开仓为1级持仓,增仓依次为2/3/4/5/6...级持仓,增仓不得大于设定的最大级数。
4、价格下滑至开仓级别对应的下一级网格线,即增开多仓,增仓头寸为配置文件中参数设置的一个基本持仓单元。
5、达到盈利目标即平仓,或者达到止损即平仓。 [code]/*网格策略入口*/
bool Entry(const CDataNodV2 & pData, CTradeInfo & OneTrade, bool bOnSimulation)
{
/*1:OneTrade初始化*/
OneTrade.Init();
/*2:捕获该合约的最小变动和乘数*/
m_MinMove = CCTPInfo::GetInstance().GetMinMove(pData.TypeIndex);
if (m_MinMove <= 0.0)
{
CLogFile::DumpSystem("Error:The GetMinMove Function Does Not Capture The MinMove!");
return false;
}
m_Multiple = CCTPInfo::GetInstance().GetMultiple(pData.TypeIndex);
if (m_Multiple <= 0.0)
{
CLogFile::DumpSystem("Error:The GetMultiple Function Does Not Capture The Multiple!");
return false;
}
/*3:获取策略计算需要数据和判断值*/
CDataManager& pDataManager = CDataManager::GetInstance();
CIndicatorContainer& pIndicators = pDataManager.m_ColumnData.m_Indicators;
double MASlowValue = pIndicators.MA_Slow_Deque[pData.TypeIndex].back();
double MAFastValue = pIndicators.MA_Fast_Deque[pData.TypeIndex].back();
if (MASlowValue <= 0) return false;
if (MAFastValue <= 0) return false;
SegmentSize = CConfigFile::GetInstance().m_SegmentSize;
Interval = CConfigFile::GetInstance().m_Interval * 2;
ProfitTarget = CConfigFile::GetInstance().m_ProfitTarget;
LossLimit = CConfigFile::GetInstance().m_LossLimit;
CurrTradingDay = CTime(pData.Time).GetDay();
bIsTradingDayChanged = (CurrTradingDay != PrevTradingDay);
PrevTradingDay = CurrTradingDay;
if (bIsTradingDayChanged) { Counter = 0; }
if (Counter >= 10000) { Counter = 0; }
PriceOfCounter[Counter] = pData.LastPrice;
bIsCounterGreaterThanInterval = (Counter >= Interval);
bIsThisPriceLessThanPrevPriceCanBuy = false;
bIsThisPriceGreaterThanPrevPriceCanSell = false;
if (bIsCounterGreaterThanInterval)
{
bIsThisPriceLessThanPrevPriceCanBuy = (PriceOfCounter[Counter] < PriceOfCounter[Counter - Interval]);
bIsThisPriceGreaterThanPrevPriceCanSell = (PriceOfCounter[Counter] > PriceOfCounter[Counter - Interval]);
}
Counter++;
/*4:检查持仓情况*/
CHoldManager& pHoldManager = CHoldManager::GetInstance();
map<int, CTradeInfo>& pHoldMap = pHoldManager.m_Hold;
int Lots = CConfigFile::GetInstance().m_Lots;
int HoldingIndex_buy = pData.TypeIndex * 10000 + buy;
bIsHolding_buy = (pHoldMap.find(HoldingIndex_buy) != pHoldMap.end());
HoldingLevel_buy = 0;
CurrProfit_buy = 0.0;
if (bIsHolding_buy)
{
HoldingLevel_buy = pHoldMap[HoldingIndex_buy].Volumn / Lots;
CurrProfit_buy = (pData.LastPrice - pHoldMap[HoldingIndex_buy].Price)*pHoldMap[HoldingIndex_buy].Volumn*m_Multiple;
}
bIsAllowedLevel_buy = (HoldingLevel_buy <= CConfigFile::GetInstance().m_MaxLevelAllowed);
int HoldingIndex_sell = pData.TypeIndex * 10000 + sell;
bIsHolding_sell = (pHoldMap.find(HoldingIndex_sell) != pHoldMap.end());
HoldingLevel_sell = 0;
CurrProfit_sell = 0.0;
if (bIsHolding_sell)
{
HoldingLevel_sell = pHoldMap[HoldingIndex_sell].Volumn / Lots;
CurrProfit_sell = (pHoldMap[HoldingIndex_sell].Price - pData.LastPrice)*pHoldMap[HoldingIndex_sell].Volumn*m_Multiple;
}
bIsAllowedLevel_sell = (HoldingLevel_sell <= CConfigFile::GetInstance().m_MaxLevelAllowed);
int Type = CStockBase::GetStockTypeFromIndex(pData.TypeIndex);
bool bTypeIsHolding = pHoldManager.m_TypeIsHolding[Type]; /*此合约所在的品种是否持仓,开仓时检查此合约所在的品种是否持仓*/
/*调试代码:策略的关键数据,和Tick数据对照*/
if (CConfigFile::GetInstance().m_bRunTestCode)//&& !bOnSimulation)
{
printf("当前Tick:");
pData.PrintDataNodV2ToScreen();
printf("慢均线值(10D)[%.2f]", MASlowValue);
printf("快均线值(5D)[%.2f]", MAFastValue);
printf("是否是新的交易日[%2d]", bIsTradingDayChanged);
printf("\n");
printf("间隔期[%d]", Interval);
printf("计数器[%d]", Counter - 1);
printf("计数器对应的价格[%.2f]", PriceOfCounter[Counter - 1]);
printf("间隔前价格[%.2f]", PriceOfCounter[Counter - 1 - Interval]);
printf("计数器是否大于间隔[%2d]", bIsCounterGreaterThanInterval);
printf("是否可买[%2d]", bIsThisPriceLessThanPrevPriceCanBuy);
printf("是否可卖[%2d]", bIsThisPriceGreaterThanPrevPriceCanSell);
printf("\n");
printf("bIsHolding_buy[%2d]", bIsHolding_buy);
printf("bIsHolding_sell[%2d]", bIsHolding_sell);
printf("HoldingLevel_buy[%2d]", HoldingLevel_buy);
printf("HoldingLevel_sell[%2d]", HoldingLevel_sell);
printf("CurrProfit_buy[%.2f]", CurrProfit_buy);
printf("CurrProfit_sell[%.2f]", CurrProfit_sell);
printf("\n");
system("pause");
}
/*BUY方向的首次开仓信号*/
if (MAFastValue > MASlowValue && !bIsHolding_buy && bIsCounterGreaterThanInterval && bIsThisPriceLessThanPrevPriceCanBuy)
{
OneTrade.tradetype = buy;
OneTrade.Price = pData.S1;/*用申卖价买入*/
OneTrade.openclosetype = open;
sprintf(OneTrade.m_Dptr, "SegmentStrategy:OpenBuy");
WritePublicInfoToTradeInfo(pData, OneTrade);
FirstOpenBuyPrice = OneTrade.Price;
Counter = 0;
return true;
}
/*SELL方向的首次开仓信号*/
if (MAFastValue < MASlowValue && !bIsHolding_sell && bIsCounterGreaterThanInterval && bIsThisPriceGreaterThanPrevPriceCanSell)
{
OneTrade.tradetype = sell;
OneTrade.Price = pData.B1;/*用申买价卖出*/
OneTrade.openclosetype = open;
sprintf(OneTrade.m_Dptr, "SegmentStrategy:OpenSell");
WritePublicInfoToTradeInfo(pData, OneTrade);
FirstOpenSellPrice = OneTrade.Price;
Counter = 0;
return true;
}
/*BUY方向的追仓信号*/
if (HoldingLevel_buy != 0 && bIsAllowedLevel_buy && (pData.LastPrice < FirstOpenBuyPrice - SegmentSize * m_MinMove * HoldingLevel_buy))
{
OneTrade.tradetype = buy;
OneTrade.Price = pData.S1;/*用申卖价买入*/
OneTrade.openclosetype = open;
sprintf(OneTrade.m_Dptr, "SegmentStrategy:AddBuy-%d", HoldingLevel_buy);
WritePublicInfoToTradeInfo(pData, OneTrade);
return true;
}
/*SELL方向的追仓信号*/
if (HoldingLevel_sell != 0 && bIsAllowedLevel_sell && (pData.LastPrice > FirstOpenSellPrice + SegmentSize * m_MinMove * HoldingLevel_sell))
{
OneTrade.tradetype = sell;
OneTrade.Price = pData.B1;/*用申买价卖出*/
OneTrade.openclosetype = open;
sprintf(OneTrade.m_Dptr, "SegmentStrategy:AddSell-%d", HoldingLevel_sell);
WritePublicInfoToTradeInfo(pData, OneTrade);
return true;
}
/*BUY方向的退出信号*/
if (bIsHolding_buy && (CurrProfit_buy >= ProfitTarget * HoldingLevel_buy || CurrProfit_buy <= LossLimit * -1))
{
OneTrade.tradetype = sell;
OneTrade.Price = pData.B1;/*用申买价卖出*/
OneTrade.openclosetype = close;
sprintf(OneTrade.m_Dptr, "SegmentStrategy:CloseSell");
WritePublicInfoToTradeInfo(pData, OneTrade);
return true;
}
/*SELL方向的退出信号*/
if (bIsHolding_sell && (CurrProfit_sell >= ProfitTarget * HoldingLevel_sell || CurrProfit_sell <= LossLimit * -1))
{
OneTrade.tradetype = buy;
OneTrade.Price = pData.S1;/*用申卖价买入*/
OneTrade.openclosetype = close;
sprintf(OneTrade.m_Dptr, "SegmentStrategy:CloseBuy");
WritePublicInfoToTradeInfo(pData, OneTrade);
return true;
}
/*策略未执行,返回false*/
return false;
}[/code] {:57:} 虽然看不懂是很厉害的样子。 系统程序更新至20200306版本
BETA.V6.3.15.20200306.X86_64
页:
[1]