public void Initialize(GraphCurveTypeList curveList, List<HLine> horizontallines, DateTime[] dateSerie, string serieName, StockDrawingItems drawingItems, int startIndex, int endIndex)
        {
            using (MethodLogger ml = new MethodLogger(this))
             {
            this.dateSerie = dateSerie;
            this.CurveList = curveList;
            this.StartIndex = startIndex;
            this.EndIndex = endIndex;
            this.serieName = serieName;
            this.drawingItems = drawingItems;
            this.horizontalLines = horizontallines;

            // Initialise undo buffer
            this.GraphActions = new List<GraphAction>();
            this.currentActionIndex = -1;

            // Initialise graphics
            this.graphic = this.CreateGraphics();
            RectangleF rect = this.graphic.VisibleClipBounds;
            rect.Inflate(new SizeF(-this.XMargin, -this.YMargin));
            this.GraphRectangle = rect;

            this.IsInitialized = true;
            this.alternateString = string.Empty;
             }
        }
Beispiel #2
0
        private void ApplyTheme()
        {
            using (MethodLogger ml = new MethodLogger(this))
            {
                try
                {
                    if (this.CurrentTheme == null || this.CurrentStockSerie == null) return;
                    if (!this.CurrentStockSerie.IsInitialised)
                    {
                        this.statusLabel.Text = ("Loading data...");
                        this.Refresh();
                    }
                    if (!this.CurrentStockSerie.Initialise() || this.CurrentStockSerie.Count == 0)
                    {
                        this.DeactivateGraphControls("Data for " + this.CurrentStockSerie.StockName +
                                                     " cannot be initialised");
                        return;
                    }
                    if (this.CurrentStockSerie.StockAnalysis.DeleteTransientDrawings() > 0)
                    {
                        this.CurrentStockSerie.PaintBarCache = null;
                    }

                    // Build curve list from definition
                    if (!this.themeDictionary.ContainsKey(currentTheme))
                    {
                        // LoadTheme
                        LoadCurveTheme(currentTheme);
                    }

                    // Force resetting the secondary serie.
                    if (themeDictionary[currentTheme]["CloseGraph"].FindIndex(s => s.StartsWith("SECONDARY")) == -1)
                    {
                        if (this.graphCloseControl.SecondaryFloatSerie != null)
                        {
                            themeDictionary[currentTheme]["CloseGraph"].Add("SECONDARY|" +
                                                                            this.graphCloseControl.SecondaryFloatSerie
                                                                                .Name);
                        }
                        else
                        {
                            themeDictionary[currentTheme]["CloseGraph"].Add("SECONDARY|NONE");
                        }
                    }

                    DateTime[] dateSerie = CurrentStockSerie.Keys.ToArray();
                    GraphCurveTypeList curveList;
                    bool skipEntry = false;
                    foreach (string entry in themeDictionary[currentTheme].Keys)
                    {
                        if (entry.ToUpper().EndsWith("GRAPH"))
                        {
                            GraphControl graphControl = null;
                            curveList = new GraphCurveTypeList();
                            switch (entry.ToUpper())
                            {
                                case "CLOSEGRAPH":
                                    graphControl = this.graphCloseControl;
                                    this.graphCloseControl.ShowVariation = Settings.Default.ShowVariation;
                                    this.graphCloseControl.Comments = this.CurrentStockSerie.StockAnalysis.Comments;
                                    break;
                                case "SCROLLGRAPH":
                                    graphControl = this.graphScrollerControl;
                                    break;
                                case "INDICATOR1GRAPH":
                                    graphControl = this.graphIndicator1Control;
                                    break;
                                case "INDICATOR2GRAPH":
                                    graphControl = this.graphIndicator2Control;
                                    break;
                                case "INDICATOR3GRAPH":
                                    graphControl = this.graphIndicator3Control;
                                    break;
                                case "VOLUMEGRAPH":
                                    if (this.CurrentStockSerie.HasVolume)
                                    {
                                        graphControl = this.graphVolumeControl;
                                        curveList.Add(new GraphCurveType(
                                            CurrentStockSerie.GetSerie(StockDataType.VOLUME),
                                            Pens.Green, true));
                                    }
                                    else
                                    {
                                        this.graphVolumeControl.Deactivate("This serie has no volume data", false);
                                        skipEntry = true;
                                    }
                                    break;
                                default:
                                    continue;
                            }

                            if (skipEntry)
                            {
                                skipEntry = false;
                                continue;
                            }
                            try
                            {
                                List<HLine> horizontalLines = new List<HLine>();

                                foreach (string line in this.themeDictionary[currentTheme][entry])
                                {
                                    string[] fields = line.Split('|');
                                    switch (fields[0].ToUpper())
                                    {
                                        case "GRAPH":
                                            string[] colorItem = fields[1].Split(':');
                                            graphControl.BackgroundColor = Color.FromArgb(int.Parse(colorItem[0]),
                                                int.Parse(colorItem[1]), int.Parse(colorItem[2]), int.Parse(colorItem[3]));
                                            colorItem = fields[2].Split(':');
                                            graphControl.TextBackgroundColor = Color.FromArgb(int.Parse(colorItem[0]),
                                                int.Parse(colorItem[1]), int.Parse(colorItem[2]), int.Parse(colorItem[3]));
                                            graphControl.ShowGrid = bool.Parse(fields[3]);
                                            colorItem = fields[4].Split(':');
                                            graphControl.GridColor = Color.FromArgb(int.Parse(colorItem[0]),
                                                int.Parse(colorItem[1]), int.Parse(colorItem[2]), int.Parse(colorItem[3]));
                                            graphControl.ChartMode =
                                                (GraphChartMode)Enum.Parse(typeof(GraphChartMode), fields[5]);
                                            if (entry.ToUpper() == "CLOSEGRAPH")
                                            {
                                                if (fields.Length >= 7)
                                                {
                                                    this.graphCloseControl.SecondaryPen =
                                                        GraphCurveType.PenFromString(fields[6]);
                                                }
                                                else
                                                {
                                                    this.graphCloseControl.SecondaryPen = new Pen(Color.DarkGoldenrod, 1);
                                                }
                                            }
                                            break;
                                        case "SECONDARY":
                                            if (this.currentStockSerie.SecondarySerie != null)
                                            {
                                                CheckSecondarySerieMenu(fields[1]);
                                                this.graphCloseControl.SecondaryFloatSerie =
                                                    this.CurrentStockSerie.GenerateSecondarySerieFromOtherSerie(
                                                        this.currentStockSerie.SecondarySerie, StockDataType.CLOSE);
                                            }
                                            else
                                            {
                                                if (fields[1].ToUpper() == "NONE" ||
                                                    !this.StockDictionary.ContainsKey(fields[1]))
                                                {
                                                    ClearSecondarySerieMenu();
                                                    this.graphCloseControl.SecondaryFloatSerie = null;
                                                }
                                                else
                                                {
                                                    if (this.StockDictionary.ContainsKey(fields[1]))
                                                    {
                                                        CheckSecondarySerieMenu(fields[1]);
                                                        this.graphCloseControl.SecondaryFloatSerie =
                                                            this.CurrentStockSerie.GenerateSecondarySerieFromOtherSerie(
                                                                this.StockDictionary[fields[1]], StockDataType.CLOSE);
                                                    }
                                                }
                                            }
                                            break;
                                        case "DATA":
                                            curveList.Add(
                                                new GraphCurveType(
                                                    CurrentStockSerie.GetSerie(
                                                        (StockDataType)Enum.Parse(typeof(StockDataType), fields[1])),
                                             fields[2], bool.Parse(fields[3])));
                                            break;
                                        case "TRAIL":
                                        case "INDICATOR":
                                            {
                                                IStockIndicator stockIndicator =
                                                    (IStockIndicator)
                                                        StockViewableItemsManager.GetViewableItem(line,
                                                            this.CurrentStockSerie);
                                                if (stockIndicator != null)
                                                {
                                                    if (entry.ToUpper() != "CLOSEGRAPH")
                                                    {
                                                        if (stockIndicator.DisplayTarget ==
                                                            IndicatorDisplayTarget.RangedIndicator)
                                                        {
                                                            IRange range = (IRange)stockIndicator;
                                                            ((GraphRangedControl)graphControl).RangeMin = range.Min;
                                                            ((GraphRangedControl)graphControl).RangeMax = range.Max;
                                                        }
                                                        else
                                                        {
                                                            ((GraphRangedControl)graphControl).RangeMin = float.NaN;
                                                            ((GraphRangedControl)graphControl).RangeMax = float.NaN;
                                                        }
                                                    }
                                                    if (
                                                        !(stockIndicator.RequiresVolumeData &&
                                                          !this.CurrentStockSerie.HasVolume))
                                                    {
                                                        curveList.Indicators.Add(stockIndicator);
                                                    }
                                                }
                                            }
                                            break;
                                        case "PAINTBAR":
                                            {
                                                IStockPaintBar paintBar =
                                                    (IStockPaintBar)
                                                        StockViewableItemsManager.GetViewableItem(line,
                                                            this.CurrentStockSerie);
                                                curveList.PaintBar = paintBar;
                                            }
                                            break;
                                        case "DECORATOR":
                                            {
                                                IStockDecorator decorator =
                                                    (IStockDecorator)
                                                        StockViewableItemsManager.GetViewableItem(line,
                                                            this.CurrentStockSerie);
                                                curveList.Decorator = decorator;
                                                this.GraphCloseControl.CurveList.ShowMes.Add(decorator);
                                            }
                                            break;
                                        case "TRAILSTOP":
                                            {
                                                IStockTrailStop trailStop =
                                                    (IStockTrailStop)
                                                        StockViewableItemsManager.GetViewableItem(line,
                                                            this.CurrentStockSerie);
                                                curveList.TrailStop = trailStop;
                                            }
                                            break;
                                        case "LINE":
                                            horizontalLines.Add(new HLine(float.Parse(fields[1]),
                                                GraphCurveType.PenFromString(fields[2])));
                                            break;
                                        default:
                                            continue;
                                    }
                                }
                                if (curveList.FindIndex(c => c.DataSerie.Name == "CLOSE") < 0)
                                {
                                    curveList.Insert(0,
                                        new GraphCurveType(CurrentStockSerie.GetSerie(StockDataType.CLOSE), Pens.Black,
                                            false));
                                }
                                if (graphControl == this.graphCloseControl)
                                {
                                    if (curveList.FindIndex(c => c.DataSerie.Name == "LOW") < 0)
                                    {
                                        curveList.Insert(0,
                                            new GraphCurveType(CurrentStockSerie.GetSerie(StockDataType.LOW), Pens.Black,
                                                false));
                                    }
                                    if (curveList.FindIndex(c => c.DataSerie.Name == "HIGH") < 0)
                                    {
                                        curveList.Insert(0,
                                            new GraphCurveType(CurrentStockSerie.GetSerie(StockDataType.HIGH), Pens.Black,
                                                false));
                                    }
                                    if (curveList.FindIndex(c => c.DataSerie.Name == "OPEN") < 0)
                                    {
                                        curveList.Insert(0,
                                            new GraphCurveType(CurrentStockSerie.GetSerie(StockDataType.OPEN), Pens.Black,
                                                false));
                                    }
                                }
                                if (
                                    !this.CurrentStockSerie.StockAnalysis.DrawingItems.ContainsKey(
                                        this.CurrentStockSerie.BarDuration))
                                {
                                    this.CurrentStockSerie.StockAnalysis.DrawingItems.Add(
                                        this.CurrentStockSerie.BarDuration, new StockDrawingItems());
                                }
                                graphControl.Initialize(curveList, horizontalLines, dateSerie,
                                    CurrentStockSerie.StockName,
                                    CurrentStockSerie.StockAnalysis.DrawingItems[this.CurrentStockSerie.BarDuration],
                                    startIndex, endIndex);
                            }
                            catch (System.Exception e)
                            {
                                StockLog.Write("Exception londing theme: " + this.currentTheme);
                                foreach (string line in this.themeDictionary[currentTheme][entry])
                                {
                                    StockLog.Write(line);
                                }
                                StockLog.Write(e);
                            }
                        }
                    }

                    // Apply Strategy

                    // Create new simulation portofolio
                    if (this.currentStockSerie.BelongsToGroup(StockSerie.Groups.BREADTH) ||
                        this.currentStockSerie.BelongsToGroup(StockSerie.Groups.COT) ||
                        this.currentStockSerie.BelongsToGroup(StockSerie.Groups.INDICATOR) ||
                        this.currentStockSerie.BelongsToGroup(StockSerie.Groups.NONE))
                    {
                        this.CurrentPortofolio = null;

                        if (this.currentStockSerie.BelongsToGroup(StockSerie.Groups.BREADTH) && this.currentStockSerie.DataProvider != StockDataProvider.FINRA)
                        {
                            string[] fields = this.currentStockSerie.StockName.Split('.');
                            this.graphCloseControl.SecondaryFloatSerie =
                                this.CurrentStockSerie.GenerateSecondarySerieFromOtherSerie(
                                    this.StockDictionary[fields[1]], StockDataType.CLOSE);
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(this.currentStrategy))
                        {
                            CurrentPortofolio =
                                this.StockPortofolioList.Find(p => p.Name == this.currentStockSerie.StockName + "_P");
                            if (CurrentPortofolio == null)
                            {
                                CurrentPortofolio = new StockPortofolio(this.currentStockSerie.StockName + "_P");
                                CurrentPortofolio.IsSimulation = true;
                                CurrentPortofolio.TotalDeposit = 1000;
                                this.StockPortofolioList.Add(CurrentPortofolio);

                                this.RefreshPortofolioMenu();
                            }

                            var selectedStrategy = StrategyManager.CreateStrategy(this.currentStrategy,
                                this.currentStockSerie,
                                null, true);
                            if (selectedStrategy != null)
                            {
                                float amount = this.currentStockSerie.GetMax(StockDataType.CLOSE) * 100f;

                                CurrentPortofolio.TotalDeposit = amount;
                                CurrentPortofolio.Clear();

                                this.currentStockSerie.GenerateSimulation(selectedStrategy,
                                    Settings.Default.StrategyStartDate, this.currentStockSerie.Keys.Last(),
                                    amount, false,
                                    false, Settings.Default.SupportShortSelling, false, 0.0f, false, 0.0f, 0.0f, 0.0f, CurrentPortofolio);
                            }

                            this.graphCloseControl.Portofolio = CurrentPortofolio;

                            if (portofolioDlg != null)
                            {
                                this.CurrentPortofolio.Initialize(this.StockDictionary);
                                portofolioDlg.SetPortofolio(this.CurrentPortofolio);
                                portofolioDlg.Activate();
                            }
                        }
                    }

                    // Reinitialise drawing
                    ResetZoom();
                    this.Cursor = Cursors.Arrow;
                }

                catch (Exception ex)
                {
                    StockAnalyzerException.MessageBox(ex);
                }
            }
        }