again doing a bit of work on this code to fit my purpose.
I worked on this in the beginning, I want to have a reset daily and tried another way.
It appeared that min & max values where not respected in all times. Maybe on purpose I don’t know. I changed that I hope.
The weighting score had to be changed because of that and it was also interesting to see the difference with weighting score on or off.
Then I found, if I optimise values for 200k bars which are nice (2s timeframe), and load these values on a 100k bar, the last day results where not the same, even though they values were reset at the beginning of the day. So a few more variables had to be reset too. Now it’s close what I seek but still not perfect as there are slight differences in the last day.
this how the code looks like for now with the changes.
// heuristics engine
if heuristicsengine then
StartingValue = aa
increment = 1 //5, 20, 10
maxincrement = 2 //5, 10 limit of no of increments either up or
reps = 1 //1 number of trades to use for analysis //2
maxvalue = 10 //20, 300, 150 //maximum allowed 12
minvalue = 2 //5, minimum allowed value
StartingValue2 = bb
increment2 = 1 //5, 10
maxincrement2 = 2 //1, 30 limit of no of increments either up/down //4
reps2 = 1 //1, 2 nos of trades to use for analysis //3
maxvalue2 = 10 //20, 300, 200 maximum allowed value
minvalue2 = 2 //5, minimum allowed value
reset = 1
weighting = 0
if reset=1 then
if openhour=0 then
ValueX = StartingValue
Valuey = StartingValue2
WinCountB = 0
WinCountB2 = 0
StratAvgB = 0
StratAvgB2 = 0
BestA = 0
BestA2 = 0
BestB = 0
BestB2 = 0
wincounta = 0 //initialize current win count
stratavga = 0 //initialize current avg strategy profit
wincounta2 = 0 //initialize current win count
stratavga2 = 0 //initialize current avg strategy profit
endif
else
once ValueX = StartingValue
once Valuey = StartingValue2
once WinCountB = 0
once WinCountB2 = 0
once StratAvgB = 0
once StratAvgB2 = 0
once BestA = 0
once BestA2 = 0
once BestB = 0
once BestB2 = 0
endif
heuristicscyclelimit = 2
once heuristicscycle = 0
once heuristicsalgo1 = 1
once heuristicsalgo2 = 0
if heuristicscycle >= heuristicscyclelimit then
if heuristicsalgo1 = 1 then
heuristicsalgo2 = 1
heuristicsalgo1 = 0
elsif heuristicsalgo2 = 1 then
heuristicsalgo1 = 1
heuristicsalgo2 = 0
endif
heuristicscycle = 0
else
once valuex = startingvalue
once valuey = startingvalue2
endif
if heuristicsalgo1 = 1 then
//heuristics algorithm 1 start
if (onmarket[1] = 1 and onmarket = 0) or (longonmarket[1] = 1 and longonmarket and countoflongshares < countoflongshares[1]) or (longonmarket[1] = 1 and longonmarket and countoflongshares > countoflongshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares < countofshortshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares > countofshortshares[1]) or (longonmarket[1] and shortonmarket) or (shortonmarket[1] and longonmarket) then
optimise = optimise + 1
endif
once valuex = startingvalue
once pincpos = a1 //positive increment position
once nincpos = a2 //negative increment position
once optimise = 0 //initialize heuristicks engine counter (must be incremented at position start or exit)
once mode1 = 0 //switches between negative and positive increments
//once wincountb = 3 //initialize best win count
//graph wincountb coloured (0,0,0) as "wincountb"
//once stratavgb = 4353 //initialize best avg strategy profit
//graph stratavgb coloured (0,0,0) as "stratavgb"
if optimise = reps then
heuristicscycle = heuristicscycle + 1
for i = 1 to reps do
if positionperf(i) > 0 then
wincounta = wincounta + 1 //increment current wincount
endif
stratavga = stratavga + (((positionperf(i)*countofposition[i]*close)*-1)*-1)
next
stratavga = stratavga/reps //calculate current avg strategy profit
//graph (positionperf(1)*countofposition[1]*100000)*-1 as "posperf1"
//graph (positionperf(2)*countofposition[2]*100000)*-1 as "posperf2"
//graph stratavga*-1 as "stratavga"
//once besta = 300
//graph besta coloured (0,0,0) as "besta"
if stratavga >= stratavgb then
stratavgb = stratavga //update best strategy profit
besta = valuex
endif
//once bestb = 300
//graph bestb coloured (0,0,0) as "bestb"
if wincounta >= wincountb then
wincountb = wincounta //update best win count
bestb = valuex
endif
if wincounta > wincountb and stratavga > stratavgb then
mode1 = 0
elsif wincounta < wincountb and stratavga < stratavgb and mode1 = 1 then
valuex = valuex - (increment*nincpos)
nincpos = nincpos + 1
mode1 = 2
elsif wincounta >= wincountb or stratavga >= stratavgb and mode1 = 1 then
valuex = min((valuex + (increment*pincpos)),maxvalue)
pincpos = pincpos + 1
mode1 = 1
elsif wincounta < wincountb and stratavga < stratavgb and mode1 = 2 then
valuex = min((valuex + (increment*pincpos)),maxvalue)
pincpos = pincpos + 1
mode1 = 1
elsif wincounta >= wincountb or stratavga >= stratavgb and mode1 = 2 then
valuex = valuex - (increment*nincpos)
nincpos = nincpos + 1
mode1 = 2
endif
if nincpos > maxincrement or pincpos > maxincrement then
if besta = bestb then
valuex = besta
Else
if weighting=1 then
If reps >= 10 Then
WeightedScore = 10
Else
WeightedScore = round((reps/100)*100)
EndIf
ValueX1 = min(round(((BestA*(20-WeightedScore)) + (BestB*WeightedScore))/20),maxvalue) //Lower Reps = Less weight assigned to Win%
valuex=max(minvalue,valuex1)
EndIf
endif
nincpos = 1
pincpos = 1
elsif valuex > maxvalue then
valuex = maxvalue
elsif valuex < minvalue then
valuex = minvalue
endif
optimise = 0
endif
// heuristics algorithm 1 end
elsif heuristicsalgo2 = 1 then
// heuristics algorithm 2 start
if (onmarket[1] = 1 and onmarket = 0) or (longonmarket[1] = 1 and longonmarket and countoflongshares < countoflongshares[1]) or (longonmarket[1] = 1 and longonmarket and countoflongshares > countoflongshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares < countofshortshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares > countofshortshares[1]) or (longonmarket[1] and shortonmarket) or (shortonmarket[1] and longonmarket) then
optimise2 = optimise2 + 1
endif
once valuey = startingvalue2
once pincpos2 = a3 //positive increment position
once nincpos2 = a4 //negative increment position
once optimise2 = 0 //initialize heuristicks engine counter (must be incremented at position start or exit)
once mode2 = 0 //switches between negative and positive increments
//once wincountb2 = 3 //initialize best win count
//graph wincountb2 coloured (0,0,0) as "wincountb2"
//once stratavgb2 = 4353 //initialize best avg strategy profit
//graph stratavgb2 coloured (0,0,0) as "stratavgb2"
if optimise2 = reps2 then
heuristicscycle = heuristicscycle + 1
for i2 = 1 to reps2 do
if positionperf(i2) > 0 then
wincounta2 = wincounta2 + 1 //increment current wincount
endif
stratavga2 = stratavga2 + (((positionperf(i2)*countofposition[i2]*close)*-1)*-1)
next
stratavga2 = stratavga2/reps2 //calculate current avg strategy profit
//graph (positionperf(1)*countofposition[1]*100000)*-1 as "posperf1-2"
//graph (positionperf(2)*countofposition[2]*100000)*-1 as "posperf2-2"
//graph stratavga2*-1 as "stratavga2"
//once besta2 = 300
//graph besta2 coloured (0,0,0) as "besta2"
if stratavga2 >= stratavgb2 then
stratavgb2 = stratavga2 //update best strategy profit
besta2 = valuey
endif
//once bestb2 = 300
//graph bestb2 coloured (0,0,0) as "bestb2"
if wincounta2 >= wincountb2 then
wincountb2 = wincounta2 //update best win count
bestb2 = valuey
endif
if wincounta2 > wincountb2 and stratavga2 > stratavgb2 then
mode2 = 0
elsif wincounta2 < wincountb2 and stratavga2 < stratavgb2 and mode2 = 1 then
valuey = valuey - (increment2*nincpos2)
nincpos2 = nincpos2 + 1
mode2 = 2
elsif wincounta2 >= wincountb2 or stratavga2 >= stratavgb2 and mode2 = 1 then
valuey = min((valuey + (increment2*pincpos2)),maxvalue2)
pincpos2 = pincpos2 + 1
mode2 = 1
elsif wincounta2 < wincountb2 and stratavga2 < stratavgb2 and mode2 = 2 then
valuey = min((valuey + (increment2*pincpos2)),maxvalue2)
pincpos2 = pincpos2 + 1
mode2 = 1
elsif wincounta2 >= wincountb2 or stratavga2 >= stratavgb2 and mode2 = 2 then
valuey = valuey - (increment2*nincpos2)
nincpos2 = nincpos2 + 1
mode2 = 2
endif
if nincpos2 > maxincrement2 or pincpos2 > maxincrement2 then
if besta2 = bestb2 then
valuey = besta2
Else
if weighting=1 then
If reps2 >= 10 Then
WeightedScore2 = 10
Else
WeightedScore2 = round((reps2/100)*100)
EndIf
ValueY1 = min(round(((BestA2*(20-WeightedScore2)) + (BestB2*WeightedScore2))/20),maxvalue2)
valuey=max(minvalue2,valuey1)
//Lower Reps = Less weight assigned to Win%
EndIf
endif
nincpos2 = 1
pincpos2 = 1
elsif valuey > maxvalue2 then
valuey = maxvalue2
elsif valuey < minvalue2 then
valuey = minvalue2
endif
optimise2 = 0
endif
endif
// heuristics algorithm 2 end
n=valuex
multiple=valuey
else
n=valuen
multiple=valuem
endif