MACDHistogramSignal

Author: Copyright � 2009, Ivan Kornilov. All rights reserved.
Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screenIt plays sound alerts
0 Views
0 Downloads
0 Favorites
MACDHistogramSignal
//+------------------------------------------------------------------+
//|                                   Copyright © 2010, Ivan Kornilov|
//|                                           MACDHistogramSignal.mq4|
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Ivan Kornilov. All rights reserved."
#property link "excelf@gmail.com"

#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  Green
#property  indicator_color2  Red
#property  indicator_level1  0.0

extern int FastEMA=55;
extern int SlowEMA=89;
extern int SignalEMA=34;
extern int NoiseFilterEMA = 8;

extern bool alertMode = true;

double macdBuffer[];
double histogramRed[];
double histogramGreen[];
double signalBuffer[];
double histogram[];

double fastEMABuffer[];
double slowEMABuffer[];
double signalEMABuffer[];

int init() {
    IndicatorBuffers(8);
    SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID, 2);
    SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID, 2);
    SetIndexStyle(2, DRAW_LINE);
    SetIndexStyle(3, DRAW_LINE);
    SetIndexDrawBegin(0, SlowEMA);
    SetIndexDrawBegin(1, SlowEMA);
    SetIndexDrawBegin(2, SlowEMA);
    
    SetIndexDrawBegin(3, SlowEMA);
    SetIndexDrawBegin(4, SlowEMA);
    IndicatorDigits(Digits + 3);

    SetIndexBuffer(0, histogramRed);
    SetIndexBuffer(1, histogramGreen);
    SetIndexBuffer(2, macdBuffer);
    SetIndexBuffer(3, signalBuffer);
    
    SetIndexBuffer(4, histogram);

    SetIndexBuffer(5, fastEMABuffer);
    SetIndexBuffer(6, slowEMABuffer);
    SetIndexBuffer(7, signalEMABuffer);

    IndicatorShortName("MACD Histogram (" + FastEMA + "," + SlowEMA + "," + SignalEMA + ")");
    SetIndexLabel(0, "Up");
    SetIndexLabel(1, "Down");
    return(0);
}

datetime barTime = 0;
int prevTicTrend = 0;
int trend = 0;

int start(){
    int counted_bars=IndicatorCounted();
    if(counted_bars < 0) {
       return(-1);
    }
    if(counted_bars>0) {
        counted_bars--;
    }
    int limit = Bars - counted_bars;
    int i;

    for(i = 0; i < limit; i++){
        fastEMABuffer[i] = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i);
        slowEMABuffer[i] = iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i);
    }
   
    for(i = 0; i < limit; i++) {
        macdBuffer[i] = 2 * fastEMABuffer[i] - 2 * slowEMABuffer[i] 
            - iMAOnArray(fastEMABuffer, Bars, FastEMA, 0, MODE_EMA, i) 
            + iMAOnArray(slowEMABuffer, Bars, SlowEMA, 0, MODE_EMA, i);
    }
    
    for(i = 0; i < limit; i++) {
       signalEMABuffer[i] = iMAOnArray(macdBuffer, Bars, SignalEMA, 0, MODE_EMA, i);
    }
    
    for(i = 0; i < limit; i++) {
        signalBuffer[i] = 2 * signalEMABuffer[i] - iMAOnArray(signalEMABuffer, Bars, SignalEMA, 0, MODE_EMA, i);
        histogram[i] = macdBuffer[i] - signalBuffer[i];
    }
    
    for(i = 0; i < limit; i++) {
        double slowingEMA = iMAOnArray(histogram, Bars, NoiseFilterEMA, 0, MODE_EMA, i);
        if(histogramGreen[0] == EMPTY_VALUE) {
            prevTicTrend = -1;
        } else {
            prevTicTrend = 1;
        }
        if(
            slowingEMA < histogram[i]
        ) {
            trend = 1;
            histogramRed[i] = histogram[i];
            histogramGreen[i] = EMPTY_VALUE;
        } else {
            trend = -1;
            histogramRed[i] = EMPTY_VALUE;
            histogramGreen[i] = histogram[i];
        }
        
        if(alertMode && i == 0 && (
            (histogramRed[1]   == EMPTY_VALUE && histogramRed[0]   != EMPTY_VALUE)
         || (histogramGreen[1] == EMPTY_VALUE && histogramGreen[0] != EMPTY_VALUE)
        )) {
            if(barTime != Time[0]) {
                if (trend > 0) {
                    Alert("MACD Histogram: " + Symbol() + " M " + Period() + ": Signal: BUY");
                } else {
                    Alert("MACD Histogram: " + Symbol() + " M " + Period() + ": Signal: SELL");
                }
            } else if(prevTicTrend != trend) {
                PlaySound("event.wav");
            }
            barTime = Time[0];
        }
    }

    
    return(0);
}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---