예제 #1
0
 public static bool IsPrice52WeeksLow(Quote quote)
 {
     var averageVolume = quote.AverageDailyVolume;
     if (averageVolume < 300000)
         return false;
     if (quote.LastTradePrice < quote.YearlyLow)
         return true;
     return false;
 }
 public void GetMessageDetailTest()
 {
     var quote = new Quote("SPPI");
     var listQuotes = new List<Quote>();
     listQuotes.Add(quote);
     Stock.StockFetcher.YahooStockEngine.Fetch(listQuotes);
     var result = MessageDetail.GetMessageDetail(listQuotes.FirstOrDefault());
     Messaging.SendEmailGmail("Stock alert", result);
     //Assert.Fail();
 }
예제 #3
0
 public static StockAnalyzerResult AnalyzeStock(Quote quote)
 {
     var result = new StockAnalyzerResult
         {
             IsPriceChangedDramatically = PriceAnalyzer.IsPriceChangedDramatically(quote),
             IsVolumeAbnormal = VolumeAnalyzer.IsVolumeAbnormal(quote),
             IsPrice52WeeksLow = PriceAnalyzer.IsPrice52WeeksLow(quote)
         };
     return result;
 }
예제 #4
0
 public static bool IsPriceChangedDramatically(Quote quote)
 {
     var price = quote.LastTradePrice;
     var lastClosedPrice = quote.PreviousClose;
     if (lastClosedPrice > price)
     {
         if ((lastClosedPrice - price) * 100 / lastClosedPrice > PricePercentageChangeInt)
             return true;
     }
     return false;
 }
예제 #5
0
        public static bool IsVolumeAbnormal(Quote quote)
        {
            var averageVolume = quote.AverageDailyVolume;
            if (averageVolume < 300000)
                return false;
            var volume = quote.Volume;

            if (volume > averageVolume && averageVolume > 0)
            {
                if (volume / averageVolume > VolumeAbnormalTimes)
                    return true;
            }

            return false;
        }
예제 #6
0
        public static string GetMessageDetail(Quote quote)
        {
            var sb = new StringBuilder();
            sb.Append("<html><body><table>");
            sb.Append("<tr><td>");
            sb.Append(string.Format(YahooFinanceLink, quote.Symbol.Trim()));
            sb.Append("</td></tr></table>");
            sb.Append("<table border=\"1\" style=\"border-collapse : collapse; border : 1px solid orange;\"><tr>");
            var i = DisplayColumns;
            foreach (PropertyInfo prop in typeof(Quote).GetProperties())
            {
                sb.Append(string.Format("<td>{0}</td><td>{1}</td>", DisplayNameHelper.GetDisplayName(prop), prop.GetValue(quote, null)));
                if (i % ((DisplayColumns - 1) == 0 ? 1 : (DisplayColumns - 1)) == 0)
                    sb.Append("</tr><tr>");
                i++;
            }
            if (i % ((DisplayColumns - 1) == 0 ? 1 : (DisplayColumns - 1)) == 1)
                sb.Append("<td></td><td></td>");

            sb.Append("</tr>");
            sb.Append("</table></body></html>");
            return sb.ToString();
        }
        public static void ScanStocks()
        {
            //Log.Error(typeof(CollectDataManager), "Test 1");
            //Log.Error(typeof(CollectDataManager),"Test tets");
            //Log.Error(typeof(CollectDataManager), "Test tets 123", new Exception("Failed to test"));
            var engine = new YahooStockEngine();

            var quoteList = new ObservableCollection<Quote>();
            var boList = new List<CompanyList>();
            using(var db=new TheFishEntities())
            {
                 boList = db.CompanyLists.Where(x=>x.Sector.Equals("Health care",StringComparison.OrdinalIgnoreCase) && x.Symbol.Length<5 ).ToList();
            }

            int i = 1;
            var quoteSingleCollectionChunk = new List<Quote>();
            foreach(var item in boList)
            {
                //if (i > 2)
                //    break;
                try
                {

                    var quote = new Quote(item.Symbol.Trim());
                    quoteSingleCollectionChunk.Add(quote);
                    if (i == StockFetchTrunk)
                    {
                        YahooStockEngine.Fetch(quoteSingleCollectionChunk);
                        //YahooStockDownoader.GetQuote(quoteSingleCollectionChunk);
                        foreach (var stockInfo in quoteSingleCollectionChunk)
                            quoteList.Add(stockInfo);
                        quoteSingleCollectionChunk = new List<Quote>();
                        i = 1;
                    }
                    i++;
                }catch(Exception ex)
                {
                    var message = ex.Message;
                }
                //i++;
            }

            try
            {
                if (quoteSingleCollectionChunk.Count > 0)
                {
                    YahooStockEngine.Fetch(quoteSingleCollectionChunk);
                    //YahooStockDownoader.GetQuote(quoteSingleCollectionChunk);
                    foreach (var stockInfo in quoteSingleCollectionChunk)
                        quoteList.Add(stockInfo);
                }
            }catch(Exception ex)
            {
                var message = ex.Message;
            }

            DateTime today = DateTime.Today;                    // earliest time today
            DateTime tomorrow = DateTime.Today.AddDays(1);      // earliest time tomorrow

            using (var db = new TheFishEntities())
            {
                foreach(var item in quoteList.ToList())
                {
                    try
                    {
                        var result = StockAnalyzer.AnalyzeStock(item);
                        var isPriceChangeFish = result.IsPriceChangedDramatically;
                        var isVolumeChangeFish = result.IsVolumeAbnormal;
                        var isPrice52WeeksLow = result.IsPrice52WeeksLow;
                        if (!(isPriceChangeFish || isVolumeChangeFish || isPrice52WeeksLow))
                            continue;
                        if (
                            db.CaughtFish.Where(
                                x => x.Symbol.Equals(item.Symbol) && x.WhenCreated > today && x.WhenCreated < tomorrow)
                              .Any())
                            continue;
                        var caughtFish = new CaughtFish();
                        caughtFish.Symbol = item.Symbol;
                        caughtFish.WhenCreated = DateTime.Now;
                        caughtFish.Price = item.LastTradePrice;
                        caughtFish.PriceChangePercentage = item.ChangeInPercent;
                        caughtFish.Volume = item.Volume;
                        if (item.AverageDailyVolume > 0 && item.Volume > 0)
                            caughtFish.VolumeChangePercentage =
                                (int) (0.5M + 100M*(item.Volume - item.AverageDailyVolume)/item.AverageDailyVolume);
                        var message = "";
                        var subject = "";
                        if (isPriceChangeFish)
                        {
                            caughtFish.FishType = 0;
                            message = string.Format(MessageText, "Price Change Alert -- ", caughtFish.Symbol,
                                                    caughtFish.Price.ToString(),
                                                    caughtFish.PriceChangePercentage.ToString(),
                                                    caughtFish.Volume.ToString(), caughtFish.VolumeChangePercentage);
                            subject = " Price Drop Alert -- " + caughtFish.Symbol;
                        }
                        else if (isVolumeChangeFish)
                        {
                            caughtFish.FishType = 1;
                            message = string.Format(MessageText, "Volume Change Alert -- ", caughtFish.Symbol,
                                                    caughtFish.Price.ToString(),
                                                    caughtFish.PriceChangePercentage.ToString(),
                                                    caughtFish.Volume.ToString(),
                                                    caughtFish.VolumeChangePercentage.ToString());
                            subject = " Volumne Change Alert -- " + caughtFish.Symbol;
                        }
                        else if (isPrice52WeeksLow)
                        {
                            caughtFish.FishType = 1;
                            message = string.Format(MessageText, "52 Weeks low price Alert -- ", caughtFish.Symbol,
                                                    caughtFish.Price.ToString(),
                                                    caughtFish.PriceChangePercentage.ToString(),
                                                    caughtFish.Volume.ToString(),
                                                    caughtFish.VolumeChangePercentage.ToString());
                            subject = " 52 Weeks Low Alert -- " + caughtFish.Symbol;
                        }
                        db.CaughtFish.Add(caughtFish);
                        db.SaveChanges();
                        Messaging.SendEmailGmail(subject, MessageDetail.GetMessageDetail(item));
                    }
                    catch (Exception ex)
                    {
                        StaticLog.Error(ex);
                    }
                }
            }
        }
        public static void GetQuote(List<Quote> quotes)
        {
            // Set the return string to null.
            var quoteList = new List<Quote>();
            try
            {
                // Use Yahoo finance service to download stock data from Yahoo
                var symbols = "";
                foreach (var symbol in quotes)
                    symbols += symbol.Symbol + ",";
                string yahooURL = @"http://download.finance.yahoo.com/d/quotes.csv?s=" +
                                  symbols + "&f=,l1c6k2oghjkva2j1";

                // Initialize a new WebRequest.
                HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(yahooURL);
                // Get the response from the Internet resource.
                HttpWebResponse webresp = (HttpWebResponse)webreq.GetResponse();
                // Read the body of the response from the server.
                var quoteDict = new Dictionary<string, Quote>();
                using (var strm = new StreamReader(webresp.GetResponseStream(), Encoding.ASCII))
                {
                    // Construct a XML in string format.
                    foreach (var item in quotes)
                    {
                        try
                        {
                            if (item.Symbol.Trim() == "")
                                continue;
                            var content = strm.ReadLine().Replace("\"", "");
                            string[] contents = content.ToString().Split(',');
                            // If contents[2] = "N/A". the stock symbol is invalid.
                            if (contents[2] == "N/A")
                            {
                                continue;
                            }
                            else
                            {
                                var quoteFromDownloader = new Quote(contents[0]);

                                quoteFromDownloader.Symbol = contents[0];
                                quoteFromDownloader.YearlyLow = Convert.ToDecimal(contents[6]);
                                quoteFromDownloader.YearlyHigh = Convert.ToDecimal(contents[7]);
                                quoteDict.Add(quoteFromDownloader.Symbol, quoteFromDownloader);
                            }
                        }
                        catch (Exception ex)
                        {
                            StaticLog.Error(ex);
                        }
                    }
                }
                foreach (var item in quotes)
                {
                    if (quoteDict.ContainsKey(item.Symbol))
                    {
                        item.YearlyLow = quoteDict[item.Symbol].YearlyLow;
                        item.YearlyHigh = quoteDict[item.Symbol].YearlyHigh;
                    }
                }
            }
            catch(Exception ex)
            {
                StaticLog.Error(ex);
            }
            // Return the stock quote data in XML format.
        }