Trend detection codes

Viewing 15 posts - 1 through 15 (of 45 total)
  • Author
    Posts
  • #178769 quote
    Francesco
    Participant
    Veteran

    Hi guys,

    I would like to know what kind of codes do you use for trend detection and which do you think are the most reliable; here’s some examples that i’ve applied before:

    // Trend Detection
    bull= (average[x](close)>average[y](close)[1])
    bear= (average[x](close)<average[y](close)[1])
    // Trend Detection
    ma1= average[x](close)
    ma2= average[y](close)
    bull= close>ma1 and close>ma2 and ma1>ma2
    
    ma3= average[x](close)
    ma4= average[y](close)
    bear= close<ma3 and close<ema4 and ma3<ema4
    #178773 quote
    phoentzs
    Participant
    Master

    You mean SMA increasing … is going very well.

    // Trend Detection
    bull= (average[x](close) > average[x](close)[1])
    bear= (average[x](close) < average[x](close)[1])
    thanked this post
    #178775 quote
    robertogozzi
    Moderator
    Master

    Another one is Higher HIGHs + higher LOWs or Lower HIGHS + Lower LOWS:

    //N = 20
    srcH = close                   //or HIGH
    srcL = close                   //or LOW
    HH1  = highest[N](srcH)
    LL1  = lowest[N](srcL)
    HH2  = highest[N](srcH[N])
    LL2  = lowest[N](srcL[N])
    UPtrend   = ((HH1 > HH2) AND (LL1 > LL2))
    DOWNtrend = ((HH1 < HH2) AND (LL1 < LL2))
    MyTrend   = 0
    IF UPtrend  AND UPtrend <> UPtrend[1] THEN
       MyTrend = 1
    ELSIF DOWNtrend AND DOWNtrend <> DOWNtrend[1] THEN
       MyTrend = -1
    ENDIF
    RETURN MyTrend AS "Trend"
    Francesco, GraHal, phoentzs and SnorreDK thanked this post
    x-20.jpg x-20.jpg MyTrend.itf
    #178781 quote
    GraHal
    Participant
    Master

    Link to above added as Log 306 here …

    Snippet Link Library

    robertogozzi and LaurentBZH35 thanked this post
    #178946 quote
    nonetheless
    Participant
    Master

    Ciao Francesco, this is what I’m mostly using for primary trend these days for short TFs  (rising or falling MA, but separate long and short):

    Timeframe (1 hour) 
    ma = average[a,t](typicalprice)
    long = ma > ma[1] 
    mb = average[a1,t1](typicalprice)
    short = mb < mb[1]
    Francesco and thanked this post
    #186785 quote
    icharttop
    Participant
    Junior

    Hi Roberto l have made a EA for MT4 but l want to code it on prorealtime but the coding different
    this is want i have done
    Function Start Time
    if is time >= H Start Hour M Start Minute
    Boolean variable ONCE to True
    Main Bar attach Start time Function(l learnt ONCE form You)
    if ONCE
    do Set BarCounter to BarCounter + 1
    Print Counter = BarCounter (text block)
    if barCounter = [65] bars
    do Set LC1 to Bar Close Shift of Lowest Close count [6] shift 1
    Set HC1 to Bar Close Shift of Highest close count [40] shift 1
    Set LHC1 to Bar Close Shift of Lowest High count [65] shift 1
    Boolean Set Once to False
    Variable Set barCounter to 0
    Print LC1 = (Variable) LC1
    Print HC1 = (Variable) HC1
    Print LHC1= (Variable) LHC1
    This is to back test if my EA is working on MT4 How would l start to write this code with ProrRealTime

    #188245 quote
    phoentzs
    Participant
    Master

    I’d like a smart assessment from some smart people here.
    In the short-term sector (M5…M1) I have several of the same systems running with different filters as far as the trend is concerned. All sort of SMAs.
    3 variants as an example:
    Version 1:
    MA > MA[1]
    Most customizable, but seems to have the greatest propensity for over-optimization.
    Variant 2:
    close > MA
    Even better and quite robust.
    Variant 3:
    close > close(20)
    Surprisingly the most robust. For whatever reason. Now I wanted to hear your experiences, what have you had?

    #188273 quote
    deletedaccount051022
    Participant
    New

    Hello phoentzs

    Interesting question.  Not sure I meet the definition of ‘smart people’  🙂 but here is my feedback which I hope is of use.  When testing different trend detection systems in a strategy I expect to see periods where the strategy doesn’t trade and a flat line appears on the equity curve.  Not trading is also a good option at times.

    With that in mind I tested the three different lines of code you posted, and ran them on higher time frames.  They didn’t fulfill my own personal requirements and the strategy kept trading. I appreciate this may differ from your own objectives here.

    Here is an example of what I use, which can be adapted to lower time frames.

     

    //Entry Filter
    Timeframe(X hours)
    FMA1 = average[p,t]
    FMA2 = average[p,t]
    F1 = FMA1 > FMA2
    Timeframe(Default)
    Midlanddave and phoentzs thanked this post
    #188275 quote
    nonetheless
    Participant
    Master

    sam, that won’t work the way you’ve written it as FMA1 and FMA2 will be identical.

    FMA2 would need a different period or type … which I’m sure is what you meant 😁

    #188276 quote
    deletedaccount051022
    Participant
    New

    Hi nonetheless

    Both “p” and “t” are different for both FMA1 and FMA2.  Perhaps I should have written it as “p1” and “p2” to make it more obvious  🙂

    #188277 quote
    nonetheless
    Participant
    Master

    yeah, i assumed that is what you would have done. I was more concerned for total noobies just copying what you had posted and then wondering why there were 0 trades …

    Midlanddave and thanked this post
    #188286 quote
    phoentzs
    Participant
    Master

    Yes, I also use this variant. As a range filter, just to stay in my direction as much as possible. Perhaps I should have said that I use the variants I posted as an exit from the position at the same time. So you’re pretty close to the course. Depending on the market and timeframe between 10…20. And here, strangely enough, the Close>close(10…20) variant works best as a filter and simultaneous exit.

    thanked this post
    #188310 quote
    robertogozzi
    Moderator
    Master

    @icharttop

    I’m so sorry, but I can’t translate that language, as I don’t know it.

    Maybe @Nicolas can.

    #188311 quote
    Nicolas
    Keymaster
    Master

    Sorry I do not see any valid language here, only icharttop’s pseudo code 😆 and I don’t understand it!

    #188347 quote
    pableitor
    Participant
    Master

    Given that there is no perfect trend indicator , what if we add several trend indicators and find a “composite” trend indicator, to average the pros & cons of each one ?

    trend = 0 // cummulative "trend strength"
    //first condition : close > close[10...20]
    c = 1 
    for i = 10 to 20
    trend = trend + (close > close[i])
    next
    trend = trend/11 //average result
    // second condition EMA(10) > EMA(30)
    c = c +1 
    trend =  trend + (exponentialAverage[10](close) > exponentialaverage[30](close))
    //third condition: close > EMA(30)
    c = c +1
    trend  = trend + (close > exponentialaverage[30](close))
    
    // more conditions .... 
    
    trend = trend/c  // average trend strength = 0...1
    return 2*trend - 1, 1, 0, -1  //  +1 strong bullish  -1 strong bearish
    robertogozzi thanked this post
    Captura-de-pantalla-2022-02-16-130904.png Captura-de-pantalla-2022-02-16-130904.png
Viewing 15 posts - 1 through 15 (of 45 total)
  • You must be logged in to reply to this topic.

Trend detection codes


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 44 replies,
has 12 voices, and was last updated by monkeys nuts
3 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/30/2021
Status: Active
Attachments: 7 files
Logo Logo
Loading...