Example #1
0
        public static Serie GetSymbolDataSerie(int stock_id, int portfolio_id, DataSourceFieldUsed data_name)
        {
            List <Candel> quotes = new List <Candel>();

            if (DashBoardCache != null && false)
            {
                var oneYearAgo_Date = DateTime.Now.AddYears(-1);

                var symbol = DashBoardCache.DashboardItems.Where(x => x.Portfolio_Id == portfolio_id)
                             .SelectMany(x => x.Symbols)
                             .Where(x => x.Symbol_ID == stock_id)
                             .FirstOrDefault();

                quotes = symbol.Quotes.Where(x => x.Date > oneYearAgo_Date).ToList();
            }
            else
            {
                quotes = QuotesService.GetSymbolQuotes(stock_id, ChartRange.Year, CandelRange.Weekly, "FREE");
            }

            Serie result = Candel.GetDataSerie(quotes, data_name, true);

            return(result);
        }
Example #2
0
        public static Serie GetDataSerie(List <Candel> quotes, DataSourceFieldUsed data_name, bool filterCandels, int rounds = 0, List <Serie> series = null)
        {
            if (filterCandels)
            {
                quotes = quotes.Where(q => q.Visible).ToList <Candel>();
            }

            Serie serie = new Serie();

            switch (data_name)
            {
            case DataSourceFieldUsed.Close:
                foreach (var quote in quotes.Where(q => q.Close > 0))
                {
                    serie.Data.Add(new SerieValue()
                    {
                        Date = quote.Date, Value = quote.Close, Visible = quote.Visible
                    });
                }
                break;

            case DataSourceFieldUsed.Open:
                foreach (var quote in quotes)
                {
                    serie.Data.Add(new SerieValue()
                    {
                        Date = quote.Date, Value = quote.Open
                    });
                }
                break;

            case DataSourceFieldUsed.Minimun:
                foreach (var quote in quotes)
                {
                    serie.Data.Add(new SerieValue()
                    {
                        Date = quote.Date, Value = quote.Minimun
                    });
                }
                break;

            case DataSourceFieldUsed.Maximun:
                foreach (var quote in quotes)
                {
                    serie.Data.Add(new SerieValue()
                    {
                        Date = quote.Date, Value = quote.Maximun
                    });
                }
                break;

            case DataSourceFieldUsed.PriceVariation:
                foreach (var quote in quotes)
                {
                    serie.Data.Add(new SerieValue()
                    {
                        Date = quote.Date, Value = (quote.Close - quote.Open)
                    });
                }
                break;

            case DataSourceFieldUsed.PercentVariation:
                foreach (var quote in quotes)
                {
                    serie.Data.Add(new SerieValue()
                    {
                        Date = quote.Date, Value = (100 * (quote.Close - quote.Open)) / quote.Open
                    });
                }
                break;

            case DataSourceFieldUsed.Stochastic:
                for (int i = 0; i < quotes.Count; i++)
                {
                    int    count = (i >= rounds) ? rounds : i + 1;
                    double min   = quotes.GetRange(Math.Max(0, i - (rounds - 1)), count).Min(c => c.Minimun);
                    double max   = quotes.GetRange(Math.Max(0, i - (rounds - 1)), count).Max(c => c.Maximun);

                    if (max != min)     //this only happens with the first value
                    {
                        serie.Data.Add(new SerieValue()
                        {
                            Date = quotes[i].Date, Value = 100 * ((quotes[i].Close - min) / (max - min)), Visible = quotes[i].Visible
                        });
                    }
                }
                break;

            case DataSourceFieldUsed.ToolTip:
                foreach (var quote in quotes)
                {
                    string datestr      = quote.Date.ToString("MMMM", CultureInfo.CreateSpecificCulture("es")).Substring(0, 3).ToUpper() + " " + quote.Date.Day + ", " + quote.Date.Year;
                    string border_color = "blue";
                    if (quote.Open > quote.Close)
                    {
                        border_color = "#a52714";
                    }
                    else if (quote.Open < quote.Close)
                    {
                        border_color = "#0f9d58";
                    }
                    string tooltip = "<div style='padding:5px;width:100%;border: 3px solid " + border_color + ";'>";
                    tooltip += "<strong>" + datestr + "</strong>";
                    tooltip += "<br/><span style='font-size:10px;'><strong>Ape: </strong>" + quote.Open.ToString("0.00") + "</span>  <span style='font-size:10px;'><strong>Cie: </strong>" + quote.Close.ToString("0.00") + "</span>";
                    tooltip += "<br/><span style='font-size:10px;'><strong>Mín: </strong>" + quote.Minimun.ToString("0.00") + "</span>  <span style='font-size:10px;'><strong>Máx: </strong>" + quote.Maximun.ToString("0.00") + "</span>";
                    tooltip += "<br/><span style='font-size:10px;'><strong>Vol: </strong>" + quote.Volume.ToString("0.##") + "</span>";
                    if (series != null)
                    {
                        foreach (var dserie in series)
                        {
                            SerieValue serie_val = dserie.Data.Where(s => s.Date.Year == quote.Date.Year && s.Date.Month == quote.Date.Month && s.Date.Day == quote.Date.Day).FirstOrDefault();
                            if (serie_val != default(SerieValue))
                            {
                                tooltip += "<br/><span style='font-size:10px;color:" + dserie.Color + "';>" + dserie.Column_Data_Label + ": " + serie_val.Value.ToString("0.00") + "</span>";
                            }
                        }
                    }
                    tooltip += "</div>";
                    serie.Data.Add(new SerieValue()
                    {
                        Date = quote.Date, Tooltip = tooltip
                    });
                }
                serie.IsTooltip = true;
                break;

            case DataSourceFieldUsed.WilliansR:
                for (int i = 0; i < quotes.Count; i++)
                {
                    int    count = (i >= rounds) ? rounds : i + 1;
                    double min   = quotes.GetRange(Math.Max(0, i - (rounds - 1)), count).Min(c => c.Minimun);
                    double max   = quotes.GetRange(Math.Max(0, i - (rounds - 1)), count).Max(c => c.Maximun);

                    if (max != min)     //this only happens with the first value
                    {
                        serie.Data.Add(new SerieValue()
                        {
                            Date    = quotes[i].Date,
                            Value   = ((max - quotes[i].Close) / (max - min)) * -100,
                            Visible = quotes[i].Visible
                        });
                    }
                }
                break;

            case DataSourceFieldUsed.OBV:
                serie.Data.Add(new SerieValue()
                {
                    Date = quotes[0].Date, Value = quotes[0].Volume, Visible = quotes[0].Visible
                });
                double anterior, hoy, value;
                for (int i = 1; i < quotes.Count; i++)
                {
                    anterior = quotes[i - 1].Close;
                    hoy      = quotes[i].Close;

                    if (hoy < anterior)
                    {
                        value = serie.Data[i - 1].Value - quotes[i].Volume;
                    }
                    else if (hoy > anterior)
                    {
                        value = serie.Data[i - 1].Value + quotes[i].Volume;
                    }
                    else
                    {
                        value = serie.Data[i - 1].Value;
                    }

                    serie.Data.Add(new SerieValue()
                    {
                        Date    = quotes[i].Date,
                        Value   = value,
                        Visible = quotes[i].Visible
                    });
                }
                break;

            default:
                foreach (var quote in quotes)
                {
                    serie.Data.Add(new SerieValue()
                    {
                        Date = quote.Date, Value = quote.Volume, Visible = quote.Visible
                    });
                }
                break;
            }
            serie.Data.Sort((x, y) => x.Date.CompareTo(y.Date));
            return(serie);
        }