Beispiel #1
0
        private void BuildParameters()
        {
            _parameters.Clear();

            List <StockChartX_IndicatorsParameters.IndicatorParameter> parameters =
                StockChartX_IndicatorsParameters.GetIndicatorParameters(_indicator.IndicatorType);

            _itemsControl.Height = parameters.Count * (_indicator._chartPanel._chartX.GetTextHeight("W") + 4);
            _parameters.Clear();
            int index = 0;

            foreach (var parameter in parameters)
            {
                _parameters.Add(string.Format("{0} = {1}", parameter.Name, _indicator.GetParameterValue(index++)));
            }
            _itemsControl.ItemsSource = _parameters;
        }
        private void SaveIndicators(XmlNode xRoot, XmlDocument xmlDocument)
        {
            XmlNode xmlIndicators = xmlDocument.CreateElement("Indicators");

            xRoot.AppendChild(xmlIndicators);
            foreach (ChartPanel chartPanel in _panelsContainer.Panels)
            {
                foreach (Indicator indicator in chartPanel.IndicatorsCollection)
                {
                    if (indicator.IsTwin)
                    {
                        continue;
                    }

                    XmlNode xmlIndicator = xmlDocument.CreateElement("Indicator");
                    xmlIndicators.AppendChild(xmlIndicator);

                    ConvertToXmlEx("Name", xmlIndicator, xmlDocument, indicator.Name);
                    ConvertToXmlEx("IndicatorType", xmlIndicator, xmlDocument, indicator.IndicatorType);
                    ConvertToXmlEx("PanelIndex", xmlIndicator, xmlDocument, indicator._chartPanel.Index);
                    ConvertToXmlEx("TotalPanelCount", xmlIndicator, xmlDocument, indicator._chartPanel._chartX.PanelsCount);
                    ConvertToXmlEx("TotalPanelsInChart", xmlIndicator, xmlDocument, _panelsContainer.Panels.Count);
                    ConvertToXmlEx("UpColor", xmlIndicator, xmlDocument, indicator.UpColor.HasValue ? indicator.UpColor.ToString() : string.Empty);
                    ConvertToXmlEx("DownColor", xmlIndicator, xmlDocument, indicator.DownColor.HasValue ? indicator.DownColor.ToString() : string.Empty);
                    ConvertToXmlEx("StrokeThicknes", xmlIndicator, xmlDocument, indicator.StrokeThickness);
                    ConvertToXmlEx("StrokeColor", xmlIndicator, xmlDocument, indicator.StrokeColor);
                    ConvertToXmlEx("Selectable", xmlIndicator, xmlDocument, indicator.Selectable);
                    ConvertToXmlEx("Visible", xmlIndicator, xmlDocument, indicator.Visible);
                    ConvertToXmlEx("StrokePattern", xmlIndicator, xmlDocument, indicator.StrokePattern);
                    ConvertToXmlEx("UserParams", xmlIndicator, xmlDocument, indicator.UserParams);
                    List <StockChartX_IndicatorsParameters.IndicatorParameter> indicatorParams =
                        StockChartX_IndicatorsParameters.GetIndicatorParameters(indicator.IndicatorType);
                    for (int i = 0; i < indicatorParams.Count; i++)
                    {
                        ConvertToXmlEx("Parameter_" + i, xmlIndicator, xmlDocument, indicator.GetParameterValue(i));
                    }
                }
            }
        }
        private void ReadIndicators(XmlNode xRoot)
        {
            XmlNode xmlIndicators = xRoot.SelectSingleNode("Indicators");

            if (xmlIndicators == null)
            {
                return;
            }

            int useablePanelsCount = UseablePanelsCount;

            foreach (XmlNode node in xmlIndicators.ChildNodes)
            {
                if (node.Name != "Indicator")
                {
                    continue;
                }

                int panelIndex         = ConvertFromXmlEx("PanelIndex", node, -1);
                int totalPanelsInChart = ConvertFromXmlEx("TotalPanelsInChart", node, -1);
                if (panelIndex == -1)
                {
                    continue;
                }

                ChartPanel chartPanel;
                if (totalPanelsInChart == -1)
                {
                    if (panelIndex >= useablePanelsCount)
                    {
                        panelIndex = 0;
                    }

                    int totalPanelCount = -1;
                    if (node.ChildNodes.OfType <XmlNode>().Any(_ => _.Name == "TotalPanelCount"))
                    {
                        totalPanelCount = ConvertFromXmlEx("TotalPanelCount", node, -1);
                    }

                    if (totalPanelCount != -1)
                    {
                        int diff     = totalPanelCount - panelIndex;
                        int currDiff = useablePanelsCount - panelIndex;
                        if (currDiff < diff)
                        {
                            panelIndex = -1;
                        }
                    }

                    chartPanel = panelIndex == -1 ? AddChartPanel() : GetPanelByIndex(panelIndex);
                }
                else
                {
                    if (panelIndex >= useablePanelsCount)
                    {
                        panelIndex = 0;
                    }

                    if (_panelsContainer.Panels.Count < totalPanelsInChart)
                    {
                        AddChartPanel();
                    }

                    chartPanel = GetPanelByIndex(panelIndex);
                }

                string indicatorName = ConvertFromXmlEx("Name", node, string.Empty);
                if (string.IsNullOrEmpty(indicatorName))
                {
                    continue;
                }

                IndicatorType indicatorType  = (IndicatorType)Enum.Parse(typeof(IndicatorType), ConvertFromXmlEx("IndicatorType", node, string.Empty));
                int           indicatorCount = GetIndicatorCountByType(indicatorType);
                if (indicatorCount > 0)
                {
                    indicatorName += indicatorCount;
                }

                Indicator indicator = AddIndicator(indicatorType, indicatorName, chartPanel, true);
                string    upColor   = ConvertFromXmlEx("UpColor", node, string.Empty);
                string    downColor = ConvertFromXmlEx("DownColor", node, string.Empty);
                if (upColor.Length > 0)
                {
                    indicator.UpColor = Utils.StringToColor(upColor);
                }

                if (downColor.Length > 0)
                {
                    indicator.DownColor = Utils.StringToColor(downColor);
                }

                indicator.StrokeThickness = ConvertFromXmlEx("StrokeThicknes", node, 1.0);
                indicator.StrokeColor     = Utils.StringToColor(ConvertFromXmlEx("StrokeColor", node, string.Empty));
                indicator.Selectable      = ConvertFromXmlEx("Selectable", node, true);
                indicator.Visible         = ConvertFromXmlEx("Visible", node, true);
                indicator.StrokePattern   = (LinePattern)Enum.Parse(typeof(LinePattern), ConvertFromXmlEx("StrokePattern", node, "Solid"));
                indicator.UserParams      = ConvertFromXmlEx("UserParams", node, false);
                List <StockChartX_IndicatorsParameters.IndicatorParameter> indicatorParams =
                    StockChartX_IndicatorsParameters.GetIndicatorParameters(indicator.IndicatorType);

                for (int i = 0; i < indicatorParams.Count; i++)
                {
                    object oValue = ConvertFromXmlEx("Parameter_" + i, node, (object)null);

                    if (!ParamsWithSeriesName.Contains(indicatorParams[i].ParameterType))
                    {
                        var type = indicatorParams[i].ValueType;
                        if (type.IsEnum)
                        {
                            oValue = System.Enum.Parse(type, oValue.ToString(), true);
                        }
                        else
                        {
                            oValue = oValue == null ? 0 : Convert.ChangeType(oValue, indicatorParams[i].ValueType, CultureInfo.InvariantCulture);
                        }

                        indicator.SetParameterValue(i, oValue);
                    }
                    else
                    {
                        //is a property that has series name
                        Debug.Assert(indicatorParams[i].ValueType == typeof(string));
                        string sValue = oValue.ToString();
                        //try to to check for series typr
                        //string[] values = sValue.Split('.');
                        int idx = sValue.IndexOf(".open", StringComparison.InvariantCultureIgnoreCase);
                        if (idx == -1)
                        {
                            idx = sValue.IndexOf(".high", StringComparison.InvariantCultureIgnoreCase);
                        }
                        if (idx == -1)
                        {
                            idx = sValue.IndexOf(".low", StringComparison.InvariantCultureIgnoreCase);
                        }
                        if (idx == -1)
                        {
                            idx = sValue.IndexOf(".close", StringComparison.InvariantCultureIgnoreCase);
                        }
                        if (idx == -1)
                        {
                            idx = sValue.IndexOf(".volume", StringComparison.InvariantCultureIgnoreCase);
                        }
                        if (idx == -1)
                        {
                            idx = sValue.IndexOf(".", StringComparison.InvariantCultureIgnoreCase);
                        }
                        if (idx != -1) //pointing to a non-existing series
                        {
                            if (string.IsNullOrEmpty(Symbol))
                            {
                                Symbol = sValue.Substring(idx);
                            }

                            string source = Symbol + "." + sValue.Substring(idx + 1);
                            Series s      = GetSeriesByName(source);
                            if (s == null)
                            {
                                source = Symbol + ".close";
                                s      = GetSeriesByName(source);
                                if (s == null && PanelsCount > 0)
                                {
                                    if (_panelsContainer.Panels[0].SeriesCount > 0)
                                    {
                                        s = _panelsContainer.Panels[0].SeriesCollection.First();
                                    }
                                }
                            }

                            if (s != null)
                            {
                                source = s.FullName;
                            }

                            indicator.SetParameterValue(i, source);
                        }
                        else
                        {
                            indicator.SetParameterValue(i, sValue);
                        }
                    }
                }
            }

            _recalc = true;
        }