I would like to start a discussion around the possibilities of making use of machine learning in ProRealTime. I have personally experimented with some heuristics algorithms in ProOrder but find it very limiting when having to optimize more than one variable. Here is an snippet of the code I use to optimize an variable called periods (i.e. Average[periods]):
// Heuristics Algorithm
once Periods = 42 //Variable to be optimized
once Optimize = 0
once Mode = 1 //switches between + and - increments
once Increment = 1 //initial increment value
once StratAvgB = 0
If Optimize = 10 Then //value is incremented by 1 at position entry or exit
WinCount = 0 //Value used to determine minimum number of winning trades
StratAvgA = StratAvgB
StratAvgB = 0
For i = 1 to 10 Do //number of repittions to run past performance analysis
If positionperf(i) > 0 Then
WinCount = WinCount + 1
EndIF
StratAvgB = StratAvgB + StrategyProfit[i]
Next
StratAvgB = StratAvgB/10
If (WinCount < 5 or StratAvgA > StratAvgB) and Mode = 1 Then
Periods = Periods + (Increment+2)
ElsIf (Winloss < 5 or StratAvgA > StratAvgB) and Mode = 2 Then
Periods = Periods - (Increment*2) - 1
mode = 1
If Increment > 5 Then
Periods = 42 //goes back to default value
Increment = 1 //increment is reset
Else
Increment = Increment + 1
EndIF
EndIF
mode = 2
Optimize = 0
EndIf
Please share your own experience. @Wing @Nicolas I know you said you have also experimented with this, would you mind to share?
WingParticipant
Veteran
Will follow the thread. Have a lot of work deadlines to deal with but in 4-5 days I may be able to share something I have been working on previously.
Thank you @Noobywan for posting my thread. The project I plan to share is not related to that one.
@juanj
Thanks for this code snippet about machine learning, it is inspiring! I’m currently on leave, so I can’t help a lot, I’m spending only 10 minutes on day on website 🙂
I made code indentation for a better comprehension of what you done, but I’m still thinking a part is missing? Sorry I’m confuse about it, but how do you know the “periods” you found with the code would have been better than the one used in the past? Would you mind adding comments starting from line 20?
Latest version of my Heuristics Algorithm (with comments):
// Heuristics Algorithm Start
Once Periods = 42 //Variable being optimized is initialized
Increment = 1
MaxIncrement = 6
Reps = 10 //Number of bars to use for analysis
once IncPos = 1 //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 = 0 //Initialize Best Win Count
once StratAvgB = 0 //Initialize Best Avg Strategy Profit
If Optimize = Reps Then
WinCountA = 0 //Initialize current Win Count
StratAvgA = 0 //Initialize current Avg Strategy Profit
For i = 1 to Reps Do
If positionperf(i) > 0 Then
WinCountA = WinLossA + 1 //Increment Current WinCount
EndIf
StratAvgA = StratAvgA + StrategyProfit[i]
Next
StratAvgA = StratAvgA/Reps //Calculate Current Avg Strategy Profit
If StratAvgA >= StratAvgB Then
StratAvgB = StratAvgA //Update Best Strategy Profit
BestA = Periods
ElsIf WinCountA >= WinCountB Then
WinCountB = WinCountB //Update Best Win Count
BestB = Periods
EndIf
If WinCountA < WinCountB or StratAvgA < StratAvgB) and Mode = 1 Then
Periods = Periods + (Increment*IncPos)
ElsIf WinCountA < WinCountB or StratAvgA < StratAvgB) and Mode = 2 Then
Periods = Periods - (Increment*IncPos)
mode = 1
If IncPos > MaxIncrement Then
If BestA = BestB Then
Periods = BestA //Periods is reset to best performing value
Else
Periods = (BestA+BestB)/2 //Periods is reset to best performing avg value
IncPos = 1
EndIf
IncPos = IncPos + Increment
EndIF
EndIF
mode = 2
Optimize = 0
EndIf
// Heuristics Algorithm End
There was an Syntax error in the above piece of code, here is the corrected and indented version:
// Heuristics Algorithm Start
once periods = 42 //Variable being optimized is initialized
Increment = 1
MaxIncrement = 6
Reps = 10 //Number of bars to use for analysis
once IncPos = 1 //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 = 0 //Initialize Best Win Count
once StratAvgB = 0 //Initialize Best Avg Strategy Profit
If Optimize = Reps Then
WinCountA = 0 //Initialize current Win Count
StratAvgA = 0 //Initialize current Avg Strategy Profit
For i = 1 to Reps Do
If positionperf(i) > 0 Then
WinCountA = WinCountA + 1 //Increment Current WinCount
EndIf
StratAvgA = StratAvgA + StrategyProfit[i]
Next
StratAvgA = StratAvgA/Reps //Calculate Current Avg Strategy Profit
If StratAvgA >= StratAvgB Then
StratAvgB = StratAvgA //Update Best Strategy Profit
BestA = Periods
ElsIf WinCountA >= WinCountB Then
WinCountB = WinCountB //Update Best Win Count
BestB = Periods
EndIf
If WinCountA < WinCountB or StratAvgA < StratAvgB and Mode = 1 Then
Periods = Periods + (Increment*IncPos)
ElsIf WinCountA < WinCountB or StratAvgA < StratAvgB and Mode = 2 Then
Periods = Periods - (Increment*IncPos)
mode = 1
EndIf
If IncPos > MaxIncrement Then
If BestA = BestB Then
Periods = BestA //Periods is reset to best performing value
Else
Periods = (BestA+BestB)/2 //Periods is reset to best performing avg value
IncPos = 1
EndIf
IncPos = IncPos + Increment
EndIF
mode = 2
Optimize = 0
EndIf
// Heuristics Algorithm End
@wing hope you have met those deadlines 🙂
I’m looking forward to the code you mentioned you were working on previously.
Regards
WingParticipant
Veteran
Alright I have attached the code to this reply. It bears resemblance to the algorithm you showcase. This is a large piece of code, including a strategy, and an attached indicator which itself calls a whole bunch of indicators. Even though it relies on machine learning, I have optimized it previously and introduced time settings etc. to work on the DAX 30m timeframe. I think it has positive performance on higher timeframes too. I last worked on this in October or something, so everything after that is OOS. It has not performed good since then, but not bad either. Nevertheless the concept I want to showcase is explained by me in this thread: https://www.prorealcode.com/topic/code-correlation/
The system’s indicator calls a bunch of other indicators, and records the average value of these different indicators before a positive or negative move. Using this it constantly produces a prediction for future price. Since market regimes change, the history of indicator values is weighted to give more importance to recent market behvaiour.
You will find no comments in the code itself, but there is code in the link above you can view for better understanding.. This is experimental code I have not cleaned up, so there are features/stuff not currently in use still included. With what I know now I can improve on it a lot, maybe with the help of your ideas.
Great topic – I’d love to stay in and develop the conversation.
Thanks a lot @Wing, I’d love to take part of further development when I’ll be back and operational… 🙂
@wing thank you for your contribution, I haven’t gone through your code yet but looking forward to seeing what I can learn from it.
I’m very excited as to the potential of what we can achieve if we all collaborate.
As I read your post I was thinking of the concept of maybe using indicators as a form of dynamic array. Not sure yet how that will work but sure I will get some insights from your work.
Below is the latest version of my heuristics algorithm, it now has the ability to base it’s increments more towards the better performing side, but still switch if performance declines.
// Heuristics Algorithm Start
once periods = 26 //initial value of the dynamic variable ('periods') to optimize
Increment = 1 //Increment value
MaxIncrement = 7 //Maximum value to increment before updating initial value
Reps = x //Number of trades to use for analysis
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 = 0 //Initialize Best Win Count i.e. win rate
once StratAvgB = 0 //Initialize Best Avg Strategy Profit i.e. avg profit/trade
If Optimize = Reps Then //Start heuristics
WinCountA = 0 //Initialize current Win Count
StratAvgA = 0 //Initialize current Avg Strategy Profit
For i = 1 to Reps Do
If positionperf(i) > 0 Then //Determine win rate of current period
WinCountA = WinCountA + 1 //Increment Current WinCount
EndIf
StratAvgA = StratAvgA + StrategyProfit[i]
Next
StratAvgA = StratAvgA/Reps //Calculate Current Avg Strategy Profit
If StratAvgA >= StratAvgB Then
StratAvgB = StratAvgA //Update Best Strategy Profit
BestA = Periods //Update BestA with best performing value
EndIf
If WinCountA >= WinCountB Then
WinCountB = WinCountB //Update Best Win Count
BestB = Periods //Update BestB with best performing value
EndIf
If WinCountA > WinCountB and StratAvgA > StratAvgB Then
Mode = 0 //Strategy is performing well with current variable, do not optimize
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 1 Then
Periods = Periods - (Increment*NIncPos)
NIncPos = NIncPos + Increment
Mode = 2 //Next cycle optimize with negative increments
ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 1 Then
Periods = Periods + (Increment*PIncPos)
PIncPos = PIncPos + Increment
Mode = 1 //Next cycle again optimize with positive increments
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 2 Then
Periods = Periods + (Increment*PIncPos)
PIncPos = PIncPos + Increment
Mode = 1 //Next cycle optimize with positive increments
ElsIf WinCountA > WinCountB or StratAvgA > StratAvgB and Mode = 2 Then
Periods = Periods - (Increment*NIncPos)
NIncPos = NIncPos + Increment
Mode = 2 //Next cycle again optimize with positive increments
EndIf
If NIncPos > MaxIncrement or PIncPos > MaxIncrement Then //Max increment value reached
If BestA = BestB Then
Periods = BestA //Update initial variable with the best performing value
Else
Periods = (BestA+BestB)/2 //Update initial variable with the average best performing value
EndIf
NIncPos = 1 //reset increment values
PIncPos = 1 //reset increment values
EndIF
Optimize = 0
EndIf
// Heuristics Algorithm End
@Nicolas I spent some time indenting the above code in the ‘Insert PRT Code’ screen to display better (same as my previous post), but seems the formatting was lost 🙁
Can you maybe assist in correcting it?
I am sharing this algorithm in the hopes that this community will help to improve it.
The algorithm can replace and dynamically adjust any variable set equal to it’s output variable called ‘ValueX’ (i.e. Average[Valuex](close)
The input and parameters to optimize are:
StartingValue //Initial or value used
ResetPeriod //Specify no of months after which to reset optimization Increment = 20
MaxIncrement //Limit of no of increments either up or down
Reps //Number of trades to use for analysis
MaxValue = //Maximum allowed value
MinValue = increment //Minimum allowed value
// Heuristics Algorithm 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
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 End
No this was only the beginning 🙂 – But there is only so much one can share