- UID
- 20074
- 积分
- 250
- 威望
- 125 布
- 龙e币
- 125 刀
- 在线时间
- 25 小时
- 注册时间
- 2019-6-15
- 最后登录
- 2023-2-15
|
- /*网格策略入口*/
- 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;
- }
复制代码 |
|