ProRealCode - Trading & Coding with ProRealTime™
Fantasio2020 whatever number of variables you prefer and works for you.
looks like I got the difference between 200k & 50k fixed on the same strategy. If reset is used, the cycle should restart again too.
One part will always be a bit doubtful, if reps 1 (ore more) is used, and in a backtest results is +0.01 and live -0.01 it can have on the impact on the rest of the strategy for value x & y.
That’s why it’s important I guess to have a reset, daily, weekly etc. depending on timeframe.
this is the code as I’ve it now.
Also interesting is to optimise the direction 1 to 6. The reset is set to daily here.
once value1a=6 // 6
once value2a=8 // 8
once heuristicsengine=1
// heuristics engine
if heuristicsengine then
StartingValue = 4
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
minvalue = 1 //5, minimum allowed value
maxvalue = 10 //20, 300, 150 //maximum allowed 12
StartingValue2 = 10
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
minvalue2 = 1 //5, minimum allowed value
maxvalue2 = 10 //20, 300, 200 maximum allowed value
reset = 1
weighting = 1
direction=1
if direction=1 then
a1=0
a2=0
a3=0
a4=0
elsif direction=2 then
a1=1
a2=0
a3=1
a4=0
elsif direction=3 then
a1=0
a2=1
a3=0
a4=1
elsif direction=4 then
a1=1
a2=0
a3=0
a4=1
elsif direction=5 then
a1=0
a2=1
a3=1
a4=0
elsif direction=6 then
a1=1
a2=1
a3=1
a4=1
endif
if reset=1 then
if intradaybarindex=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
if reset=1 then
if intradaybarindex=0 then
heuristicscycle = 0
endif
else
once heuristicscycle = 0
endif
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
value1=valuex
value2=valuey
else
value1=value1a
value2=value2a
endif
get error when using your last code paul
ullle73 what errors?
Paul’s code above is not a complete Algo with Buy and Sell etc.
Hi Juan, Hi All, I’m following your posts and grateful that you share a lot of fancy stuff here. Thanks. I’d like to better understand the operating mode of the so called ML and have a few questions:
1/ where does the programme store the various results learned from the past?
2/ what happen if the Algo is stopped for some reason? Are we starting again from scratch the “learning from the past” process? I noticed that even the Algo performance (calculated by PRT) are reset to zero when the same Algo is stopped and restarted, so I guess there is now way to capitalize on previous learning if the Algo is stopped, is that right? if so, is there a way to overcome this?
3/ have you been running “live” the same programme with and without ML module? Backtest seems to give better results? are you really getting better results in rel trading?
Facinating ML!!!
Thanks
Hi Khaled, I am unfortunately not very active on the forum lately as I am kept just way too busy between PRT development and my other work. But to briefly answer your questions:
1. It is stored locally in the respective variables defined in the code (WinCount, StratAvg, Best, etc.)
2. Yes unfortunately the variables are reset. Although they can be initialized using the commented out once code and using the graph command in backtest to find the latest values.
3. I use this successfully in most of my live strategies but only using a single heuristics set applied to a single variable not multiple ones as in the code above
Hope this helps
Hi Juan, Thank you for taking the time to answer my questions. I see in your ML Code that you don’t have CONDITIONS (IF C1 THEN..) before BUY and SHORTSELL instructions, which is not a problem in itself. However, in my case, the pending orders arenot cancelled automatically at the end of the candle, so I’m carying orders with irrelevant market conditions, which not only prevent other Orders with more “recent” conditions to come in but also sometimes stop the Algo…
Do you have a trick to make the pending orders cancel at the end of each candle?
Thanks
Khaled
defparam cumulateorders = false
defparam preloadbars = 2000
TIMEFRAME (15 minutes)
N = 1 //Number of Contracts
Condition = 1
/////////////////////////////////////////
// CALCULATION OF VALUE X AND VALUE Y //
////////////////////////////////////////
boxsizeL = max ( ValueX , 20)
boxsizeS = max ( ValueY , 10)
renkomaxL = round(close / boxsizeL) * boxsizeL
renkominL = renkomaxL - boxsizeL
renkomaxS = round(close / boxsizeS) * boxsizeS
renkominS = renkomaxS - boxsizeS
if high > renkomaxl + boxsizel then
renkomaxl = renkomaxl + boxsizel
renkominl = renkominl + boxsizel
endif
if low < renkomins - boxsizes then
renkomaxs = renkomaxs - boxsizes
renkomins = renkomins - boxsizes
endif
IF time>=133000 AND time>200000 then
spread = 0.5
ELSIF time>=200000 AND time>220000 then
spread = 2.5
ELSIF time>=220000 AND time>133000 then
spread = 1
ENDIF
IF CONDITION=1 THEN
BUY N CONTRACT at (renkoMaxL + boxSizeL + spread) STOP
SELLSHORT N CONTRACT at (renkoMinS - boxSizeS - spread) STOP
ENDIF
SET STOP pTRAILING 5
SET TARGET pPROFIT 1000
I have a doubt, and I am not joking, nor hesitating.
You are dealing with a very interesting topic, but with so many explanations, so many codes, and so many posts it is impossible for me to know if there is something useful.
I don’t know, it would be interesting for someone to explain in a single post what the code is for and if there is any specific code that is really useful.
I have lost myself.
You should be thankful to those many explanations, codes and posts 🙂
If it’s useful you can only understand by yourself with your own tests on your own strategies.
I was skeptical too and I’ve made a quick and dirty test. I’ve put together a simple LONG only code if SAR>Close and EMA21>EMA8 then buy 1 contract (Nasdaq cash 1€, 15 min TF) and exit if SAR<Close and Close<EMA8. This strategy run on a Tick by Tick mode over the period 10 Oct. 2018 to 30 Oct. 2020 (2 years) did generate a Win Rate 40.04%, Profit Ratio 1.09x and Capital gain 2048€ (attached results and ITF file).
I’ve added a simple ML code on 1 variable (courtesy of juanj) that you can find at (https://www.prorealcode.com/topic/machine-learning-in-proorder/), and the results are better: Win Rate 40.1%, Profit Ratio 1.2x and Capital gain 3057€ (attached results and ITF file). The increment in € absolute gain is +50%!
You can also note that the number of losing positions have been reduced from 950 to 814 and the Drawdown has been reduced from 1817€ to 1161€. The biggest loss went from -191€ to -113€.
So, even though I didn’t write the ML code and have nothing to sell 🙂 it seems like it’s working !
Others have had different experience?
But it is that in the meantime post and explanations I do not reach any conclusion.
Not only do I not know exactly how learning works, but I have also tried to include some algorithms in my strategies and, either I can’t, or the statistics come out exactly the same.
I am not a professional programmer.
I don’t know what, or how to include learning in my strategies
Está bien Khaled, he introducido ese código.
Problems!
Okay Khaled, I entered that code. Problems!
Only post in the language of the forum that you are posting in. For example English only in the English speaking forums and French only in the French speaking forums.
Thank you 🙂
Machine Learning in ProOrder ProRealTime
This topic contains 454 replies,
has 32 voices, and was last updated by Khaled
4 years, 1 month ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| 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.