ProRealCode - Trading & Coding with ProRealTime™
some first code, not sure if it’s correct completely, but it looks the way i’am after.
I want that the code automatically adjust a few parameters of a strategy if it determines that long or short doesn’t work anymore.
It’s like live adjustments within the margins set. First off I needed the data to work with so this is a start.
// period reset
once periodr=1 //0all;1day;2week;3month;4year
if periodr=0 then
if barindex=0 then
longperf=0
shortperf=0
endif
elsif periodr=1 then
if day<>day[1] then
longperf=0
shortperf=0
endif
elsif periodr=2 then
if dayofweek=0 then
longperf=0
shortperf=0
endif
elsif periodr=3 then
if month<>month[1] then
longperf=0
shortperf=0
endif
elsif periodr=4 then
if year<>year[1] then
longperf=0
shortperf=0
endif
endif
if longonmarket[1] and (not onmarket or shortonmarket) then
if strategyprofit[1]>=strategyprofit[2] then
longperf=longperf+positionperf(1)*100
else
longperf=longperf-positionperf(1)*100
endif
endif
if shortonmarket[1] and (not onmarket or longonmarket) then
if strategyprofit[1]>=strategyprofit[2] then
shortperf=shortperf+positionperf(1)*100
else
shortperf=shortperf-positionperf(1)*100
endif
endif
graph longperf coloured(0,0,255,255) as "long performance"
graph shortperf coloured(255,0,0,255) as "short performance"
Machine learning … great idea! Got to be the way to go Paul?
Did you see the work Leo did on Neural Network Indicators (links below)? Might spark some ideas for you.
I didn’t understand Leo’s code, but I just spotted something I hadn’t appreciated before!
Also now you are doing above, my task today is to read up on the difference between Machine Learning and Neural Networks.
https://www.prorealcode.com/topic/neural-networks-programming-with-prorealtime/
https://www.prorealcode.com/topic/discussion-on-leos-neural-networks/
I know the topics, but never realised it could be usefull for my goal. I only need 1 parameter to be adjust while trading, so maybe that will fit the purpose. Thanks for the links!
In meantime, something simple which can be used with code above.
Stop trading, long or short each, when profits are >2% or losses <-2%. It depends when the period-reset is set to start trading again.
// limit & secure profit daily
if longperf>2 or longperf<-2 then
tradelong=0
else
tradelong=1
endif
if shortperf>2 or shortperf<-2 then
tradeshort=0
else
tradeshort=1
endif
//
If longposition and tradelong then
I did a lot of work on a code that adapted an indicator setting to what had worked best recently and the back tests were all fantastic but the forward tests turned out to be terrible. It seems that no matter what we do there is some form of curve fitting when using historical data and so I gave up on the concept. Plus at the time without arrays it was a coding nightmare!
I have developed and refined a ‘plug and play’ self contained heuristics algorithm over the last few years that can dynamically adjust any parameter based on it’s performance. I use it in all of my strategies. Let me know if you are interested.
Sounds interesting Juanj. I’m sure lots of people would like to see how that works.
@Vonasi I would then need to direct your attention to this topic I started in 2017: https://www.prorealcode.com/topic/machine-learning-in-proorder/
This is the early idea and birth place of the strategy. I will post the latest version there
I’ve picked up on this. It’s a version of ML in a basic way. It does what I had in mind.
if previous traderesult is positive/negative, it increase (or decrease) the parameter within max/min values
There’s a reset, currently it reset’s the positionperformance for a period, it doesn’t reset the boxsize in a proper way.
And a mention to the snippet above which is optional with below. It calculates the % for the period and if the realised performance exceeds the preset value it stops trading for that period.
if positions have +0.5 +0.2+0.6+1.0 and the total is bigger then i.e. 2%, it won’t take new trades that period. Same for losses. It’s no stoploss, because it looks at realised trade results.
// dynamic parameter adjustment
once periodr = 1 // [0]none;[1]day;[2]week;[3]month;[4]year (reset period)
once direction= dd // [0] or [1]opposite
once valuex = aa
once valuey = bb
increment = 5
minvalue = 15
maxvalue = 40
// main setup
if periodr=0 then
if barindex=0 then
longperf=0
shortperf=0
endif
elsif periodr=1 then
if day<>day[1] then
longperf=0
shortperf=0
endif
elsif periodr=2 then
if dayofweek=0 then
longperf=0
shortperf=0
endif
elsif periodr=3 then
if month<>month[1] then
longperf=0
shortperf=0
endif
elsif periodr=4 then
if year<>year[1] then
longperf=0
shortperf=0
endif
endif
if longonmarket[1] and (not onmarket or shortonmarket) then
if strategyprofit[1]>=strategyprofit[2] then
longperf=longperf+positionperf(1)*100
else
longperf=longperf-positionperf(1)*100
endif
endif
if shortonmarket[1] and (not onmarket or longonmarket) then
if strategyprofit[1]>=strategyprofit[2] then
shortperf=shortperf+positionperf(1)*100
else
shortperf=shortperf-positionperf(1)*100
endif
endif
if direction=1 then
if longperf>longperf[1] then
if valuex+increment <= maxvalue then
valuex=valuex+increment
else
valuex=valuex
endif
elsif longperf<longperf[1] then
if valuex-increment >= minvalue then
valuex=valuex-increment
else
valuex=valuex
endif
endif
if shortperf>shortperf[1] then
if valuey+increment <= maxvalue then
valuey=valuey+increment
else
valuey=valuey
endif
elsif shortperf<shortperf[1] then
if valuey-increment >= minvalue then
valuey=valuey-increment
else
valuey=valuey
endif
endif
else
if longperf>longperf[1] then
if valuex-increment >= minvalue then
valuex=valuex-increment
else
valuex=valuex
endif
elsif longperf<longperf[1] then
if valuex+increment <= maxvalue then
valuex=valuex+increment
else
valuex=valuex
endif
endif
if shortperf>shortperf[1] then
if valuey-increment >= minvalue then
valuey=valuey-increment
else
valuey=valuey
endif
elsif shortperf<shortperf[1] then
if valuey+increment <= maxvalue then
valuey=valuey+increment
else
valuey=valuey
endif
endif
endif
graph longperf*10 coloured(0,0,255,255) as "long performance (*10)"
graph shortperf*10 coloured(255,0,0,255) as "short performance (*10)"
boxsizes=valuey
boxsizel=valuex
graph valuex coloured(0,200,0) as "boxsize long"
graph valuey coloured(200,0,0) as "boxsize short"
quick fix for the valuex/y reset when periodr > 0
optimise direction 0-1 & valuex/y
// dynamic parameter adjustment
once periodr = 0 // [0]none;[1]day;[2]week;[3]month;[4]year (reset period)
once direction= 0 // [0] or [1]opposite
once valuex = 45
once valuey = 45
once startvalueL=valuex
once startvalueS=valuey
increment = 5
minvalue = 10
maxvalue = 50
// main setup
if periodr=0 then
if barindex=0 then
longperf=0
shortperf=0
endif
elsif periodr=1 then
if day<>day[1] then
longperf=0
shortperf=0
endif
elsif periodr=2 then
if dayofweek=0 then
longperf=0
shortperf=0
endif
elsif periodr=3 then
if month<>month[1] then
longperf=0
shortperf=0
endif
elsif periodr=4 then
if year<>year[1] then
longperf=0
shortperf=0
endif
endif
if longonmarket[1] and (not onmarket or shortonmarket) then
if strategyprofit[1]>=strategyprofit[2] then
longperf=longperf+positionperf(1)*100
else
longperf=longperf-positionperf(1)*100
endif
endif
if shortonmarket[1] and (not onmarket or longonmarket) then
if strategyprofit[1]>=strategyprofit[2] then
shortperf=shortperf+positionperf(1)*100
else
shortperf=shortperf-positionperf(1)*100
endif
endif
if direction=1 then
if longperf<>0 then
if longperf>longperf[1] then
if valuex+increment <= maxvalue then
valuex=valuex+increment
else
valuex=valuex
endif
elsif longperf<longperf[1] then
if valuex-increment >= minvalue then
valuex=valuex-increment
else
valuex=valuex
endif
endif
else
valuex=startvalueL
endif
if shortperf<>0 then
if shortperf>shortperf[1] then
if valuey+increment <= maxvalue then
valuey=valuey+increment
else
valuey=valuey
endif
elsif shortperf<shortperf[1] then
if valuey-increment >= minvalue then
valuey=valuey-increment
else
valuey=valuey
endif
endif
else
valuey=startvalueS
endif
else
if longperf<>0 then
if longperf>longperf[1] then
if valuex-increment >= minvalue then
valuex=valuex-increment
else
valuex=valuex
endif
elsif longperf<longperf[1] then
if valuex+increment <= maxvalue then
valuex=valuex+increment
else
valuex=valuex
endif
endif
else
valuex=startvalueL
endif
if shortperf<>0 then
if shortperf>shortperf[1] then
if valuey-increment >= minvalue then
valuey=valuey-increment
else
valuey=valuey
endif
elsif shortperf<shortperf[1] then
if valuey+increment <= maxvalue then
valuey=valuey+increment
else
valuey=valuey
endif
endif
else
valuey=startvalueS
endif
endif
graph longperf*10 coloured(0,0,255,255) as "long performance (*10)"
graph shortperf*10 coloured(255,0,0,255) as "short performance (*10)"
boxsizes=valuey
boxsizel=valuex
graph valuex coloured(0,200,0) as "boxsize long"
graph valuey coloured(200,0,0) as "boxsize short"
a try, up 7 parameters! (vectorial)
it bouches around between min & max value and it checks if it’s better to increase after a win or decrease for each parameter. There is a reset which can be usefull. It only needs to test 0 – 1 except for the reset period if used.
valuex1 ->angle long
valuex2 -> angle short
valuex3-7 for the other parameters from the strategy.
remove once from mma/mmb etc.
// dynamic parameter adjustment
once periodr = pp // [0]none;[1]day;[2]week;[3]month;[4]year (reset period)
once route1 = p1 // [0] or [1]opposite specific for long
once route2 = p2 // [0] or [1]opposite specific for short
once route3 = p3 // [0] or [1]opposite
once route4 = p4 // [0] or [1]opposite
once route5 = p5 // [0] or [1]opposite
once route6 = p6 // [0] or [1]opposite
once route7 = p7 // [0] or [1]opposite
// long angle long
increment1 = 2
minvalue1 = 16
maxvalue1 = 32
// short angle short
increment2 = 2
minvalue2 = 16
maxvalue2 = 32
//
increment3 = 1 //periodea 10
minvalue3 = 8
maxvalue3 = 12
//
increment4 = 1 //nbchandeliera 15
minvalue4 = 13
maxvalue4 = 17
//
increment5 = 1 //periodeb 20
minvalue5 = 18
maxvalue5 = 22
//
increment6 = 1 //nbchandelierb 35
minvalue6 = 30
maxvalue6 = 36
//
increment7 = 1 //lag 5
minvalue7 = 0
maxvalue7 = 6
once valuex1 = (minvalue1+maxvalue1)/2
once valuex2 = (minvalue2+maxvalue2)/2
once valuex3 = (minvalue3+maxvalue3)/2
once valuex4 = (minvalue4+maxvalue4)/2
once valuex5 = (minvalue5+maxvalue5)/2
once valuex6 = (minvalue6+maxvalue6)/2
once valuex7 = (minvalue6+maxvalue7)/2
once startvalue1=valuex1
once startvalue2=valuex2
once startvalue3=valuex3
once startvalue4=valuex4
once startvalue5=valuex5
once startvalue6=valuex6
once startvalue7=valuex7
// main setup
if periodr=0 then
if barindex=0 then
longperf=0
shortperf=0
endif
elsif periodr=1 then
if day<>day[1] then
longperf=0
shortperf=0
endif
elsif periodr=2 then
if dayofweek=0 then
longperf=0
shortperf=0
endif
elsif periodr=3 then
if month<>month[1] then
longperf=0
shortperf=0
endif
elsif periodr=4 then
if year<>year[1] then
longperf=0
shortperf=0
endif
endif
if longonmarket[1] and (not onmarket or shortonmarket) then
if strategyprofit[1]>=strategyprofit[2] then
longperf=longperf+positionperf(1)*100
else
longperf=longperf-positionperf(1)*100
endif
endif
if shortonmarket[1] and (not onmarket or longonmarket) then
if strategyprofit[1]>=strategyprofit[2] then
shortperf=shortperf+positionperf(1)*100
else
shortperf=shortperf-positionperf(1)*100
endif
endif
if route1=1 then
if longperf<>0 then
if longperf>longperf[1] then
if valuex1+increment1 <= maxvalue1 then
valuex1=valuex1+increment1
else
valuex1=valuex1
endif
elsif longperf<longperf[1] then
if valuex1-increment1 >= minvalue1 then
valuex1=valuex1-increment1
else
valuex1=valuex1
endif
endif
else
valuex1=startvalue1
endif
else
if longperf<>0 then
if longperf>longperf[1] then
if valuex1-increment1 >= minvalue1 then
valuex1=valuex1-increment1
else
valuex1=valuex1
endif
elsif longperf<longperf[1] then
if valuex1+increment1 <= maxvalue1 then
valuex1=valuex1+increment1
else
valuex1=valuex1
endif
endif
else
valuex1=startvalue1
endif
endif
if route2=1 then
if shortperf<>0 then
if shortperf>shortperf[1] then
if valuex2+increment2 <= maxvalue2 then
valuex2=valuex2+increment2
else
valuex2=valuex2
endif
elsif shortperf<shortperf[1] then
if valuex2-increment2 >= minvalue2 then
valuex2=valuex2-increment2
else
valuex2=valuex2
endif
endif
else
valuex2=startvalue2
endif
else
if shortperf<>0 then
if shortperf>shortperf[1] then
if valuex2-increment2 >= minvalue2 then
valuex2=valuex2-increment2
else
valuex2=valuex2
endif
elsif shortperf<shortperf[1] then
if valuex2+increment2 <= maxvalue2 then
valuex2=valuex2+increment2
else
valuex2=valuex2
endif
endif
else
valuex2=startvalue2
endif
endif
if route3=1 then //periodea
if shortperf<>0 or longperf<>0 then
if shortperf>shortperf[1] or longperf>longperf[1] then
if valuex3+increment3 <= maxvalue3 then
valuex3=valuex3+increment3
else
valuex3=valuex3
endif
elsif shortperf<shortperf[1] or longperf<longperf[1] then
if valuex3-increment3 >= minvalue3 then
valuex3=valuex3-increment3
else
valuex3=valuex3
endif
endif
else
valuex3=startvalue3
endif
else
if shortperf<>0 or longperf<>0 then
if shortperf>shortperf[1] or longperf>longperf[1] then
if valuex3-increment3 >= minvalue3 then
valuex3=valuex3-increment3
else
valuex3=valuex3
endif
elsif shortperf<shortperf[1] or longperf<longperf[1] then
if valuex3+increment3 <= maxvalue3 then
valuex3=valuex3+increment3
else
valuex3=valuex3
endif
endif
else
valuex3=startvalue3
endif
endif
if route4=1 then //nbchandeliera
if shortperf<>0 or longperf<>0 then
if shortperf>shortperf[1] or longperf>longperf[1] then
if valuex4+increment4 <= maxvalue4 then
valuex4=valuex4+increment4
else
valuex4=valuex4
endif
elsif shortperf<shortperf[1] or longperf<longperf[1] then
if valuex4-increment4 >= minvalue4 then
valuex4=valuex4-increment4
else
valuex4=valuex4
endif
endif
else
valuex4=startvalue4
endif
else
if shortperf<>0 or longperf<>0 then
if shortperf>shortperf[1] or longperf>longperf[1] then
if valuex4-increment4 >= minvalue4 then
valuex4=valuex4-increment4
else
valuex4=valuex4
endif
elsif shortperf<shortperf[1] or longperf<longperf[1] then
if valuex4+increment4 <= maxvalue4 then
valuex4=valuex4+increment4
else
valuex4=valuex4
endif
endif
else
valuex4=startvalue4
endif
endif
if route5=1 then //periodeb
if shortperf<>0 or longperf<>0 then
if shortperf>shortperf[1] or longperf>longperf[1] then
if valuex5+increment5 <= maxvalue5 then
valuex5=valuex5+increment5
else
valuex5=valuex5
endif
elsif shortperf<shortperf[1] or longperf<longperf[1] then
if valuex5-increment5 >= minvalue5 then
valuex5=valuex5-increment5
else
valuex5=valuex5
endif
endif
else
valuex5=startvalue5
endif
else
if shortperf<>0 or longperf<>0 then
if shortperf>shortperf[1] or longperf>longperf[1] then
if valuex5-increment5 >= minvalue5 then
valuex5=valuex5-increment5
else
valuex5=valuex5
endif
elsif shortperf<shortperf[1] or longperf<longperf[1] then
if valuex5+increment5 <= maxvalue5 then
valuex5=valuex5+increment5
else
valuex5=valuex5
endif
endif
else
valuex5=startvalue5
endif
endif
if route6=1 then //nbchandelierb
if shortperf<>0 or longperf<>0 then
if shortperf>shortperf[1] or longperf>longperf[1] then
if valuex6+increment6 <= maxvalue6 then
valuex6=valuex6+increment6
else
valuex6=valuex6
endif
elsif shortperf<shortperf[1] or longperf<longperf[1] then
if valuex6-increment6 >= minvalue6 then
valuex6=valuex6-increment6
else
valuex6=valuex6
endif
endif
else
valuex6=startvalue6
endif
else
if shortperf<>0 or longperf<>0 then
if shortperf>shortperf[1] or longperf>longperf[1] then
if valuex6-increment6 >= minvalue6 then
valuex6=valuex6-increment6
else
valuex6=valuex6
endif
elsif shortperf<shortperf[1] or longperf<longperf[1] then
if valuex6+increment6 <= maxvalue6 then
valuex6=valuex6+increment6
else
valuex6=valuex6
endif
endif
else
valuex6=startvalue6
endif
endif
if route7=1 then //lag
if shortperf<>0 or longperf<>0 then
if shortperf>shortperf[1] or longperf>longperf[1] then
if valuex7+increment7 <= maxvalue7 then
valuex7=valuex7+increment7
else
valuex7=valuex7
endif
elsif shortperf<shortperf[1] or longperf<longperf[1] then
if valuex7-increment7 >= minvalue7 then
valuex7=valuex7-increment7
else
valuex7=valuex7
endif
endif
else
valuex7=startvalue7
endif
else
if shortperf<>0 or longperf<>0 then
if shortperf>shortperf[1] or longperf>longperf[1] then
if valuex7-increment7 >= minvalue7 then
valuex7=valuex7-increment7
else
valuex7=valuex7
endif
elsif shortperf<shortperf[1] or longperf<longperf[1] then
if valuex7+increment7 <= maxvalue7 then
valuex7=valuex7+increment7
else
valuex7=valuex7
endif
endif
else
valuex7=startvalue7
endif
endif
Line 52 should not be
once valuex7 = (minvalue7+maxvalue7)/2
?
yes, saw it later after posting. More bugs most likely. Need a reset day/week/month i.e. on 5 min. to make sense, if it does anyway. The min+max/2 value can be replace with a preferred number. I.e. lag, put in 5, with increment 5 and it will switch between 0 and 5. (with minvalue=0/maxvalue=5)
Clear.. btw i was thinking: you can choose 0 and 1 to select the direction of the increment; it’s not possible to have both? The system can handle at the same time the decision of going up and down?
Altered the min/max values and got it to where it’s very close to the non-dynamic version. Tried using a fixed number for startvalue but (min+max)/2 was better. Feels like it needs just another little push to get up into positive territory!
// dynamic parameter adjustment
once periodr = 1 // [0]none;[1]day;[2]week;[3]month;[4]year (reset period)
once route1 = 1 // [0] or [1]opposite specific for long
once route2 = 1 // [0] or [1]opposite specific for short
once route3 = 1 // [0] or [1]opposite
once route4 = 1 // [0] or [1]opposite
once route5 = 1 // [0] or [1]opposite
once route6 = 1 // [0] or [1]opposite
once route7 = 1 // [0] or [1]opposite
// long angle long 45
increment1 = 2
minvalue1 = 37
maxvalue1 = 53
// short angle short 34
increment2 = 2
minvalue2 = 26
maxvalue2 = 42
//
increment3 = 1 //periodea 10
minvalue3 = 8
maxvalue3 = 12
//
increment4 = 1 //nbchandeliera 14
minvalue4 = 12
maxvalue4 = 16
//
increment5 = 1 //periodeb 22
minvalue5 = 20
maxvalue5 = 24
//
increment6 = 1 //nbchandelierb 34
minvalue6 = 32
maxvalue6 = 36
//
increment7 = 1 //lag 4
minvalue7 = 0
maxvalue7 = 6
dynamic live parameter adjustment
This topic contains 31 replies,
has 8 voices, and was last updated by Francesco
5 years, 7 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 03/02/2020 |
| Status: | Active |
| Attachments: | 4 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.