//+------------------------------------------------------------------+
//|                                               MACD_ADX.mq5       |
//|                                                   Manfred Uschan |
//|                        Konvertiert zu MQL5 von Antigravity       |
//+------------------------------------------------------------------+
#property copyright "Manfred Uschan"
#property link      "http://www.tradingschule.com"
#property version   "1.00"
#property description "MACD/ADX Kombinations-Indikator"
//| converted to MQL5 by Elliott Waves 2.0                           |
//+------------------------------------------------------------------+
//---------------------------------------------
#property indicator_separate_window
#property indicator_minimum -100
#property indicator_maximum 100
#property indicator_buffers 7
#property indicator_plots   7
//---------------------------------------------
// Plot 0: ADX +DI Histogramm (Bullish)
#property indicator_label1  "ADX+DI"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrRoyalBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  5

// Plot 1: ADX -DI Histogramm (Bearish)
#property indicator_label2  "ADX-DI"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  clrCrimson
#property indicator_style2  STYLE_SOLID
#property indicator_width2  5

// Plot 2: ADX Inaktiv Histogramm
#property indicator_label3  "ADX Inactive"
#property indicator_type3   DRAW_HISTOGRAM
#property indicator_color3  clrDimGray
#property indicator_style3  STYLE_SOLID
#property indicator_width3  5

// Plot 3: Null-Linie
#property indicator_label4  "Zero Line"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrNavy
#property indicator_style4  STYLE_SOLID
#property indicator_width4  2

// Plot 4: MACD Main
#property indicator_label5  "MACD Main"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrCrimson
#property indicator_style5  STYLE_SOLID
#property indicator_width5  2

// Plot 5: MACD Signal
#property indicator_label6  "MACD Signal"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrRoyalBlue
#property indicator_style6  STYLE_SOLID
#property indicator_width6  2

// Plot 6: Entry Line
#property indicator_label7  "Entry"
#property indicator_type7   DRAW_LINE
#property indicator_color7  clrYellow
#property indicator_style7  STYLE_SOLID
#property indicator_width7  2

// Level bei 0
#property indicator_level1  0.0
//---------------------------------------------
// Eingabeparameter
input int ADXPeriod = 28; // ADX Periode
input int ADXLevel = 24; // ADX Level Schwelle
input int FastEmaPeriod = 12; // MACD Fast EMA
input int SlowEmaPeriod = 26; // MACD Slow EMA
input int SignalPeriod = 9; // MACD Signal Periode
input int CalcLimit = 300; // Berechnungslimit
input bool ShowSpread = false; // Spread anzeigen
input ENUM_BASE_CORNER Corner = CORNER_RIGHT_LOWER; // Spread Position
input color ColorSpread = clrYellow; // Spread Farbe
//---------------------------------------------
// Indikator-Buffer
double buf_adxplusdi[];
double buf_adxminusdi[];
double buf_adxinactive[];
double buf_null[];
double buf_macdmain[];
double buf_macdsignal[];
double buf_entry[];

// Hilfsvariablen
double buf_adxplusdival[];
double buf_adxminusdival[];
double buf_adxval[];

// Handles für die Indikatoren
int handle_adx;
int handle_macd;

// Für Spread-Anzeige
double Poin;
int n_digits = 0;
double divider = 1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   // Indikator-Buffer zuweisen
    SetIndexBuffer(0, buf_adxplusdi, INDICATOR_DATA);
    SetIndexBuffer(1, buf_adxminusdi, INDICATOR_DATA);
    SetIndexBuffer(2, buf_adxinactive, INDICATOR_DATA);
    SetIndexBuffer(3, buf_null, INDICATOR_DATA);
    SetIndexBuffer(4, buf_macdmain, INDICATOR_DATA);
    SetIndexBuffer(5, buf_macdsignal, INDICATOR_DATA);
    SetIndexBuffer(6, buf_entry, INDICATOR_DATA);
   
   // Hilfs-Arrays als normale Arrays
    ArraySetAsSeries(buf_adxplusdival, true);
    ArraySetAsSeries(buf_adxminusdival, true);
    ArraySetAsSeries(buf_adxval, true);
   
   // Arrays als Series setzen
    ArraySetAsSeries(buf_adxplusdi, true);
    ArraySetAsSeries(buf_adxminusdi, true);
    ArraySetAsSeries(buf_adxinactive, true);
    ArraySetAsSeries(buf_null, true);
    ArraySetAsSeries(buf_macdmain, true);
    ArraySetAsSeries(buf_macdsignal, true);
    ArraySetAsSeries(buf_entry, true);
   
   // Indikator-Handles erstellen
    handle_adx = iADX(_Symbol, PERIOD_CURRENT, ADXPeriod);
    handle_macd = iMACD(_Symbol, PERIOD_CURRENT, FastEmaPeriod, SlowEmaPeriod, SignalPeriod, PRICE_CLOSE);
   
    if(handle_adx == INVALID_HANDLE || handle_macd == INVALID_HANDLE)
    {
        Print("Fehler beim Erstellen der Indikator - Handles!");
        return(INIT_FAILED);
    }
   
   // Indikator-Name setzen
    IndicatorSetString(INDICATOR_SHORTNAME, "MACD_ADX(" + IntegerToString(ADXPeriod) + ", " +
    IntegerToString(FastEmaPeriod) + ", " + IntegerToString(SlowEmaPeriod) + ")");
   
    return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // Handles freigeben
    if(handle_adx != INVALID_HANDLE)
    IndicatorRelease(handle_adx);
    if(handle_macd != INVALID_HANDLE)
    IndicatorRelease(handle_macd);
   
   // Spread-Label entfernen
    ObjectDelete(0, "Spread");
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
   // Prüfen ob genug Daten vorhanden
    if(rates_total < ADXPeriod || rates_total < SlowEmaPeriod)
    return(0);
   
   // Spread anzeigen falls aktiviert
    if(ShowSpread)
    showSpread();
   
   // Berechnungslimit festlegen
    int limit = MathMin(CalcLimit, rates_total - prev_calculated);
    if(prev_calculated == 0)
    limit = MathMin(CalcLimit, rates_total);
   
   // Temporäre Arrays für ADX-Werte
    double adx_main[], adx_plusdi[], adx_minusdi[];
    double macd_main[], macd_signal[];
   
    ArraySetAsSeries(adx_main, true);
    ArraySetAsSeries(adx_plusdi, true);
    ArraySetAsSeries(adx_minusdi, true);
    ArraySetAsSeries(macd_main, true);
    ArraySetAsSeries(macd_signal, true);
   
   // ADX-Werte kopieren
    if(CopyBuffer(handle_adx, 0, 0, limit, adx_main) <= 0) return(0); // MODE_MAIN
    if(CopyBuffer(handle_adx, 1, 0, limit, adx_plusdi) <= 0) return(0); // MODE_PLUSDI
    if(CopyBuffer(handle_adx, 2, 0, limit, adx_minusdi) <= 0) return(0); // MODE_MINUSDI
   
   // MACD-Werte kopieren
    if(CopyBuffer(handle_macd, 0, 0, limit, macd_main) <= 0) return(0); // MAIN_LINE
    if(CopyBuffer(handle_macd, 1, 0, limit, macd_signal) <= 0) return(0); // SIGNAL_LINE
   
   // Hauptschleife
    for(int i = 0; i < limit && i < ArraySize(adx_main); i++)
    {
        double adxplusdi = adx_plusdi[i];
        double adxminusdi = - adx_minusdi[i]; // Negativ für Darstellung
        double adxval = adx_main[i];
        double macdmain = macd_main[i];
        double macdsignal = macd_signal[i];
      
      // ADX +DI und -DI Buffer setzen
        buf_adxplusdi[i] = adxplusdi;
        buf_adxminusdi[i] = adxminusdi;
      
      // ADX Inaktiv Buffer (zeigt den "schwächeren" Wert)
        if(adxplusdi < - adxminusdi)
        buf_adxinactive[i] = adxplusdi;
        else
        buf_adxinactive[i] = adxminusdi;
      
      // Null-Linie bleibt leer (wird durch Level dargestellt)
        buf_null[i] = 0;
      
      // MACD Logik - Linien bei ±70 und ±90 zur Richtungsanzeige
        if(macdmain > 0 && macdsignal > macdmain)
        {
            buf_macdmain[i] = 90;
            buf_macdsignal[i] = 70;
        }
        else if(macdmain > 0 && macdsignal < macdmain)
        {
            buf_macdmain[i] = 70;
            buf_macdsignal[i] = 90;
        }
        else if(macdmain < 0 && macdsignal < macdmain)
        {
            buf_macdmain[i] = - 90;
            buf_macdsignal[i] = - 70;
        }
        else if(macdmain < 0 && macdsignal > macdmain)
        {
            buf_macdmain[i] = - 70;
            buf_macdsignal[i] = - 90;
        }
        else
        {
            buf_macdmain[i] = 0;
            buf_macdsignal[i] = 0;
        }
      
      // Entry Buffer (momentan ungenutzt)
        buf_entry[i] = EMPTY_VALUE;
    }
   
    return(rates_total);
}
//+------------------------------------------------------------------+
//| Spread-Anzeige Funktion                                          |
//+------------------------------------------------------------------+
void showSpread()
{
   // Punktgröße für verschiedene Broker-Typen ermitteln
    if(_Point == 0.00001)
    Poin = 0.0001; // 5 Digits
    else if(_Point == 0.001)
    Poin = 0.01; // 3 Digits
    else
    Poin = _Point; // Normal
   
   // Label erstellen falls nicht vorhanden
    if(ObjectFind(0, "Spread") < 0)
    {
        ObjectCreate(0, "Spread", OBJ_LABEL, 0, 0, 0);
        ObjectSetInteger(0, "Spread", OBJPROP_CORNER, Corner);
        ObjectSetInteger(0, "Spread", OBJPROP_XDISTANCE, 10);
        ObjectSetInteger(0, "Spread", OBJPROP_YDISTANCE, 30);
        ObjectSetInteger(0, "Spread", OBJPROP_FONTSIZE, 12);
        ObjectSetString(0, "Spread", OBJPROP_FONT, "Arial");
        ObjectSetInteger(0, "Spread", OBJPROP_COLOR, ColorSpread);
    }
   
   // Aktuellen Spread abrufen (expliziter Cast zu double)
    double spreadVal = (double)SymbolInfoInteger(_Symbol, SYMBOL_SPREAD);
   
   // Für 5-Digit Broker den Spread durch 10 teilen
    if(Poin > _Point)
    {
        divider = 10.0;
        n_digits = 1;
    }
    else
    {
        divider = 1.0;
        n_digits = 0;
    }
   
    ObjectSetString(0, "Spread", OBJPROP_TEXT, "Spread: " + DoubleToString(NormalizeDouble(spreadVal / divider, 1), n_digits));
}
//+------------------------------------------------------------------+