コード例 #1
0
        public static bool FillSimpleMovingAvg(ref Stock stk, int periodShort = 0, int periodLong = 0)
        {
            //get defaults if zero
            if (periodShort == 0)
            {
                periodShort = stk.indicators.SMAPeriodShort;
            }
            if (periodLong == 0)
            {
                periodLong = stk.indicators.SMAPeriodLong;
            }

            //int valuesAdded = 0;

            IIndicator SMAShort = new SimpleMovingAverage(periodShort);
            IIndicator SMALong  = new SimpleMovingAverage(periodLong);

            stk.indicators.SMAShort = SMAShort.CalculateIndicator(stk.MarketHistory);
            stk.indicators.SMALong  = SMALong.CalculateIndicator(stk.MarketHistory);

            return(true);

            //Dictionary<string, decimal> tempLong = new Dictionary<string, decimal>();
            //Dictionary<string, decimal> tempShort = new Dictionary<string, decimal>();

            //foreach (MarketData m in stk.MarketHistory)
            //{
            //    var lastClosesShort = stk.MarketHistory.GetLastNClosingPrices(periodShort, m.dateStr);
            //    var lastMClosesLong = stk.MarketHistory.GetLastNClosingPrices(periodLong, m.dateStr);

            //    decimal avgShort = lastClosesShort.Average();
            //    decimal avgLong = lastMClosesLong.Average();

            //    //short eval
            //    if (lastClosesShort.Count() == periodShort)
            //    {
            //        try
            //        {
            //            stk.indicators.SMAShort.Add(m.dateStr, avgShort);
            //        }
            //        catch (Exception)
            //        {
            //            stk.indicators.SMAShort[m.dateStr] = avgShort;
            //        }
            //    }
            //    else
            //    {
            //        try
            //        {
            //            stk.indicators.SMAShort.Add(m.dateStr, 0m);
            //        }
            //        catch
            //        {
            //            stk.indicators.SMAShort[m.dateStr] = 0m;
            //        }
            //    }

            //    //long eval
            //    if (lastMClosesLong.Count() == periodLong)
            //    {
            //        try
            //        {
            //            stk.indicators.SMALong.Add(m.dateStr, avgLong);
            //        }
            //        catch (Exception)
            //        {
            //            stk.indicators.SMALong[m.dateStr] = avgLong;
            //        }
            //    }
            //    else
            //    {
            //        try
            //        {
            //            stk.indicators.SMALong.Add(m.dateStr, 0m);
            //        }
            //        catch
            //        {
            //            stk.indicators.SMALong[m.dateStr] = 0m;

            //        }
            //    }

            //    tempLong.Add(m.dateStr, avgLong);
            //    tempShort.Add(m.dateStr, avgShort);

            //    valuesAdded += 2;
            //}

            //return valuesAdded > 0;
        }