Carver Hybrid ProOrder Code

Viewing 15 posts - 1 through 15 (of 45 total)
  • Author
    Posts
  • #116639 quote
    grimweasel47
    Participant
    Senior

    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.

    defparam cumulateorders=false
    
    // --- settings
    balance = 10000 //balance of the strategy when activated the first time
    minlot = 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 strategy
    if dayofweek=1 then //Monday
    daytrading=1
    endif
    if dayofweek=2 then // Tuesday
    daytrading=1
    endif
    if dayofweek=3 then // Wednesday
    daytrading=1
    endif
    if dayofweek=4 then //Thursday
    daytrading=1
    endif
    if dayofweek=5 then // Friday
    daytrading=0
    endif
    if dayofweek=6 or dayofweek=7 then //Sat and Sun
    daytrading=0
    endif
    //-- indicators
    ema = exponentialaverage[63](close)
    ema2 = exponentialaverage[21](close)
    EMAabove = ema2>ema
    EMAbelow = ema2<ema
    atr = averagetruerange[24]
    hh = highest[1](high)+1
    ll = lowest[1](low)-1
    //set Breakout
    LongBO = CALL"Carver 1"[close >0.01]
    ShortBO= CALL"Carver 1"[close <0.00]
    
    
    Long = emaabove and LongBO
    Short = emabelow and ShortBO
    
    
    if intradaybarindex=0 then
    alreadytraded = 0
    case = 0
    levelhi = 0
    levello = 0
    endif
    
    if onmarket or (onmarket[1] and not onmarket) or (currentprofit<>strategyprofit) then
    alreadytraded = 1
    endif
    
    //if time=activationtime  then
    // case 1 : If price candle touches MA (even wicks) then look at high or low of signal candle
    if high>ema and low<ema then
    case = 1
    levelhi = 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 hrs
    if close >ema and long  and case = 0 then
    case = 2
    levelhi = 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 hrs
    if close <ema and short and case = 0 then
    case = 3
    levello = ll[1]
    endif
    //endif
    
    if alreadytraded = 0 then
    //money management
    if case=1 then
    daytrading=0
    else
    StopLoss = 1*ATR
    endif
    Risk = riskpercent/100
    //calculate contracts
    equity = balance + StrategyProfit
    maxrisk = round(equity*Risk)
    size = max(minlot,abs(round((maxrisk/StopLoss)/PointValue)*pipsize))
    //in all cases put pending orders on market
    while case <> 0 and daytrading=1  do //(time >= activationtime and time <=LimitHour) do
    if levelhi>0 then
    buy size contract at levelhi stop
    endif
    if levello>0 then
    sellshort size contract at levello stop
    endif
    wend
    endif
    
    // Friday 22:00 Close ALL operations.
    IF onmarket and (DayOfWeek = 5 AND time >= 220000) THEN
    SELL AT MARKET
    EXITSHORT AT MARKET
    ENDIF
    
    //set target and profit
    
    if case = 1 then
    daytrading=0
    endif
    if case = 2 or case = 3 then
    set target profit 1.4*ATR
    set stop loss StopLoss
    endif
    
    
    currentprofit = 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"
    
    n=21
     
    a = highest[n](high[0])
    b = lowest[n](low[0])
    c = (a+b)/2
     
    scaledprice = (close-c)/(a-b)
    zero =0
    return scaledprice as "scaled price"
    
    #116656 quote
    Nicolas
    Keymaster
    Master

    Because 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.

    #116657 quote
    robertogozzi
    Moderator
    Master

    Try replacing line 89 with:

    while Xcase <> 0 and daytrading=1 and not OnMarket do

    add this line before line 89:

    xCase = case

    and add this line just before line 95 (wend):

    xCase = 0

    this could trick ProOrder into thinking the loop is not infinite (not tested)

    #116682 quote
    grimweasel47
    Participant
    Senior

    Super 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

    defparam cumulateorders=false
    // --- settings
    balance = 10000 //balance of the strategy when activated the first time
    minlot = 1 //minimum lot size for the current instrument (example: 1 for DAX)
    riskpercent = 2 //risk percent per trade
    //settings for days to trade
    if dayofweek=1 then //Monday
    daytrading=1
    endif
    if dayofweek=2 then // Tuesday
    daytrading=1
    endif
    if dayofweek=3 then // Wednesday
    daytrading=1
    endif
    if dayofweek=4 then //Thursday
    daytrading=1
    endif
    if dayofweek=5 then // Friday
    daytrading=1
    endif
    if dayofweek=6 or dayofweek=7 then //Sat and Sun
    daytrading=0
    endif
    //-- indicators
    
    //emas
    ema = exponentialaverage[21](close)
    ema2 = exponentialaverage[63](close)
    EMAabove = ema2>ema
    EMAbelow = ema2<ema
    //ATR
    atr = averagetruerange[24]
    //Highest/lowest candle
    hh = highest[1](high)+1
    ll = lowest[1](low)-1
    //set Breakout
    LongBO = CALL"Carver 1"[close >0.01]
    ShortBO= CALL"Carver 1"[close <0.00]
    
    //Trade entry rules
    Long = emaabove and LongBO
    Short = emabelow and ShortBO
    
    if intradaybarindex=0 then
    alreadytraded = 0
    case = 0
    levelhi = 0
    levello = 0
    endif
    
    if onmarket or (onmarket[1] and not onmarket) or (currentprofit<>strategyprofit) then
    alreadytraded = 1
    endif
    
    //case 2 : If price is above the MA then only trade long BUT only above the highest high of the past 24 hrs
    if close >ema and long  and case = 0 then
    case = 2
    levelhi = 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 hrs
    if close <ema and short and case = 0 then
    case = 3
    levello = ll[1]
    endif
    //endif
    XCase=case
    if alreadytraded = 0 then
    //money management
    //if case=1 then
    //daytrading=0
    //else
    StopLoss = 1*ATR
    endif
    //Risk Setting
    Risk = riskpercent/100
    
    //calculate contracts
    equity = balance + StrategyProfit
    maxrisk = round(equity*Risk)
    size = max(minlot,abs(round((maxrisk/StopLoss)/PointValue)*pipsize))
    
    //in all cases put pending orders on market
    while Xcase <> 0 and daytrading=1 and not OnMarket do //(time >= activationtime and time <=LimitHour) do
    if levelhi>0 then
    buy size contract at levelhi stop
    endif
    if levello>0 then
    sellshort size contract at levello stop
    endif
    xCase=0
    wend
    
    // Friday 22:00 Close ALL operations.
    IF onmarket and (DayOfWeek = 5 AND time >= 220000) THEN
    SELL AT MARKET
    EXITSHORT AT MARKET
    ENDIF
    
    //set target and profit
    
    if case = 2 or case = 3 then
    set target profit 1.4*ATR
    set stop loss StopLoss
    endif
    
    currentprofit = 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"
    
    #116712 quote
    robertogozzi
    Moderator
    Master

    I 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:

    hh = high+1
    ll = low-1

    or (as I encourage you to do, to make your code portable to all instruments):

    hh = high + 1*pipsize
    ll = low  - 1*pipsize
    #116764 quote
    GraHal
    Participant
    Master

    Rightly or wrongly 🙂 … I got it going and I left Line 35 and 36 as original (for now anyway).

    .itf attached

    I’m interested so please post any improvements.  I will post same also.

    grim.jpg grim.jpg GrimCarverHybrid-DAX-1H-v1.itf
    #116862 quote
    grimweasel47
    Participant
    Senior

    Thanks 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!

    defparam cumulateorders=false
    // --- settings
    balance = 10000 //balance of the strategy when activated the first time
    minlot = 1 //minimum lot size for the current instrument (example: 1 for DAX)
    riskpercent = 2 //risk percent per trade
    //settings for days to trade
    if dayofweek=1 then //Monday
    daytrading=1
    endif
    if dayofweek=2 then // Tuesday
    daytrading=1
    endif
    if dayofweek=3 then // Wednesday
    daytrading=1
    endif
    if dayofweek=4 then //Thursday
    daytrading=1
    endif
    if dayofweek=5 then // Friday
    daytrading=1
    endif
    if dayofweek=6 or dayofweek=7 then //Sat and Sun
    daytrading=0
    endif
    //-- indicators
    
    //emas
    ema = exponentialaverage[63](close)
    ema2 = exponentialaverage[21](close)
    EMAabove = ema2>ema
    EMAbelow = ema2<ema
    //ATR
    atr = averagetruerange[24]
    //Highest/lowest candle
    hh = high + 1*pipsize
    ll = low - 1*pipsize
    //set Breakout
    MA= 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 rules
    Long = emaabove and LongBO
    Short = emabelow and ShortBO
    
    if intradaybarindex=0 then
    alreadytraded = 0
    case = 0
    levelhi = 0
    levello = 0
    endif
    
    if onmarket or (onmarket[1] and not onmarket) or (currentprofit<>strategyprofit) then
    alreadytraded = 1
    endif
    
    //case 2 : If price is above the MA then only trade long BUT only above the highest high of the past 24 hrs
    if close >ema and long  and case = 0 then
    case = 2
    levelhi = 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 hrs
    if close <ema and short and case = 0 then
    case = 3
    levello = ll[1]
    endif
    //endif
    XCase=case
    if alreadytraded = 0 then
    //money management
    //if case=1 then
    //daytrading=0
    //else
    StopLoss = 1.4*ATR
    endif
    //Risk Setting
    Risk = riskpercent/100
    
    //calculate contracts
    equity = balance + StrategyProfit
    maxrisk = round(equity*Risk)
    size = max(minlot,abs(round((maxrisk/StopLoss)/PointValue)*pipsize))
    
    //in all cases put pending orders on market
    while Xcase <> 0 and daytrading=1 and not OnMarket do //(time >= activationtime and time <=LimitHour) do
    if levelhi>0 then
    buy size contract at levelhi stop
    endif
    if levello>0 then
    sellshort size contract at levello stop
    endif
    xCase=0
    wend
    
    // Friday 22:00 Close ALL operations.
    IF onmarket and (DayOfWeek = 5 AND time >= 220000) THEN
    SELL AT MARKET
    EXITSHORT AT MARKET
    ENDIF
    
    //set target and profit
    
    if case = 2 or case = 3 then
    set target profit 1.7*ATR
    set stop loss stoploss
    endif
    
    currentprofit = 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"
    
    2020-01-15_1133.png 2020-01-15_1133.png 2020-01-15_1135.png 2020-01-15_1135.png
    #116905 quote
    grimweasel47
    Participant
    Senior

    Ok, 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.

    defparam cumulateorders=false
    // --- settings
    balance = 10000 //balance of the strategy when activated the first time
    minlot = 1 //minimum lot size for the current instrument (example: 1 for DAX)
    riskpercent = 2 //risk percent per trade
    //settings for days to trade
    if dayofweek=1 then //Monday
    daytrading=1
    endif
    if dayofweek=2 then // Tuesday
    daytrading=1
    endif
    if dayofweek=3 then // Wednesday
    daytrading=1
    endif
    if dayofweek=4 then //Thursday
    daytrading=1
    endif
    if dayofweek=5 then // Friday
    daytrading=1
    endif
    if dayofweek=6 or dayofweek=7 then //Sat and Sun
    daytrading=0
    endif
    //-- indicators
    
    //emas
    ema = exponentialaverage[63](close)
    ema2 = exponentialaverage[21](close)
    EMAabove = ema2>ema
    EMAbelow = ema2<ema
    //ATR
    atr = averagetruerange[21]
    //Highest/lowest candle
    hh = high+1*pipsize
    ll = low-1*pipsize
    //set Breakout
    MA= 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 rules
    Long = emaabove and LongBO
    Short = emabelow and ShortBO
    
    if intradaybarindex=0 then
    alreadytraded = 0
    case = 0
    levelhi = 0
    levello = 0
    endif
    
    if onmarket or (onmarket[1] and not onmarket) or (currentprofit<>strategyprofit) then
    alreadytraded = 1
    endif
    
    //case 2 : If price is above the MA then only trade long BUT only above the highest high of the past 24 hrs
    if  long  and close >ema and case = 0 then
    case = 2
    levelhi = 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 hrs
    if short and close < ema and case = 0 then
    case = 3
    levello = ll[1]
    endif
    //endif
    XCase=case
    if alreadytraded = 0 then
    //money management
    //if case=1 then
    //daytrading=0
    //else
    StopLoss = 1.4*ATR
     
    endif
    //Risk Setting
    Risk = riskpercent/100
    
    //calculate contracts
    equity = balance + StrategyProfit
    maxrisk = round(equity*Risk)
    size = max(minlot,abs(round((maxrisk/StopLoss)/PointValue)*pipsize))
    
    //in all cases put pending orders on market
    while Xcase <> 0 and daytrading=1 and not OnMarket do //(time >= activationtime and time <=LimitHour) do
    if levelhi>0 then
    buy size contract at levelhi stop
    endif
    if levello>0 then
    sellshort size contract at levello stop
    endif
    xCase=0
    wend
    
    // Friday 22:00 Close ALL operations.
    IF onmarket and (DayOfWeek = 5 AND time >= 220000) THEN
    SELL AT MARKET
    EXITSHORT AT MARKET
    ENDIF
    
    //set target and profit
    
    if case = 2 or case = 3 then
    set target profit 10*ATR
    set stop loss stoploss trailing ATR*0.2
    
    endif
    
    currentprofit = 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"
    
    GraHal and soad41 thanked this post
    2020-01-15_1710.png 2020-01-15_1710.png
    #116908 quote
    GraHal
    Participant
    Master

    Wow! Looks great … you are flying man!! 🙂

    What did you do in the City? Good to have an ex Gordon Gekko on board!! 🙂

    #116910 quote
    grimweasel47
    Participant
    Senior

    Haha 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…

    GraHal thanked this post
    #116923 quote
    Vonasi
    Moderator
    Master

    which is why I think most lower TF trading is pointless

    Finally someone who thinks like I do!

    grimweasel47 thanked this post
    #116931 quote
    Lifen
    Participant
    Senior

    Hi Grimweasel,

    Thanks for sharing your strategy but I cannot test it as the Indicator “Carver 1” doesn’t work.

    n=21
     
    a = highest[n](high[0])
    b = lowest[n](low[0])
    c = (a+b)/2
     
    scaledprice = (close-c)/(a-b)
    zero =0
    return scaledprice as "scaled price"

    it seems there is a problem with zero ?

    Can you check ? Thanks in advance

    #116934 quote
    grimweasel47
    Participant
    Senior

    Hi 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.

    #116960 quote
    GraHal
    Participant
    Master

    @Lifen comment out (with //) zero=0 as it is not used anyway??

    #116973 quote
    Francesco
    Participant
    Veteran

    When i try to run the strategy the system says: The “Carver 1″ function retrieved from the strategy is named with 1 parameter instead of 0 expected”

Viewing 15 posts - 1 through 15 (of 45 total)
  • You must be logged in to reply to this topic.

Carver Hybrid ProOrder Code


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 44 replies,
has 11 voices, and was last updated by grimweasel47
6 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/13/2020
Status: Active
Attachments: 17 files
Logo Logo
Loading...