Backtest Nicolas "TrendFollowingMovingAverage"

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #136236 quote
    MajorBasse
    Participant
    Average

    Hi everyone. I got interested in Nicolas Trend Following Moving Average and decided to backtest a simple strategy based on his indicator, which can be found here: https://www.prorealcode.com/prorealtime-indicators/trend-following-moving-averages/.

    However I seem to not fully understand the code which makes it hard to make a backtest. I would simply like to buy when the entire “cloud” is green and sell when it is red. Alternatively, buy when close crosses over the “cloud” and sell when it crosses under. As it stands, the program doesn´t make any trades whatsoever. Could someone please check my code and see what I´m doing wrong?

    // --- settings
    //prd = 20 //Period to Check Trend
    //rateinp = 1 //Trend Channel Rate %  minval = 0.1, step = 0.1
    //ulinreg = 1 // 1=true, Use Linear Regression
    //linprd = 10 //Linear Regression Period, minval = 2
    //matype = 1 //ma type 0=sma, 1-ema, etc.
    // --- end of settings
    
    DEFPARAM PRELOADBARS = 100
    
    matype = ExponentialAverage[p](close)
    ulinreg = 1
    
    rate = rateinp / 100
    pricerange = highest[100](close) - lowest[100](close)
    chan = pricerange * rate
    
    p = 5
    while p<=100 do
    //trend
    masrc = average[p,matype](close)
    if ulinreg then
    ma = LinearRegression[linprd](masrc)
    else
    ma = masrc
    endif
    
    hh = highest[prd](ma)
    ll = lowest[prd](ma)
    diff = abs(hh-ll)
    
    if diff>chan then
    if ma>ll+chan then
    trend=1
    elsif ma<hh-chan then
    trend=-1
    else
    trend=0
    endif
    else
    trend=0
    endif
    
    ret = trend*diff/chan
    
    alpha = min(255,(80+abs(ret*10)))
    if ret<0 then
    r=255
    g=0
    else
    r=0
    g=255
    endif
    
    $prev[p] = ma
    p=p+5
    wend
    
    if trend = 1 then
    buy 10 contracts at market
    endif
    
    if trend = -1 then 
    sell at market
    endif 
    
    
    
    
    #136239 quote
    GraHal
    Participant
    Master

    Is line 55 correct?

    prev is not used anywhere else in the strategy and what is the $ doing?

    $prev[p] = ma
    #136241 quote
    MajorBasse
    Participant
    Average

    Hello GraHal. If you check the original code from Nicolas indicator you see that before line 59 there is the “drawsegment” line. Since this line is not required in Probacktest I decided to remove it. Perhaps prev[p] should be removed aswell?

    #136242 quote
    GraHal
    Participant
    Master

    Anyway, have you got PRT v11 as Nicolas Trend Following Moving Average only works on V11 onwards!

    #136243 quote
    MajorBasse
    Participant
    Average

    Yes I know, I am using V11 for this backtest so it shouldn´t be a problem. I´m simply wondering why it is not taking any trades?

    #136253 quote
    Nicolas
    Keymaster
    Master

    There is something wrong by the calculation in the loop. I’m investigating it and let you know if I can make it working.

    The code i’m using is this one: (for debugging purpose)

    DEFPARAM PRELOADBARS = 10000
    
    // --- settings
    prd = 20 //Period to Check Trend
    rateinp = 1 //Trend Channel Rate %  minval = 0.1, step = 0.1
    ulinreg = 0 // 1=true, Use Linear Regression
    linprd = 10 //Linear Regression Period, minval = 2
    matype = 1 //ma type 0=sma, 1-ema, etc.
    // --- end of settings
    
    matype = ExponentialAverage[p](close)
    ulinreg = 1
    
    rate = rateinp / 100
    pricerange = highest[100](close) - lowest[100](close)
    chan = pricerange * rate
    
    p = 5
    count=0
    while p<=100 do
     
    //trend
    masrc = average[p,matype](close)
    if ulinreg then
    ma = LinearRegression[linprd](masrc)
    else
    ma = masrc
    endif
    
    hh = highest[prd](ma)
    ll = lowest[prd](ma)
    diff = abs(hh-ll)
    
    if diff>chan then
    if ma>ll+chan then
    trend=1
    elsif ma<hh-chan then
    trend=-1
    else
    trend=0
    endif
    else
    trend=0
    endif
    
    if chan>0 then 
    ret = trend*diff/chan
    endif
    
    alpha = min(255,(80+abs(ret*10)))
    if ret<0 then
    r=255
    g=0
    else
    r=0
    g=255
    endif
    
    p=p+5
    count=count+1
    wend
    
    
    if trend = 1 then
    buy 10 contracts at market
    endif
    
    
    //graph ret
    graph count
    graphonprice hh
    graphonprice ll
    graphonprice ma
    graphonprice ll+chan
    GraHal thanked this post
    #136349 quote
    MajorBasse
    Participant
    Average

    Thank you very much for taking the time to investigate @Nicolas. I hope we can find the solution!

    #136371 quote
    Nicolas
    Keymaster
    Master

    I solved the problem, you made a change in the code that result in a wrong calculation of the moving average … anyway, here is the version that buy only (but with no exit either in loss or profit, i let you manage that!).

    //DEFPARAM PRELOADBARS = 10000
    defparam cumulateorders=false
    
    // --- settings
    prd = 20 //Period to Check Trend
    rateinp = 1 //Trend Channel Rate %  minval = 0.1, step = 0.1
    ulinreg = 0 // 1=true, Use Linear Regression
    linprd = 10 //Linear Regression Period, minval = 2
    matype = 1 //ma type 0=sma, 1-ema, etc.
    // --- end of settings
    
    //matype = ExponentialAverage[p](close)
    ulinreg = 1
    
    rate = rateinp / 100
    pricerange = highest[100](close) - lowest[100](close)
    chan = pricerange * rate
    
    p = 5
    count=0
    green=0
    red=0
    while p<=100 do
     
    //trend
    masrc = average[p,matype](close)
    if ulinreg then
    ma = LinearRegression[linprd](masrc)
    else
    ma = masrc
    endif
    
    hh = highest[prd](ma)
    ll = lowest[prd](ma)
    diff = abs(hh-ll)
    
    if diff>chan then
    if ma>ll+chan then
    trend=1
    elsif ma<hh-chan then
    trend=-1
    else
    trend=0
    endif
    else
    trend=0
    endif
    
    if chan>0 then
    ret = trend*diff/chan
    endif
    
    alpha = min(255,(80+abs(ret*10)))
    if ret<0 then
    r=255
    g=0
    red=red+1
    else
    r=0
    g=255
    green=green+1
    endif
    
    p=p+5
    count=count+1
    
    wend
    
    
    if green=count then
    buy 1 contracts at market
    endif
    
    
    //graph ret
    //graph count
    //graphonprice hh
    //graphonprice ll
    //graphonprice ma
    //graphonprice ll+chan
    
    #136383 quote
    MajorBasse
    Participant
    Average

    Excellent, thank you very much for the help Nicolas!

    #136393 quote
    MajorBasse
    Participant
    Average

    Sorry to disturb you again @Nicolas but the system is still not working properly. I tried the “green = count” command and the entrys are almost completely wrong, it even enters trades when the average is red. So I tried writing and indicator with a simple “green crosses over red” command, and it shows exactly and properly where I want to enter (white arrows in the attached image). So I tried writing the same function into the backtest but the entrys are still the same (way to late after the green trend has started and during red periods) as shown by the blue arrows. I´ve tried a bunch of different entry criterias now and none of them seem to enter where they are supposed to, i.e where the white arrows are. Here is the most recent code I´ve tried:

    //DEFPARAM PRELOADBARS = 10000
    DEFPARAM CUMULATEORDERS = FALSE 
    
    // --- settings
    prd = 20 //Period to Check Trend
    rateinp = 1 //Trend Channel Rate %  minval = 0.1, step = 0.1
    ulinreg = 0 // 1=true, Use Linear Regression
    linprd = 10 //Linear Regression Period, minval = 2
    matype = 1 //ma type 0=sma, 1-ema, etc.
    // --- end of settings
    
    //matype = ExponentialAverage[p](close)
    ulinreg = 1
    
    rate = rateinp / 100
    pricerange = highest[100](close) - lowest[100](close)
    chan = pricerange * rate
    
    p = 5
    count=0
    green=0
    red=0
    while p<=100 do
     
    //trend
    masrc = average[p,matype](close)
    if ulinreg then
    ma = LinearRegression[linprd](masrc)
    else
    ma = masrc
    endif
    
    hh = highest[prd](ma)
    ll = lowest[prd](ma)
    diff = abs(hh-ll)
    
    if diff>chan then
    if ma>ll+chan then
    trend=1
    elsif ma<hh-chan then
    trend=-1
    else
    trend=0
    endif
    else
    trend=0
    endif
    
    if chan>0 then
    ret = trend*diff/chan
    endif
    
    alpha = min(255,(80+abs(ret*10)))
    if ret<0 then
    r=255
    g=0
    red=red+1
    else
    r=0
    g=255
    green=green+1
    endif
    
    p=p+5
    count=count+1
    
    wend
    
    
    if green crosses over red then
    buy 10 contracts at market
    endif
    
    if red crosses over green then
    sell at market
    endif 
    
    
    
    //graph ret
    //graph count
    //graphonprice hh
    //graphonprice ll
    //graphonprice ma
    //graphonprice ll+chan
    
    #136394 quote
    Nicolas
    Keymaster
    Master

    graph green

    graph red

    should give you valuable informations.

    #136484 quote
    MajorBasse
    Participant
    Average

    Yes I can see that the “graph green” and “graph red” correlates with where my system enters and exits a trade. But those graph instructions doesn´t correlate with the actual colours of the moving average at all so it still doesn´t help me any further. Seems to me like the “graph green” and “graph red” instructions display a green trend when the moving average is obviously red and vice versa?

    #136533 quote
    Nicolas
    Keymaster
    Master

    Settings of the strategy the same as the one of the indicator displayed on the chart? Linear regression use or not, period, ..

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

Backtest Nicolas "TrendFollowingMovingAverage"


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
MajorBasse @majorbasse Participant
Summary

This topic contains 12 replies,
has 3 voices, and was last updated by Nicolas
5 years, 8 months ago.

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