How to define only the first correction after the start of a new trend

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #164796 quote
    tijuanataxi
    Participant
    New

    How to define the exclusive output of the first correction after the start of a new trend?

    Dear all!
    Unfortunately I am still quite inexperienced and I have no idea how to solve this. I am asking for the output of the first correction after the start of a new trend in the screener.
    Trend is defined as EMA 100 > EMA 200, as well as momentum higher timeframe. (that is clear)
    Correction is to be defined by a negative histogram for uptrends or vice versa.

    I want to avoid the output of further corrections in the trend, only the first correction should be relevant. I hope someone can possibly solve this. Many thanks in advance.

    query-prorealcode.jpg query-prorealcode.jpg
    #164799 quote
    robertogozzi
    Moderator
    Master

    I tested it and it seems to work as expected:

    IF BarIndex <= 1 THEN
       CrossOver = 0
       Flag      = 0
    ENDIF
    Ema100 = average[100,1](close)
    Ema200 = average[200,1](close)
    MyMACD = MACD[12,26,9](close)
    IF Ema100 CROSSES OVER Ema200 THEN
       CrossOver = 1
       Flag      = 0
    ELSIF Ema100 CROSSES UNDER Ema200 THEN
       CrossOver = 0
       Flag      = 0
    ENDIF
    MacdCross = MyMacd CROSSES UNDER 0
    IF Flag THEN
       MacdCross = 0
    ELSIF CrossOver AND MacdCross THEN
       Flag = 1
    ENDIF
    SCREENER[MacdCross]
    tijuanataxi thanked this post
    #164802 quote
    tijuanataxi
    Participant
    New

    wow, that was really fast, thanks a lot! But as far as I can see I’m not getting the results I want, I only changed the code slightly because I am using a very slow macd (30/60/25). e.g. usdhuf, tf 1 hour, is returned as hit, but there is already the 4th correction (macd negative). But anyway I got an idea how I can fix this. Thanks a lot

    I used

    IF BarIndex <= 1 THEN
    CrossOver = 0
    Flag      = 0
    ENDIF
    Ema100 = average[100,1](close[1])
    Ema200 = average[200,1](close[1])
    MyMACD = MACD[30,60,25](close[1])
    IF Ema100 CROSSES OVER Ema200 THEN
    CrossOver = 1
    Flag      = 0
    ELSIF Ema100 CROSSES UNDER Ema200 THEN
    CrossOver = 0
    Flag      = 0
    ENDIF
    MacdCross = MyMacd CROSSES UNDER 0
    IF Flag THEN
    MacdCross = 0
    ELSIF CrossOver AND MacdCross THEN
    Flag = 1
    ENDIF
    SCREENER[MacdCross]
    macd-pro-real-code.jpg macd-pro-real-code.jpg
    #164811 quote
    tijuanataxi
    Participant
    New

    I think i have to add a count so that the flags / histogram are counted while the trend is still present and an output only occurs when count = 1.

    #177977 quote
    tijuanataxi
    Participant
    New

    Hi guys, I haven’t dealt with this question for a while and would now like to try to get an answer. The above code does not give the desired results in the screener. The question would be the first correction defined as a MACD cross after the start of a trend, e.g. by an EMA cross. Unfortunately, my knowledge is not sufficient for this. Maybe someone has an idea.

    trend-example.jpg trend-example.jpg
    #177979 quote
    Swingueur
    Participant
    Average

    Hello,
    Indeed you may need a count. Though the code of @robertogozzi looks good, I would try something like this (not tested):

    IF BarIndex <= 1 THEN
       CrossOver = 0
       Flag      = 0
    ENDIF
    Ema100 = average[100,1](close)
    Ema200 = average[200,1](close)
    MyMACD = MACD[12,26,9](close)
    IF Ema100 CROSSES OVER Ema200 THEN
       CrossOver = 1
       Flag      = 0
    ELSIF Ema100 CROSSES UNDER Ema200 THEN
       CrossOver = 0
       Flag      = 0
    ENDIF
    MacdCross = MyMacd CROSSES UNDER 0
    IF CrossOver AND MacdCross THEN
       Flag = Flag+1
    ENDIF
    SCREENER[Flag=1]
    #177980 quote
    tijuanataxi
    Participant
    New

    Line 5 to line 17 makes perfect sense to me, but I don’t understand counting events logically.
    The suggested code unfortunately does not work and gives no results. It seems that the counting does not work.
    Thanks nevertheless!

    #178008 quote
    Swingueur
    Participant
    Average

    Hello,
    To better know how the code works, you may start to develop an indicator to put on price that will show your signals. Then you will be able to screen more confidently I believe.

    IF BarIndex <= 1 THEN
       CrossOver = 0
       Flag      = 0
    ENDIF
    Ema100 = average[100,1](close)
    Ema200 = average[200,1](close)
    MyMACD = MACD[12,26,9](close)
    IF Ema100 CROSSES OVER Ema200 THEN
       CrossOver = 1
       Flag      = 0
    ELSIF Ema100 CROSSES UNDER Ema200 THEN
       CrossOver = 0
       Flag      = 0
    ENDIF
    MacdCross = MyMacd CROSSES UNDER 0
    IF CrossOver AND MacdCross THEN
       Flag = Flag+1
       DRAWARROWUP(barindex,low-1*AverageTrueRange[14])coloured(204,0,0)
       DRAWTEXT("N#Flag#",barindex,low-1.5*AverageTrueRange[14],SansSerif,Bold,11)coloured(0,0,0)
    ENDIF
    RETURN

    PS: The screener may be SCREENER[CrossOver and MacdCross and Flag=1]

    #178012 quote
    robertogozzi
    Moderator
    Master

    My code is working as expected, with any MACD setting you choose, as from my pic.

    Try increasing the units on your chart, an Ema200 will require many more bars than its periods to be correctly calculated. Use at least 1K bars.

    x-13.jpg x-13.jpg
    #178026 quote
    tijuanataxi
    Participant
    New

    I’m sorry, but I don’t think that’s the case.
    Attached is the screener, as well as the results in 4 hours.
    As an example USDCAD – it gives me every single correction, but only the very first is asked for.

    chart-result.jpg chart-result.jpg screener-result.jpg screener-result.jpg
    #178032 quote
    robertogozzi
    Moderator
    Master

    Yes, you asked for the very first one in your first post “I am asking for the output of the first correction after the start of a new trend in the screener“.

    #178037 quote
    tijuanataxi
    Participant
    New

    Yes, indeed, and can write a code for a screener that gives me any correction, but I am not able to filter only the first correction after the start of a new trend – that is what I want to learn

    #178047 quote
    robertogozzi
    Moderator
    Master

    This one will allow you to detect a) the first correction only or b) all corrections:

    IF BarIndex <= 1 THEN
       CrossOver = 0
       Flag      = 0
       FirstOnly = 1     //1=detect ONLY the first correction,  0=detect ALL corrections
    ENDIF
    Ema100 = average[100,1](close)
    Ema200 = average[200,1](close)
    MyMACD = MACD[30,60,25](close)
    IF Ema100 CROSSES OVER Ema200 THEN
       CrossOver = 1
       Flag      = 0
    ELSIF Ema100 CROSSES UNDER Ema200 THEN
       CrossOver = 0
       Flag      = 0
    ENDIF
    MacdCross = MyMacd CROSSES UNDER 0
    IF Flag THEN
       MacdCross = 0
    ELSIF CrossOver AND MacdCross THEN
       IF FirstOnly THEN
          Flag = 1
       ENDIF
    ENDIF
    SCREENER[MacdCross]

    simply change 1 to 0 in line 4 to detect ALL corrections.

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

How to define only the first correction after the start of a new trend


ProScreener: Market Scanners & Detection

New Reply
Author
Summary

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

Topic Details
Forum: ProScreener: Market Scanners & Detection
Language: English
Started: 03/21/2021
Status: Active
Attachments: 6 files
Logo Logo
Loading...