private void CheckStop(ViewData data, StockQuote quote /*, decimal dividends*/) { bool showAlert = false; Decimal stopPrice = data.fixedStop; if (data.fixedStop != 0 && data.fixedStop >= quote.close) { showAlert = true; } if (data.stopPercent != 0) { if (data.useTrailing) { Decimal highestClose = data.highestClose; if (quote.highest > data.highestClose) { highestClose = quote.highest; } Decimal referencePrice = highestClose; //if (data.useDividends) // referencePrice -= dividends; if (data.useOptions) { referencePrice -= data.options; } Decimal subtractedPercentage = referencePrice * data.stopPercent / 100; stopPrice = referencePrice - subtractedPercentage; if (quote.close <= stopPrice) { showAlert = true; } } else { Decimal referencePrice = data.buyPrice; //if (data.useDividends) // referencePrice -= data.dividends; if (data.useOptions) { referencePrice -= data.options; } Decimal subtractedPercentage = referencePrice * data.stopPercent / 100; stopPrice = referencePrice - subtractedPercentage; if (quote.close <= stopPrice) { showAlert = true; } } } if (showAlert) { StopAlert alert = new StopAlert { symbol = data.symbol, close = quote.close, stop = stopPrice }; stopAlerts.Add(alert); } }
//private double sumOfDividends = 0; /// <summary> /// Retrieves prices for a given stock/fund from the starting date to the present /// </summary> /// <param name="stock"> Stock symbol </param> /// <param name="start"> Start date </param> /// <returns> true if data received successfully, false if not </returns> public override Task <bool> GetQuote(string stock, DateTime start, out StockQuote quote) { MarketData stockData = AlphaVantage.Stock(stock, AVTimeSeries.Stock_Daily, AVOutputSize.full, AlphaVantageAPIKey); if (stockData == null) { quote.date = DateTime.Today; quote.close = 0; quote.highest = 0; return(Task.FromResult(false)); } int tries = 0; while (stockData.Bars.Count == 0 && tries++ <= 5) { Thread.Sleep(10000); stockData = AlphaVantage.Stock(stock, AVTimeSeries.Stock_Daily, AVOutputSize.full, AlphaVantageAPIKey); if (stockData == null) { quote.date = DateTime.Today; quote.close = 0; quote.highest = 0; return(Task.FromResult(false)); } } if (stockData.Bars.Count == 0) { quote.date = DateTime.Today; quote.close = 0; quote.highest = 0; return(Task.FromResult(false)); } DateTime date = DateTime.Today; double close = 0; double highest = 0; bool first = true; foreach (var item in stockData.Bars) { if (first) { date = item.Key; close = item.Value.close; first = false; } if (item.Value.close > highest) { highest = item.Value.close; } //if (item.Value.dividend_amount != 0) // sumOfDividends += item.Value.dividend_amount; if (item.Key < start) { break; } } quote.date = date; quote.close = (decimal)close; quote.highest = (decimal)highest; return(Task.FromResult(true)); }
/// <summary> /// Retrieves dividends for a given stock/fund from the starting date to the present /// </summary> /// <param name="stock"> Stock symbol </param> /// <param name="start"> Start date </param> /// <returns> true if data received successfully, false if not </returns> //public abstract Task<bool> GetDividends(string stock, DateTime start, out decimal dividends); public abstract Task <bool> GetCryptoQuote(string currency, DateTime start, out StockQuote quote);
/// <summary> /// Retrieves prices for a given stock/fund from the starting date to the present /// </summary> /// <param name="stock"> Stock symbol </param> /// <param name="start"> Start date </param> /// <returns> true if data received successfully, false if not </returns> public abstract Task <bool> GetQuote(string stock, DateTime start, out StockQuote quote);
StockQuote ConvertCSVQuote(string csv) { string[][] table = CsvTextToStringTable(csv, ','); Decimal highestClose = 0, lastClose = 0, close = 0; DateTime lastDate = new DateTime(1970, 1, 1); if (table.Length > 1) { for (int i = 1; i <= table.Length - 1; i++) { if (table[i].Length == 7) { DateTime t1; double t2; double t3; double t4; double t5; double t6; long t7; if (System.DateTime.TryParse(table[i][0], ci, System.Globalization.DateTimeStyles.None, out t1) && double.TryParse(table[i][1], System.Globalization.NumberStyles.Currency, ci, out t2) && double.TryParse(table[i][2], System.Globalization.NumberStyles.Currency, ci, out t3) && double.TryParse(table[i][3], System.Globalization.NumberStyles.Currency, ci, out t4) && double.TryParse(table[i][4], System.Globalization.NumberStyles.Currency, ci, out t5) && double.TryParse(table[i][6], System.Globalization.NumberStyles.Currency, ci, out t6) && long.TryParse(table[i][5], System.Globalization.NumberStyles.Integer, ci, out t7)) { //qd.TradingDate = t1; //qd.Open = t2; //qd.High = t3; //qd.Low = t4; //qd.Close = t5; //qd.CloseAdjusted = t6; //qd.Volume = t7; if (i == 1) { lastDate = t1; lastClose = (Decimal)t5; } close = (Decimal)t5; if (close > highestClose) highestClose = close; //lst.Add(qd); } } } } StockQuote quote = new StockQuote(lastDate, lastClose, highestClose); return quote; }
/// <summary> /// Retrieves prices for a given stock/fund from the starting date to the present /// </summary> /// <param name="stock"> Stock symbol </param> /// <param name="start"> Start date </param> /// <returns> true if data received successfully, false if not </returns> public override bool GetQuote(string stock, DateTime start, out StockQuote quote) { string url = DownloadURL(stock, start, DateTime.Now, false); string msg = String.Format("GetQuote URL = {0}", url); Logger.Log(msg); try { string csvData = client.DownloadString(url); quote = ConvertCSVQuote(csvData); Logger.Log(csvData); if (quote.close == 0 && quote.date.Year == 1970 && quote.highest == 0) return false; } catch (WebException e) { MessageBox.Show("Download quote failed for " + stock + " : " + e.Message); quote = new StockQuote(new DateTime(1900, 1, 1), 0, 0); return false; } return true; }
/// <summary> /// Retrieves prices for a given stock/fund from the starting date to the present /// </summary> /// <param name="stock"> Stock symbol </param> /// <param name="start"> Start date </param> /// <returns> true if data received successfully, false if not </returns> public abstract bool GetQuote(string stock, DateTime start, out StockQuote quote);