public OnRspQryInvestorPositionArgs(ref CThostFtdcInvestorPositionField pInvestorPosition, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { this.pInvestorPosition = pInvestorPosition; this.pRspInfo = pRspInfo; this.nRequestID = nRequestID; this.bIsLast = bIsLast; }
private void OnRspQryInvestorPosition_callback(object sender, ref CThostFtdcInvestorPositionField pInvestorPosition, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { if (null != OnRspQryInvestorPosition) { OnRspQryInvestorPosition(this, new OnRspQryInvestorPositionArgs(ref pInvestorPosition, ref pRspInfo, nRequestID, bIsLast)); } }
public void FireOnRspReqQryInvestorPosition(CThostFtdcInvestorPositionField pInvestorPosition) { if (null != OnRspReqQryInvestorPosition) { OnRspReqQryInvestorPosition(pInvestorPosition); } }
private void OnRspQryInvestorPosition(IntPtr pTraderApi, ref CThostFtdcInvestorPositionField pInvestorPosition, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { if (0 == pRspInfo.ErrorID) { string key = GetPositionKey(pInvestorPosition); _dictPositions[key] = pInvestorPosition; CTPAPI.GetInstance().FireOnRspReqQryInvestorPosition(pInvestorPosition); timerPonstion.Enabled = false; timerPonstion.Enabled = true; } else { tdlog.Error("nRequestID:{0},ErrorID:{1},OnRspQryInvestorPosition:{2}", nRequestID, pRspInfo.ErrorID, pRspInfo.ErrorMsg); EmitError(nRequestID, pRspInfo.ErrorID, "OnRspQryInvestorPosition:" + pRspInfo.ErrorMsg); } }
private void OnRspQryInvestorPosition(IntPtr pTraderApi, ref CThostFtdcInvestorPositionField pInvestorPosition, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { if (0 == pRspInfo.ErrorID) { _dbInMemInvestorPosition.InsertOrReplace( pInvestorPosition.InstrumentID, pInvestorPosition.PosiDirection, pInvestorPosition.HedgeFlag, pInvestorPosition.PositionDate, pInvestorPosition.Position); timerPonstion.Enabled = false; timerPonstion.Enabled = true; } else { EmitError(nRequestID, pRspInfo.ErrorID, "OnRspQryInvestorPosition:" + pRspInfo.ErrorMsg); } }
private void CTPOnRspQryInvestorPosition(ref CThostFtdcInvestorPositionField pInvestorPosition, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { if (!string.IsNullOrEmpty(pInvestorPosition.InstrumentID)) { var f = pInvestorPosition; _listPosi.Add(f); } if (bIsLast) { foreach (var g in _listPosi.GroupBy(p => p.InstrumentID + "_" + p.PosiDirection)) { var id = g.First(); //整理持仓数据 HedgeType hedge = HedgeType.Speculation; switch (id.HedgeFlag) { case TThostFtdcHedgeFlagType.THOST_FTDC_HF_Speculation: hedge = HedgeType.Speculation; break; case TThostFtdcHedgeFlagType.THOST_FTDC_HF_Arbitrage: hedge = HedgeType.Arbitrage; break; case TThostFtdcHedgeFlagType.THOST_FTDC_HF_Hedge: hedge = HedgeType.Hedge; break; } DirectionType dire = DirectionType.Buy; if (g.First().PosiDirection == TThostFtdcPosiDirectionType.THOST_FTDC_PD_Short) { dire = DirectionType.Sell; } var key = id.InstrumentID + "_" + dire; var pf = DicPositionField.GetOrAdd(key, new PositionField { InstrumentID = id.InstrumentID, Direction = dire, Hedge = hedge, }); //if (pInvestorPosition.PositionDate == TThostFtdcPositionDateType.THOST_FTDC_PSD_Today) //{ // pf.TdPosition = pInvestorPosition.Position; // pf.TdCost = pf.TdPosition == 0 ? 0 : (pInvestorPosition.PositionCost /*pInvestorPosition.TodayPosition * pf.TdPosition*/); //} //if (pInvestorPosition.PositionDate == TThostFtdcPositionDateType.THOST_FTDC_PSD_History) //{ // pf.YdPosition = pInvestorPosition.Position; // pf.YdCost = pInvestorPosition.PreSettlementPrice * pf.YdPosition * DicInstrumentField[pf.InstrumentID].VolumeMultiple; // //pf.YdCost = pInvestorPosition.PositionCost / pInvestorPosition.YdPosition * pf.YdPosition; //} pf.Position = g.Sum(n => n.Position); // pf.TdPosition + pf.YdPosition; pf.TdPosition = g.Sum(n => n.TodayPosition); pf.YdPosition = pf.Position - pf.TdPosition; // g.Sum(n => n.YdPosition); pf.Price = pf.Position <= 0 ? 0 : (g.Sum(n => n.PositionCost) / DicInstrumentField[pf.InstrumentID].VolumeMultiple / pf.Position); pf.CloseProfit = g.Sum(n => n.CloseProfit); pf.PositionProfit = g.Sum(n => n.PositionProfit); pf.Commission = g.Sum(n => n.Commission); pf.Margin = g.Sum(n => n.UseMargin); } TradingAccount.CloseProfit = _listPosi.Sum(n => n.CloseProfit); TradingAccount.PositionProfit = _listPosi.Sum(n => n.PositionProfit); TradingAccount.Commission = _listPosi.Sum(n => n.Commission); TradingAccount.Fund = TradingAccount.PreBalance + TradingAccount.CloseProfit + TradingAccount.PositionProfit - TradingAccount.Commission; TradingAccount.FrozenCash = _listPosi.Sum(n => n.FrozenCash); //由查帐户资金函数处理,原因:保证金有单边收的情况无法用持仓统计 //TradingAccount.CurrMargin = _listPosi.Sum(n => n.UseMargin); //TradingAccount.Available = TradingAccount.Fund - TradingAccount.CurrMargin - TradingAccount.FrozenCash; //TradingAccount.Risk = TradingAccount.CurrMargin / TradingAccount.Fund; _listPosi.Clear();//清除,以便得到结果是重新添加 } }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(CThostFtdcInvestorPositionField obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
private void OnRspQryInvestorPosition_3(ref CThostFtdcInvestorPositionField pInvestorPosition, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { OnRspQryInvestorPosition_1(this, ref pInvestorPosition, ref pRspInfo, nRequestID, bIsLast); }
private void OnRspQryInvestorPosition_3(IntPtr pTraderApi, ref CThostFtdcInvestorPositionField pInvestorPosition, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { OnRspQryInvestorPosition_1(this, pTraderApi, ref pInvestorPosition, ref pRspInfo, nRequestID, bIsLast); }
private void OnRspQryInvestorPosition_callback(IntPtr pTraderApi, ref CThostFtdcInvestorPositionField pInvestorPosition, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { if (null != OnRspQryInvestorPosition) { OnRspQryInvestorPosition(this, new OnRspQryInvestorPositionArgs(pTraderApi, ref pInvestorPosition, ref pRspInfo, nRequestID, bIsLast)); } }
public override void OnRspQryInvestorPosition(CThostFtdcInvestorPositionField pInvestorPosition, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { _callbackApi.OnRspQryInvestorPosition(pInvestorPosition, pRspInfo, nRequestID, bIsLast); }
public OnRspQryInvestorPositionArgs(IntPtr pTraderApi, ref CThostFtdcInvestorPositionField pInvestorPosition, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { this.pTraderApi = pTraderApi; this.pInvestorPosition = pInvestorPosition; this.pRspInfo = pRspInfo; this.nRequestID = nRequestID; this.bIsLast = bIsLast; }
///请求查询投资者持仓响应 public void OnRspQryInvestorPosition(CThostFtdcInvestorPositionField pInvestorPosition, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { Console.WriteLine("OnRspQryInvestorPosition"); }
private string GetPositionKey(CThostFtdcInvestorPositionField p) { return(GetPositionKey(p.InstrumentID, p.PosiDirection, p.HedgeFlag, p.PositionDate)); }
void OnRspReqQryInvestorPosition(CThostFtdcInvestorPositionField pInvestorPosition) { Console.WriteLine("==持仓"); Console.WriteLine(pInvestorPosition.InstrumentID); }
/// <summary> /// 查询持仓回报 /// </summary> private void CTradeApi_OnRspQryInvestorPosition(ref CThostFtdcInvestorPositionField pInvestorPosition, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { if (!string.IsNullOrEmpty(pInvestorPosition.InstrumentID)) { var f = pInvestorPosition; if (f.Position > 0) //Position字段即总仓位 { this._ListPositionField.Add(f); } } if (bIsLast) { this._isFirstTimeLogin = false; foreach (var g in _ListPositionField.GroupBy(p => p.InstrumentID + "_" + p.PosiDirection + "_" + p.HedgeFlag)) { var id = g.First(); var instrumentID = id.InstrumentID; var direction = ConvertFunctions.TThostFtdcPosiDirectionType_To_Direction(id.PosiDirection); var hedge = ConvertFunctions.TThostFtdcHedgeFlagType_To_HedgeFlag(id.HedgeFlag); var key = new Tuple <string, Direction, HedgeFlag>(instrumentID, direction, hedge); var pos = this._DictInvestorPosition.GetOrAdd(key, new Position(instrumentID, direction, hedge)); #region 持仓赋值 pos.TdPosition = g.Sum(n => n.TodayPosition); pos.YdPosition = g.Sum(n => n.YdPosition); if (pos.TotalPosition == 0) { this._DictInvestorPosition.TryRemove(key, out pos); continue; } pos.BrokerID = id.BrokerID; pos.InvestorID = id.InvestorID; pos.PositionDateType = g.Max(n => ConvertFunctions.TThostFtdcPositionDateType_To_PositionDateType(n.PositionDate));//枚举类型值:今 = 1;昨 = 2; pos.LongFrozen = g.Sum(n => n.LongFrozen); pos.ShortFrozen = g.Sum(n => n.ShortFrozen); pos.LongFrozenAmount = g.Sum(n => n.LongFrozenAmount); pos.ShortFrozenAmount = g.Sum(n => n.ShortFrozenAmount); pos.OpenVolume = g.Sum(n => n.OpenVolume); pos.CloseVolume = g.Sum(n => n.CloseVolume); pos.OpenAmount = g.Sum(n => n.OpenAmount); pos.CloseAmount = g.Sum(n => n.CloseAmount); pos.PositionCost = g.Sum(n => n.PositionCost); pos.PreMargin = g.Sum(n => n.PreMargin); pos.UseMargin = g.Sum(n => n.UseMargin); pos.FrozenMargin = g.Sum(n => n.FrozenMargin); pos.FrozenCash = g.Sum(n => n.FrozenCash); pos.FrozenCommission = g.Sum(n => n.FrozenCommission); pos.CashIn = g.Sum(n => n.CashIn); pos.Commission = g.Sum(n => n.Commission); pos.CloseProfit = g.Sum(n => n.CloseProfit); pos.PositionProfit = g.Sum(n => n.PositionProfit); pos.PreSettlementPrice = g.Sum(n => n.PreSettlementPrice); pos.SettlementPrice = g.Sum(n => n.SettlementPrice); pos.TradingDay = g.Max(n => n.TradingDay); pos.OpenCost = g.Sum(n => n.OpenCost); pos.ExchangeMargin = g.Sum(n => n.ExchangeMargin); pos.CombPosition = g.Sum(n => n.CombPosition); pos.CombLongFrozen = g.Sum(n => n.CombLongFrozen); pos.CombShortFrozen = g.Sum(n => n.CombShortFrozen); pos.CloseProfitByDate = g.Sum(n => n.CloseProfitByDate); pos.CloseProfitByTrade = g.Sum(n => n.CloseProfitByTrade); pos.MarginRateByMoney = id.MarginRateByMoney; pos.MarginRateByVolume = id.MarginRateByVolume; pos.StrikeFrozen = g.Sum(n => n.StrikeFrozen); pos.StrikeFrozenAmount = g.Sum(n => n.StrikeFrozenAmount); pos.AbandonFrozen = g.Sum(n => n.AbandonFrozen); InstrumentField instrument; this._DictInstrumentField.TryGetValue(pos.InstrumentID, out instrument); if (instrument != null) {//OpenProfit是自设字段,表示开仓后到现在的总盈亏 pos.AvgOpenPrice = pos.OpenCost / pos.TotalPosition / instrument.VolumeMultiple; if (pos.PosiDirection == Direction.Buy) { pos.OpenProfit = (pos.LastPrice - pos.AvgOpenPrice) * pos.TotalPosition * instrument.VolumeMultiple; } else if (pos.PosiDirection == Direction.Sell) { pos.OpenProfit = -(pos.LastPrice - pos.AvgOpenPrice) * pos.TotalPosition * instrument.VolumeMultiple; } } #endregion pos.Notify(""); //外部函数引发调用 this._OnPosition?.Invoke(pos); } this._OnPositionChanged?.Invoke(this._DictInvestorPosition); #region TradingAccount //TradingAccount.CloseProfit = _listPosi.Sum(n => n.CloseProfit); //TradingAccount.PositionProfit = _listPosi.Sum(n => n.PositionProfit); //TradingAccount.Commission = _listPosi.Sum(n => n.Commission); //TradingAccount.Fund = TradingAccount.PreBalance + TradingAccount.CloseProfit + TradingAccount.PositionProfit - TradingAccount.Commission; //TradingAccount.FrozenCash = _listPosi.Sum(n => n.FrozenCash); ////由查帐户资金函数处理,原因:保证金有单边收的情况无法用持仓统计 ////TradingAccount.CurrMargin = _listPosi.Sum(n => n.UseMargin); ////TradingAccount.Available = TradingAccount.Fund - TradingAccount.CurrMargin - TradingAccount.FrozenCash; ////TradingAccount.Risk = TradingAccount.CurrMargin / TradingAccount.Fund; #endregion _ListPositionField.Clear();//清除,以便得到结果是重新添加 } _isQryPositionCompleted = bIsLast; }