ProRealCode - Trading & Coding with ProRealTime™
@GraHal try this 🙂 The HeuristicsCycleLimit parameter will determine how many ‘cycles’ each algorithm gets. If someone is up to it I also think we should look at introducing different performance metrics like Drawdown vs Profit Ratio to the existing WinRate (WinCount) and AvgProfit (StratAvg) metrics
HeuristicsCycleLimit = 2
If HeuristicsCycle >= HeuristicsCycleLimit Then
If HeuristicsAlgo1 = 1 Then
HeuristicsAlgo2 = 1
HeuristicsAlgo1 = 0
ElsIf HeuristicsAlgo2 = 1 Then
HeuristicsAlgo1 = 1
HeuristicsAlgo2 = 0
EndIf
HeuristicsCycle = 0
EndIf
If HeuristicsAlgo1 = 1 Then
//Heuristics Algorithm 1 Start
If onmarket[1] = 1 and onmarket = 0 Then
optimize = optimize + 1
EnDif
StartingValue = 200
ResetPeriod = 8 //Specify no of months after which to reset optimization
Increment = 20
MaxIncrement = 4 //Limit of no of increments either up or down
Reps = 3 //Number of trades to use for analysis
MaxValue = 270 //Maximum allowed value
MinValue = increment //Minimum allowed value
once monthinit = month
once yearinit = year
If (year = yearinit and month = (monthinit + ResetPeriod)) or (year = (yearinit + 1) and ((12 - monthinit) + month = ResetPeriod)) Then
ValueX = StartingValue
WinCountB = 0
StratAvgB = 0
BestA = 0
BestB = 0
monthinit = month
yearinit = year
EndIf
once ValueX = StartingValue
once PIncPos = 1 //Positive Increment Position
once NIncPos = 1 //Neative Increment Position
once Optimize = 0 ////Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)
once Mode = 1 //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 Optimize = Reps Then
WinCountA = 0 //Initialize current Win Count
StratAvgA = 0 //Initialize current Avg Strategy Profit
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]*100000)*-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
Mode = 0
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 1 Then
ValueX = ValueX - (Increment*NIncPos)
NIncPos = NIncPos + 1
Mode = 2
ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 1 Then
ValueX = ValueX + (Increment*PIncPos)
PIncPos = PIncPos + 1
Mode = 1
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 2 Then
ValueX = ValueX + (Increment*PIncPos)
PIncPos = PIncPos + 1
Mode = 1
ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 2 Then
ValueX = ValueX - (Increment*NIncPos)
NIncPos = NIncPos + 1
Mode = 2
EndIf
If NIncPos > MaxIncrement or PIncPos > MaxIncrement Then
If BestA = BestB Then
ValueX = BestA
Else
If reps >= 10 Then
WeightedScore = 10
Else
WeightedScore = round((reps/100)*100)
EndIf
ValueX = round(((BestA*(20-WeightedScore)) + (BestB*WeightedScore))/20) //Lower Reps = Less weight assigned to Win%
EndIf
NIncPos = 1
PIncPos = 1
ElsIf ValueX > MaxValue Then
ValueX = MaxValue
ElsIf ValueX < MinValue Then
ValueX = MinValue
EndIF
Optimize = 0
EndIf
// Heuristics Algorithm 1 End
ElsIf HeuristicsAlgo2 = 1 Then
// Heuristics Algorithm 2 Start
If onmarket[1] = 1 and onmarket = 0 Then
optimize2 = optimize2 + 1
Endif
StartingValue2 = 200
ResetPeriod2 = 8 //Specify no of months after which to reset optimization
Increment2 = 20
MaxIncrement2 = 4 //Limit of no of increments either up or down
Reps2 = 3 //Number of trades to use for analysis
MaxValue2 = 270 //Maximum allowed value
MinValue2 = increment //Minimum allowed value
once monthinit2 = month
once yearinit2 = year
If (year = yearinit2 and month = (monthinit2 + ResetPeriod2)) or (year = (yearinit2 + 1) and ((12 - monthinit2) + month = ResetPeriod2)) Then
ValueY = StartingValue2
WinCountB2 = 0
StratAvgB2 = 0
BestA2 = 0
BestB2 = 0
monthinit2 = month
yearinit2 = year
EndIf
once ValueY = StartingValue2
once PIncPos2 = 1 //Positive Increment Position
once NIncPos2 = 1 //Neative Increment Position
once Optimize2 = 0 ////Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)
once Mode2 = 1 //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 Optimize2 = Reps2 Then
WinCountA2 = 0 //Initialize current Win Count
StratAvgA2 = 0 //Initialize current Avg Strategy Profit
HeuristicsCycle = HeuristicsCycle + 1
For i2 = 1 to Reps2 Do
If positionperf(i) > 0 Then
WinCountA2 = WinCountA2 + 1 //Increment Current WinCount
EndIf
StratAvgA2 = StratAvgA2 + (((PositionPerf(i)*countofposition[i]*100000)*-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
Mode = 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 = ValueY + (Increment2*PIncPos2)
PIncPos2 = PIncPos2 + 1
Mode = 1
ElsIf WinCountA2 < WinCountB2 and StratAvgA2 < StratAvgB2 and Mode2 = 2 Then
ValueY = ValueY + (Increment2*PIncPos2)
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 = BestA
Else
If reps2 >= 10 Then
WeightedScore2 = 10
Else
WeightedScore2 = round((reps2/100)*100)
EndIf
ValueY = round(((BestA2*(20-WeightedScore2)) + (BestB2*WeightedScore2))/20) //Lower Reps = Less weight assigned to Win%
EndIf
NIncPos2 = 1
PIncPos2 = 1
ElsIf ValueY > MaxValue2 Then
ValueY = MaxValue2
ElsIf ValueY < MinValue2 Then
ValueY = MinValue2
EndIF
Optimize2 = 0
EndIf
// Heuristics Algorithm 2 End
EndIf
Here is the version of the algo outputting ‘ValueY
Referring to Line numbers of the version2 / ValueY posted with the link above, the ‘2’ is missed off …
Line 123 – increment
Line 157 & 160 – i
Line 180 & 188 – Mode
Line 201 – BestA
I’m not trying to be clever, I ran the ValueY version on its own as part of a trial of my ‘barindex idea’ and the syntax checker told me the above were not defined! 🙂
I’ll now have a go with the HeuristicsCycleLimit.
As an aside … anybody got any ideas / code to identify even & odd number barindex ?
identify even & odd number barindex
Good how ideas come when away from the screen!? 🙂
I’m not there yet, but I’m thinking around ?? …
If barindex mod 2 = 1 Then //odd barindex
AND
If barindex mod 2 = 0 Then //even barindex
Below is the HeuristicsCycleLimit code with the corrections mentioned 2 posts above … 2 was missing here and there in Algo 2.
Least I can do to save JuanJ some time for letting us in on his coding gems! 🙂
HeuristicsCycleLimit = 2
If HeuristicsCycle >= HeuristicsCycleLimit Then
If HeuristicsAlgo1 = 1 Then
HeuristicsAlgo2 = 1
HeuristicsAlgo1 = 0
ElsIf HeuristicsAlgo2 = 1 Then
HeuristicsAlgo1 = 1
HeuristicsAlgo2 = 0
EndIf
HeuristicsCycle = 0
EndIf
If HeuristicsAlgo1 = 1 Then
//Heuristics Algorithm 1 Start ////////////////////////////////////////////////////
If onmarket[1] = 1 and onmarket = 0 Then
optimize = optimize + 1
EnDif
StartingValue = 8 //200
ResetPeriod = 8 //Specify no of months after which to reset optimization
Increment = 2 //20
MaxIncrement = 24 //4 //Limit of no of increments either up or down
Reps = 3 //Number of trades to use for analysis
MaxValue = 48 //270 //Maximum allowed value
MinValue = increment //Minimum allowed value
once monthinit = month
once yearinit = year
If (year = yearinit and month = (monthinit + ResetPeriod)) or (year = (yearinit + 1) and ((12 - monthinit) + month = ResetPeriod)) Then
ValueX = StartingValue
WinCountB = 0
StratAvgB = 0
BestA = 0
BestB = 0
monthinit = month
yearinit = year
EndIf
once ValueX = StartingValue
once PIncPos = 1 //Positive Increment Position
once NIncPos = 1 //Neative Increment Position
once Optimize = 0 ////Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)
once Mode = 1 //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 Optimize = Reps Then
WinCountA = 0 //Initialize current Win Count
StratAvgA = 0 //Initialize current Avg Strategy Profit
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]*100000)*-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
Mode = 0
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 1 Then
ValueX = ValueX - (Increment*NIncPos)
NIncPos = NIncPos + 1
Mode = 2
ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 1 Then
ValueX = ValueX + (Increment*PIncPos)
PIncPos = PIncPos + 1
Mode = 1
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 2 Then
ValueX = ValueX + (Increment*PIncPos)
PIncPos = PIncPos + 1
Mode = 1
ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 2 Then
ValueX = ValueX - (Increment*NIncPos)
NIncPos = NIncPos + 1
Mode = 2
EndIf
If NIncPos > MaxIncrement or PIncPos > MaxIncrement Then
If BestA = BestB Then
ValueX = BestA
Else
If reps >= 10 Then
WeightedScore = 10
Else
WeightedScore = round((reps/100)*100)
EndIf
ValueX = round(((BestA*(20-WeightedScore)) + (BestB*WeightedScore))/20) //Lower Reps = Less weight assigned to Win%
EndIf
NIncPos = 1
PIncPos = 1
ElsIf ValueX > MaxValue Then
ValueX = MaxValue
ElsIf ValueX < MinValue Then
ValueX = MinValue
EndIF
Optimize = 0
EndIf
// Heuristics Algorithm 1 End //////////////////////////////////////////////////////
ElsIf HeuristicsAlgo2 = 1 Then
// Heuristics Algorithm 2 Start ////////////////////////////////////////////////////
If onmarket[1] = 1 and onmarket = 0 Then
optimize2 = optimize2 + 1
Endif
StartingValue2 = 16 //200
ResetPeriod2 = 8 //Specify no of months after which to reset optimization
Increment2 = 2 //20
MaxIncrement2 = 24 //4 //Limit of no of increments either up or down
Reps2 = 3 //Number of trades to use for analysis
MaxValue2 = 48 //270 //Maximum allowed value
MinValue2 = increment2 //Minimum allowed value
once monthinit2 = month
once yearinit2 = year
If (year = yearinit2 and month = (monthinit2 + ResetPeriod2)) or (year = (yearinit2 + 1) and ((12 - monthinit2) + month = ResetPeriod2)) Then
ValueY = StartingValue2
WinCountB2 = 0
StratAvgB2 = 0
BestA2 = 0
BestB2 = 0
monthinit2 = month
yearinit2 = year
EndIf
once ValueY = StartingValue2
once PIncPos2 = 1 //Positive increment2 Position
once NIncPos2 = 1 //Neative increment2 Position
once Optimize2 = 0 ////Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)
once Mode2 = 1 //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 Optimize2 = Reps2 Then
WinCountA2 = 0 //Initialize current Win Count
StratAvgA2 = 0 //Initialize current Avg Strategy Profit
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]*100000)*-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 = ValueY + (Increment2*PIncPos2)
PIncPos2 = PIncPos2 + 1
Mode2 = 1
ElsIf WinCountA2 < WinCountB2 and StratAvgA2 < StratAvgB2 and Mode2 = 2 Then
ValueY = ValueY + (Increment2*PIncPos2)
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 reps2 >= 10 Then
WeightedScore2 = 10
Else
WeightedScore2 = round((reps2/100)*100)
EndIf
ValueY = round(((BestA2*(20-WeightedScore2)) + (BestB2*WeightedScore2))/20) //Lower Reps = Less weight assigned to Win%
EndIf
NIncPos2 = 1
PIncPos2 = 1
ElsIf ValueY > MaxValue2 Then
ValueY = MaxValue2
ElsIf ValueY < MinValue2 Then
ValueY = MinValue2
EndIF
Optimize2 = 0
EndIf
// Heuristics Algorithm 2 End ////////////////////////////////////////////////////////
JuanJ you sure below code works please?
I can’t get any results using below … despite trying what I thought was logical re changes etc! 🙂
Maybe you try it if you get chance please?
Or anybody else get below to work (using the full code above)?
HeuristicsCycleLimit = 2
If HeuristicsCycle >= HeuristicsCycleLimit Then
If HeuristicsAlgo1 = 1 Then
HeuristicsAlgo2 = 1
HeuristicsAlgo1 = 0
ElsIf HeuristicsAlgo2 = 1 Then
HeuristicsAlgo1 = 1
HeuristicsAlgo2 = 0
EndIf
HeuristicsCycle = 0
EndIf
If HeuristicsAlgo1 = 1 Then
Ah! I forgot to initialize the variables:
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
EndIf
I should be able to GRAPH ValueX and ValueY as normal?
If yes then mine isn’t working with the 2 Algos as ValueX and ValueY GRAPH as constant starting values.
I’m going to start again with one Algo! 🙂
I give up! I can’t get it to work, it doesnt give bad results … it doesn’t even even start.
I’m sure it is something simple, although I’m sure I’ve tried everything simple!? 🙂
I thought I was getting 1 x Algo to work. but I realise now that I was optimising the starting value and the starting value continued to be used for all trades!
Seems to have gone quiet after the first few flurries of excitement?
Anybody else got it to work?
@Juanj I have changed your values to my respective values etc, but how are you defining … Best Avg Strategy Profit?
I feel somewhat stupid but I don’t have a clue what this heuristic algo code does ! Maybe I’m not the only one…can anybody explain in a few sentences how does it work?
Thanks
can anybody explain in a few sentences how does it work
It optimises 1 x variable value up / down 1 x pre-set increment depending on the performance of the previous 3 (pre-set number) trades.
I am reading between the lines of the code (as I can’t follow it all) but what I’m not clear about is … how does the heuristics decide that the one or all of the past 3 trades were not just bad trades anyway?? As we all know we can get a bad run for while.
I guess the heuristics must be comparing against an average profit … which is why I asked JuanJ how Best Avg Strategy Profit is defined as this must be critical.
But at this stage I’d be happy if I could see the GRAPH of ValueX moving from it’s starting value! 🙂
@GraHal Please share the ITF file of the strategy you wish to implement the algorithm to and I will have a look to see where the problem is.
Then to further elaborate on GraHal’s explanation:
The algorithm also keeps track of the following:
It then measures and saves the same metrics but only for the last evaluation period (StratAvgA and WinCountA) and compares them to the above.
It then starts by systematically incrementing the target variables both up and down to see what appears to improve performance most
There are also some other calculations going on but that is mostly to achieve the above.
I just added the machine learning into a system, here’s the results (before and after).
Very impressive, and also the robustness test went up in total average!
I will have a look to see where the problem is.
Thank you, I may take you up on the offer! 🙂
I am learning stuff by me keep asking myself … “why the eff isn’t it working?” and as your code is applicable to any strategy it is time well sweated over! But it sure don’t feel like it yet! 🙂
I got it going on another strategy.
Should your heuristic code work on strategies that switch position from Long to Short with no bars not onmarket ?
Should your heuristic code work on strategies that switch position from Long to Short with no bars not onmarket ?
Yip that is the problem. Solution;
Replace this:
If onmarket[1] = 1 and onmarket = 0 Then
optimize = optimize + 1
EndIf
With This:
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
optimize = optimize + 1
EndIf
hi juanj
one algo works, but 2 fails somewhere.
I created a basic strategy on a second timeframe for testing. Can you see what is wrong please below?
defparam cumulateorders = false
defparam preloadbars = 10000
defparam flatbefore = 080000
defparam flatafter = 220000
period1=7
period2=14
HeuristicsCycleLimit = 2
If HeuristicsCycle >= HeuristicsCycleLimit Then
If HeuristicsAlgo1 = 1 Then
HeuristicsAlgo2 = 1
HeuristicsAlgo1 = 0
ElsIf HeuristicsAlgo2 = 1 Then
HeuristicsAlgo1 = 1
HeuristicsAlgo2 = 0
EndIf
HeuristicsCycle = 0
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
optimize = optimize + 1
EndIf
StartingValue = period1
ResetPeriod = 3 //Specify no of months after which to reset optimization
Increment = 1
MaxIncrement = 7 //Limit of no of increments either up or down
Reps = 3 //Number of trades to use for analysis
MinValue = 2 //Minimum allowed value
MaxValue = 14 //Maximum allowed value
If monthinit = 1 or monthinit = 3 or monthinit = 5 or monthinit = 7 or monthinit = 8 or monthinit = 10 or monthinit = 12 Then
MonthDays = 31
ElsIf monthinit = 4 or monthinit = 6 or monthinit = 9 or monthinit = 11 Then
MonthDays = 30
ElsIf monthinit = 2 Then
If (yearinit/4 = round(yearinit/4)) or (yearinit/400 = round(yearinit/400)) Then //haha not sure how exactly to do this
MonthDays = 29 //leap year
Else
MonthDays = 28
EndIf
EndIf
If (month = monthinit and day = dayinit + ResetPeriod) or (month = monthinit + 1 and (day + (MonthDays - dayinit)) >= ResetPeriod) Then
ValueX = StartingValue
WinCountB = 0
StratAvgB = 0
BestA = 0
BestB = 0
dayinit = day
monthinit = month
yearinit = year
EndIf
once ValueX = StartingValue
once PIncPos = 1 //Positive Increment Position
once NIncPos = 1 //Neative Increment Position
once Optimize = 0 ////Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)
once Mode = 1 //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 Optimize = Reps Then
WinCountA = 0 //Initialize current Win Count
StratAvgA = 0 //Initialize current Avg Strategy Profit
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]*100000)*-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
Mode = 0
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 1 Then
ValueX = ValueX - (Increment*NIncPos)
NIncPos = NIncPos + 1
Mode = 2
ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 1 Then
ValueX = ValueX + (Increment*PIncPos)
PIncPos = PIncPos + 1
Mode = 1
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 2 Then
ValueX = ValueX + (Increment*PIncPos)
PIncPos = PIncPos + 1
Mode = 1
ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 2 Then
ValueX = ValueX - (Increment*NIncPos)
NIncPos = NIncPos + 1
Mode = 2
EndIf
If NIncPos > MaxIncrement or PIncPos > MaxIncrement Then
If BestA = BestB Then
ValueX = BestA
Else
If reps >= 10 Then
WeightedScore = 10
Else
WeightedScore = round((reps/100)*100)
EndIf
ValueX = round(((BestA*(20-WeightedScore)) + (BestB*WeightedScore))/20) //Lower Reps = Less weight assigned to Win%
EndIf
NIncPos = 1
PIncPos = 1
ElsIf ValueX > MaxValue Then
ValueX = MaxValue
ElsIf ValueX < MinValue Then
ValueX = MinValue
EndIF
Optimize = 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
optimize2 = optimize2 + 1
EndIf
StartingValue2 = period2
ResetPeriod2 = 3 //Specify no of months after which to reset optimization
Increment2 = 1
MaxIncrement2 = 7 //Limit of no of increments either up or down
Reps2 = 3 //Number of trades to use for analysis
MinValue2 = 15 //Minimum allowed value
MaxValue2 = 28 //Maximum allowed value
If monthinit2 = 1 or monthinit2 = 3 or monthinit2 = 5 or monthinit2 = 7 or monthinit2 = 8 or monthinit2 = 10 or monthinit2 = 12 Then
MonthDays2 = 31
ElsIf monthinit2 = 4 or monthinit2 = 6 or monthinit2 = 9 or monthinit2 = 11 Then
MonthDays2 = 30
ElsIf monthinit2 = 2 Then
If (yearinit2/4 = round(yearinit2/4)) or (yearinit2/400 = round(yearinit2/400)) Then //haha not sure how exactly to do this
MonthDays2 = 29 //leap year
Else
MonthDays2 = 28
EndIf
EndIf
If (month = monthinit2 and day = dayinit2 + ResetPeriod2) or (month = monthinit2 + 1 and (day + (MonthDays2 - dayinit2)) >= ResetPeriod2) Then
ValueY = StartingValue2
WinCountB2 = 0
StratAvgB2 = 0
BestA2 = 0
BestB2 = 0
dayinit2 = day
monthinit2 = month
yearinit2 = year
EndIf
once ValueY = StartingValue2
once PIncPos2 = 1 //Positive Increment Position
once NIncPos2 = 1 //Neative Increment Position
once Optimize2 = 0 ////Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)
once Mode2 = 1 //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 Optimize2 = Reps2 Then
WinCountA2 = 0 //Initialize current Win Count
StratAvgA2 = 0 //Initialize current Avg Strategy Profit
HeuristicsCycle = HeuristicsCycle + 1
For i2 = 1 to Reps2 Do
If positionperf(i) > 0 Then
WinCountA2 = WinCountA2 + 1 //Increment Current WinCount
EndIf
StratAvgA2 = StratAvgA2 + (((PositionPerf(i)*countofposition[i]*100000)*-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
Mode = 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 = ValueY + (Increment2*PIncPos2)
PIncPos2 = PIncPos2 + 1
Mode = 1
ElsIf WinCountA2 < WinCountB2 and StratAvgA2 < StratAvgB2 and Mode2 = 2 Then
ValueY = ValueY + (Increment2*PIncPos2)
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 = BestA
Else
If reps2 >= 10 Then
WeightedScore2 = 10
Else
WeightedScore2 = round((reps2/100)*100)
EndIf
ValueY = round(((BestA2*(20-WeightedScore2)) + (BestB2*WeightedScore2))/20) //Lower Reps = Less weight assigned to Win%
EndIf
NIncPos2 = 1
PIncPos2 = 1
ElsIf ValueY > MaxValue2 Then
ValueY = MaxValue2
ElsIf ValueY < MinValue2 Then
ValueY = MinValue2
EndIF
Optimize2 = 0
EndIf
// Heuristics Algorithm 2 End
EndIf
c1=average[valuex](close)
c2=average[valuey](close)
//
condbuy =c1 crosses over c2 and rsi[14](close)<70
condsell=c1 crosses under c2 and rsi[14](close)>30
//
if condbuy then
buy at market
endif
if condsell then
sellshort at market
endif
pp=positionperf(0)*100
if pp<-0.125 then
sell at market
exitshort at market
endif
set stop %loss 0.5 // exit sooner on performance criteria above
set target %profit 0.25
graph valuex coloured(121,141,35,255) as "fastperiod1"
graph valuey coloured(255,0,0,255) as "slowperiod"
Machine Learning in ProOrder ProRealTime
This topic contains 454 replies,
has 32 voices, and was last updated by Khaled
4 years ago.
| Forum: | ProOrder support |
| Language: | English |
| Started: | 08/06/2017 |
| Status: | Active |
| Attachments: | 207 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.