コード例 #1
0
ファイル: FutureTrade.cs プロジェクト: fffweb/MoneyFramework
        /// <summary>
        /// 发出报单出现问题回调函数
        /// </summary>
        /// <param name="pInputOrder"></param>
        /// <param name="pRspInfo"></param>
        public static void _client_ErrRtnOrderInsert(CThostFtdcInputOrderField_M pInputOrder, CThostFtdcRspInfoField_M pRspInfo)
        {
            TradeRecord.GetInstance().MarkFailure(Convert.ToInt16(pInputOrder.OrderRef), pRspInfo.ErrorMsg);

            if (pInputOrder.CombOffsetFlag_0 == (byte)(FutureTradeOffSet.Open))
            {
                //只有开仓涉及冻结资金
                //交易失败,需要释放冻结掉的期货交易资金
                accountMonitor.UpdateRiskFrozonAccount(pInputOrder.UserID, pInputOrder.InstrumentID, pInputOrder.VolumeTotalOriginal * (-1), pInputOrder.VolumeTotalOriginal * pInputOrder.LimitPrice * (-1), "F", pInputOrder.Direction.ToString());
            }
        }
コード例 #2
0
ファイル: FutureTrade.cs プロジェクト: botvs/MoneyFramework
 /// <summary>
 /// 发出报单出现问题回调函数
 /// </summary>
 /// <param name="pInputOrder"></param>
 /// <param name="pRspInfo"></param>
 static void _client_ErrRtnOrderInsert(CThostFtdcInputOrderField_M pInputOrder, CThostFtdcRspInfoField_M pRspInfo)
 {
     //throw new NotImplementedException();
     TradeRecord.GetInstance().MarkFailure(Convert.ToInt16(pInputOrder.OrderRef), pRspInfo.ErrorMsg);
 }
コード例 #3
0
ファイル: FutureTrade.cs プロジェクト: fffweb/MoneyFramework
        /// <summary>
        /// 期货交易工作线程
        /// </summary>
        /// <param name="para"></param>
        public void FutureTradeSubThreadProc(object para)
        {
            string ErrorMsg = string.Empty;

            //令该线程为前台线程
            Thread.CurrentThread.IsBackground = true;

            DateTime lastmessagetime = DateTime.Now;

            TradeParaPackage _tpp = (TradeParaPackage)para;

            //当前线程编号
            int _threadNo = _tpp._threadNo;

            sublog.LogEvent("线程 :" + _threadNo.ToString() + " 开始执行");

            //用作发送心跳包的时间标记
            DateTime _markedTime = DateTime.Now;

            //控制期货交易线程执行
            bool _threadRunControl = true;

            //获取未成交期货委托
            List <ER_TAOLI_TABLE> ERs = DBAccessLayer.GetInCompletedERRecord("F");

            if (ERs != null)
            {
                foreach (ER_TAOLI_TABLE ER in ERs)
                {
                    decimal price = (ER.ER_VOLUME_TOTAL_ORIGINAL == 0 ? 0 : Convert.ToDecimal(ER.ER_FROZEN_MONEY / ER.ER_VOLUME_TOTAL_ORIGINAL));
                    string  code  = ER.ER_CODE;

                    TradeRecord.GetInstance().SubscribeIncompleteOrder("F", ER.ER_CODE, ER.ER_ID, ER.ER_DIRECTION.ToString(), Convert.ToInt16(ER.ER_VOLUME_TOTAL_ORIGINAL), price, Convert.ToInt16(ER.ER_ORDER_REF), Convert.ToInt16("0"), Convert.ToInt16(ER.ER_OFFSETFLAG.Trim()), ER.ER_USER);
                }
            }

            while (_threadRunControl)
            {
                //初始化完成前,不接收实际交易
                queue_future_excuteThread.SetThreadBusy(_threadNo);

                _client.Connect();

                //状态 DISCONNECTED -> CONNECTED
                while (status != FutureTradeThreadStatus.CONNECTED)
                {
                    Thread.Sleep(10);
                }

                _client.ReqUserLogin();

                //状态 CONNECTED -> LOGIN
                while (status != FutureTradeThreadStatus.LOGIN)
                {
                    Thread.Sleep(10);
                }


                if (ERs != null)
                {
                    foreach (ER_TAOLI_TABLE ER in ERs)
                    {
                        _client.QryOrder(ER.ER_ORDER_EXCHANGE_ID, "", "", "", ER.ER_ID.PadLeft(12));
                    }
                }

                while (true)
                {
                    Thread.Sleep(10);

                    if ((DateTime.Now - GlobalHeartBeat.GetGlobalTime()).TotalMinutes > 10)
                    {
                        sublog.LogEvent("线程 :" + _threadNo.ToString() + "心跳停止 , 最后心跳 : " + GlobalHeartBeat.GetGlobalTime().ToString());
                        _threadRunControl = false;
                        break;
                    }

                    if (lastmessagetime.Second != DateTime.Now.Second)
                    {
                        KeyValuePair <string, object> message1 = new KeyValuePair <string, object>("THREAD_FUTURE_TRADE_WORKER", (object)_threadNo);
                        queue_system_status.GetQueue().Enqueue((object)message1);
                    }

                    if (queue_future_excuteThread.GetQueue(_threadNo).Count < 2)
                    {
                        queue_future_excuteThread.SetThreadFree(_threadNo);
                        status = FutureTradeThreadStatus.FREE;
                    }
                    else
                    {
                        status = FutureTradeThreadStatus.BUSY;
                    }

                    if (queue_future_excuteThread.GetQueue(_threadNo).Count > 0)
                    {
                        List <TradeOrderStruct> trades = (List <TradeOrderStruct>)queue_future_excuteThread.FutureExcuteQueue[_threadNo].Dequeue();
                        if (trades == null)
                        {
                            continue;
                        }
                        if (trades.Count > 0)
                        {
                            sublog.LogEvent("线程 :" + _threadNo.ToString() + " 执行交易数量 : " + trades.Count);
                        }

                        if (trades.Count == 0)
                        {
                            continue;
                        }

                        foreach (TradeOrderStruct order in trades)
                        {
                            CTP_CLI.CThostFtdcInputOrderField_M args = new CThostFtdcInputOrderField_M();

                            //填写委托参数

                            args.BrokerID            = CommConfig.BROKER;
                            args.InvestorID          = CommConfig.INVESTOR;
                            args.InstrumentID        = order.cSecurityCode;
                            args.Direction           = Convert.ToByte(order.cTradeDirection);
                            args.CombOffsetFlag_0    = Convert.ToByte(order.cOffsetFlag);
                            args.VolumeTotalOriginal = Convert.ToInt16(order.nSecurityAmount);
                            args.LimitPrice          = Convert.ToDouble(order.dOrderPrice);
                            args.OrderRef            = order.OrderRef.ToString();
                            args.OrderPriceType      = Convert.ToByte("50");
                            args.CombHedgeFlag_0     = Convert.ToByte('1');
                            args.MinVolume           = 1;
                            args.ContingentCondition = Convert.ToByte('1');
                            args.TimeCondition       = Convert.ToByte('3');
                            args.VolumeCondition     = Convert.ToByte('1');
                            args.UserID           = order.cUser;
                            args.ForceCloseReason = Convert.ToByte('0');
                            args.IsAutoSuspend    = 0;
                            args.UserForceClose   = 0;

                            //提交报单委托
                            //步骤完成后线程任务结束
                            //返回工作交由回调函数处理
                            _client.OrderInsert(args);

                            //创建记录
                            RecordItem item = new RecordItem();
                            item.AveragePrice        = 0;
                            item.Code                = order.cSecurityCode;
                            item.CombOffsetFlag      = Convert.ToInt16(order.cOffsetFlag);
                            item.OrderRef            = order.OrderRef;
                            item.OrderStatus         = 0;
                            item.OrderSysID          = "0";
                            item.Orientation         = order.cTradeDirection;
                            item.Price               = Convert.ToDecimal(order.dOrderPrice);
                            item.Status              = TradeDealStatus.ORDERING;
                            item.StrategyId          = order.belongStrategy;
                            item.Type                = "1";
                            item.VolumeTotalOriginal = item.VolumeTotal = Convert.ToInt32(order.nSecurityAmount);
                            item.VolumeTraded        = 0;
                            item.User                = order.cUser;


                            TradeRecord.GetInstance().CreateOrder(order.OrderRef, item);
                        }
                    }
                }
            }
        }
コード例 #4
0
ファイル: FutureTrade.cs プロジェクト: botvs/MoneyFramework
        /// <summary>
        /// 期货交易工作线程
        /// </summary>
        /// <param name="para"></param>
        public void FutureTradeSubThreadProc(object para)
        {
            string ErrorMsg = string.Empty;

            //令该线程为前台线程
            Thread.CurrentThread.IsBackground = true;

            DateTime lastmessagetime = DateTime.Now;

            TradeParaPackage _tpp = (TradeParaPackage)para;

            //当前线程编号
            int _threadNo = _tpp._threadNo;

            if(_threadNo == 0)
            {
                //默认0号期货交易线程即测试线程
                BROKER = TEST_BROKER;
                INVESTOR = TEST_INVESTOR;
                ADDRESS = TEST_ADDRESS;
                PASSWORD = TEST_PASSWORD;
            }

            sublog.LogEvent("线程 :" + _threadNo.ToString() + " 开始执行");

            //用作发送心跳包的时间标记
            DateTime _markedTime = DateTime.Now;

            //控制期货交易线程执行
            bool _threadRunControl = true;

            while (_threadRunControl)
            {
                //初始化完成前,不接收实际交易
                queue_future_excuteThread.SetThreadBusy(_threadNo);

                _client.Connect();

                //状态 DISCONNECTED -> CONNECTED
                while (status != FutureTradeThreadStatus.CONNECTED)
                {
                    Thread.Sleep(10);
                }

                _client.ReqUserLogin();

                //状态 CONNECTED -> LOGIN
                while (status != FutureTradeThreadStatus.LOGIN)
                {
                    Thread.Sleep(10);
                }

                while (true)
                {

                    Thread.Sleep(10);

                    if ((DateTime.Now - GlobalHeartBeat.GetGlobalTime()).TotalMinutes > 10)
                    {
                        sublog.LogEvent("线程 :" + _threadNo.ToString() + "心跳停止 , 最后心跳 : " + GlobalHeartBeat.GetGlobalTime().ToString());
                        _threadRunControl = false;
                        break;
                    }

                    if (lastmessagetime.Second != DateTime.Now.Second)
                    {
                        KeyValuePair<string, object> message1 = new KeyValuePair<string, object>("THREAD_FUTURE_TRADE_WORKER", (object)_threadNo);
                        queue_system_status.GetQueue().Enqueue((object)message1);
                    }

                    if (queue_future_excuteThread.GetQueue(_threadNo).Count < 2)
                    {
                        queue_future_excuteThread.SetThreadFree(_threadNo);
                        status = FutureTradeThreadStatus.FREE;
                    }
                    else
                    {
                        status = FutureTradeThreadStatus.BUSY;
                    }

                    if (queue_future_excuteThread.GetQueue(_threadNo).Count > 0)
                    {
                        List<TradeOrderStruct> trades = (List<TradeOrderStruct>)queue_future_excuteThread.FutureExcuteQueue[_threadNo].Dequeue();
                        if (trades == null) continue;
                        if (trades.Count > 0) { sublog.LogEvent("线程 :" + _threadNo.ToString() + " 执行交易数量 : " + trades.Count); }

                        if (trades.Count == 0) { continue; }

                        foreach (TradeOrderStruct order in trades)
                        {


                            CTP_CLI.CThostFtdcInputOrderField_M args = new CThostFtdcInputOrderField_M();

                            //填写委托参数

                            args.BrokerID = BROKER;
                            args.InvestorID = INVESTOR;
                            args.InstrumentID = order.cSecurityCode;
                            args.Direction = Convert.ToByte(order.cTradeDirection);
                            args.CombOffsetFlag_0 =  Convert.ToByte(order.cOffsetFlag);
                            args.VolumeTotalOriginal = Convert.ToInt16(order.nSecurityAmount);
                            args.LimitPrice = Convert.ToDouble(order.dOrderPrice);
                            args.OrderRef = order.OrderRef.ToString();
                            args.OrderPriceType =Convert.ToByte(order.cOrderPriceType);
                            args.CombHedgeFlag_0 = Convert.ToByte('1');
                            args.MinVolume = 1;
                            args.ContingentCondition = Convert.ToByte('1');      
                            args.TimeCondition = Convert.ToByte('3');
                            args.VolumeCondition = Convert.ToByte('1');

                            args.ForceCloseReason = Convert.ToByte('0');
                            args.IsAutoSuspend = 0;
                            args.UserForceClose = 0;

                            //提交报单委托
                            //步骤完成后线程任务结束
                            //返回工作交由回调函数处理
                            _client.OrderInsert(args);

                            //创建记录
                            RecordItem item = new RecordItem();
                            item.AveragePrice = 0;
                            item.Code = order.cSecurityCode;
                            item.CombOffsetFlag = Convert.ToInt16(order.cOffsetFlag);
                            item.OrderRef = order.OrderRef;
                            item.OrderStatus = 0;
                            item.OrderSysID = "0";
                            item.Orientation = order.cTradeDirection;
                            item.Price = Convert.ToDecimal(order.dOrderPrice);
                            item.Status = TradeDealStatus.ORDERING;
                            item.StrategyId = order.belongStrategy;
                            item.Type = "1";
                            item.VolumeTotalOriginal = item.VolumeTotal = Convert.ToInt32(order.nSecurityAmount);
                            item.VolumeTraded = 0;
                            
                            TradeRecord.GetInstance().CreateOrder(order.OrderRef, item);
                        }

                    }
                }
            }
        }