/// <summary> /// 字符串转换成持仓对象 /// </summary> /// <param name="positionResult"></param> /// <returns></returns> public static SecurityPosition ConvertToStockPosition(String positionResult) { if (positionResult == null || positionResult.Length == 0) { return(null); } String[] positionFields = positionResult.Split(" ".ToCharArray()); if (positionFields == null || positionFields.Length < 14) { return(null); } int index = 0; SecurityPosition position = new SecurityPosition(); position.m_stockCode = positionFields[index++]; position.m_stockName = positionFields[index++]; position.m_stockBalance = CStrA.ConvertStrToDouble(positionFields[index++]); position.m_availableBalance = CStrA.ConvertStrToDouble(positionFields[index++]); position.m_volume = CStrA.ConvertStrToDouble(positionFields[index++]); position.m_frozenVolume = CStrA.ConvertStrToDouble(positionFields[index++]); position.m_positionProfit = CStrA.ConvertStrToDouble(positionFields[index++]); position.m_positionCost = CStrA.ConvertStrToDouble(positionFields[index++]); position.m_positionProfitRatio = CStrA.ConvertStrToDouble(positionFields[index++]); position.m_marketPrice = CStrA.ConvertStrToDouble(positionFields[index++]); position.m_marketValue = CStrA.ConvertStrToDouble(positionFields[index++]); position.m_redemptionVolume = CStrA.ConvertStrToDouble(positionFields[index++]); position.m_marketName = positionFields[index++]; position.m_investorAccount = positionFields[index++]; return(position); }
/// <summary> /// 设置股票账户持仓信息 /// </summary> /// <param name="stockPositionResult"></param> private void SetSecurityPosition(String stockPositionResult) { m_dictSecurityPositions.Clear(); if (stockPositionResult == null || stockPositionResult.Length == 0) { return; } String[] lines = stockPositionResult.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (lines == null || lines.Length < 2) { return; } m_gridPositionAccount.BeginUpdate(); m_gridPositionAccount.ClearRows(); for (int i = 1; i < lines.Length; i++) { SecurityPosition position = SecurityPosition.ConvertToStockPosition(lines[i]); if (position != null) { m_dictSecurityPositions[position.m_stockCode] = position; GridRow row = new GridRow(); m_gridPositionAccount.AddRow(row); GridCell cell = new GridStringCell(position.m_stockCode); row.AddCell(0, cell); cell = new GridStringCell(position.m_stockName); row.AddCell(1, cell); cell = new GridDoubleCell(position.m_stockBalance); row.AddCell(2, cell); cell = new GridDoubleCell(position.m_availableBalance); row.AddCell(3, cell); cell = new GridDoubleCell(position.m_volume); row.AddCell(4, cell); cell = new GridDoubleCell(position.m_frozenVolume); row.AddCell(5, cell); cell = new GridDoubleCell(position.m_positionProfit); row.AddCell(6, cell); cell = new GridDoubleCell(position.m_positionCost); row.AddCell(7, cell); cell = new GridDoubleCell(position.m_positionProfitRatio); row.AddCell(8, cell); cell = new GridDoubleCell(position.m_marketPrice); row.AddCell(9, cell); cell = new GridDoubleCell(position.m_marketValue); row.AddCell(10, cell); cell = new GridDoubleCell(position.m_redemptionVolume); row.AddCell(11, cell); cell = new GridStringCell(position.m_marketName); row.AddCell(12, cell); cell = new GridStringCell(position.m_investorAccount); row.AddCell(13, cell); } } m_gridPositionAccount.EndUpdate(); m_gridPositionAccount.Invalidate(); }
/// <summary> /// /// </summary> /// <param name="param"></param> public void DealStrategyThread(object param) { SecurityLatestData latestData = param as SecurityLatestData; if (latestData == null) { return; } if (m_securityStrategySettingCurrnet == null || m_securityStrategySettingCurrnet.m_strategyType != 0) { return; } SecurityRangeTradeCondition securityRangeTradeCondition = JsonConvert.DeserializeObject <SecurityRangeTradeCondition>(m_securityStrategySettingCurrnet.m_strategySettingInfo); if (securityRangeTradeCondition == null) { return; } bool isInitBuild = securityRangeTradeCondition.m_initBuildFlag; float bottomRangePrice = securityRangeTradeCondition.m_bottomRangePrice; float topRangePrice = securityRangeTradeCondition.m_topRangePrice; // 当前价格超过了区间的上限价格或者低于区间的下限,则不做处理 if (latestData.m_close > topRangePrice || latestData.m_close < bottomRangePrice) { return; } // 已经建仓完成 if (isInitBuild) { bool isBasePrice = securityRangeTradeCondition.m_isBasePrice; float lastDealPrice = securityRangeTradeCondition.m_lastDealPrice; int buyCount = securityRangeTradeCondition.m_buyCount; int sellCount = securityRangeTradeCondition.m_sellCount; float lowLastDealBuy = securityRangeTradeCondition.m_lowLastDealBuy; float overLastDealSell = securityRangeTradeCondition.m_overLastDealSell; bool isCrossBuy = securityRangeTradeCondition.m_isCrossBuy; bool isCrossSell = securityRangeTradeCondition.m_isCrossSell; // 当前价格和上次委托未成交价格的比较 double diffPrice1 = latestData.m_close - m_lastCommissionNoTradePrice; if (diffPrice1 == 0) { return; } if (diffPrice1 > 0 && diffPrice1 < overLastDealSell) { return; } if (diffPrice1 < 0 && Math.Abs(diffPrice1) < lowLastDealBuy) { return; } // 计算当前价格和上次成交价格的差值 double diffPrice = latestData.m_close - lastDealPrice; if (diffPrice > 0) { SecurityPosition postion = null; if (!m_dictSecurityPositions.TryGetValue(latestData.m_code, out postion)) { // 没有持仓信息,不做处理 return; } // 高于上次成交价格 int readSellCount = 0; int sepaCount = (int)(diffPrice / overLastDealSell); if (sepaCount < 1) { // 价格没有达到预期值 return; } if (isCrossSell) { readSellCount = sepaCount * sellCount; } else { readSellCount = sellCount; } if (readSellCount < 100) { return; } // 股票余额小于可卖数量 if (postion.m_stockBalance < readSellCount) { readSellCount = (int)postion.m_stockBalance; } OrderInfo info = new OrderInfo(); info.m_code = CStrA.ConvertDBCodeToDealCode(latestData.m_code); info.m_price = (float)Math.Round(latestData.m_close, 2); info.m_qty = readSellCount; m_lastCommissionNoTradePrice = info.m_price; AutoTradeService.Sell(info); Thread.Sleep(3000); THSDealInfo req = new THSDealInfo(); req.m_operateType = 4; req.m_reqID = DataCenter.ThsDealService.GetRequestID(); DataCenter.ThsDealService.AddTHSDealReq(req); } else if (diffPrice < 0) { if (m_securityTradingAccount == null) { // 没有资金账户信息 return; } // 低于上次成交价格 int readBuyCount = 0; int sepaCount = (int)(Math.Abs(diffPrice) / overLastDealSell); if (sepaCount < 1) { // 价格没有达到预期值 return; } if (isCrossBuy) { readBuyCount = sepaCount * buyCount; } else { readBuyCount = buyCount; } if (readBuyCount < 100) { return; } int capitalAllowBuyCount = (int)((m_securityTradingAccount.m_capitalBalance - m_securityTradingAccount.m_frozenCash) / latestData.m_close) / 100 * 100; // 资金余额小于可买的数量 if (capitalAllowBuyCount < readBuyCount) { readBuyCount = capitalAllowBuyCount; } OrderInfo info = new OrderInfo(); info.m_code = CStrA.ConvertDBCodeToDealCode(latestData.m_code); info.m_price = (float)Math.Round(latestData.m_close, 2); info.m_qty = readBuyCount; m_lastCommissionNoTradePrice = info.m_price; AutoTradeService.Buy(info); Thread.Sleep(3000); THSDealInfo req = new THSDealInfo(); req.m_operateType = 3; req.m_reqID = DataCenter.ThsDealService.GetRequestID(); DataCenter.ThsDealService.AddTHSDealReq(req); } } else { } }