[CODE]using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
namespace WealthLab.Strategies
{
public class Hurst1 : WealthScript
{
private StrategyParameter winLen;
public Hurst1()
{
this.winLen = CreateParameter("WinLength", 100, 20, 1000, 1);
}
protected sealed override void Execute()
{
int winLen = this.winLen.ValueInt;
if (winLen > Bars.Count - 1)
throw new ArgumentOutOfRangeException(@"Длина окна не должна превышать Bars.Count");
DataSeries HurstSeries = new DataSeries(Bars, "HurstSeries(" + Bars.Symbol + "," + this.winLen + ")");
HurstSeries.FirstValidValue = winLen;
double[] h = new double[Bars.Count];
h[0] = 0;
for (int i = 1; i < Bars.Count; i++)
h = Math.Log(Bars.Close / Bars.Close[i - 1]);
for (int bar = winLen; bar < Bars.Count; bar++)
{
int startBar = bar - (winLen - 1);
double sum = 0d;
double sumofsquares = 0d;
//MLS:
double c1, c2, g1, g2;
c1 = c2 = g1 = g2 = 0d;
const int first_n = 8;
int mn = winLen - first_n;
for (int n = first_n; n < winLen; n++)
{
int currentBar = startBar + n;
//Рассчитываем среднее mean и СКО Sn на текущем окне
if (n == first_n)
{
for (int i = startBar; i <= startBar + first_n; i++)
{
sum += h;
sumofsquares += h*h;
}
}
else
{
sum += h[currentBar];
sumofsquares += h[currentBar] * h[currentBar];
}
double mean = sum/(n + 1); //Среднее на текущем окне
double Sn = Math.Sqrt(sumofsquares/(n + 1) - mean*mean); //СКО на текущем окне
double Rn = 0d;
double Rmax = Double.MinValue;
double Rmin = Double.MaxValue;
for (int k = startBar; k <= currentBar; k++)
{
Rn += h[k] - mean;
Rmax = Rn > Rmax ? Rn : Rmax;
Rmin = Rn < Rmin ? Rn : Rmin;
}
Rn = Rmax - Rmin;
//MLS:
double lnNn = Math.Log(n + 1);
double Qn = Math.Log(Rn / Sn);
c1 += lnNn * lnNn;
c2 += lnNn;
g1 += lnNn * Qn;
g2 += Qn;
}
//Коэфф. Хёрста равен наклону МНК-прямой:
HurstSeries[bar] = (mn * g1 - c2 * g2) / (mn * c1 - c2 * c2);
if (Double.IsNaN(HurstSeries[bar]))
{
PrintDebug("NaN в вычислениях коэффициента Хёрста!");
HurstSeries[bar] = HurstSeries[bar - 1];
}
else if (Double.IsInfinity(HurstSeries[bar]))
{
PrintDebug("Infinity в вычислениях коэффициента Хёрста!");
HurstSeries[bar] = HurstSeries[bar - 1];
}
}
ChartPane HurstPane = CreatePane(40, true, true);
PlotSeries(HurstPane, HurstSeries, Color.Blue, WealthLab.LineStyle.Solid, 1);
DrawLine(HurstPane, 0, 0.5, Bars.Count - 1, 0.5, Color.Red, WealthLab.LineStyle.Dashed, 1);
SetPaneMinMax(HurstPane, 0, 1);
PrintDebug("MaxValue = " + HurstSeries.MaxValue + "; MinValue = " + HurstSeries.MinValue);
PrintDebug("ContainsInfinity = " + HurstSeries.ContainsInfinity);
}
}
}[/CODE]