Carver Hybrid ProOrder Code
Forums › ProRealTime English forum › ProOrder support › Carver Hybrid ProOrder Code
- This topic has 44 replies, 11 voices, and was last updated 4 years ago by grimweasel47.
-
-
01/13/2020 at 1:45 PM #116639
Hi Folks
I’m moving on from my basic breakout indicator to try and code a simple trend following system with some ATR inputs for risk sizing and profit taking.
The system should trade the Daily TF of xxx FX pair. The rules are quite simple.
Long
21ema >63ema AND Carver 1 indicator >0 AND price is greater than previous day’s close
Short
21ema <63ema AND Carver 1 Indicator <0 AND price is lower than previous day’s close
I’m getting a infinite loop error so any help would be appreciated! Also any other glaring code errors you see would be insightful!
I have placed the indicator code for Carver 1 below the Pro Real Code.
System123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125defparam cumulateorders=false// --- settingsbalance = 10000 //balance of the strategy when activated the first timeminlot = 1 //minimum lot size for the current instrument (example: 1 for DAX)riskpercent = 2 //risk percent per trade//activationtime = 220000//Close time of the candle to activate the strategy//LimitHour = 130000 //Time to close off strategyif dayofweek=1 then //Mondaydaytrading=1endifif dayofweek=2 then // Tuesdaydaytrading=1endifif dayofweek=3 then // Wednesdaydaytrading=1endifif dayofweek=4 then //Thursdaydaytrading=1endifif dayofweek=5 then // Fridaydaytrading=0endifif dayofweek=6 or dayofweek=7 then //Sat and Sundaytrading=0endif//-- indicatorsema = exponentialaverage[63](close)ema2 = exponentialaverage[21](close)EMAabove = ema2>emaEMAbelow = ema2<emaatr = averagetruerange[24]hh = highest[1](high)+1ll = lowest[1](low)-1//set BreakoutLongBO = CALL"Carver 1"[close >0.01]ShortBO= CALL"Carver 1"[close <0.00]Long = emaabove and LongBOShort = emabelow and ShortBOif intradaybarindex=0 thenalreadytraded = 0case = 0levelhi = 0levello = 0endifif onmarket or (onmarket[1] and not onmarket) or (currentprofit<>strategyprofit) thenalreadytraded = 1endif//if time=activationtime then// case 1 : If price candle touches MA (even wicks) then look at high or low of signal candleif high>ema and low<ema thencase = 1levelhi = CALL"#floor and ceil"[high,10.0,1]levello = CALL"#floor and ceil"[low,10.0,-1]endif//case 2 : If price is above the MA then only trade long BUT only above the highest high of the past 24 hrsif close >ema and long and case = 0 thencase = 2levelhi = hh[1]endif//case 3 : If price is below the MA then only trade short BUT only below the lowest low of the past 24 hrsif close <ema and short and case = 0 thencase = 3levello = ll[1]endif//endifif alreadytraded = 0 then//money managementif case=1 thendaytrading=0elseStopLoss = 1*ATRendifRisk = riskpercent/100//calculate contractsequity = balance + StrategyProfitmaxrisk = round(equity*Risk)size = max(minlot,abs(round((maxrisk/StopLoss)/PointValue)*pipsize))//in all cases put pending orders on marketwhile case <> 0 and daytrading=1 do //(time >= activationtime and time <=LimitHour) doif levelhi>0 thenbuy size contract at levelhi stopendifif levello>0 thensellshort size contract at levello stopendifwendendif// Friday 22:00 Close ALL operations.IF onmarket and (DayOfWeek = 5 AND time >= 220000) THENSELL AT MARKETEXITSHORT AT MARKETENDIF//set target and profitif case = 1 thendaytrading=0endifif case = 2 or case = 3 thenset target profit 1.4*ATRset stop loss StopLossendifcurrentprofit = strategyprofit//debugging//graph case as "case"//graph time=activationtime coloured(100,120,133) as "activation time!"//graph time//graph ema//graph levelhi coloured(0,200,0) as "level high"//graph levello coloured(200,0,0) as "level low"//graph size as "mm"Carver 1 Indy123456789n=21a = highest[n](high[0])b = lowest[n](low[0])c = (a+b)/2scaledprice = (close-c)/(a-b)zero =0return scaledprice as "scaled price"01/13/2020 at 2:53 PM #116656Because there is indeed an infinite loop! You are creating it at line 89, once the loop is started you are waiting in it that the “case” variable change, while it can’t since there is no instruction to change it in the loop! 🙂
Anyway, there is no need to use loop there, just replace the WHILE/WEND instructions with a IF/ENDIF block.
01/13/2020 at 3:02 PM #116657Try replacing line 89 with:
1while Xcase <> 0 and daytrading=1 and not OnMarket doadd this line before line 89:
1xCase = caseand add this line just before line 95 (wend):
1xCase = 0this could trick ProOrder into thinking the loop is not infinite (not tested)
01/13/2020 at 6:56 PM #116682Super thanks Guys – that’s got rid of the Infinite Loop. I have tidied up the code too but alas it’s not taking any trades in the backtest? I have adapted this from a code that is working. Is there something the code is missing or not doing? It should only trade on the daily TF?
Thanks
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117defparam cumulateorders=false// --- settingsbalance = 10000 //balance of the strategy when activated the first timeminlot = 1 //minimum lot size for the current instrument (example: 1 for DAX)riskpercent = 2 //risk percent per trade//settings for days to tradeif dayofweek=1 then //Mondaydaytrading=1endifif dayofweek=2 then // Tuesdaydaytrading=1endifif dayofweek=3 then // Wednesdaydaytrading=1endifif dayofweek=4 then //Thursdaydaytrading=1endifif dayofweek=5 then // Fridaydaytrading=1endifif dayofweek=6 or dayofweek=7 then //Sat and Sundaytrading=0endif//-- indicators//emasema = exponentialaverage[21](close)ema2 = exponentialaverage[63](close)EMAabove = ema2>emaEMAbelow = ema2<ema//ATRatr = averagetruerange[24]//Highest/lowest candlehh = highest[1](high)+1ll = lowest[1](low)-1//set BreakoutLongBO = CALL"Carver 1"[close >0.01]ShortBO= CALL"Carver 1"[close <0.00]//Trade entry rulesLong = emaabove and LongBOShort = emabelow and ShortBOif intradaybarindex=0 thenalreadytraded = 0case = 0levelhi = 0levello = 0endifif onmarket or (onmarket[1] and not onmarket) or (currentprofit<>strategyprofit) thenalreadytraded = 1endif//case 2 : If price is above the MA then only trade long BUT only above the highest high of the past 24 hrsif close >ema and long and case = 0 thencase = 2levelhi = hh[1]endif//case 3 : If price is below the MA then only trade short BUT only below the lowest low of the past 24 hrsif close <ema and short and case = 0 thencase = 3levello = ll[1]endif//endifXCase=caseif alreadytraded = 0 then//money management//if case=1 then//daytrading=0//elseStopLoss = 1*ATRendif//Risk SettingRisk = riskpercent/100//calculate contractsequity = balance + StrategyProfitmaxrisk = round(equity*Risk)size = max(minlot,abs(round((maxrisk/StopLoss)/PointValue)*pipsize))//in all cases put pending orders on marketwhile Xcase <> 0 and daytrading=1 and not OnMarket do //(time >= activationtime and time <=LimitHour) doif levelhi>0 thenbuy size contract at levelhi stopendifif levello>0 thensellshort size contract at levello stopendifxCase=0wend// Friday 22:00 Close ALL operations.IF onmarket and (DayOfWeek = 5 AND time >= 220000) THENSELL AT MARKETEXITSHORT AT MARKETENDIF//set target and profitif case = 2 or case = 3 thenset target profit 1.4*ATRset stop loss StopLossendifcurrentprofit = strategyprofit//debugging//graph case as "case"//graph time=activationtime coloured(100,120,133) as "activation time!"//graph time//graph ema//graph levelhi coloured(0,200,0) as "level high"//graph levello coloured(200,0,0) as "level low"//graph size as "mm"01/14/2020 at 1:26 AM #116712I cannot test it because I don’t have the indicator you are using, but I want to remark that lines 35-36 could be logically incorrect. I don’t know what you want to achieve with those two lines, but, despite what they look like, they could be easily written as:
12hh = high+1ll = low-1or (as I encourage you to do, to make your code portable to all instruments):
12hh = high + 1*pipsizell = low - 1*pipsize01/14/2020 at 12:13 PM #11676401/15/2020 at 12:36 PM #116862Thanks GraHal – I actually got it working. I changed the code to that of what Robert suggested at lines 35 and 36.
There are a couple of tweaks that I think are worthy of testing. Limiting the profits with a trend system based on ATR could be counter-productive as one needs to collect maximum rent from the trends that keep going. Would it be better to implement a trailing stop as Carver suggests?
Also, when calling an indicator how can we ‘test’ for differing lengths in Carver 1? Carver conducted extensive back testing in his book and the best lengths for Carver 1 are 10,20,40,80,160 and 320. When calling an indicator does it use the value on the chart being tested? Can it be changed in the code (ie tested under an ‘n’ variable)?
Also, this system doesn’t add on. Carver recommends taking a ‘nibble’ at the first entry signal and then scaling in, protecting profits with a trailing stop. Could this be added to the code as it’s a little beyond my capabilities.
I have also added a 10ema to the Carver 1- the reason being I only want to initiate a trade (long) if Carver 1 is >0 but ALSO >10ema to prove value is not falling.
I don’t want to complicate the system as that goes against the simplicity of a trend breakout system. Many of the big hedge funds will tell you that overly complex systems that backtest well will nearly always fail in real markets. The fewer rules, the better the expected Sharpe Ratio of the system. To increase the Sharpe you are better adding more markets than more trading rules – you achieve a much better payoff!
Version 3123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118defparam cumulateorders=false// --- settingsbalance = 10000 //balance of the strategy when activated the first timeminlot = 1 //minimum lot size for the current instrument (example: 1 for DAX)riskpercent = 2 //risk percent per trade//settings for days to tradeif dayofweek=1 then //Mondaydaytrading=1endifif dayofweek=2 then // Tuesdaydaytrading=1endifif dayofweek=3 then // Wednesdaydaytrading=1endifif dayofweek=4 then //Thursdaydaytrading=1endifif dayofweek=5 then // Fridaydaytrading=1endifif dayofweek=6 or dayofweek=7 then //Sat and Sundaytrading=0endif//-- indicators//emasema = exponentialaverage[63](close)ema2 = exponentialaverage[21](close)EMAabove = ema2>emaEMAbelow = ema2<ema//ATRatr = averagetruerange[24]//Highest/lowest candlehh = high + 1*pipsizell = low - 1*pipsize//set BreakoutMA= CALL"Carver 1"[exponentialaverage[10](close)]LongBO = CALL"Carver 1"[close >0.01 and close > MA]ShortBO= CALL"Carver 1"[close <-0.01 and close < MA]//Trade entry rulesLong = emaabove and LongBOShort = emabelow and ShortBOif intradaybarindex=0 thenalreadytraded = 0case = 0levelhi = 0levello = 0endifif onmarket or (onmarket[1] and not onmarket) or (currentprofit<>strategyprofit) thenalreadytraded = 1endif//case 2 : If price is above the MA then only trade long BUT only above the highest high of the past 24 hrsif close >ema and long and case = 0 thencase = 2levelhi = hh[1]endif//case 3 : If price is below the MA then only trade short BUT only below the lowest low of the past 24 hrsif close <ema and short and case = 0 thencase = 3levello = ll[1]endif//endifXCase=caseif alreadytraded = 0 then//money management//if case=1 then//daytrading=0//elseStopLoss = 1.4*ATRendif//Risk SettingRisk = riskpercent/100//calculate contractsequity = balance + StrategyProfitmaxrisk = round(equity*Risk)size = max(minlot,abs(round((maxrisk/StopLoss)/PointValue)*pipsize))//in all cases put pending orders on marketwhile Xcase <> 0 and daytrading=1 and not OnMarket do //(time >= activationtime and time <=LimitHour) doif levelhi>0 thenbuy size contract at levelhi stopendifif levello>0 thensellshort size contract at levello stopendifxCase=0wend// Friday 22:00 Close ALL operations.IF onmarket and (DayOfWeek = 5 AND time >= 220000) THENSELL AT MARKETEXITSHORT AT MARKETENDIF//set target and profitif case = 2 or case = 3 thenset target profit 1.7*ATRset stop loss stoplossendifcurrentprofit = strategyprofit//debugging//graph case as "case"//graph time=activationtime coloured(100,120,133) as "activation time!"//graph time//graph ema//graph levelhi coloured(0,200,0) as "level high"//graph levello coloured(200,0,0) as "level low"//graph size as "mm"01/15/2020 at 6:18 PM #116905Ok, so I have added a Trailing stop to the code and the changes to the system (DAX anyway) seem impressive. I backtested in 50% of the data available and then did WF analysis on the other half of the data, and came out with around the same results of 60% winning trades and Gain/Loss of 6.4 times
Since this seems too good to be true I wonder what I’m missing? I also added spread of 1.5
I’ve used ATR of 21 and also the 21ema and 63ema (3x this as one business quarter) as I know from my time in the City, that the ‘machine’ (algos) at many CTA/Hedge funds are mainly interested in 1 month momentum.
Version 4123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120defparam cumulateorders=false// --- settingsbalance = 10000 //balance of the strategy when activated the first timeminlot = 1 //minimum lot size for the current instrument (example: 1 for DAX)riskpercent = 2 //risk percent per trade//settings for days to tradeif dayofweek=1 then //Mondaydaytrading=1endifif dayofweek=2 then // Tuesdaydaytrading=1endifif dayofweek=3 then // Wednesdaydaytrading=1endifif dayofweek=4 then //Thursdaydaytrading=1endifif dayofweek=5 then // Fridaydaytrading=1endifif dayofweek=6 or dayofweek=7 then //Sat and Sundaytrading=0endif//-- indicators//emasema = exponentialaverage[63](close)ema2 = exponentialaverage[21](close)EMAabove = ema2>emaEMAbelow = ema2<ema//ATRatr = averagetruerange[21]//Highest/lowest candlehh = high+1*pipsizell = low-1*pipsize//set BreakoutMA= CALL"Carver 1"[exponentialaverage[10](close)]LongBO = CALL"Carver 1"[close >0.01 and close > MA]ShortBO= CALL"Carver 1"[close <-0.01 and close < MA]//Trade entry rulesLong = emaabove and LongBOShort = emabelow and ShortBOif intradaybarindex=0 thenalreadytraded = 0case = 0levelhi = 0levello = 0endifif onmarket or (onmarket[1] and not onmarket) or (currentprofit<>strategyprofit) thenalreadytraded = 1endif//case 2 : If price is above the MA then only trade long BUT only above the highest high of the past 24 hrsif long and close >ema and case = 0 thencase = 2levelhi = hh[1]endif//case 3 : If price is below the MA then only trade short BUT only below the lowest low of the past 24 hrsif short and close < ema and case = 0 thencase = 3levello = ll[1]endif//endifXCase=caseif alreadytraded = 0 then//money management//if case=1 then//daytrading=0//elseStopLoss = 1.4*ATRendif//Risk SettingRisk = riskpercent/100//calculate contractsequity = balance + StrategyProfitmaxrisk = round(equity*Risk)size = max(minlot,abs(round((maxrisk/StopLoss)/PointValue)*pipsize))//in all cases put pending orders on marketwhile Xcase <> 0 and daytrading=1 and not OnMarket do //(time >= activationtime and time <=LimitHour) doif levelhi>0 thenbuy size contract at levelhi stopendifif levello>0 thensellshort size contract at levello stopendifxCase=0wend// Friday 22:00 Close ALL operations.IF onmarket and (DayOfWeek = 5 AND time >= 220000) THENSELL AT MARKETEXITSHORT AT MARKETENDIF//set target and profitif case = 2 or case = 3 thenset target profit 10*ATRset stop loss stoploss trailing ATR*0.2endifcurrentprofit = strategyprofit//debugging//graph case as "case"//graph time=activationtime coloured(100,120,133) as "activation time!"//graph time//graph ema//graph levelhi coloured(0,200,0) as "level high"//graph levello coloured(200,0,0) as "level low"//graph size as "mm"01/15/2020 at 6:25 PM #11690801/15/2020 at 6:33 PM #116910Haha that’s why I think the backtest is throwing up bad results!
Just worked at NYSE Euronext for a year (which is why I think most lower TF trading is pointless as the Algos have all those timeframes sewn-up – they will always be quicker, have co-location with the matching engines etc). Then worked in Private Wealth mostly since…
1 user thanked author for this post.
01/15/2020 at 8:23 PM #116923which is why I think most lower TF trading is pointless
Finally someone who thinks like I do!
1 user thanked author for this post.
01/15/2020 at 9:46 PM #116931Hi Grimweasel,
Thanks for sharing your strategy but I cannot test it as the Indicator “Carver 1” doesn’t work.
123456789n=21a = highest[n](high[0])b = lowest[n](low[0])c = (a+b)/2scaledprice = (close-c)/(a-b)zero =0return scaledprice as "scaled price"it seems there is a problem with zero ?
Can you check ? Thanks in advance
01/15/2020 at 10:43 PM #116934Hi Lifen
Sorry I’m not sure why? Did you create the indicator and place into your local library and charts, otherwise the call function won’t work? The code is exactly as I have added to my chart.
01/16/2020 at 9:22 AM #11696001/16/2020 at 10:48 AM #116973 -
AuthorPosts
Find exclusive trading pro-tools on