Example #1
0
 /// <summary>
 /// 获取自动下单机。
 /// </summary>
 /// <param name="traderIdentify">自动下单机标识。</param>
 /// <returns></returns>
 public AutoTrader GetAutoTrader(Guid traderIdentify)
 {
     lock (m_syncObj)
     {
         AutoTrader trader = null;
         m_autoTraderDic.TryGetValue(traderIdentify, out trader);
         return(trader);
     }
 }
Example #2
0
        private void TraderManager_OnAddAutoTrader(Guid traderIdentify)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new AddAutoTraderEventHandel(TraderManager_OnAddAutoTrader), traderIdentify);
                return;
            }

            AutoTrader autoTrader = USeManager.Instance.AutoTraderManager.GetAutoTrader(traderIdentify);

            AddArbitrageOrderControl(autoTrader);
        }
Example #3
0
 private void M_autoTraderManager_OnArbitrageOrderChanged(Guid tradeIdentify)
 {
     try
     {
         AutoTrader        trader         = m_autoTraderManager.GetAutoTrader(tradeIdentify);
         USeArbitrageOrder arbitrageOrder = trader.GetArbitrageOrder();
         m_dataSaver.AddSaveTask(arbitrageOrder);
     }
     catch (Exception ex)
     {
         Debug.Assert(false, ex.Message);
     }
 }
Example #4
0
        /// <summary>
        /// 新增套利单控件。
        /// </summary>
        /// <param name="autoTrader"></param>
        private void AddArbitrageOrderControl(AutoTrader autoTrader)
        {
            if (ExistArbitrageOrderControl(autoTrader.TraderIdentify) == false)
            {
                ArbitrageOrderControl orderControl = CreateArbitrageOrderControl(autoTrader);

                this.panelOrderContainer.Controls.Add(orderControl);
                m_orderControlList.Add(orderControl);
                orderControl.Initialize();
            }
            else
            {
                Debug.Assert(false);
            }
        }
Example #5
0
        /// <summary>
        /// 创建套利单控件。
        /// </summary>
        /// <param name="arbitrageOrder"></param>
        /// <returns></returns>
        private ArbitrageOrderControl CreateArbitrageOrderControl(AutoTrader autoTrader)
        {
            ArbitrageOrderControl orderItemControl = new ArbitrageOrderControl(autoTrader);

            orderItemControl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            orderItemControl.Dock        = System.Windows.Forms.DockStyle.Top;
            orderItemControl.Location    = new System.Drawing.Point(0, 0);
            orderItemControl.Name        = "arbitrageItemControl1";
            orderItemControl.Size        = new System.Drawing.Size(836, 153);
            orderItemControl.TabIndex    = 0;

            orderItemControl.Initialize();

            return(orderItemControl);
        }
Example #6
0
        /// <summary>
        /// 移除自动下单机。
        /// </summary>
        /// <param name="traderIdentify">自动下单机标识。</param>
        public void RemoveAutoTrader(Guid traderIdentify)
        {
            AutoTrader trader = null;

            lock (m_syncObj)
            {
                if (m_autoTraderDic.TryGetValue(traderIdentify, out trader) == false)
                {
                    Debug.Assert(false);
                    return;
                }
            }

            if (trader.State == AutoTraderState.Enable)
            {
                throw new Exception(string.Format("套利单[{0}]为运行状态,请先停止在移除", trader.Alias));
            }
            if (trader.IsFinish == false)
            {
                throw new Exception(string.Format("套利单[{0}]当前状态为 {1} ,不能移除", trader, trader.ArbitrageOrderState.ToDescription()));
            }
            if (trader.HasUnFinishOrderBook)
            {
                throw new Exception(string.Format("套利单[{0}]有未完成委托单 ,不能移除", trader));
            }

            trader.OnNotify -= AutoTrader_OnNotify;
            trader.OnAlarm  -= AutoTrader_OnAlarm;
            trader.OnArbitrageOrderChanged -= AutoTrader_OnArbitrageOrderChanged;

            lock (m_syncObj)
            {
                m_autoTraderDic.Remove(traderIdentify);
            }

            string           text   = "移除套利单成功";
            AutoTraderNotice notice = new AutoTraderNotice(trader.TraderIdentify, trader.Alias, AutoTraderNoticeType.Infomation, text);

            SafeFireAutoTraderNotice(notice);
            trader.WriteTraderNoticeLog(notice);

            SafeFireRemoveAutoTrader(traderIdentify);
        }
Example #7
0
 private void btnRefresh_Click(object sender, EventArgs e)
 {
     if (m_arbitrageOrder == null)
     {
         return;
     }
     try
     {
         AutoTrader        trader         = USeManager.Instance.AutoTraderManager.GetAutoTrader(m_arbitrageOrder.TraderIdentify);
         USeArbitrageOrder arbitrageOrder = trader.GetArbitrageOrder();
         Debug.Assert(arbitrageOrder != null);
         m_arbitrageOrder = arbitrageOrder;
         BindData();
     }
     catch (Exception ex)
     {
         Debug.Assert(false, ex.Message);
     }
 }
Example #8
0
        /// <summary>
        /// 创建新的套利订单。
        /// </summary>
        /// <param name="argument">套利单参数。</param>
        /// <param name="currentInvestor">当前投资者帐号。</param>
        public AutoTrader CreateNewAutoTrader(ArbitrageArgument argument, InvestorAccount currentInvestor)
        {
            Guid traderIdentify = CreateNewAutoTraderIdentify();
            int  aliasNum       = CreateNewAutoTraderAliasNum();

            USeArbitrageOrder arbitrageOrder = new USeArbitrageOrder();

            arbitrageOrder.TraderIdentify = traderIdentify;
            arbitrageOrder.AliasNum       = aliasNum;
            arbitrageOrder.State          = ArbitrageOrderState.None;
            arbitrageOrder.BrokerId       = currentInvestor.BrokerId;
            arbitrageOrder.Account        = currentInvestor.Account;
            arbitrageOrder.Argument       = argument.Clone();
            arbitrageOrder.CreateTime     = DateTime.Now;

            TaskOrderSetting taskOrderSetting = m_systemConfigManager.GetTaskOrderSetting();
            //任务组待开仓或者平仓时在创建
            AutoTrader trader = new AutoTrader();

            trader.SetRecordLogger(USeManager.Instance.CommandLogger);
            trader.SetTryOrderCondition(taskOrderSetting.TaskMaxTryCount, taskOrderSetting.TryOrderMinInterval);
            trader.Initialize(arbitrageOrder, m_orderDriver, m_quoteDriver, m_alarmManager);

            trader.OnNotify += AutoTrader_OnNotify;
            trader.OnAlarm  += AutoTrader_OnAlarm;
            trader.OnArbitrageOrderChanged += AutoTrader_OnArbitrageOrderChanged;
            trader.OnStateChanged          += AutoTrader_OnStateChanged;

            lock (m_syncObj)
            {
                m_autoTraderDic.Add(traderIdentify, trader);
            }

            SafeFireAddAutoTrader(traderIdentify);
            string           text   = "创建套利单成功";
            AutoTraderNotice notice = new AutoTraderNotice(trader.TraderIdentify, trader.Alias, AutoTraderNoticeType.Infomation, text);

            SafeFireAutoTraderNotice(notice);
            trader.WriteTraderNoticeLog(notice);

            return(trader);
        }
Example #9
0
        /// <summary>
        /// 初始化。
        /// </summary>
        /// <param name="arbitrageOrderList">套利单信息。</param>
        /// <param name="orderDriver">交易驱动。</param>
        /// <param name="quoteDriver">行情驱动。</param>
        /// <param name="alarmManager">预警管理类。</param>
        /// <param name="systemConfigManager">系统配置管理类。</param>
        public void Initialize(List <USeArbitrageOrder> arbitrageOrderList, USeOrderDriver orderDriver, USeQuoteDriver quoteDriver, AlarmManager alarmManager, SystemConfigManager systemConfigManager)
        {
            m_orderDriver         = orderDriver;
            m_quoteDriver         = quoteDriver;
            m_alarmManager        = alarmManager;
            m_systemConfigManager = systemConfigManager;

            m_systemConfigManager.OnSystemSettingChanged += M_systemConfigManager_OnSystemSettingChanged1;

            if (arbitrageOrderList == null || arbitrageOrderList.Count <= 0)
            {
                return;
            }

            lock (m_syncObj)
            {
                int maxAliasNum = 0;  // 当日别名最大号码

                TaskOrderSetting taskOrderSetting = m_systemConfigManager.GetTaskOrderSetting();
                foreach (USeArbitrageOrder arbitrageOrder in arbitrageOrderList)
                {
                    AutoTrader autoTrader = new AutoTrader();
                    autoTrader.SetRecordLogger(USeManager.Instance.CommandLogger);
                    autoTrader.SetTryOrderCondition(taskOrderSetting.TaskMaxTryCount, taskOrderSetting.TryOrderMinInterval);
                    autoTrader.Initialize(arbitrageOrder, m_orderDriver, m_quoteDriver, m_alarmManager);
                    autoTrader.OnNotify                += AutoTrader_OnNotify;
                    autoTrader.OnAlarm                 += AutoTrader_OnAlarm;
                    autoTrader.OnStateChanged          += AutoTrader_OnStateChanged;
                    autoTrader.OnArbitrageOrderChanged += AutoTrader_OnArbitrageOrderChanged;
                    m_autoTraderDic.Add(autoTrader.TraderIdentify, autoTrader);

                    if (arbitrageOrder.CreateTime.Date == DateTime.Today && arbitrageOrder.AliasNum > maxAliasNum)
                    {
                        maxAliasNum = arbitrageOrder.AliasNum;
                    }
                }

                m_aliasIdCrateor.Set(maxAliasNum);
            }
        }
Example #10
0
 public ArbitrageOpenArgumentSetForm(AutoTrader autoTrader)
 {
     m_autoTrader = autoTrader;
     InitializeComponent();
 }
        /// <summary>
        /// 创建组合套利单下单参数
        /// </summary>
        private bool CreateNewArbitrageOrder()
        {
            USeInstrument          nearInstrument = this.cbxNearInstrument.SelectedItem as USeInstrument;
            USeInstrument          farInstrument  = this.cbxFarInstrument.SelectedItem as USeInstrument;
            ArbitrageOperationSide operationSide  = this.arbitrageOperationSideControl.OperationSide;

            ArbitrageOpenArgument openArg = new ArbitrageOpenArgument();

            if (operationSide == ArbitrageOperationSide.BuyNearSellFar)
            {
                openArg.BuyInstrument  = nearInstrument;
                openArg.SellInstrument = farInstrument;
                openArg.BuyInstrumentOrderPriceType  = this.orderPriceTypeControl_OpenNearArg.OrderPriceType;
                openArg.SellInstrumentOrderPriceType = this.orderPriceTypeControl_OpenFarArg.OrderPriceType;
            }
            else if (operationSide == ArbitrageOperationSide.SellNearBuyFar)
            {
                openArg.BuyInstrument  = farInstrument;
                openArg.SellInstrument = nearInstrument;
                openArg.BuyInstrumentOrderPriceType  = this.orderPriceTypeControl_OpenFarArg.OrderPriceType;
                openArg.SellInstrumentOrderPriceType = this.orderPriceTypeControl_OpenNearArg.OrderPriceType;
            }
            else
            {
                Debug.Assert(false);
            }
            openArg.NearOrderPriceType = this.orderPriceTypeControl_OpenNearArg.OrderPriceType;
            openArg.FarOrderPriceType  = this.orderPriceTypeControl_OpenFarArg.OrderPriceType;

            openArg.PreferentialSide = this.preferentialSideControl_OpenArg.PreferentialSide;
            openArg.OpenCondition    = new PriceSpreadCondition()
            {
                PriceSpreadSide      = this.priceSpreadSideControl_OpenSpreadArg.PriceSpreadSide,
                PriceSpreadThreshold = this.nudPriceSpreadThreshold_OpenArg.Value
            };
            openArg.TotalOrderQty    = (int)this.nudTotalOrderQty_OpenArg.Value;
            openArg.OrderQtyUint     = (int)this.nudOrderQtyUint_OpenArg.Value;
            openArg.DifferentialUnit = (int)this.nudDifferentialUnit_OpenArg.Value;


            ArbitrageCloseArgument closeArg = new ArbitrageCloseArgument();

            if (operationSide == ArbitrageOperationSide.BuyNearSellFar)
            {
                closeArg.BuyInstrument  = farInstrument;
                closeArg.SellInstrument = nearInstrument;
                closeArg.BuyInstrumentOrderPriceType  = this.orderPriceTypeControl_CloseFarArg.OrderPriceType;
                closeArg.SellInstrumentOrderPriceType = this.orderPriceTypeControl_CloseNearArg.OrderPriceType;
            }
            else if (operationSide == ArbitrageOperationSide.SellNearBuyFar)
            {
                closeArg.BuyInstrument  = nearInstrument;
                closeArg.SellInstrument = farInstrument;
                closeArg.BuyInstrumentOrderPriceType  = this.orderPriceTypeControl_CloseNearArg.OrderPriceType;
                closeArg.SellInstrumentOrderPriceType = this.orderPriceTypeControl_CloseFarArg.OrderPriceType;
            }
            closeArg.NearOrderPriceType = this.orderPriceTypeControl_CloseNearArg.OrderPriceType;
            closeArg.FarOrderPriceType  = this.orderPriceTypeControl_CloseFarArg.OrderPriceType;

            closeArg.PreferentialSide = this.preferentialSideControl_CloseArg.PreferentialSide;
            closeArg.CloseCondition   = new PriceSpreadCondition()
            {
                PriceSpreadSide      = this.priceSpreadSideControl_CloseSpreadArg.PriceSpreadSide,
                PriceSpreadThreshold = this.nudPriceSpreadThreshold_CloseArg.Value
            };
            closeArg.OrderQtyUint     = (int)this.nudOrderQtyUint_CloseArg.Value;
            closeArg.DifferentialUnit = (int)this.nudDifferentialUnit_CloseArg.Value;

            ArbitrageStopLossArgument stopLossArg = null;

            if (this.cbxStopLossFlag.Checked)
            {
                stopLossArg = new ArbitrageStopLossArgument();
                stopLossArg.StopLossCondition = new PriceSpreadCondition()
                {
                    PriceSpreadSide      = this.priceSpreadSideControl_StopLossArg.PriceSpreadSide,
                    PriceSpreadThreshold = this.nudPriceSpreadThreshold_StopLossArg.Value
                };
            }


            List <ArbitrageAlarmArgument> alarmArgList = new List <ArbitrageAlarmArgument>();

            if (m_dataSourceAlarm != null && m_dataSourceAlarm.Count > 0)
            {
                foreach (ArbitrageAlarmArgumentViewModel alarmView in m_dataSourceAlarm)
                {
                    alarmArgList.Add(ArbitrageAlarmArgumentViewModel.CreatAlarmData(alarmView));
                }
            }

            ArbitrageArgument argument = new ArbitrageArgument();

            argument.ProductID      = m_product.ProductCode;
            argument.NearInstrument = nearInstrument;
            argument.FarInstrument  = farInstrument;
            argument.OperationSide  = operationSide;

            argument.OpenArg     = openArg;
            argument.CloseArg    = closeArg;
            argument.StopLossArg = stopLossArg;
            argument.AlarmArgs   = alarmArgList;

            string errorMessage = string.Empty;

            if (VerifyMargin(argument.OpenArg, out errorMessage) == false)
            {
                USeFuturesSpiritUtility.ShowWarningMessageBox(this, errorMessage);
                return(false);
            }

            decimal evaluateMargin = EvaluateMargin(argument.OpenArg);
            string  text           = string.Format("套利单预计占用保证金 {0},确定跟单么?", evaluateMargin.ToString("#,0"));

            if (DialogResult.Yes != USeFuturesSpiritUtility.ShowYesNoMessageBox(this, text))
            {
                return(false);
            }

            try
            {
                AutoTraderManager traderManager = USeManager.Instance.AutoTraderManager;
                Debug.Assert(traderManager != null);

                AutoTrader trader = traderManager.CreateNewAutoTrader(argument, USeManager.Instance.LoginUser);
                trader.BeginOpen();
                //[yangming]创建后应该启动跟单
                trader.StartOpenOrCloseMonitor();

                USeManager.Instance.DataSaver.AddSaveTask(trader.GetArbitrageOrder());

                //同时保存所有的ArbitrageArgument便于下次修改
            }
            catch (Exception ex)
            {
                USeFuturesSpiritUtility.ShowWarningMessageBox(this, ex.Message);
                return(false);
            }

            return(true);
        }
 public ArbitrageOrderControl(AutoTrader autoTrader)
 {
     m_porfitCalcTimer = new System.Threading.Timer(CalculateProfitCallback, false, Timeout.Infinite, Timeout.Infinite);
     m_autoTrader      = autoTrader;
     InitializeComponent();
 }