Beispiel #1
0
        public static Symbol GetSymbolInfo(int symbol_ID)
        {
            Symbol result = new Symbol();

            using (ctaDBEntities entities = new ctaDBEntities())
            {
                var stock = entities.Stocks.Where(x => x.Id == symbol_ID).FirstOrDefault();

                result.Symbol_ID           = symbol_ID;
                result.Symbol_Name         = stock.symbol;
                result.Symbol_Company_Name = stock.name;
                result.Description         = stock.description;
                result.Symbol_Market_ID    = stock.market_id;
                result.Symbol_Market       = stock.Market.name;

                result.Intradiary_Info = QuotesService.GetSymbolIntradiaryInfo(symbol_ID);

                var lastQuote = stock.Stock_Quote.OrderByDescending(itm => itm.date_round).FirstOrDefault();

                if (lastQuote != null)
                {
                    result.Intradiary_Info.Minimun = lastQuote.minimun;
                    result.Intradiary_Info.Maximun = lastQuote.maximun;
                    result.Intradiary_Info.Volume  = (double)lastQuote.volume;
                }

                if (!(entities.Database.Connection.State == ConnectionState.Closed))
                {
                    entities.Database.Connection.Close();
                }
            }

            return(result);
        }
Beispiel #2
0
        public static UserDashboard GetDashboard(string username)
        {
            using (ctaDBEntities entities = new ctaDBEntities())
            {
                UserDashboard result = new UserDashboard(username);
                entities.Database.Connection.Open();

                int    user_id         = UserService.GetUserId(username);
                string user_type       = entities.Tenants.Where(t => t.Id == user_id).Select(t => t.Tenant_Type.Name).First();
                var    user_portfolios = entities.Portfolios.Where(ptfolio => ptfolio.user_id == user_id);

                foreach (var portfolio in user_portfolios)
                {
                    DashboardItem dashboard_item = new DashboardItem()
                    {
                        Portfolio = portfolio.name, Symbols = new List <ctaCOMMON.Charts.Symbol>(), Portfolio_Id = portfolio.Id
                    };
                    foreach (var stock in portfolio.Portfolio_Stock)
                    {
                        List <Candel> quotes       = new List <Candel>();
                        Candel        today_candel = null;
                        foreach (var quote in stock.Stock.Stock_Quote.Where(s => s.date_round < DateTime.Now.AddDays(-1) || user_type != "FREE").OrderBy(itm => itm.date_round))
                        {
                            Candel candel = new Candel()
                            {
                                Date = quote.date_round, Open = quote.opening, Close = quote.closing, Minimun = quote.minimun, Maximun = quote.maximun, Volume = (double)quote.volume
                            };
                            quotes.Add(candel);
                            today_candel = candel;
                        }
                        SymbolIntradiaryInfo symbolIntradiaryInfo = QuotesService.GetSymbolIntradiaryInfo(stock.stock_id);
                        if (today_candel != null)
                        {
                            symbolIntradiaryInfo.Volume  = today_candel.Volume;
                            symbolIntradiaryInfo.Maximun = today_candel.Maximun;
                            symbolIntradiaryInfo.Minimun = today_candel.Minimun;
                        }

                        ctaCOMMON.Charts.Symbol dashboard_item_symbol = new ctaCOMMON.Charts.Symbol()
                        {
                            Symbol_ID = stock.stock_id, Symbol_Name = stock.Stock.symbol, Symbol_Company_Name = stock.Stock.name, Symbol_Market_ID = stock.Stock.Market.Id, Symbol_Market = stock.Stock.Market.name, Quotes = quotes.OrderBy(q => q.Date).ToList(), Intradiary_Info = symbolIntradiaryInfo
                        };
                        dashboard_item.Symbols.Add(dashboard_item_symbol);
                    }

                    result.AddDashboardItem(dashboard_item);
                }

                entities.Database.Connection.Close();

                DashBoardCache = result;
                return(result);
            }
        }
Beispiel #3
0
        public static List <SimplifiedSymbolQuotes> GetQuotesValues(int stockType_id, int?marketIndex_id)
        {
            List <SimplifiedSymbolQuotes> list_result = new List <SimplifiedSymbolQuotes>();

            using (ctaDBEntities entities = new ctaDBEntities())
            {
                entities.Database.Connection.Open();

                var stocksSet = entities.Stocks.Where(x => x.Stock_Type.Id == stockType_id && x.active.Value);

                if (marketIndex_id.HasValue)
                {
                    stocksSet = stocksSet.Where(x => x.MarketIndex_Stock.Any(y => y.marketindex_id == marketIndex_id.Value));
                }

                foreach (var stock in stocksSet)
                {
                    var intradiaryInfo = QuotesService.GetSymbolIntradiaryInfo(stock.Id);

                    var lastQuote = stock.Stock_Quote.OrderByDescending(itm => itm.date_round).FirstOrDefault();

                    var simplifiedQuote = new SimplifiedSymbolQuotes
                    {
                        Symbol_ID        = stock.Id,
                        Symbol           = stock.symbol,
                        PercentVariation = intradiaryInfo.ChangePercent,
                        Variation        = intradiaryInfo.Change > 0 ? 1 : intradiaryInfo.Change < 0 ? -1 : 0,
                        VariationValue   = Math.Round(intradiaryInfo.Change, 2),
                        CurrentAskPrice  = intradiaryInfo.LastTradePrice,
                        Opening          = intradiaryInfo.Opening
                    };

                    if (lastQuote != null)
                    {
                        simplifiedQuote.Minimum = lastQuote.minimun;
                        simplifiedQuote.Maximum = lastQuote.maximun;
                    }

                    list_result.Add(simplifiedQuote);
                }

                if (!(entities.Database.Connection.State == ConnectionState.Closed))
                {
                    entities.Database.Connection.Close();
                }
            }

            return(list_result);
        }
Beispiel #4
0
        private static ctaCOMMON.Charts.Symbol GetSymbol(int portfolio_id, int symbol_id, ChartRange chartRange, CandelRange candelRange, bool withQuotes)
        {
            if (DashBoardCache != null && DashBoardCache.DashboardItems.Count > 0 && false)
            {
                var symbol = DashBoardCache.DashboardItems.Where(x => x.Portfolio_Id == portfolio_id)
                             .SelectMany(x => x.Symbols)
                             .Where(x => x.Symbol_ID == symbol_id)
                             .FirstOrDefault();

                return(symbol);
            }
            else
            {
                ctaCOMMON.Charts.Symbol symbol = new ctaCOMMON.Charts.Symbol();

                using (ctaDBEntities entities = new ctaDBEntities())
                {
                    entities.Database.Connection.Open();
                    string user_type    = (portfolio_id == 0) ? "FREE" : entities.Portfolios.Where(p => p.Id == portfolio_id).Select(p => p.Tenant.Tenant_Type.Name).First();
                    var    stock_entity = entities.Stocks.Where(s => s.Id == symbol_id).First();

                    symbol.Portfolio_ID        = portfolio_id;
                    symbol.Symbol_ID           = stock_entity.Id;
                    symbol.Symbol_Name         = stock_entity.symbol;
                    symbol.Symbol_Company_Name = stock_entity.name;
                    symbol.Symbol_Market_ID    = stock_entity.market_id;
                    symbol.Symbol_Market       = stock_entity.Market.name;
                    symbol.Intradiary_Info     = QuotesService.GetSymbolIntradiaryInfo(symbol_id);
                    if (withQuotes)
                    {
                        symbol.Quotes = QuotesService.GetSymbolQuotes(symbol.Symbol_ID, chartRange, candelRange, user_type);
                    }

                    entities.Database.Connection.Close();
                }

                return(symbol);
            }
        }
Beispiel #5
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);
        }
Beispiel #6
0
        public static string ReadMarketIntradiaryData(string market_name, int market_id, int utc_time_offset, out bool error, params string[] otherParams)
        {
            error = false;
            DateTime date = DateTime.UtcNow.AddHours(utc_time_offset);

            string status     = "O";
            bool   marketOpen = MarketService.IsMarketOpen(market_id, date, out status);

            if (marketOpen)
            {
                try
                {
                    //switch (market_name)
                    //{
                    //    case "NYSE":
                    //        QuotesFromEOD quotesFromEODny = new QuotesFromEOD();
                    //        quotesFromEODny.SaveQuotesToDB(market_name, market_id, utc_time_offset);
                    //        break;
                    //    case "NASDAQ":
                    //        QuotesFromEOD quotesFromEODna = new QuotesFromEOD();
                    //        quotesFromEODna.SaveQuotesToDB(market_name, market_id, utc_time_offset);
                    //        break;
                    //    case "BCBA":
                    //        QuotesFromBOLSAR quotesFromBolsar = new QuotesFromBOLSAR();
                    //        quotesFromBolsar.SaveQuotesToDB(market_name, market_id, utc_time_offset);
                    //        break;
                    //    default:
                    //        return "Quotes for other markets than NYSE, NASDAQ or BCBA are not implemented";
                    //}
                    return("Quotes Updated");
                }
                catch (Exception ex)
                {
                    error = true;
                    EmailSender.SendErrorUpdatingIntradiary("Message: " + ex.Message + Environment.NewLine + "Source: " + ex.Source + Environment.NewLine + "StackTrace: " + ex.StackTrace + Environment.NewLine + ((ex.InnerException != null) ? "Inner Message: " + ex.InnerException.Message + Environment.NewLine + "Inner Source: " + ex.InnerException.Source + Environment.NewLine + "Inner StackTrace: " + ex.InnerException.StackTrace : ""), market_name, date);
                    return("Error Updating Intradiry: " + ex.Message);
                }
            }
            else
            {
                error = true;
                if (date.Hour == 5 && status == "C")
                {
                    try
                    {
                        QuotesService.ClearIntradiaryDataByMarketID(market_name);
                        //EmailSender.DeletingIntradiaryData("Intradiry Deleted", market_name, date);
                        return("Market Close --> Intradiry Deleted");
                    }
                    catch (Exception ex)
                    {
                        EmailSender.SendErrorClearIntradiaryData("Message: " + ex.Message + Environment.NewLine + "Source: " + ex.Source + Environment.NewLine + "StackTrace: " + ex.StackTrace + Environment.NewLine + ((ex.InnerException != null) ? "Inner Message: " + ex.InnerException.Message + Environment.NewLine + "Inner Source: " + ex.InnerException.Source + Environment.NewLine + "Inner StackTrace: " + ex.InnerException.StackTrace : ""), market_name, date);
                        return("Error deleting Intradiry: " + ex.Message);
                    }
                }
                else
                {
                    //EmailSender.SendMarketCloseEmail(market_name, date, status);
                    return("Market Close");
                }
            }
        }