using System;
using System.Collections.Generic;
using MarketData.MarketDataModel;
using MarketData.Utils;
using System.Linq;
using MarketData.Numerical;
using System.Text;
namespace MarketData.Generator.MGSHMomentum
{
public class SlopeManager
{
private double bandSlopeSpreadKL5;
private double bandSlopeSpreadKL10;
private double bandSlopeSpreadKL30;
private double bandSlopeSpreadKL60;
private double bandSlopeSpreadKL90;
public SlopeManager(BollingerBandElementsByDate bollingerBandElementsByDate, DateTime analysisDate)
{
bandSlopeSpreadKL5 = GetKLBandSpreadSlope(bollingerBandElementsByDate, analysisDate, 5);
bandSlopeSpreadKL10 = GetKLBandSpreadSlope(bollingerBandElementsByDate, analysisDate, 10);
bandSlopeSpreadKL30 = GetKLBandSpreadSlope(bollingerBandElementsByDate, analysisDate, 30);
bandSlopeSpreadKL60 = GetKLBandSpreadSlope(bollingerBandElementsByDate, analysisDate, 60);
bandSlopeSpreadKL90 = GetKLBandSpreadSlope(bollingerBandElementsByDate, analysisDate, 90);
}
public bool Verbose { get; set; } = true;
///
/// The pattern follows.. 90, 60, 30, 10 ,5
///
/// ++0-0
///
public bool IsMatchKLSpread(String bandStringKLSpread)
{
StringBuilder sbKLSPread = new StringBuilder();
sbKLSPread.Append(GetValue(bandSlopeSpreadKL90)).Append(GetValue(bandSlopeSpreadKL60)).Append(GetValue(bandSlopeSpreadKL30)).Append(GetValue(bandSlopeSpreadKL10)).Append(GetValue(bandSlopeSpreadKL5));
return AreEqual(bandStringKLSpread, sbKLSPread.ToString());
}
public String GetKLSpread()
{
StringBuilder sbKLSPread = new StringBuilder();
sbKLSPread.Append(GetValue(bandSlopeSpreadKL90)).Append(GetValue(bandSlopeSpreadKL60)).Append(GetValue(bandSlopeSpreadKL30)).Append(GetValue(bandSlopeSpreadKL10)).Append(GetValue(bandSlopeSpreadKL5));
return sbKLSPread.ToString();
}
public int FindFirstMatchRtoL(String bandString, char match)
{
for (int index = bandString.Length - 1; index >= 0; index--)
{
if (bandString[index].Equals(match)) {
switch (index) {
case 0:
return 90;
case 1:
return 60;
case 2:
return 30;
case 3:
return 10;
case 4:
return 5;
default:
return 10;
}
}
}
return 10;
}
///
/// Where str1 may have a wild card '?'
///
///
///
///
private bool AreEqual(String str1,String str2)
{
if(str1.Length!=str2.Length)return false;
for(int index=0;index0.00)return "+";
return "-";
}
private double GetKLBandSpreadSlope(BollingerBandElementsByDate bollingerBandElementsByDate, DateTime fromDate, int dayCount)
{
List bollingerBandElements = new List();
DateGenerator dateGenerator = new DateGenerator();
List historicalDates = dateGenerator.GenerateHistoricalDates(fromDate, dayCount);
foreach (DateTime historicalDate in historicalDates) {
if (!bollingerBandElementsByDate.ContainsKey(historicalDate)) {
continue;
}
bollingerBandElements.Add(bollingerBandElementsByDate[historicalDate]);
}
List spreadElements = new List();
foreach (BollingerBandElement bollingerBandElement in bollingerBandElements) {
spreadElements.Add(bollingerBandElement.K - bollingerBandElement.L);
}
double[] elements = spreadElements.ToArray();
elements = Numerics.Reverse(ref elements);
double[] logElements = new double[elements.Length];
for (int index = 0; index < elements.Length; index++) {
logElements[index] = Math.Log(elements[index]);
}
LeastSquaresResultWithR2 leastSquaresResult = LeastSquaresHelper.CalculateLeastSquaresWithR2(logElements);
return leastSquaresResult.Slope;
}
private double GetKBandSlope(BollingerBandElementsByDate bollingerBandElementsByDate,DateTime fromDate, int dayCount)
{
List bollingerBandElements = new List();
DateGenerator dateGenerator = new DateGenerator();
List historicalDates = dateGenerator.GenerateHistoricalDates(fromDate, dayCount);
foreach(DateTime historicalDate in historicalDates)
{
if(!bollingerBandElementsByDate.ContainsKey(historicalDate))
{
continue;
}
bollingerBandElements.Add(bollingerBandElementsByDate[historicalDate]);
}
List bandElements = new List();
foreach(BollingerBandElement bollingerBandElement in bollingerBandElements)
{
bandElements.Add(bollingerBandElement.K);
}
double[] elements = bandElements.ToArray();
elements = Numerics.Reverse(ref elements);
double[] logElements = new double[elements.Length];
for(int index=0;index bollingerBandElements = new List();
DateGenerator dateGenerator = new DateGenerator();
List historicalDates = dateGenerator.GenerateHistoricalDates(fromDate, dayCount);
foreach(DateTime historicalDate in historicalDates)
{
if(!bollingerBandElementsByDate.ContainsKey(historicalDate))
{
continue;
}
bollingerBandElements.Add(bollingerBandElementsByDate[historicalDate]);
}
List bandElements = new List();
foreach(BollingerBandElement bollingerBandElement in bollingerBandElements)
{
bandElements.Add(bollingerBandElement.L);
}
double[] elements = bandElements.ToArray();
elements = Numerics.Reverse(ref elements);
double[] logElements = new double[elements.Length];
for(int index=0;index