当前位置: 首页>>代码示例>>C#>>正文


C# SortedMap.SMA方法代码示例

本文整理汇总了C#中SortedMap.SMA方法的典型用法代码示例。如果您正苦于以下问题:C# SortedMap.SMA方法的具体用法?C# SortedMap.SMA怎么用?C# SortedMap.SMA使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SortedMap的用法示例。


在下文中一共展示了SortedMap.SMA方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CouldCalculateSMAInRealTime

        public void CouldCalculateSMAInRealTime() {
            var sm = new SortedMap<int, double>();

            Task.Run(async () => {

                for (int i = 0; i < 20; i++) {
                    sm.Add(i, i);
                }

                await Task.Delay(100);

                for (int i = 20; i < 100; i++) {
                    await Task.Delay(1); // 15 msec
                    sm.Add(i, i);
                }
                sm.IsMutable = false;
            });


            var sma = sm.SMA(10, true);

            var c = sma.GetCursor();

            while (c.MoveNext(CancellationToken.None).Result) {
                Console.WriteLine("Key: {0}, value: {1}", c.CurrentKey, c.CurrentValue);
            }

        }
开发者ID:kevmal,项目名称:Spreads,代码行数:28,代码来源:CursorSeriesExtensions.cs

示例2: CouldCalculateIncompleteMovingAverage

        public void CouldCalculateIncompleteMovingAverage() {
            var sm = new SortedMap<int, double>();
            for (int i = 0; i < 20; i++) {
                sm.Add(i, i);
            }

            var sma = sm.SMA(2, true).ToSortedMap();

            var c = 0;
            foreach (var kvp in sma) {
                if (c == 0) {
                    Assert.AreEqual(c, kvp.Value);
                } else {
                    Assert.AreEqual(0.5 * (c + (double)(c - 1)), kvp.Value);
                }

                c++;
            }

        }
开发者ID:kevmal,项目名称:Spreads,代码行数:20,代码来源:CursorSeriesExtensions.cs

示例3: ZipLagIssue11Test

        public void ZipLagIssue11Test() {
            OptimizationSettings.CombineFilterMapDelegates = true;

            var data = new SortedMap<DateTime, double>();

            var count = 5000;

            for (int i = 0; i < count; i++) {
                data.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }

            var sma = data.SMA(20, true).Lag(1u);
            Console.WriteLine($"laggedSma count: {sma.Count()}");

            var deviation = sma;

            // this line or any other intermediate enumeration affect the last line
            Console.WriteLine($"deviation count: {deviation.Count()}");

            var direction = deviation;//.Map(x => (Math.Sign(x)));

            Console.WriteLine($"direction count: {direction.Count()}");

            var diff = direction.ZipLag(1u, (c, p) => c - p); //.ToSortedMap();

            Console.WriteLine($"Count: {diff.Count()}");

            Assert.IsTrue(diff.Count() > 0);
        }
开发者ID:b-e-n-j,项目名称:Spreads,代码行数:29,代码来源:MiscCursorsTests.cs

示例4: CouldCalculateMovingAverageIncomplete

        public void CouldCalculateMovingAverageIncomplete() {
            var sm = new SortedMap<DateTime, double>();

            var count = 100000;

            for (int i = 0; i < count; i++) {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }

            // slow implementation
            var sw = new Stopwatch();
            sw.Start();
            var ma = sm.SMA(20, true); //.ToSortedMap();
            var c = 1;
            foreach (var m in ma) {
                if (c <= 20) {
                    //Console.WriteLine(m.Value);
                } else if (m.Value != c - 19 + 8.5) {
                    Console.WriteLine(m.Value);
                    throw new ApplicationException("Invalid value");
                }
                c++;
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
            // 
        }
开发者ID:b-e-n-j,项目名称:Spreads,代码行数:28,代码来源:MiscCursorsTests.cs

示例5: CouldCalculateMovingAverage

        public void CouldCalculateMovingAverage() {
            var sm = new SortedMap<DateTime, double>();

            var count = 1000000;

            for (int i = 0; i < count; i++) {
                sm.Add(DateTime.UtcNow.Date.AddSeconds(i), i);
            }

            // slow implementation
            var sw = new Stopwatch();
            sw.Start();
            var ma = sm.SMA(20); //.ToSortedMap();

            var cursor = ma.GetCursor();
            cursor.MoveNext();
            //var cc = 0;
            //while (cursor.MoveNext())
            //{
            //    cc++;
            //}
            //Console.WriteLine(cc);
            //if (cursor.MoveNext())
            //{
            //    throw new ApplicationException("Moved next after MoveNext() returned false");
            //}
            //cursor.MoveFirst();
            var c = 1;
            //foreach (var m in ma) {
            cursor.Reset();
            while (cursor.MoveNext()) {
                if (cursor.CurrentValue != c + 8.5)
                {
                    Console.WriteLine(cursor.CurrentValue);// m.Value);
                    Console.WriteLine($"Error c: {c}");
                    throw new ApplicationException("Invalid value");
                }
                c++;
                if (c == 999982)
                {
                    Console.WriteLine("Catch me");
                }
            }
            sw.Stop();
            Console.WriteLine($"Final c: {c}");
            Console.WriteLine("SMA, elapsed: {0}, ops: {1}", sw.ElapsedMilliseconds, (int)((double)count / (sw.ElapsedMilliseconds / 1000.0)));
            ma = null;
            
            GC.Collect(3, GCCollectionMode.Forced, true);
            Thread.Sleep(2000);
            // NB! In release mode this must print that ToSortedMap task exited, in Debug mode GC does not collect SM and weak reference stays alive
             
        }
开发者ID:b-e-n-j,项目名称:Spreads,代码行数:53,代码来源:MiscCursorsTests.cs

示例6: SmaDeviationTest

        public void SmaDeviationTest()
        {
            var data = new SortedMap<DateTime, double>();
            for (int i = 1; i < 101; i++)
            {
                data.Add(DateTime.UtcNow.Date.AddMinutes(i), i);
            }

            var sma = data.SMA(20, true);
            var deviation = (data/sma - 1);
            var deviationSm = deviation;
            var smaDirection = deviation.Map(Math.Sign);
            Assert.AreEqual(100, smaDirection.Count());
            Assert.AreEqual(100, deviation.Count());
            
        }
开发者ID:Applied-Duality,项目名称:Spreads,代码行数:16,代码来源:ReadmeTests.cs


注:本文中的SortedMap.SMA方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。