Panel CreateSessionPanel(PlatformExpertSession session)
        {
            Panel panel = new Panel();

            // Session not found, create new button for it.
            PlatformExpertSessionControl sessionChartControl = new PlatformExpertSessionControl();

            sessionChartControl.Dock = DockStyle.Fill;
            sessionChartControl.CreateControl();
            sessionChartControl.Session = session;

            panel.Controls.Add(sessionChartControl);

            if (session.OrderExecutionProvider != null)
            {
                Splitter splitter = new SplitterEx();
                splitter.Dock = DockStyle.Bottom;
                panel.Controls.Add(splitter);
                splitter.Height = 6;

                OrdersControl ordersControl = new OrdersControl();
                ordersControl.Dock = DockStyle.Bottom;
                ordersControl.CreateControl();
                ordersControl.SingleSession   = session;
                ordersControl.VisibleChanged += delegate(object sender, EventArgs e)
                {
                    splitter.Visible = ordersControl.Visible;
                };
                //ordersControl.Manager = this._expert.Manager;
                panel.Controls.Add(ordersControl);

                sessionChartControl.CorrespondingOrdersControl = ordersControl;

                Splitter splitter2 = new Splitter();
                splitter2.Dock = DockStyle.Bottom;
                panel.Controls.Add(splitter2);

                AccountControl accountControl = new AccountControl();
                accountControl.Dock = DockStyle.Bottom;
                accountControl.CreateControl();
                accountControl.Account = session.OrderExecutionProvider.DefaultAccount;
                panel.Controls.Add(accountControl);
            }

            panel.Tag = sessionChartControl;

            panel.Visible = false;
            panel.Dock    = DockStyle.Fill;

            return(panel);
        }
        void CreateSessionPane(bool trading)
        {
            if (listViewQuotes.SelectedItems.Count < 1 || Component.Delivery == null || Component.DataSourceId.HasValue == false)
            {
                return;
            }

            ListViewItem    item = listViewQuotes.SelectedItems[0];
            LocalExpertHost host = new LocalExpertHost(item.Text, typeof(ManualTradeExpert));

            Component.Platform.RegisterComponent(host);

            RuntimeDataSessionInformation info = Component.Delivery.GetSymbolRuntimeSessionInformation((Symbol)item.Tag);

            if (info == null)
            {
                SystemMonitor.OperationError("Failed to obtain symbol session, operation can not be performed.");
                MessageBox.Show("Failed to obtain symbol session, operation can not be performed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string operationResultMessage;

            ComponentId?tradeSourceId = null;

            if (trading)
            {
                SortedDictionary <int, List <ComponentId> > compatibleExecutioners =
                    Component.GetCompatibleOrderExecutionSources(Component.DataSourceId.Value, info.Info.Symbol, SourceTypeEnum.Live | SourceTypeEnum.OrderExecution);

                if (compatibleExecutioners.Count == 0 ||
                    compatibleExecutioners[GeneralHelper.EnumerableFirstThrows <int>(compatibleExecutioners.Keys)].Count == 0)
                {
                    MessageBox.Show("Failed to find order execution source for this symbol. Trading can not be initiated.");
                    return;
                }

                tradeSourceId = compatibleExecutioners[GeneralHelper.EnumerableFirstThrows <int>(compatibleExecutioners.Keys)][0];
            }

            PlatformExpertSession session = host.CreateExpertSession(info.Info, Component.DataSourceId.Value,
                                                                     tradeSourceId, false, out operationResultMessage);

            if (session == null)
            {
                SystemMonitor.OperationError(operationResultMessage);
                MessageBox.Show(operationResultMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (host.RegisterExpertSession(session))
            {
                // Try to run a 1hour chart, or if not, any chart available.
                bool oneHourFound = false;
                foreach (TimeSpan span in session.DataProvider.AvailableDataBarProviderPeriods)
                {
                    if (span == TimeSpan.FromHours(1))
                    {
                        session.DataProvider.ObtainDataBarProvider(TimeSpan.FromHours(1));
                        oneHourFound = true;
                        break;
                    }
                }

                if (oneHourFound == false && session.DataProvider.AvailableDataBarProviderPeriods.Length > 0)
                {
                    session.DataProvider.ObtainDataBarProvider(session.DataProvider.AvailableDataBarProviderPeriods[0]);
                }

                // Allow the pending control requests to be created and performed before floating it.
                this.BeginInvoke(new GeneralHelper.GenericDelegate <LocalExpertHost>(SetControlFloat), host);
            }
            else
            {
                SystemMonitor.OperationError("Failed to register session.");
                MessageBox.Show("Failed to register session.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCreateSession_Click(object sender, EventArgs e)
        {
            if (symbolSelectControl.SelectedSymbols.Count == 0)
            {
                MessageBox.Show("No symbol selected.");
                return;
            }

            ComponentId?orderExecuteId = null;
            int         compatibility  = 0;

            if ((radioButtonSimulationTrading.Checked || radioButtonLiveTrading.Checked) &&
                comboBoxOrderExecutionSources.SelectedIndex < _orderExecutionSourcesIds.Count)
            {
                orderExecuteId = _orderExecutionSourcesIds[comboBoxOrderExecutionSources.SelectedIndex];
                compatibility  = _orderExecutionSourcesCompatibilities[comboBoxOrderExecutionSources.SelectedIndex];
            }

            string operationResultMessage;

            DataSessionInfo?sessionInfo = Host.GetSymbolDataSessionInfo(symbolSelectControl.SelectedDataSourceId.Value,
                                                                        symbolSelectControl.SelectedSymbols[0]);

            if (sessionInfo.HasValue == false)
            {
                MessageBox.Show("Failed to initiate session for this symbol.");
                return;
            }

            if (_host.GetExpertSession(sessionInfo.Value) != null)
            {
                MessageBox.Show("Session for this symbol/source already exists.");
                return;
            }

            if (compatibility < 100 && radioButtonLiveTrading.Checked)
            {
                if (MessageBox.Show("You are creating a live session with order execution provider [" + orderExecuteId.Value.Name + "], that has low compatilibity level with selected data source. It is likely this is not the default order execution provider for this data source." + Environment.NewLine + Environment.NewLine + "Do you wish to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                {
                    return;
                }
            }

            PlatformExpertSession session = _host.CreateExpertSession(sessionInfo.Value, symbolSelectControl.SelectedDataSourceId.Value,
                                                                      orderExecuteId, radioButtonSimulationTrading.Checked, out operationResultMessage);

            if (session == null || _host.RegisterExpertSession(session) == false)
            {
                MessageBox.Show("Failed to create session [" + operationResultMessage + "].", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                // TODO: add support for tick dataDelivery.
                TimeSpan period = TimeSpan.FromHours(1);

                if (symbolSelectControl.SelectedPeriod.HasValue)
                {
                    period = symbolSelectControl.SelectedPeriod.Value;
                }
                else
                {
                    if (GeneralHelper.EnumerableContains <TimeSpan>(session.DataProvider.AvailableDataBarProviderPeriods, period) == false)
                    {// We can not use 1hour because it is not available.
                        if (session.DataProvider.AvailableDataBarProviderPeriods.Length > 0)
                        {
                            period = session.DataProvider.AvailableDataBarProviderPeriods[0];
                        }
                        else
                        {
                            SystemMonitor.OperationWarning("Failed to establish period for session, going for default (1 hour) although not marked as available.");
                        }
                    }
                }

                if (session.DataProvider.ObtainDataBarProvider(period) == false)
                {
                    SystemMonitor.OperationError("Failed to obtain data bar provide for period [" + period.ToString() + "].");
                }

                if (SessionCreatedEvent != null)
                {
                    SessionCreatedEvent();
                }
            }

            this.Hide();
        }