Ejemplo n.º 1
0
 public LinesController(int count, int percision = 2)
 {
     Averager  = new AverageValue(count, percision);
     Lines     = new List <AverageLine>();
     TimeLines = new Dictionary <LineType, TimeLine <KeyValuePair <DateTime, decimal> > >();
     LineMaker = new LineMaker(1000);
 }
Ejemplo n.º 2
0
    public static IEnumerable <double> AverageWindow(this IEnumerable <double> samples, int WindowLength)
    {
        if (samples is null)
        {
            throw new ArgumentNullException(nameof(samples));
        }
        switch (WindowLength)
        {
        case < 1: throw new ArgumentOutOfRangeException(nameof(WindowLength), WindowLength, "Длина окна должна быть больше 0");

        case 1:
        {
            foreach (var sample in samples)
            {
                yield return(sample);
            }
            yield break;
        }
        }

        var average = new AverageValue(WindowLength);

        foreach (var sample in samples)
        {
            yield return(average.AddValue(sample));
        }
    }
Ejemplo n.º 3
0
        public void Ctor_StartValue_Test()
        {
            var start_value = rnd.NextDouble() * 100;

            var average = new AverageValue(start_value);

            Assert.AreEqual(start_value, average.Value);
            Assert.AreEqual(start_value, average.StartValue);
            Assert.AreEqual(1, average.ValuesCount);
            Assert.AreEqual(-1, average.Length);
        }
Ejemplo n.º 4
0
        public void Ctor_Length_Test()
        {
            var length = rnd.Next(5, 15);

            var average = new AverageValue(length);

            Assert.AreEqual(length, average.Length);
            Assert.AreEqual(0, average.ValuesCount);
            Assert.AreEqual(0, average.Value);
            Assert.IsTrue(double.IsNaN(average.StartValue));
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            AverageValue average = delegate(double a, double b, double c)
            {
                return((a + b + c) / 3);
            };

            var ave = average(3, 3, 4);

            Console.WriteLine(ave);

            Console.ReadKey();
        }
Ejemplo n.º 6
0
        public void AddValue_Test()
        {
            const int count   = 10;
            var       data    = Enumerable.Range(0, count).Select(n => (double)n).ToArray();
            var       average = data.Average();

            var average_value = new AverageValue();

            foreach (var v in data)
            {
                average_value.AddValue(v);
            }

            Assert.AreEqual(average, average_value.Value);
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;

            AverageValue myAverageValue = delegate(int x, int y, int z)
            {
                return((x + y + z) / (double)3);
            };

            var result = myAverageValue(1, 9, 13);

            Console.WriteLine(result);

            Console.ReadKey();
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="circuit">the circuit to simulate</param>
 /// <param name="signalList">signals to stimulate the circuit with</param>
 public Simulation(Circuit circuit, SignalList signalList)
 {
     m_Timer = new Timer(50);
     //leave this false to prevent a step to take action when another one is still processing
     m_Timer.AutoReset = false;
     m_Timer.Elapsed  += new ElapsedEventHandler(SimulationTimerElapsed);
     m_Circuit         = circuit;
     m_EditorSignals   = signalList;
     //signalList.OnListChanged
     m_SignalList   = new SignalList();
     m_CurrentStep  = 0;
     m_StepTimeSpan = new AverageValue();
     GenerateSignals();
     SimulationState = SimulationState.Ready;
 }
        public static void FindAverageValue()
        {
            Console.WriteLine(new string('-', 30));
            Console.WriteLine("Enter first number: ");
            int i = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter second number: ");
            int j = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter third number: ");
            int k = Convert.ToInt32(Console.ReadLine());

            AverageValue averageValue = (a, b, c) => (double)(a + b + c) / 3;

            Console.WriteLine("Average value of entered numbers equals {0:##.###}", averageValue(i, j, k));
        }
Ejemplo n.º 10
0
        private AverageValue HandleEvents(IEnumerable <Event <double> > events)
        {
            var averageValue = new AverageValue();

            foreach (var e in events)
            {
                if (e.IsAddEvent)
                {
                    averageValue += e.Value;
                }
                else
                {
                    averageValue -= e.Value;
                }
            }
            return(averageValue);
        }
Ejemplo n.º 11
0
        public void RollingAverage_Test()
        {
            const int count = 10;
            var       data  = Enumerable.Range(0, count).Select(n => (double)n).ToArray();

            var average_value = new AverageValue();

            var expected = new double[data.Length];
            var actual   = new double[data.Length];

            for (var i = 0; i < data.Length; i++)
            {
                actual[i]   = average_value.AddValue(data[i]);
                expected[i] = data.Take(i + 1).Average();
            }

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 12
0
        public static IEnumerable <float> RollingAverage([NotNull] this IEnumerable <float> collection, int length)
        {
            var average = new AverageValue(length);

            return(collection.Select(value => (float)average.AddValue(value)));
        }
Ejemplo n.º 13
0
        private void CalculateDaily(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return;
            }
            var scripts     = Config.Instance.INFO.ScriptSetting.Scripts;
            var macdsetting = Config.Instance.INFO.MACDSetting;
            var rsisetting  = Config.Instance.INFO.RSISetting;

            // Delete MACD, RSI values
            if ((CheckTable <DBStkMACDEntity>() && scripts.ContainsKey("DEL_MACD_BY_CD")) &&
                (CheckTable <DBStkRSIEntity>() && scripts.ContainsKey("DEL_RSI_BY_CD")))
            {
                var lprm_macd = accessor.CreateParameter("CODE", code);
                var lcmd_macd = accessor.CreateCommand(scripts["DEL_MACD_BY_CD"], new List <DbParameter>()
                {
                    lprm_macd
                });
                var lprm_rsi = accessor.CreateParameter("CODE", code);
                var lcmd_rsi = accessor.CreateCommand(scripts["DEL_RSI_BY_CD"], new List <DbParameter>()
                {
                    lprm_rsi
                });
                if (accessor.ExecuteSQLCommand(new List <DbCommand>()
                {
                    lcmd_macd, lcmd_rsi
                }) < 0)
                {
                    logger.Write(TYPE.ERROR, string.Format("delete MACD and RSI data failed.({0})", code));
                    logger.Write(TYPE.ERROR, accessor.LastError);
                    return;
                }
            }
            else
            {
                return;
            }

            // Retrieve Stocks
            var page = new EntityPage <DBTStkDailyEntity>(
                new Clause("Code = {Code}").AddParam("Code", code),
                new Sort().Add("ID", Sort.Orientation.asc), 100, accessor);
            List <DBTStkDailyEntity> lst_daily = null; DateTime ldt_date = DateTime.Today;
            MACD macd = null; RSI rsi = null; AverageValue daily30 = null;

            #region Cache For MACD
            SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache1 = null;
            SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache2 = null;
            LinearList <KeyValuePair <DateTime, decimal> > list1 = null;
            LinearList <KeyValuePair <DateTime, decimal> > list2 = null;
            #endregion
            #region Cache For AVG
            SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache3 = null;
            SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache4 = null;
            LinearList <KeyValuePair <DateTime, decimal> > list3 = null;
            LinearList <KeyValuePair <DateTime, decimal> > list4 = null;
            #endregion
            Func <KeyValuePair <DateTime, decimal>, decimal> fValue = (pair) => { return(pair.Value); };
            Func <KeyValuePair <DateTime, decimal>, KeyValuePair <DateTime, decimal>, LinearList <KeyValuePair <DateTime, decimal> >, bool> fSwitch1 = (v1, v2, segment) => {
                switch (segment.Orietion)
                {
                case 0:
                    return(false);

                case 1:
                    if (v2.Value >= v1.Value)
                    {
                        return(false);
                    }
                    break;

                case -1:
                    if (v2.Value <= v1.Value)
                    {
                        return(false);
                    }
                    break;
                }
                return(true);
            };
            Func <LinearList <KeyValuePair <DateTime, decimal> >, LinearList <KeyValuePair <DateTime, decimal> >, bool> fSwitch2 = (values, segment) =>
            {
                if (segment.Orietion == 0 || segment.Orietion == values.Orietion)
                {
                    return(false);
                }
                return(values.Count >= 10);
            };
            int pageno = 0; var count = 0;
            while ((lst_daily = page.Retrieve(++pageno)).Count > 0)
            {
                for (int i = 0; i < lst_daily.Count; i++, count++)
                {
                    try
                    {
                        var daily = lst_daily[i];
                        ldt_date = daily.Date;
                        if (pageno == 1 && i == 0)
                        {
                            macd    = new MACD(daily.Close);
                            rsi     = new RSI(rsisetting.N1, rsisetting.N2, rsisetting.N3, daily.Close);
                            daily30 = new AverageValue(30, 4);
                            daily30.Add(daily.Close);
                            cache1 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >(
                                new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) },
                                fSwitch1, null);
                            cache2 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >(
                                new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) },
                                null, fSwitch2);

                            cache3 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >(
                                new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) },
                                fSwitch1, null);
                            cache4 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >(
                                new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) },
                                null, fSwitch2);
                        }
                        else
                        {
                            macd.Add(daily.Close);
                            rsi.Add(daily.Close);
                            daily30.Add(daily.Close);
                            if (cache1.Add(new KeyValuePair <DateTime, decimal>(daily.Date, macd.DEA), out list1))
                            {
                                cache2.Add(list1, out list2);
                            }
                            if (cache3.Add(new KeyValuePair <DateTime, decimal>(daily.Date, daily30.Average), out list3))
                            {
                                cache4.Add(list3, out list4);
                            }
                        }

                        var lent_macd = new DBStkMACDEntity(daily.ID);
                        lent_macd.EMA12 = macd.EMA12;
                        lent_macd.EMA26 = macd.EMA26;
                        lent_macd.DIFF  = macd.DIFF;
                        lent_macd.DEA   = macd.DEA;
                        lent_macd.BAR   = macd.BAR;

                        var lent_rsi = new DBStkRSIEntity(daily.ID);
                        lent_rsi.RSI1       = rsi.RSI1;
                        lent_rsi.RSI2       = rsi.RSI2;
                        lent_rsi.RSI3       = rsi.RSI3;
                        lent_rsi.RSI1MaxEma = rsi.RSI1MaxEma;
                        lent_rsi.RSI1ABSEma = rsi.RSI1ABSEma;
                        lent_rsi.RSI2MaxEma = rsi.RSI2MaxEma;
                        lent_rsi.RSI2ABSEma = rsi.RSI2ABSEma;
                        lent_rsi.RSI3MaxEma = rsi.RSI3MaxEma;
                        lent_rsi.RSI3ABSEma = rsi.RSI3ABSEma;

                        accessor.SaveEntity(lent_macd, lent_rsi);
                        accessor.Commit();
                    }
                    catch { }
                }
            }
            if (count > 120)
            {
                cache2.Add(cache1.Segment, out list2);
                cache4.Add(cache3.Segment, out list4);

                var lent_sum = new DBStkSummaryResultEntity();
                lent_sum.Code          = code;
                lent_sum.Time          = DateTime.Now;
                lent_sum.PAVG30_ORIENT = cache4.Segment.Orietion;
                lent_sum.PAVG30_DAYS   = cache4.Segment.Count;
                lent_sum.PAVG30_VALUE  = cache4.Segment.Slope;
                lent_sum.PAVG30_Date1  = cache4.Segment[0].Key;
                lent_sum.PAVG30_Date2  = cache4.Segment[cache4.Segment.Count - 1].Key;

                lent_sum.DEA_ORIENT = cache2.Segment.Orietion;
                lent_sum.DEA_DAYS   = cache2.Segment.Count;
                lent_sum.DEA_VALUE  = cache2.Segment.Slope;
                lent_sum.DEA_Date1  = cache2.Segment[0].Key;
                lent_sum.DEA_Date2  = cache2.Segment[cache2.Segment.Count - 1].Key;

                lent_sum.RSI_Date   = ldt_date;
                lent_sum.RSI_VALUE1 = rsi.RSI1;
                lent_sum.RSI_VALUE2 = rsi.RSI2;
                lent_sum.RSI_VALUE3 = rsi.RSI3;

                if (CheckTable <DBStkSummaryResultEntity>())
                {
                    accessor.SetDBAccessor2(lent_sum);
                    lent_sum.GenerateID();
                    lent_sum.Save();
                }
            }
            if (page != null)
            {
                page.Dispose();
            }
            if (cache1 != null)
            {
                cache1.Dispose();
            }
            if (cache2 != null)
            {
                cache2.Dispose();
            }
            if (cache3 != null)
            {
                cache3.Dispose();
            }
            if (cache4 != null)
            {
                cache4.Dispose();
            }
            if (daily30 != null)
            {
                daily30.Dispose();
            }
        }