MACD Divergence setup

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #172660 quote
    Mike Boorman
    Participant
    Average

    I know there have been plenty of indicators written for divergence in this forum, but having looked through all of them I still can’t quite grasp how to adapt them for my purposes, so I wonder if anyone can help me with this scenario?

    I want to capture the bar that contains the previous lowest low in the range (in this case it would be the 9th bar – see screengrab), and then calculate whether or not the MACD at that bar is lower than the MACD at current bar close.

    Currently my code is only able to calculate divergence of the MACD based on current bar close versus the bar that is defined by the static lookback range (in this case 15).

    Does anyone know how I could query the MACD with a lookback bar that is derived from a previous low?

    Thank you in advance…

    DEFPARAM CumulateOrders = False
    
    
    back=15
    //bars to look back for calculating low
    
    longentryflag=0
    
    macconf= MACD[12,26,9](close) > MACD[12,26,9][back]
    
    IF low < lowest[back] and close > open and close > ExponentialAverage[5](low) THEN
    longentryflag=1
    
    
    if not onmarket and macconf and longentryflag=1 then
    sl=max(5,AverageTrueRange[14](close))
    tp= sl*2
    Set target profit tp
    Set stop loss sl
    BUY 1 PERPOINT AT MARKET
    endif
    ENDIF
    
    graph macconf
    Screen-Shot-2021-06-28-at-15.55.22.png Screen-Shot-2021-06-28-at-15.55.22.png
    #172706 quote
    robertogozzi
    Moderator
    Master

    1. assume the current LOW is the lowest within back bars
    2. scan the previous back bars for a previous low, starting from back-1 bars, for another back bars
    3. us a FOR…NEXT iteration to find the bar where that happened, THAT will be the bar when you can check MACD for a divergence

    CurLow = lowest[back](low)
    IF low = CurLow THEN
       CurMACD  = MACD(12,26,9)(close)
       PriorLow = lowest[back](low[back])
       FOR i = 0 TO (Back - 1)
          IF low[i + Back] = PriorLow THEN
             PriorMACD = MACD(12,26,9)(close)[i + back]
             PriorBAR  = BarIndex[i + back]
             Break
          ENDIF
       NEXT
    ENDIF

    Not tested.

    Mike Boorman thanked this post
    #172722 quote
    Mike Boorman
    Participant
    Average

    Thank you very much Roberto. I think this is close but there is still something I am doing incorrectly. The only way I could get my whole code to work was by querying the MACD with this: CurMACD > PriorMACD…….. but that is only doing a static read of [back], as before.

    I’ve attached a screengrab of where there should be an entry at 15:00, based on a rising MACD from the low at 14:05. My “macconf” needs to be true for there to be an entry at 15:00. I’m guessing that maybe I need to use PriorBAR somewhere else in the code? But I’m not sure how. Would you mind helping me here?

    Thank you 🙂

    DEFPARAM CumulateOrders = False
    
    back=15
    
    longentryflag=0
    
    CurLow = lowest[back](low)
    IF low = CurLow THEN
    CurMACD  = MACD[12,26,9](close)
    PriorLow = lowest[back](low[back])
    FOR i = 0 TO (Back - 1)
    IF low[i + Back] = PriorLow THEN
    PriorMACD = MACD[12,26,9](close)[i + back]
    PriorBAR  = BarIndex[i + back]
    Break
    ENDIF
    NEXT
    ENDIF
    
    macconf= CurMACD > PriorMACD
    
    IF close < PriorLow and close > ExponentialAverage[5](low) THEN
    longentryflag=1
    
    
    if not onmarket and macconf and longentryflag=1 then
    sl=max(5,AverageTrueRange[14](close))
    tp= sl*1
    Set target profit tp
    Set stop loss sl
    BUY 1 PERPOINT AT MARKET
    endif
    ENDIF
    
    graph macconf
    
    Screen-Shot-2021-06-29-at-14.08.43.png Screen-Shot-2021-06-29-at-14.08.43.png
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

MACD Divergence setup


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by Mike Boorman
4 years, 7 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/28/2021
Status: Active
Attachments: 2 files
Logo Logo
Loading...