//+--------------------------------------------------------------------------------------+
//| Pedriz Predictor.mq4
//+--------------------------------------------------------------------------------------+
#property copyright "Juan M. Blasco"


#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red

extern int IndicatorDisplacement = 150;
extern bool Show_Trend = true;
extern bool Only_Show_Valid_Triggers = true;
//extern bool Send_Mail = false;
//---- buffers
double UPTrend[];
double DNTrend[];
double UPTrigger[];
double DNTrigger[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init() {
    //---- additional buffers are used for counting
    IndicatorBuffers(4);
    //---- indicators

    SetIndexLabel(2, NULL);
    SetIndexLabel(3, NULL);

    if (Period() == PERIOD_M15) {
        SetIndexLabel(0, NULL);
        SetIndexLabel(1, NULL);
        SetIndexStyle(0, DRAW_LINE, STYLE_DOT, 1, Blue);
        SetIndexStyle(1, DRAW_LINE, STYLE_DOT, 1, Red);
    } else {
        SetIndexLabel(0, "UP TREND");
        SetIndexLabel(1, "DOWN TREND");
        SetIndexShift(0, 3);
        SetIndexShift(1, 3);
        SetIndexStyle(0, DRAW_LINE, 0, 2, Turquoise); // Blue);
        SetIndexStyle(1, DRAW_LINE, 0, 2, Salmon); // Red);
    }
    SetIndexStyle(2, DRAW_ARROW, EMPTY, 3, Blue);
    SetIndexStyle(3, DRAW_ARROW, EMPTY, 3, Red);
    SetIndexArrow(2, SYMBOL_ARROWUP);
    SetIndexArrow(3, SYMBOL_ARROWDOWN);
    SetIndexBuffer(0, UPTrend);
    SetIndexBuffer(1, DNTrend);
    SetIndexBuffer(2, UPTrigger);
    SetIndexBuffer(3, DNTrigger);

    //----
    return (0);
}
//+------------------------------------------------------------------+

int deinit() {
    return (0);
}
//+------------------------------------------------------------------+

static string preEMAname = "Predictor EMA";
static string preRSIname = "Predictor RSI";

double ma5now, ma10now, ma5prev, ma10prev, ma5now4H, ma10now4H, ma5prev4H, ma10prev4H;
int maCross, StochCHANGE, maCross4H, StochCHANGE4H, MACDChange;
double RSI, Stoch1, Stoch2, Signal1, Signal2, MACDChange1, MACDChange2, RSI4H, Stoch4H1, Stoch4H2, Signal4H1, Signal4H2;
string CRLF;

int start() {
    int i, counted_bars = IndicatorCounted();
    int Trigger, i4h;
    static int TrendON;
    string text;
    int bufidx;

    CRLF = CharToStr(13) + CharToStr(10);

    if (Bars <= 100) return (0);
    //---- check for possible errors
    if (counted_bars < 0) return (-1);
    //---- the last counted bar will be recounted
    if (counted_bars > 0) counted_bars--;
    i = Bars - counted_bars;

    while (i >= -2) // 0)
    {
        //---- TREND4H
        i4h = iBarShift(NULL, PERIOD_H4, iTime(NULL, 0, i + 1), true) + 1;

        if (i4h >= 0) bufidx = 2;
        else bufidx = 0;

        ma5now4H = iCustom(NULL, PERIOD_H4, preEMAname, 5, bufidx, i4h);
        ma10now4H = iCustom(NULL, PERIOD_H4, preEMAname, 10, bufidx, i4h);
        ma5prev4H = iCustom(NULL, PERIOD_H4, preEMAname, 5, bufidx, i4h + 1);
        ma10prev4H = iCustom(NULL, PERIOD_H4, preEMAname, 10, bufidx, i4h + 1);

        //Print(StringConcatenate("ma5now: ", ma5now4H, " ma10now: ", ma10now4H,
        //    " ma5prev4h: ", ma5prev4H, " ma10prev4h: ", ma10prev4H));

        maCross4H = 0;
        if (ma5now4H > ma5prev4H && ma10now4H > ma10prev4H && ma5prev4H < ma10prev4H && ma5now4H > ma10now4H)
            maCross4H = 1;
        else if (ma5now4H < ma5prev4H && ma10now4H < ma10prev4H && ma5prev4H > ma10prev4H && ma5now4H < ma10now4H)
            maCross4H = 2;
        
        RSI4H = iCustom(NULL, PERIOD_H4, preRSIname, 9, PRICE_CLOSE, i4h +1 ) ;
        
        if ((maCross4H == 1) && (RSI4H > 50) ) TrendON = 1;
        if ((maCross4H == 2) && (RSI4H < 50) ) TrendON = -1;

        //if (Show_Trend) {
        if (PERIOD_H4 == Period())
            if (TrendON == 1) {
                UPTrend[i] = Low[i] + IndicatorDisplacement*Point;
                DNTrend[i] = EMPTY_VALUE;
                SetIndexLabel(0, "UP TREND");
                SetIndexLabel(1, NULL);
            } else if (TrendON == -1) {
                DNTrend[i] = High[i] - IndicatorDisplacement*Point;
                UPTrend[i] = EMPTY_VALUE;
                SetIndexLabel(0, NULL);
                SetIndexLabel(1, "DOWN TREND");
            }


        //-------------------------

        if (PERIOD_M15 == Period())         {
            //---- TRIGGERS

            ma5now = iCustom(NULL, 0, preEMAname, 5, bufidx, i -1 );
            ma10now = iCustom(NULL, 0, preEMAname, 10, bufidx, i -1);
            ma5prev = iCustom(NULL, 0, preEMAname, 5, bufidx, i );
            ma10prev = iCustom(NULL, 0, preEMAname, 10, bufidx, i);

            if (ma5now > ma5prev && ma10now > ma10prev && ma5prev < ma10prev && ma5now > ma10now) maCross = 1;
            else if (ma5now < ma5prev && ma10now < ma10prev && ma5prev > ma10prev && ma5now < ma10now) maCross = 2;
            else maCross = 0;
            
            RSI = iCustom(NULL, 0, preRSIname, 9, PRICE_CLOSE, i -1 ) ;

            if ((maCross == 1) && (RSI > 52) && ((TrendON == 1) || (Only_Show_Valid_Triggers == false))) Trigger = 1;
            if ((maCross == 2) && (RSI < 48) && ((TrendON == -1) || (Only_Show_Valid_Triggers == false))) Trigger = 2;

            if ((UPTrigger[i + 1] != 0x7fffffff) || (DNTrigger[i + 1] != 0x7fffffff)) Trigger = 0;

            if (Trigger == 1) UPTrigger[i + 1] = High[i + 1] + 10 * Point;
            if (Trigger == 2) DNTrigger[i + 1] = Low[i + 1] - 10 * Point;

            if (Trigger > 0) {
                Print(TimeDay(Time[i]) + "/" + TimeMonth(Time[i]) + "/" + TimeYear(Time[i]) + " at " + TimeHour(Time[i]) + ":" + TimeMinute(Time[i]));
                Print(Day() + "/" + Month() + "/" + Year() + " at " + Hour() + ":" + Minute());
            }

        }

        //-------------------------

        Trigger = 0;
        i--;
    }
    return (0);
}

//-------------------------------------------------------+++