Files
marketdata/MarketDataLib/Generator/Indicators/NarrowRangeIndicator.cs
2024-02-22 14:52:53 -05:00

48 lines
1.5 KiB
C#

using MarketData.MarketDataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MarketData.Generator.Indicators
{
public class NarrowRangeIndicator
{
private NarrowRangeIndicator()
{
}
// It is assumed that prices are in descending date order. (i.e.) The most recent price is in the lowest index position
public static bool IsNarrowRangeEntry(DateTime date,Prices prices,int dayCount)
{
if(!IsInsideDay(date,prices))return false;
if(!IsNarrowestRange(date,prices,dayCount))return false;
return true;
}
private static bool IsInsideDay(DateTime date,Prices prices)
{
int index=prices.FindIndex(x=>x.Date.Date.Equals(date));
if(-1==index||index+1>=prices.Count)return false;
if(!(prices[index].High<=prices[index+1].High&&prices[index].Low>=prices[index+1].Low))return false;
return true;
}
private static bool IsNarrowestRange(DateTime date,Prices prices,int dayCount)
{
int index=prices.FindIndex(x => x.Date.Date.Equals(date));
double range=GetRange(prices,index);
if(index+dayCount>=prices.Count-1)return false;
for(int dayIndex=index+1;dayIndex<index+dayCount;dayIndex++)
{
double nextRange=GetRange(prices,dayIndex);
if(nextRange<range)return false;
}
return true;
}
private static double GetRange(Prices prices,int index)
{
double range=prices[index].High-prices[index].Low;
return Math.Abs(range);
}
}
}