Inputs: BarNo(400), PopSize(200),
MaxGenerations(2000), MaxPeaks(100), debug(TRUE);
{ Details of GA Lib at the end of this file}
DefineDLLFunc: "GH_TS.DLL", INT, "SETSERIAL", long
{serial};
DefineDLLFunc: "GH_TS.DLL", INT, "SETGENSEED", int {SeedNo};
DefineDLLFunc: "GH_TS.DLL", INT, "GETNEXTPOPNUM";
DefineDLLFunc: "GH_TS.DLL", INT, "MAKEPOP", int {PopNo},
int {PopSize};
DefineDLLFunc: "GH_TS.DLL", INT, "KILLPOP", int {PopNo};
DefineDLLFunc: "GH_TS.DLL", INT, "MAKECHROM", int {PopNo},
int {ChromNo},
int {Resolution}, float {Max}, float
{Min}, int {ChromType};
DefineDLLFunc: "GH_TS.DLL", INT, "SETSTRATEGY", int {PopNo}, int
{Selection},
float {GenGap}, float {Ratio};
DefineDLLFunc: "GH_TS.DLL", INT, "SETOPERATORS", int {PopNo},
int {CrossType},
float {CrossRate}, float {MutRate};
DefineDLLFunc: "GH_TS.DLL", INT, "SETDIVERSITY", int {PopNo},
float {DivRate}, float {DivRad};
DefineDLLFunc: "GH_TS.DLL", FLOAT, "GETCHROMVALUE", int {PopNo},
int {IndivNo}, int {ChromNo};
DefineDLLFunc: "GH_TS.DLL", INT, "PUTFITNESS", int {PopNo},
int {IndivNo}, float {CalcFitness};
DefineDLLFunc: "GH_TS.DLL", INT, "FINDBESTINDIV", int {PopNo};
DefineDLLFunc: "GH_TS.DLL", FLOAT, "FINDBESTVAL", int {PopNo};
DefineDLLFunc: "GH_TS.DLL", INT, "REPRODUCE", int {PopNo};
Vars: PopNum(-1), RandomStart(TRUE), IndivCount(0), ChromCount(0),
GenerCount(0),
ELITIST_SELECTION(1), SIMPLE_CONT_CROSS(1);
Vars: GenSeed(1), FitnessValue(0), NewBestFitFound(FALSE), BestFitness(-99999),
CountPeak(0), CountPlateau(0), BestIndiv(-1);
Arrays: Chrom[2](0), BestChrom[2](0), ChromValue[100,3](0);
{PopSize, NumChroms}
If CurrentBar = 1 then {Init GA}
Begin
if RandomStart = FALSE then SETGENSEED(GenSeed); {GenSeed
< 32,767 }
SETSERIAL( ... your GH serial number ... );
PopNum = GETNEXTPOPNUM();
if PopNum = -1 and debug then Print("Problem with GA Lib...");
MAKEPOP(PopNum,PopSize);
{
The Number of Chromosomes is problem specific: Each individual will carry 3
chromosomes here
The format is:
- PopNum = Population Number, we only use one population here, but if several
indicators call the GA library
a unique PopNum will be generated each time
- Chrom Num from 0 to 2 here
- Max value for the chromosome
- Min value
- continuous (0), or integer (1)
}
MAKECHROM(PopNum,0,2,30,-30,0); {Chrom 0} { in the fitness function: threshold}
MAKECHROM(PopNum,1,2,30,1,1); {Chrom 1} { in the fitness function: XMA Len1}
MAKECHROM(PopNum,2,2,30,1,1); {Chrom 2} { in the fitness function: XMA Len2}
{ Strategy parameters:
ELISTIST_SELECTION is recommended
Here a generation gap of 96% is set, meaning that only the top 2% will survive
to the next generation
}
SETSTRATEGY(PopNum,ELITIST_SELECTION,0.98,3);
{ Operators:
Crossover rate = 0.96
Mutation Rate = 0.02
}
SETOPERATORS(PopNum,SIMPLE_CONT_CROSS,0.96,0.02);
{
Diversity: optional search in the neighbourhood of solutions
Keep 0 to turn it off
Diversity rate = 0.1 (max = 1)
Diversity radious = 0.1 (max = 1)
SETDIVERSITY(PopNum, 0, 0); may often be sufficient, and run faster
}
SETDIVERSITY(PopNum,0.1,0.1);
For IndivCount = 0 to PopSize -1
Begin
For ChromCount = 0 to 2 {3 chromosomes}
Begin
Chrom[ChromCount] = GETCHROMVALUE(PopNum,IndivCount,ChromCount);
End;
FitnessValue = FITNESS_3(Chrom[0],Chrom[1],Chrom[2]);
PUTFITNESS(PopNum,IndivCount,FitnessValue);
End;
{BestIndiv = FINDBESTINDIV(PopNum);
Print("BestIndiv=",BestIndiv);}
if debug then Print("GA Setup OK");
End;
If CurrentBar = BarNo then
Begin
if debug then Print("GA starts...");
GenerCount = 0;
While ((GenerCount < MaxGenerations) and (CountPeak < MaxPeaks))
Begin
NewBestFitFound = FALSE;
if debug then Print("Gen Loop/Peak:", GenerCount, CountPeak);
REPRODUCE(PopNum);
For IndivCount = 0 to PopSize -1
Begin
For ChromCount = 0 to 2 {3 chromosomes}
Begin
Chrom[ChromCount] = GETCHROMVALUE(PopNum,IndivCount,ChromCount);
End;
FitnessValue = FITNESS_3(Chrom[0],Chrom[1],Chrom[2]);
{store Fitness in DLL memory. By default highest fitness means best fitness}
PUTFITNESS(PopNum,IndivCount,FitnessValue);
if FitnessValue > BestFitness then
begin
BestFitness = FitnessValue;
BestIndiv = IndivCount;
BestChrom[0] = Chrom[0];
BestChrom[1] = Chrom[1];
BestChrom[2] = Chrom[2];
if debug then Print("New BestFit found:", BestIndiv, BestFitness, GenerCount);
NewBestFitFound = TRUE;
end;
{if debug then Print("Fitness found:", IndivCount,
FitnessValue, Chrom[0],Chrom[1],Chrom[2]);}
End;
{BestIndiv = FINDBESTINDIV(PopNum);
BestFitness = FINDBESTVAL(PopNum);
if debug then Print("BestInd/BestFit: ", BestIndiv, BestFitness);
If BestFitness > PrevBestFitness then
Begin
PrevBestFitness = BestFitness;
if debug then Print("New BestFit found:", BestIndiv, BestFitness, GenerCount);
For ChromCount = 0 to 2 {3 chromosomes}
Begin
BestChrom[ChromCount] = GETCHROMVALUE(PopNum,BestIndiv,ChromCount);
End;
End}
if NewBestFitFound then CountPeak = CountPeak + 1;
GenerCount = GenerCount + 1;
End;
if debug then Print("Max Fitness found:", BestFitness);
if debug then Print("BestChroms=",BestChrom[0],BestChrom[1],BestChrom[2]);
End;
If CurrentBar > BarNo Then
Begin
Value1 = iff(Xaverage(MACD(C,BestChrom[1],BestChrom[2]),3) crosses over
BestChrom[0],1,0);
Value2 = iff(Xaverage(MACD(C,BestChrom[1],BestChrom[2]),3) crosses below
BestChrom[0],-1,0);
Plot1(Value1,"BUY");
Plot2(Value2,"SELL");
End;
if LastBarOnChart then KILLPOP(PopNum);
|