Пример #1
0
        public static List <StockMarketDaily> ReadInDowJonesFile()
        {
            List <StockMarketDaily> dowJonesDailyList = new List <StockMarketDaily>();

            try
            {
                var lines = File.ReadAllLines("data/dow_jones_data.csv");

                foreach (var line in lines)
                {
                    try
                    {
                        var      lineItems    = line.Split(',');
                        DateTime date         = DateTime.Parse(lineItems[0]);
                        decimal  closingValue = decimal.Parse(lineItems[1]);
                        var      daily        = new StockMarketDaily(date, closingValue);
                        dowJonesDailyList.Add(daily);
                    }
                    catch (Exception e)
                    {
                        // do nothing
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error reading in file");
            }

            return(dowJonesDailyList);
        }
        public override InvestmentAction GetActionForDay(DateTime currentDay, StockMarketHistoricalData data)
        {
            decimal recentLow  = decimal.MaxValue;
            decimal recentHigh = decimal.MinValue;

            DateTime windowDate = currentDay.AddDays(-365);

            while (windowDate.IsBefore(currentDay))
            {
                StockMarketDaily stockMarketForDay = data.GetDataForDate(windowDate);
                if (stockMarketForDay.ClosingValue < recentLow)
                {
                    recentLow = stockMarketForDay.ClosingValue;
                }

                if (stockMarketForDay.ClosingValue > recentHigh)
                {
                    recentHigh = stockMarketForDay.ClosingValue;
                }
                windowDate = windowDate.AddDays(1);
            }
            decimal buyPercentage = 10.0M / 100.0M;
            var     dayData       = data.GetDataForDate(currentDay);
            decimal minBuyRange   = recentHigh - (dayData.ClosingValue * buyPercentage);

            decimal minBuyRangeWithDerivative = recentHigh - (dayData.ClosingValue * buyPercentage * 2);

            decimal sellPercentage = 10.0M / 100.0M;

            decimal maxSellRange = recentLow + (dayData.ClosingValue * sellPercentage);

            if (dayData.ClosingValue > minBuyRange)
            {
                return(InvestmentAction.Buy);
            }
            else if (dayData.ClosingValue < maxSellRange)
            {
                return(InvestmentAction.Sell);
            }
            else
            {
                return(InvestmentAction.NoAction);
            }
        }
Пример #3
0
        private static StockMarketHistoricalData ParseResponse(string json)
        {
            List <StockMarketDaily> dailyValues = new List <StockMarketDaily>();

            var result = JsonConvert.DeserializeObject <JsonStockResponse>(json);

            var dictionary = result.TimeSeriesDaily;

            foreach (var key in dictionary.Keys)
            {
                var date       = DateTime.Parse(key);
                var day        = dictionary[key];
                var closeValue = decimal.Parse(day.AdjustedClose);
                var daily      = new StockMarketDaily(new DateTime(date.Year, date.Month, date.Day), closeValue);
                dailyValues.Add(daily);
            }

            return(new StockMarketHistoricalData(dailyValues));
        }