170 lines
7.3 KiB
C#
170 lines
7.3 KiB
C#
using KLHZ.Trader.Core.Declisions.Models;
|
|
|
|
namespace KLHZ.Trader.Core.Declisions.Utils
|
|
{
|
|
internal static class HistoryProcessingInstruments
|
|
{
|
|
internal static PeriodPricesInfo GetPriceDiffForTimeSpan(this PriceHistoryCacheUnit unit, TimeSpan timeShift, TimeSpan timeSpan)
|
|
{
|
|
var res = new PeriodPricesInfo(false, 0, 0, 0, 0, 0, timeSpan, 0);
|
|
var data = unit.GetData();
|
|
var times = data.timestamps;
|
|
var prices = data.prices;
|
|
if (times.Length < 2) return res;
|
|
var lastPriceTime = times[times.Length - 1];
|
|
var intervalEnd = lastPriceTime - timeShift;
|
|
var intervalStart = intervalEnd - timeSpan;
|
|
var max = float.MinValue;
|
|
var min = float.MaxValue;
|
|
|
|
var intervaEndIndex = -1;
|
|
var intervaStartIndex = -1;
|
|
for (int i = times.Length - 1; i > -1; i--)
|
|
{
|
|
if (times[i] <= intervalEnd && intervaEndIndex < 0)
|
|
{
|
|
intervaEndIndex = i;
|
|
}
|
|
|
|
if (prices[i] > max && intervaEndIndex >= 0)
|
|
{
|
|
max = prices[i];
|
|
}
|
|
if (prices[i] < min && intervaEndIndex >= 0)
|
|
{
|
|
min = prices[i];
|
|
}
|
|
if (times[i] <= intervalStart && intervaStartIndex < 0)
|
|
{
|
|
intervaStartIndex = i;
|
|
if (intervaStartIndex != intervaEndIndex && intervaEndIndex >= 0)
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (intervaStartIndex >= 0 && intervaEndIndex >= 0)
|
|
{
|
|
res = new PeriodPricesInfo(
|
|
true,
|
|
prices[intervaStartIndex],
|
|
prices[intervaEndIndex],
|
|
prices[intervaEndIndex] - prices[intervaStartIndex],
|
|
min,
|
|
max,
|
|
timeSpan, intervaEndIndex - intervaStartIndex);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
internal static bool CheckStable(this PeriodPricesInfo data, float meanfullDiff)
|
|
{
|
|
meanfullDiff = Math.Abs(meanfullDiff);
|
|
return data.Success && Math.Abs(data.PeriodDiff) < 1.5 * meanfullDiff && Math.Abs(data.PeriodMax - data.PeriodMin) < 2 * meanfullDiff;
|
|
}
|
|
|
|
internal static bool CheckGrowing(this PeriodPricesInfo data, float meanfullDiff)
|
|
{
|
|
return meanfullDiff > 0 && data.Success && data.PeriodDiff > meanfullDiff && Math.Abs(data.PeriodMax - data.PeriodMin) < 3 * Math.Abs(data.PeriodDiff);
|
|
}
|
|
|
|
internal static bool CheckFalling(this PeriodPricesInfo data, float meanfullDiff)
|
|
{
|
|
meanfullDiff = -meanfullDiff;
|
|
return meanfullDiff < 0 && data.Success && data.PeriodDiff < meanfullDiff && Math.Abs(data.PeriodMax - data.PeriodMin) < 3 * Math.Abs(data.PeriodDiff);
|
|
}
|
|
|
|
internal static float CalcTrendRelationAbs(PeriodPricesInfo first, PeriodPricesInfo second)
|
|
{
|
|
var k1 = Math.Abs(first.PeriodDiff) / Math.Abs(first.Period.TotalSeconds);
|
|
var k2 = Math.Abs(second.PeriodDiff) / Math.Abs(second.Period.TotalSeconds);
|
|
if (k2 == 0 && k1 != 0) return 1000;
|
|
return (float)(k1 / k2);
|
|
}
|
|
|
|
internal static bool CheckDowntrendEnding(this PriceHistoryCacheUnit unit, TimeSpan firstPeriod, TimeSpan secondPeriod, float meanfullDiff)
|
|
{
|
|
var totalDiff = unit.GetPriceDiffForTimeSpan(TimeSpan.Zero, firstPeriod + secondPeriod);
|
|
var startDiff = unit.GetPriceDiffForTimeSpan(secondPeriod, firstPeriod);
|
|
var endDiff = unit.GetPriceDiffForTimeSpan(TimeSpan.Zero, secondPeriod);
|
|
var isEndStable = endDiff.CheckStable(meanfullDiff);
|
|
var isEndGrown = endDiff.CheckGrowing(meanfullDiff);
|
|
var isStartFalls = startDiff.CheckFalling(meanfullDiff);
|
|
var isTotalFalls = totalDiff.CheckFalling(meanfullDiff);
|
|
|
|
var trendRelation = CalcTrendRelationAbs(startDiff, endDiff);
|
|
var res = totalDiff.Success && isStartFalls && (isEndStable || isEndGrown) && trendRelation >= 2;
|
|
|
|
if (startDiff.Success)
|
|
{
|
|
|
|
}
|
|
return res;
|
|
}
|
|
|
|
internal static bool CheckUptrendEnding(this PriceHistoryCacheUnit unit, TimeSpan firstPeriod, TimeSpan secondPeriod, float meanfullDiff)
|
|
{
|
|
var totalDiff = unit.GetPriceDiffForTimeSpan(TimeSpan.Zero, firstPeriod + secondPeriod);
|
|
var startDiff = unit.GetPriceDiffForTimeSpan(secondPeriod, firstPeriod);
|
|
var endDiff = unit.GetPriceDiffForTimeSpan(TimeSpan.Zero, secondPeriod);
|
|
var isEndStable = endDiff.CheckStable(meanfullDiff);
|
|
var isEndFalls = endDiff.CheckFalling(meanfullDiff);
|
|
var isStartGrows = startDiff.CheckGrowing(meanfullDiff);
|
|
|
|
var trendRelation = CalcTrendRelationAbs(startDiff, endDiff);
|
|
var isEndLocal = endDiff.PeriodDiff == 0 && endDiff.Count == 2;
|
|
var res = totalDiff.Success && isStartGrows && (isEndStable || isEndFalls) && (trendRelation >= 2 && !isEndLocal);
|
|
|
|
if (res)
|
|
{
|
|
|
|
}
|
|
return res;
|
|
}
|
|
|
|
internal static bool CheckDowntrendStarting(this PriceHistoryCacheUnit unit, TimeSpan firstPeriod, TimeSpan secondPeriod, float meanfullDiff)
|
|
{
|
|
var totalDiff = unit.GetPriceDiffForTimeSpan(TimeSpan.Zero, firstPeriod + secondPeriod);
|
|
var startDiff = unit.GetPriceDiffForTimeSpan(secondPeriod, firstPeriod);
|
|
var endDiff = unit.GetPriceDiffForTimeSpan(TimeSpan.Zero, secondPeriod);
|
|
|
|
|
|
var isEndFalls = endDiff.CheckFalling(meanfullDiff);
|
|
var isStartStable = startDiff.CheckStable(meanfullDiff);
|
|
var isStartGrows = startDiff.CheckGrowing(meanfullDiff);
|
|
|
|
var trendRelation = CalcTrendRelationAbs(endDiff, startDiff);
|
|
return totalDiff.Success && (isStartStable || isStartGrows) && isEndFalls && trendRelation >= 2;
|
|
}
|
|
|
|
internal static bool CheckUptrendStarting(this PriceHistoryCacheUnit unit, TimeSpan firstPeriod, TimeSpan secondPeriod, float meanfullDiff)
|
|
{
|
|
var totalDiff = unit.GetPriceDiffForTimeSpan(TimeSpan.Zero, firstPeriod + secondPeriod);
|
|
var startDiff = unit.GetPriceDiffForTimeSpan(secondPeriod, firstPeriod);
|
|
var endDiff = unit.GetPriceDiffForTimeSpan(TimeSpan.Zero, secondPeriod);
|
|
|
|
|
|
var isEndGrows = endDiff.CheckGrowing(meanfullDiff);
|
|
var isStartStable = startDiff.CheckStable(meanfullDiff);
|
|
var isStartFalls = startDiff.CheckStable(meanfullDiff);
|
|
|
|
var trendRelation = CalcTrendRelationAbs(endDiff, startDiff);
|
|
var res = totalDiff.Success && (isStartStable || isStartFalls) && isEndGrows && endDiff.PeriodDiff > meanfullDiff;
|
|
|
|
if (isStartStable)
|
|
{
|
|
res &= trendRelation >= 2;
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
if (res)
|
|
{
|
|
|
|
}
|
|
return res;
|
|
}
|
|
}
|
|
}
|