diff --git a/KLHZ.Trader.Core.Math/Common/Lines.cs b/KLHZ.Trader.Core.Math/Common/Lines.cs index b116f8a..f932781 100644 --- a/KLHZ.Trader.Core.Math/Common/Lines.cs +++ b/KLHZ.Trader.Core.Math/Common/Lines.cs @@ -13,7 +13,7 @@ public static bool IsLinesCrossing(DateTime time1, DateTime time2, decimal val1_1, decimal val1_2, decimal val2_1, decimal val2_2) { var dtime = (decimal)(time2 - time1).TotalSeconds; - + if (dtime == 0) return false; var dval1 = val1_2 - val1_1; var k1 = dval1 / dtime; var b1 = val1_1; @@ -25,8 +25,11 @@ if (k1 != k2) { var cross = LinesCrossing(k1, b1, k2, b2); - var crossingTimestamp = time1.AddSeconds((double)cross.x); - return crossingTimestamp >= time1 && crossingTimestamp <= time2; + if (cross.x>=0 && cross.x <= dtime) + { + var crossingTimestamp = time1.AddSeconds((double)cross.x); + return crossingTimestamp >= time1 && crossingTimestamp <= time2; + } } return false; } diff --git a/KLHZ.Trader.Core.Tests/MovingAverageTests.cs b/KLHZ.Trader.Core.Tests/MovingAverageTests.cs new file mode 100644 index 0000000..1680478 --- /dev/null +++ b/KLHZ.Trader.Core.Tests/MovingAverageTests.cs @@ -0,0 +1,27 @@ +using KLHZ.Trader.Core.DataLayer.Entities.Prices; +using KLHZ.Trader.Core.Math.Common; +using KLHZ.Trader.Core.Math.Declisions.Services.Cache; +using KLHZ.Trader.Core.Math.Declisions.Utils; + +namespace KLHZ.Trader.Core.Tests +{ + public class MovingAverageTests + { + [Test] + public void Test1() + { + var cacheUnit = new PriceHistoryCacheUnit2(""); + for (int i = 0; i < 5 * PriceHistoryCacheUnit2.CacheMaxLength; i++) + { + cacheUnit.AddData(new PriceChange() { Figi = "", Ticker = "", Value = i, Time = DateTime.UtcNow }); + if (i >= 500) + { + var data = cacheUnit.GetData().Result; + Assert.IsTrue(data.prices.Length == PriceHistoryCacheUnit2.CacheMaxLength); + Assert.IsTrue(data.prices.Last() == i); + var res = MovingAverage.CheckByWindowAverageMean(data.timestamps, data.prices, 100); + } + } + } + } +} \ No newline at end of file