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

45 lines
1.5 KiB
C#

using MarketData.MarketDataModel;
using MarketData.Numerical;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MarketData.Generator.Indicators
{
public class PriceTrendIndicatorResult
{
public PriceTrendIndicatorResult(bool isUptrend,double highPriceSlope=double.NaN,double lowPriceSlope=double.NaN)
{
this.IsUpTrend=isUptrend;
this.HighPriceSlope=highPriceSlope;
this.LowPriceSlope=lowPriceSlope;
}
public bool IsUpTrend{get;private set;}
public double HighPriceSlope{get;private set;}
public double LowPriceSlope{get; private set;}
}
public class PriceTrendIndicator
{
private PriceTrendIndicator()
{
}
public static PriceTrendIndicatorResult IsUptrend(Prices prices,int dayCount)
{
if(null==prices||prices.Count<dayCount) return new PriceTrendIndicatorResult(false);
float[] highPrices=prices.GetPricesHigh();
highPrices=highPrices.Take(dayCount).ToArray();
highPrices=Numerics.Reverse(ref highPrices);
double highPricesSlope=Numerics.Slope(highPrices);
if(highPricesSlope<0) return new PriceTrendIndicatorResult(false);
float[] lowPrices=prices.GetPricesLow();
lowPrices=lowPrices.Take(dayCount).ToArray();
lowPrices=Numerics.Reverse(ref lowPrices);
double lowPricesSlope=Numerics.Slope(lowPrices);
if(lowPricesSlope<0) return new PriceTrendIndicatorResult(false);
return new PriceTrendIndicatorResult(true,highPricesSlope,lowPricesSlope);
}
}
}