inputs: ts1(5),ts2(10),ts3(20),plotROverS(false),plotRS(false),plotT1(false),
plotT2(false),plotT3(false),plotHe(true),t1Color(red),t2Color(green),t3Color(blue),heColor(yellow),
writeToDisk(false),filePath("C:\Users\Brian\Documents\")
;
variables:
nTs(3),
thisMean(0),
thisBar(0),
thisIdx(0),
thisSumDifference(0),
thisMax(0),
thisMin(0),
thisSumDifferenceSquared(0),
R(0),
R1(0),
R2(0),
R3(0),
S(0),
S1(0),
S2(0),
S3(0),
R1OverS1(0),
R2OverS2(0),
R3OverS3(0),
t1H(0),
t2H(0),
t3H(0),
logT1RS(0),
logT2RS(0),
logT3RS(0),
logT1(0),
logT2(0),
logT3(0),
hE(0),
t1OK(false),
t2OK(false),
t3OK(false),
nTSD(0),
fileName("")
;
Arrays:
t1.Array[](0),t2.Array[](0),t3.Array[](0)
;
once begin
Array_SetMaxIndex(t1.Array, ts1+1);
Array_SetMaxIndex(t2.Array, ts2+1);
Array_SetMaxIndex(t3.Array, ts3+1);
fileName = filePath+getsymbolname+"_"+NumToStr(barinterval,0)+"_"+NumToStr(bartype,0)+"_"+"Hurst.csv";end;
if currentBar > ts3+1 then begin
for Value1 = 1 to nTs begin
if Value1 = 1 then begin
Value2 = ts1;
t1OK = false;
for thisBar = 1 To Value2 begin
t1.Array[thisBar-1] = Close[thisBar]-Close[thisBar-1];
end;
thisMean = 0;
for thisBar = 0 To Value2-1 begin
thisMean = thisMean + t1.Array[thisBar];
end;
thisMean = thisMean/Value2;
//Print(D," ",T," t1 Mean ",thisMean:1:7);
Value4 = 0;
thisSumDifference = 0;
thisSumDifferenceSquared = 0;
for thisBar = 0 To Value2-1 begin
//Print(" thisBar ",thisBar," of ",Value2-1);
Value3 = t1.Array[thisBar] - thisMean;
thisSumDifference = thisSumDifference + Value3;
thisSumDifferenceSquared = thisSumDifferenceSquared + Value3 * Value3;
if thisBar = 0 then begin
thisMax = thisSumDifference;
thisMin = thisMax;
end
else begin
if thisSumDifference > thisMax then
thisMax = thisSumDifference;
if thisSumDifference < thisMin then
thisMin = thisSumDifference;
end;
end;
R = thisMax - thisMin;
R1 = R;
S = SquareRoot(thisSumDifferenceSquared/Value2);
S1 = S;
if S1 <> 0 then
R1OverS1 = R1/S1;
t1H = .5;
if S <> 0 then begin
t1OK = true;
Value3 = R/S;
logT1RS = Log(Value3);
logT1 = Log(Value2);
t1H = logT1RS/logT1;
end;
//Print(" t1H ",t1H);
end
else if Value1 = 2 then begin
//Print(" ts2 ");
Value2 = ts2;
t2OK= false;
for thisBar = 1 To Value2 begin
t2.Array[thisBar-1] = Close[thisBar]-Close[thisBar-1];
end;
thisMean = 0;
for thisBar = 0 To Value2-1 begin
thisMean = thisMean + t2.Array[thisBar];
end;
thisMean = thisMean/Value2;
//(D," ",T," t2 Mean ",thisMean:1:7);
Value4 = 0;
thisSumDifference = 0;
thisSumDifferenceSquared = 0;
for thisBar = 0 To Value2-1 begin
//Print(" thisBar ",thisBar," of ",Value2-1);
Value3 = t2.Array[thisBar] - thisMean;
thisSumDifference = thisSumDifference + Value3;
thisSumDifferenceSquared = thisSumDifferenceSquared + Value3 * Value3;
if thisBar = 0 then begin
thisMax = thisSumDifference;
thisMin = thisMax;
end
else begin
if thisSumDifference > thisMax then
thisMax = thisSumDifference;
if thisSumDifference < thisMin then
thisMin = thisSumDifference;
end;
end;
R = thisMax - thisMin;
R2 = R;
S = SquareRoot(thisSumDifferenceSquared/Value2);
S2 = S;
if S2 <> 0 then
R2OverS2 = R2/S2;
t2H = .5;
if S <> 0 then begin
t2OK = true;
Value3 = R/S;
logT2RS = Log(Value3);
logT2 = Log(Value2);
t2H = logT2RS/logT2;
end;
//Print(" t2H ",t2H);
end
else begin
//Print(" ts3 ");
Value2 = ts3;
t3OK = false;
for thisBar = 1 To Value2 begin
t3.Array[thisBar-1] = Close[thisBar]-Close[thisBar-1];
end;
thisMean = 0;
for thisBar = 0 To Value2-1 begin
thisMean = thisMean + t3.Array[thisBar];
end;
thisMean = thisMean/Value2;
//Print(D," ",T," t3 Mean ",thisMean:1:7);
Value4 = 0;
thisSumDifference = 0;
thisSumDifferenceSquared = 0;
for thisBar = 0 To Value2-1 begin
//Print(" thisBar ",thisBar," of ",Value2-1);
Value3 = t3.Array[thisBar] - thisMean;
thisSumDifference = thisSumDifference + Value3;
thisSumDifferenceSquared = thisSumDifferenceSquared + Value3 * Value3;
if thisBar = 0 then begin
thisMax = thisSumDifference;
thisMin = thisMax;
end
else begin
if thisSumDifference > thisMax then
thisMax = thisSumDifference;
if thisSumDifference < thisMin then
thisMin = thisSumDifference;
end;
end;
R = thisMax - thisMin;
R3 = R;
S = SquareRoot(thisSumDifferenceSquared/Value2);
S3 = S;
if S3 <> 0 then
R3OverS3 = R3/S3;
t3H = .5;
if S <> 0 then begin
t3OK = true;
Value3 = R/S;
logT3RS = Log(Value3);
logT3 = Log(Value2);
t3H = logT3RS/logT3;
end;
//Print(" t3H ",t3H);
end;
end;
Value1 = 0;
nTSD = 0;
if t1OK then begin
nTSD = 1;
Value1 = logT1;
Value2 = logT1RS;
end;
if t2OK then begin
nTSD = nTSD+1;
Value1 = Value1 + logT2;
Value2 = Value2 + logt2RS;
end;
if t3OK then begin
nTSD = nTSD+1;
Value1 = Value1 + logT3;
Value2 = Value2 + logt3RS;
end;
//Value1 = (logT1+logT2+logT3)/nTs; // xAvg
//Value2 = (logT1RS+logT2RS+logT3RS)/nTs; // yAvg;
value3 = 0;
value4 = 1;
if nTSD <> 0 then begin
Value1 = Value1 / nTSD;
Value2 = Value2 / nTSD;
if t1OK then begin
Value3 = (logT1-Value1)*(logT1RS-Value2);
Value4 = (logT1-Value1)*(logT1-Value1);
end;
if t2OK then begin
Value3 = Value3 + (logT2-Value1)*(logT2RS-Value2);
Value4 = Value4 + (logT2-Value1)*(logT2-Value1);
end;
if t3OK then begin
Value3 = Value3 + (logT3-Value1)*(logT3RS-Value2);
Value4 = Value4 + (logT3-Value1)*(logT3-Value1);
end;
//Value3 = (logT1-Value1)*(logT1RS-Value2) + (logT2-Value1)*(logT2RS-Value2) + (logT3-Value1)*(logT3RS-Value2);
//Value4 = (logT1-Value1)*(logT1-Value1) + (logT2-Value1)*(logT2-Value1) + (logT3-Value1)*(logT3-Value1);
end;
hE = Value3/Value4;
if plotROverS then begin
Plot1(R1OverS1,"R1OverS1");
setPlotColor(1,t1Color);
Plot2(R2OverS2,"R2OverS2");
setPlotColor(2,t2Color);
Plot3(R3OverS3,"R3OverS3");
end
else if plotRS then begin
Plot1(R1,"R1");
setPlotColor(1,t1Color);
Plot2(R2,"R2");
setPlotColor(2,t2Color);
Plot3(R3,"R3");
setPlotColor(3,t3Color);
Plot4(S1,"S1");
setPlotColor(4,t1Color);
Plot5(S2,"S2");
setPlotColor(5,t2Color);
Plot6(S3,"S3");
setPlotColor(6,t3Color);
end
else begin
if plotT1 then begin
Plot1(t1H,"t1H");
setPlotColor(1,t1Color);
end;
if plotT2 then begin
Plot2(t2H,"t2H");
setPlotColor(2,t2Color);
end;
if plotT3 then begin
Plot3(t3H,"t3H");
setPlotColor(3,t3Color);
end;
if plotHe then begin
Plot4(hE,"hE");
setPlotColor(4,hEColor);
end;
Plot5(.5,"mid");
setPlotColor(5,RGB(128,128,128));
end;
if writeToDisk then begin
Print(File(fileName),D:7:0,",",Time_s:6:0,",",
NumToStr(t1H,3),",",
NumToStr(t2H,3),",",
NumToStr(t3H,3),",",
NumToStr(he,3));
end; end;
|