ProRealCode - Trading & Coding with ProRealTime™
What you have posted is not a complete strategy. Where are the buy and sell orders?
Hi @vonasi,
This is the complete code. I’ve managed to get it working by removing SlowLength and FastLength as the error stated the code did not use either variable. Also I’m not sure if i have the UKDSL correct as i’ve removed the US version. Would really appreciate it if someone could just check over that section just to make sure its correct:
DEFPARAM CumulateOrders = false
DEFPARAM PRELOADBARS = 1000
SL = 200
SlowPipDisplace = 0
FastPipDisplace = 0
length = 200
startingvalue = 50
startingvalue2 = 5
// --------- UK DAY LIGHT SAVINGS MONTHS ---------------- //
mar = month = 3 // MONTH START
oct = month = 10 // MONTH END
IF ( dayofweek >= 0 and mar AND 31-day<7 ) OR ( month > 3 AND month < 10 ) OR ( oct AND 31-day > 6 ) OR (dayofweek = 4 AND oct AND day<31) OR (dayofweek = 3 AND oct AND day+1<31) OR (dayofweek = 2 AND oct AND day+2<31) OR (dayofweek = 1 AND oct AND day+3<31) OR (dayofweek = 0 AND oct AND day+4<31) OR (dayofweek = 5 AND oct AND 31-day<7) THEN
UKDLS=1
ELSE
UKDLS=0
ENDIF
timeok = NOT(time >051500- UKDLS AND time <053000 - UKDLS) AND NOT(time >060000 - UKDLS AND time <070000 - UKDLS)
//startingvalue = 15 //5, 100, 10 boxsize
increment = 5 //5, 20, 10
maxincrement = 7 //5, 10 limit of no of increments either up or down
reps = 3 //1 number of trades to use for analysis //2
maxvalue = 70 //20, 300, 150 //maximum allowed value
minvalue = 50 //5, minimum allowed value
//startingvalue2 = 55 //5, 100, 50 stop loss
increment2 = 3 //5, 10
maxincrement2 = 7 //1, 30 limit of no of increments either up/down //4
reps2 = 3 //1, 2 nos of trades to use for analysis //3
maxvalue2 = 25 //20, 300, 200 maximum allowed value
minvalue2 = 5 //5, minimum allowed value
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 = 1 //positive increment position
once nincpos = 1 //negative increment position
once optimise = 0 //initialize heuristicks engine counter (must be incremented at position start or exit)
once mode1 = 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 optimise = 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]*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 = valuex + (increment*pincpos)
pincpos = pincpos + 1
mode1 = 1
elsif wincounta < wincountb and stratavga < stratavgb and mode1 = 2 then
valuex = valuex + (increment*pincpos)
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 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
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 = 1 //positive increment position
once nincpos2 = 1 //negative increment position
once optimise2 = 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 optimise2 = 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]*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 = 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
optimise2 = 0
endif
// heuristics algorithm 2 end
endif
//GRAPH valuex
//GRAPH valuey
timeframe(1 day)
volindic = (averagetruerange[5](close)/close)*100
timeframe(5 minute)
thigh1 = Highest[valuex](high)+ SlowPipDisplace*pointsize
tlow1 = Lowest[valuex](low)- SlowPipDisplace*pointsize
thigh2 = Highest[valuey](high)+ FastPipDisplace*pointsize
tlow2 = Lowest[valuey](low)- FastPipDisplace*pointsize
if barindex>2 then
if Close>line1[1] then
line1 = tlow1
else
line1 = thigh1
endif
if Close>line2[1] then
line2 = tlow2
else
line2 = thigh2
endif
endif
if (Close[0]<line1[0] and Close[0]<line2[0]) then
trend = 1
endif
if (Close[0]>line1[0] and Close[0]>line2[0]) then
trend = -1
endif
if (line1[0]>line2[0] or trend[0] = 1) then
trena = 1
endif
if (line1[0]<line2[0] or trend[0] = -1) then
trena = -1
endif
if trena<>trena[1] then
if trena=1 then
//bear
prefecttrend = 2
else
//bull
prefecttrend = 1
endif
endif
timeframe(default)
bollMA = average[length, 1](close)//50,1
STDDEV = STD[length]
bollUP = bollMA + 2 * STDDEV
bollDOWN = bollMA - 2 * STDDEV
bollPercent = 100 * (close - bollDOWN) / (bollUP - bollDOWN)
//====== Enter market - start =====
// LONG side
C1 = bollPercent > 60 AND prefecttrend[1] = 2 AND prefecttrend = 1
IF timeok AND Not OnMarket AND C1 AND volindic < 3.5 THEN
BUY 1 CONTRACT AT MARKET
SET STOP pLOSS SL
ENDIF
// SHORT side
C2 = bollPercent < 40 AND prefecttrend[1] = 1 AND prefecttrend = 2
IF timeok AND Not OnMarket AND C2 AND volindic < 3.5 THEN
SELLSHORT 1 CONTRACT AT MARKET
SET STOP pLOSS SL
ENDIF
//====== Enter market - end =====
//====== Exit market - start =====
X1 = prefecttrend[1] = 1 AND prefecttrend = 2
IF LONGONMARKET AND X1 THEN
SELL AT MARKET
ENDIF
X2 = prefecttrend[1] = 2 AND prefecttrend = 1
IF SHORTONMARKET AND X2 THEN
EXITSHORT AT MARKET
ENDIF
// Avoid losing trade not caught by training
tradegain = POSITIONPERF * 100
rangelevel = 100* (range)/close
gainenough = summation[barindex - tradeindex](tradegain > 0.2) > 1
closefast = gainenough AND rangelevel < 0.5 AND POSITIONPERF * 100 <= 0.1
IF closefast THEN
EXITSHORT AT MARKET
SELL AT MARKET
ENDIF
//====== Exit market - end =====
//====== Trailing Stop mechanism - start =====
trailingstart = (0.5 * SL ) / pointsize
trailingstep = (0.25 * SL ) / pointsize
//resetting variables when no trades are on market
if not onmarket then
priceexit = 0
endif
//case LONG order
if longonmarket then
//first move (breakeven)
IF priceexit=0 AND close-tradeprice(1) >= trailingstart*pointsize THEN
priceexit = tradeprice(1) + trailingstep*pointsize
ENDIF
//next moves
IF priceexit>0 THEN
P2 = close-priceexit >= trailingstart*pointsize
IF P2 THEN
priceexit = priceexit + trailingstep*pointsize
ENDIF
ENDIF
endif
//case SHORT order
if shortonmarket then
//first move (breakeven)
IF priceexit=0 AND tradeprice(1)-close >= trailingstart*pointsize THEN
priceexit = tradeprice(1) - trailingstep*pointsize
ENDIF
//next moves
IF priceexit>0 THEN
P2 = priceexit-close >= trailingstart*pointsize
IF P2 THEN
priceexit = priceexit - trailingstep*pointsize
ENDIF
ENDIF
endif
//exit on trailing stop price levels
if onmarket and priceexit>0 then
EXITSHORT AT priceexit STOP
SELL AT priceexit STOP
endif
//====== Trailing Stop mechanism - end =====
Could you please explain in simple terms what this section of the code is doing and should the same values be used with the UKDLS code?
timeok = NOT(time >051500- USDLS AND time <053000 - USDLS) AND NOT(time >060000 - USDLS AND time <070000 - USDLS)
Best Wishes,
Plaedies
Hi Guys,
On reflection of my last post i’m assuming this piece of software holds off from any trades during 2 periods :
05:15 – 05:30
06:00 – 07:00
As this is considered too much volitility? Would that be correct?
Best wishes,
Plaedies
Hi Plaedis,
This section is to avoid high spread period where you add the DLS (-USDLS) when it has to apply.
Code line 308 : volindic <3.5 is for high volatility
Thanks @Yomihou,
As I live in the UK I have deleted the USDLS code and instead used the below.. Would that be correct for the Wall Street DJI?
// --------- UK DAY LIGHT SAVINGS MONTHS ---------------- //
mar = month = 3 // MONTH START
oct = month = 10 // MONTH END
IF ( dayofweek >= 0 and mar AND 31-day<7 ) OR ( month > 3 AND month < 10 ) OR ( oct AND 31-day > 6 ) OR (dayofweek = 4 AND oct AND day<31) OR (dayofweek = 3 AND oct AND day+1<31) OR (dayofweek = 2 AND oct AND day+2<31) OR (dayofweek = 1 AND oct AND day+3<31) OR (dayofweek = 0 AND oct AND day+4<31) OR (dayofweek = 5 AND oct AND 31-day<7) THEN
UKDLS=1
ELSE
UKDLS=0
ENDIF
timeok = NOT(time >051500- UKDLS AND time <053000 - UKDLS) AND NOT(time >060000 - UKDLS AND time <070000 - UKDLS)
Hello All,
Sorry that I wasn’t available for couple of days. Thank you @Vonasi for helping to answer those question.
Due to much confusion with the time zone, let me create another version change to new york time zone, it will probably remove the need of conversion of daylight saving.
In fact, I realize I should not need to do that. You can actually change the Time Zone from Options -> Platform options -> Time zones & Trading hours -> Display all data in this time zone: <select the UTC+0800>
You can also prepare it into the ProOrder for demo or live account. Once prepared, it will record the preset UTC+0800. Then you may change back to your original time zone if you wish for other strategies.
Great, so should I revert back to the original USDLS code and use the platform to add the UTC+8 ?
Kind regards,
Plaedies
Great, so should I revert back to the original USDLS code and use the platform to add the UTC+8 ?
@Plaedies, yes, should revert it back.
Hi I am also new and having trouble. When trying to automate it says I need to edit the code. I tried doing as below but it didnt work?
DEFPARAM CumulateOrders = false
DEFPARAM PRELOADBARS = 1000
SL = 100
SlowPipDisplace = 0
FastPipDisplace = 0
SlowLength = 15
FastLength = 85
centertrend = 1
length = 27
// --------- US DAY LIGHT SAVINGS MONTHS ---------------- //
mar = month = 3 // MONTH START
nov = month = 11 // MONTH END
IF (month > 3 AND month < 11) OR (mar AND day>14) OR (mar AND day-dayofweek>7) OR (nov AND day<=dayofweek AND day<7) THEN
USDLS=010000
ELSE
USDLS=0
ENDIF
timeok = NOT(time >051500- USDLS AND time <053000 - USDLS) AND NOT(time >060000 - USDLS AND time <070000 - USDLS)
timeframe(5 minute)
thigh1 = Highest[SlowLength](high)+ SlowPipDisplace*pointsize
tlow1 = Lowest[SlowLength](low)- SlowPipDisplace*pointsize
thigh2 = Highest[FastLength](high)+ FastPipDisplace*pointsize
tlow2 = Lowest[FastLength](low)- FastPipDisplace*pointsize
if barindex>2 then
if Close>line1[1] then
line1 = tlow1
else
line1 = thigh1
endif
if Close>line2[1] then
line2 = tlow2
else
line2 = thigh2
endif
endif
if (Close[0]<line1[0] and Close[0]<line2[0]) then
trend = 1
endif
if (Close[0]>line1[0] and Close[0]>line2[0]) then
trend = -1
endif
if (line1[0]>line2[0] or trend[0] = 1) then
trena = 1
endif
if (line1[0]<line2[0] or trend[0] = -1) then
trena = -1
endif
if trena<>trena[1] then
if trena=1 then
//bear
prefecttrend = 2
else
//bull
prefecttrend = 1
endif
endif
timeframe(default)
once bb = src
if barindex>length then
src = (highest[length](high)+lowest[length](low))/2
rising = src-src[length]>0
falling = src-src[length]<0
aa = rising or falling
bb = exponentialaverage[centertrend](aa*src+(1-aa)*bb[1])
//—-
if bb>bb[1] then
//bull
trendimpulse = 1
elsif bb<bb[1] then
//bear
trendimpulse = 2
endif
endif
//====== Enter market - start =====
// LONG side
C1 = trendimpulse = 1 AND prefecttrend[1] = 2 AND prefecttrend = 1
IF timeok AND Not OnMarket AND C1 THEN
BUY 1 CONTRACT AT MARKET
SET STOP pLOSS SL
ENDIF
// SHORT side
C2 = trendimpulse = 2 AND prefecttrend[1] = 1 AND prefecttrend = 2
IF timeok AND Not OnMarket AND C2 THEN
SELLSHORT 1 CONTRACT AT MARKET
SET STOP pLOSS SL
ENDIF
//====== Enter market - end =====
//====== Exit market - start =====
X1 = prefecttrend[1] = 1 AND prefecttrend = 2
IF LONGONMARKET AND X1 THEN
SELL AT MARKET
ENDIF
X2 = prefecttrend[1] = 2 AND prefecttrend = 1
IF SHORTONMARKET AND X2 THEN
EXITSHORT AT MARKET
ENDIF
//====== Exit market - end =====
Lines 23-62 are in blue in the code editor – like something is wrong?
@abro163 , the colored “zones” simply shows the Timeframe. Line 23-60 in this case means that the code within those lines are for the 5 minute Timeframe.
Thanks keewee 🙂 Anyhow I figured it out, you have to remove all the variables from the code editor optimization window.
I’m confused about this part of the code:
if (line1[0]>line2[0] or trend[0] = 1) then
trena = 1
endif
if (line1[0]<line2[0] or trend[0] = -1) then
trena = -1
endif
Its logic seem to favor trena=-1 (since it’s only 1 if trend = 1 and line1>=line2). How does this make sense?
Also, I’m confused by the numbers switching from 1 for bullish to -1 for bullish to 2 for bearish, etc. It would make more sense to just use 1 for bullish and -1 for bearish imho.
Does anyone else have any comments about the code? Maybe we can improve it together 😉
Strategy TrendImpulse v1
This topic contains 74 replies,
has 6 voices, and was last updated by thomas2004ch
3 years, 1 month ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 04/27/2020 |
| Status: | Active |
| Attachments: | 19 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.