private List <TradeCommandSecurity> GetSelectCommandSecurities(OpenPositionItem openItem, int commandId, List <OpenPositionSecurityItem> selectedSecuItems)
        {
            List <TradeCommandSecurity> cmdSecuItems = new List <TradeCommandSecurity>();

            foreach (var item in selectedSecuItems)
            {
                if (item.Selection && item.MonitorId == openItem.MonitorId)
                {
                    TradeCommandSecurity secuItem = new TradeCommandSecurity
                    {
                        CommandId     = commandId,
                        SecuCode      = item.SecuCode,
                        SecuType      = item.SecuType,
                        CommandAmount = item.EntrustAmount,
                        CommandPrice  = item.CommandPrice,
                        CurrentPrice  = item.LastPrice,
                        EntrustStatus = EntrustStatus.NoExecuted
                    };

                    if (secuItem.SecuType == SecurityType.Stock)
                    {
                        secuItem.EDirection = EntrustDirection.BuySpot;
                    }
                    else
                    {
                        secuItem.EDirection = EntrustDirection.SellOpen;
                    }

                    cmdSecuItems.Add(secuItem);
                }
            }

            return(cmdSecuItems);
        }
        public OrderConfirmItem GetSubmitItem(OpenPositionItem openItem)
        {
            string instanceCode = string.Format("{0}-{1}-{2}", openItem.PortfolioId, openItem.TemplateId, DateFormat.Format(DateTime.Now, ConstVariable.DateFormat1));

            openItem.InstanceCode = instanceCode;

            OrderConfirmItem orderItem = null;
            //Open the dialog
            OpenPositionDialog dialog = new OpenPositionDialog();

            dialog.Owner         = this;
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.OnLoadControl(dialog, null);
            dialog.OnLoadData(dialog, openItem);
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                orderItem = (OrderConfirmItem)dialog.GetData();
                dialog.Dispose();
            }
            else
            {
                dialog.Dispose();
            }

            return(orderItem);
        }
        private void CalcEntrustAmount(OpenPositionItem monitorItem, int opCopies)
        {
            //获得模板中设置的基准份数
            var templateItem = _templateBLL.GetTemplate(monitorItem.TemplateId);
            int bmkCopies    = 0;

            if (templateItem != null)
            {
                bmkCopies = templateItem.FutureCopies;
            }
            else
            {
                bmkCopies = 1;
            }

            //真正的份数有模板设置份数和操作份数相乘得到
            int actualCopies = bmkCopies * opCopies;

            var secuItems = _securityDataSource.Where(p => p.MonitorId == monitorItem.MonitorId);

            foreach (var secuItem in secuItems)
            {
                if (secuItem.SecuType == SecurityType.Stock)
                {
                    secuItem.EntrustAmount = actualCopies * secuItem.WeightAmount;
                }
                else if (secuItem.SecuType == SecurityType.Futures)
                {
                    secuItem.EntrustAmount = opCopies * secuItem.WeightAmount;
                }
            }
        }
        private void CalcEntrustAmount(OpenPositionItem monitorItem)
        {
            var secuItems = _securityDataSource.Where(p => p.MonitorId == monitorItem.MonitorId);

            foreach (var secuItem in secuItems)
            {
                secuItem.EntrustAmount = monitorItem.Copies * secuItem.WeightAmount;
            }
        }
 public void RemoveSecurityData(OpenPositionItem monitorItem)
 {
     for (int i = _securityDataSource.Count - 1; i >= 0; i--)
     {
         var secuItem = _securityDataSource[i];
         if (secuItem.MonitorId == monitorItem.MonitorId)
         {
             _securityDataSource.RemoveAt(i);
         }
     }
 }
        private void MonitorGridView_NumericUpDownValueChanged(int newValue, int rowIndex, int columnIndex)
        {
            if (rowIndex < 0 || rowIndex >= _monitorDataSource.Count)
            {
                return;
            }
            //该事件触发时,绑定数据还没有被同步
            OpenPositionItem monitorItem = _monitorDataSource[rowIndex];

            CalcEntrustAmount(monitorItem, newValue);

            securityGridView.Invalidate();
        }
        public void LoadSecurityData(OpenPositionItem monitorItem)
        {
            int bmkCopies    = 1;
            var templateItem = _templateBLL.GetTemplate(monitorItem.TemplateId);

            if (templateItem != null)
            {
                bmkCopies = templateItem.FutureCopies;
                //Load the future
                OpenPositionSecurityItem futureItem = new OpenPositionSecurityItem
                {
                    Selection     = true,
                    MonitorId     = monitorItem.MonitorId,
                    MonitorName   = monitorItem.MonitorName,
                    SecuCode      = monitorItem.FuturesContract,
                    SecuName      = monitorItem.FuturesContract,
                    WeightAmount  = templateItem.FutureCopies,
                    EntrustAmount = monitorItem.Copies * templateItem.FutureCopies,
                    EDirection    = EntrustDirection.SellOpen,
                    SecuType      = SecurityType.Futures
                };

                _securityDataSource.Add(futureItem);
            }

            List <TemplateStock>            stocks    = _templateBLL.GetStocks(monitorItem.TemplateId);
            List <OpenPositionSecurityItem> secuItems = new List <OpenPositionSecurityItem>();

            int actualCopies = bmkCopies * monitorItem.Copies;

            foreach (var stock in stocks)
            {
                OpenPositionSecurityItem secuItem = new OpenPositionSecurityItem
                {
                    Selection     = true,
                    MonitorId     = monitorItem.MonitorId,
                    MonitorName   = monitorItem.MonitorName,
                    SecuCode      = stock.SecuCode,
                    SecuName      = stock.SecuName,
                    WeightAmount  = stock.Amount,
                    EntrustAmount = actualCopies * stock.Amount,
                    EDirection    = EntrustDirection.BuySpot,
                    SecuType      = SecurityType.Stock
                };

                _securityDataSource.Add(secuItem);
            }
        }
        private OpenPositionItem GetOpenPositionItem(OrderConfirmItem orderItem)
        {
            var newOpenItem = new OpenPositionItem
            {
                MonitorId       = orderItem.MonitorId,
                MonitorName     = orderItem.MonitorName,
                PortfolioId     = orderItem.PortfolioId,
                PortfolioName   = orderItem.PortfolioName,
                TemplateId      = orderItem.TemplateId,
                TemplateName    = orderItem.TemplateName,
                Copies          = orderItem.Copies,
                FuturesContract = orderItem.FuturesContract,
                InstanceCode    = orderItem.InstanceCode,
                Notes           = orderItem.Notes ?? string.Empty,
            };

            return(newOpenItem);
        }
Exemple #9
0
        public List <OpenPositionItem> GetOpenItems()
        {
            int userId       = LoginManager.Instance.GetUserId();
            var monitorItems = _monitorunitdao.GetActive();
            List <OpenPositionItem> openItems = new List <OpenPositionItem>();

            foreach (var monitorItem in monitorItems)
            {
                if (_permissionManager.HasPermission(userId, monitorItem.MonitorUnitId, ResourceType.MonitorUnit, PermissionMask.View))
                {
                    OpenPositionItem openItem = new OpenPositionItem
                    {
                        TemplateId      = monitorItem.StockTemplateId,
                        TemplateName    = monitorItem.StockTemplateName,
                        MonitorId       = monitorItem.MonitorUnitId,
                        MonitorName     = monitorItem.MonitorUnitName,
                        PortfolioId     = monitorItem.PortfolioId,
                        PortfolioName   = monitorItem.PortfolioName,
                        FuturesContract = monitorItem.BearContract,
                        Copies          = 1,
                    };

                    var portfolio = _productBLL.GetById(monitorItem.PortfolioId);
                    if (portfolio != null)
                    {
                        openItem.PortfolioCode = portfolio.PortfolioNo;
                        openItem.FundCode      = portfolio.FundCode;
                        openItem.FundName      = portfolio.FundName;
                    }

                    var template = _templateBLL.GetTemplate(monitorItem.StockTemplateId);
                    if (template != null)
                    {
                        openItem.BenchmarkId = template.Benchmark;
                    }

                    openItems.Add(openItem);
                }
            }

            return(openItems);
        }
        private void MonitorGridView_UpdateRelatedDataGrid(UpdateDirection direction, int rowIndex, int columnIndex)
        {
            if (rowIndex < 0 || rowIndex >= _monitorDataSource.Count)
            {
                return;
            }

            OpenPositionItem monitorItem = _monitorDataSource[rowIndex];

            switch (direction)
            {
            case UpdateDirection.Select:
            {
                LoadSecurityData(monitorItem);
            }
            break;

            case UpdateDirection.UnSelect:
            {
                RemoveSecurityData(monitorItem);
            }
            break;

            case UpdateDirection.Increase:
            {
                CalcEntrustAmount(monitorItem);

                //monitorGridView.Invalidate();
                securityGridView.Invalidate();
            }
            break;

            case UpdateDirection.Decrease:
            {
                CalcEntrustAmount(monitorItem);
                //monitorGridView.Invalidate();
                securityGridView.Invalidate();
            }
            break;
            }
        }
 private void CalcEntrustAmount(OpenPositionItem monitorItem)
 {
     CalcEntrustAmount(monitorItem, monitorItem.Copies);
 }
        private bool Form_LoadData(object sender, object data)
        {
            if (data == null)
            {
                return(false);
            }
            if (!(data is OpenPositionItem))
            {
                return(false);
            }
            _originOpenItem = data as OpenPositionItem;
            if (_originOpenItem == null)
            {
                return(false);
            }

            //
            this.tbPortfolio.Text = _originOpenItem.PortfolioName;
            this.tbTemlate.Text   = string.Format("{0}-{1}", _originOpenItem.TemplateId, _originOpenItem.TemplateName);
            this.tbFutures.Text   = _originOpenItem.FuturesContract;
            this.tbCopies.Text    = string.Format("{0}", _originOpenItem.Copies);
            this.tbCopies.Enabled = false;
            this.tbBias.Text      = "0";

            DateTime now       = DateTime.Now;
            DateTime startDate = new DateTime(now.Year, now.Month, now.Day, 9, 15, 0);
            DateTime endDate   = new DateTime(now.Year, now.Month, now.Day, 15, 15, 0);

            this.tbStartDate.Text = DateFormat.Format(startDate, ConstVariable.DateFormat1);
            this.tbEndDate.Text   = DateFormat.Format(endDate, ConstVariable.DateFormat1);
            this.tbStartTime.Text = DateFormat.Format(startDate, ConstVariable.TimeFormat1);
            this.tbEndTime.Text   = DateFormat.Format(endDate, ConstVariable.TimeFormat1);

            //Initialize the instancecode
            var instances       = _tradeInstanceBLL.GetAllInstance();
            var targetInstances = instances.Where(p => p.MonitorUnitId == _originOpenItem.MonitorId && p.TemplateId == _originOpenItem.TemplateId).ToList();

            ComboOption comboOption = new ComboOption
            {
                Items = new List <ComboOptionItem>()
            };

            if (targetInstances == null || targetInstances.Find(p => p.InstanceCode.Equals(_originOpenItem.InstanceCode)) == null)
            {
                ComboOptionItem currentItem = new ComboOptionItem
                {
                    Id   = _originOpenItem.InstanceCode,
                    Name = _originOpenItem.InstanceCode
                };

                comboOption.Items.Add(currentItem);
            }

            if (targetInstances != null && targetInstances.Count > 0)
            {
                foreach (var instance in targetInstances)
                {
                    ComboOptionItem item = new ComboOptionItem
                    {
                        Id   = instance.InstanceCode,
                        Name = instance.InstanceCode
                    };

                    comboOption.Items.Add(item);
                }
            }

            ComboBoxUtil.SetComboBox(this.cbInstanceCode, comboOption);
            ComboBoxUtil.SetComboBoxSelect(this.cbInstanceCode, _originOpenItem.InstanceCode);

            return(true);
        }
        /// <summary>
        /// Submit a new the TradeCommand to open the position. It will create a new row in the tradecommand table.
        /// It will check whether the tradeinstance is existed and it will create a new tradeinstance if there is no one.
        /// </summary>
        /// <param name="openItem">An object of OpenPositionItem contains some basic information, such as portfolio,
        /// monitor, instancecode, entrust direction.
        /// </param>
        /// <param name="secuItems">A list of detailed securities will be entrusted.</param>
        /// <param name="startDate">The trade command start date and time.</param>
        /// <param name="endDate">The trade command end date and time.</param>
        /// <returns>An integer value to indicate whether it is successful or fail. A positive value means success.
        /// Otherwise failure.</returns>
        public int SubmitOpenPosition(OpenPositionItem openItem, List <OpenPositionSecurityItem> secuItems, DateTime startDate, DateTime endDate)
        {
            int    instanceId   = -1;
            string instanceCode = openItem.InstanceCode;
            var    instance     = _tradeInstanceBLL.GetInstance(instanceCode);

            if (instance != null && !string.IsNullOrEmpty(instance.InstanceCode) && instance.InstanceCode.Equals(instanceCode))
            {
                instanceId = instance.InstanceId;
                instance.OperationCopies += openItem.Copies;
                _tradeInstanceBLL.Update(instance, secuItems);
            }
            else
            {
                Model.UI.TradeInstance tradeInstance = new Model.UI.TradeInstance
                {
                    InstanceCode     = instanceCode,
                    PortfolioId      = openItem.PortfolioId,
                    MonitorUnitId    = openItem.MonitorId,
                    TemplateId       = openItem.TemplateId,
                    StockDirection   = EntrustDirection.BuySpot,
                    FuturesContract  = openItem.FuturesContract,
                    FuturesDirection = EntrustDirection.SellOpen,
                    OperationCopies  = openItem.Copies,
                    StockPriceType   = StockPriceType.NoLimit,
                    FuturesPriceType = FuturesPriceType.NoLimit,
                    Status           = TradeInstanceStatus.Active,
                };

                tradeInstance.Owner = LoginManager.Instance.GetUserId();
                instanceId          = _tradeInstanceBLL.Create(tradeInstance, secuItems);
            }

            int ret = -1;

            if (instanceId > 0)
            {
                //success! Will send generate TradingCommand
                Model.Database.TradeCommand cmdItem = new Model.Database.TradeCommand
                {
                    InstanceId        = instanceId,
                    ECommandType      = CommandType.Arbitrage,
                    EExecuteType      = ExecuteType.OpenPosition,
                    CommandNum        = openItem.Copies,
                    EStockDirection   = EntrustDirection.BuySpot,
                    EFuturesDirection = EntrustDirection.SellOpen,
                    EEntrustStatus    = EntrustStatus.NoExecuted,
                    EDealStatus       = DealStatus.NoDeal,
                    ModifiedTimes     = 1,
                    DStartDate        = startDate,
                    DEndDate          = endDate,
                    Notes             = openItem.Notes,
                };

                var cmdSecuItems = GetSelectCommandSecurities(openItem.MonitorId, -1, secuItems);

                ret = SubmitInternal(cmdItem, cmdSecuItems);
            }
            else
            {
                //TODO: error message
            }

            return(ret);
        }