//+------------------------------------------------------------------+
//|                                           MacdPatternTrader5.mq4 |
//|                                                     FORTRADER.RU |
//|                                              http://FORTRADER.RU |
//+------------------------------------------------------------------+

/*
 
Looking for an interpreter for the English version of the magazine on partnership.

Страница обсуждения советника, последние новости и обновления:
http://fortrader.ru/services/ea/single?id=7

Download last EA version:
http://fortrader.ru/services/ea/single?id=7
*/

#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://fortrader.ru"
#property version   "5.00"


extern string s8 = "Включение ECN";
extern int ecn = 0;
extern string s2 = "Настройки MACD";
extern int lowema = 13;
extern int fastema = 5;
extern string s5 = "Настройки MA";
extern int perema1 = 7;
extern int perema2 = 21;
extern int persma3 = 98;
extern int perema4 = 365;
extern string s1 = "Параметры уровней";
extern double buy_ur_signal1 = 0.003;
extern double buy_ur_signal2 = 0.001;
extern double buy_ur_signal_cancel = 0;
extern double sell_ur_signal1 = -0.003;
extern double sell_ur_signal2 = -0.001;
extern double sell_ur_signal_cancel = 0;

extern string s4 = "Торговые парметры";
extern int EA_Magic = 165004;
extern double Lots = 0.1;
extern int MinimalTakeProfit = 800;


int signal,signalsell,nummodb, nummods;; 

void OnTick()
  {
  int ret;
  
    double min_pips_tp, new_tp;
    if (Digits == 5 || Digits == 3) {
        min_pips_tp = MinimalTakeProfit * 10;
    }
   
    //загружаем индикаторы
    double macdcurr = iMACD(NULL, 0, fastema, lowema, 1, PRICE_CLOSE, MODE_MAIN, 1);
    double macdlast = iMACD(NULL, 0, fastema, lowema, 1, PRICE_CLOSE, MODE_MAIN, 2);
    double macdlast3 = iMACD(NULL, 0, fastema, lowema, 1, PRICE_CLOSE, MODE_MAIN, 3);
    
    if(macdcurr > buy_ur_signal1) {signal=1;}
    if(macdcurr < buy_ur_signal_cancel) {signal=0;}
    if(macdcurr < buy_ur_signal2 && signal==1){signal = 2;}
    if(macdcurr > buy_ur_signal2 && signal==2){
  
        if ((TakeProfit(1) - Ask) < min_pips_tp * Point) {
            new_tp = Ask + min_pips_tp * Point;
        } else {
            new_tp = TakeProfit(1);
        }
        if(ecn==0)
        {    
        ret = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, StopLoss(1),  new_tp, "FORTRADER.RU", EA_Magic, 0, Red);
        }
        else
        {
        ret = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0,  0, "FORTRADER.RU", EA_Magic, 0, Red);
        OrderModifyAfterOpen(ret, StopLoss(1),new_tp);
        }
      signal=0;
      nummodb = 0;
    }

    if(macdcurr < sell_ur_signal1) {signalsell=1;}
    if(macdcurr > sell_ur_signal_cancel) {signalsell=0;}
    if(macdcurr > sell_ur_signal2 && signalsell==1){signalsell = 2;}
    if(macdcurr < sell_ur_signal2 && signalsell==2){
  
         if ((Bid - TakeProfit(0)) < min_pips_tp * Point) {
            new_tp = Bid - min_pips_tp * Point;
        } else {
            new_tp = TakeProfit(0);
        }
        if(ecn==0)
        {
         ret = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, StopLoss(0), new_tp, "FORTRADER.RU", EA_Magic, 0, Red);
        }
        else
        {
         ret = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3,0, 0, "FORTRADER.RU", EA_Magic, 0, Red);
         OrderModifyAfterOpen(ret, StopLoss(0),new_tp);
        }
      signalsell=0;
      nummods = 0;
    }    
    
   //закрытие сделок
    double ema1 = iMA(NULL, 0, perema1, 0, MODE_EMA, PRICE_CLOSE, 1);
    double ema2 = iMA(NULL, 0, perema2, 0, MODE_EMA, PRICE_CLOSE, 1);
    double sma1 = iMA(NULL, 0, persma3, 0, MODE_SMA, PRICE_CLOSE, 1);
    double ema3 = iMA(NULL, 0, perema4, 0, MODE_EMA, PRICE_CLOSE, 1);

    for (int i = 0; i < OrdersTotal(); i++) {
        ret = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        if (OrderType() == OP_BUY && OrderProfit() > 0 && Close[1] > ema2 && nummodb == 0 && OrderMagicNumber()==EA_Magic) {
           ret =  OrderClose(OrderTicket(), NormalizeDouble(OrderLots() / 3, 2), Bid, 3, Violet);
            nummodb++;
            return;
        }

        if (OrderType() == OP_BUY && OrderProfit() > 0 && High[1] > (sma1 + ema3) / 2 && nummodb == 1 && OrderMagicNumber()==EA_Magic) {
           ret =  OrderClose(OrderTicket(), NormalizeDouble(OrderLots() / 2, 2), Bid, 3, Violet);
            nummodb++;
             return;
        }

        if (OrderType() == OP_SELL && OrderProfit() > 0 && Close[1] < ema2 && nummods == 0 && OrderMagicNumber()==EA_Magic) {
           ret =  OrderClose(OrderTicket(), NormalizeDouble(OrderLots() / 3, 2), Ask, 3, Violet);
            nummods++;
             return;
        }

        if (OrderType() == OP_SELL && OrderProfit() > 0 && Low[1] < (sma1 + ema3) / 2 && nummods == 1 && OrderMagicNumber()==EA_Magic) {
           ret =  OrderClose(OrderTicket(), NormalizeDouble(OrderLots() / 2, 2), Ask, 3, Violet);
            nummods++;
             return;
        }
    }
    
   
  }
  
int  OrderModifyAfterOpen(int ordertiket, double stoploss, double takeprofit)
{int i;bool err;

   for( i=1; i<=OrdersTotal(); i++)        
   {
      if(OrderSelect(i-1,SELECT_BY_POS)==true)
       {  
        if(OrderTicket()==ordertiket && OrderMagicNumber()==EA_Magic)  
        {                
           err=OrderModify(OrderTicket(),OrderOpenPrice(),stoploss,takeprofit,0,Red);
           if(err==false)
            {
             Print("OrderModify failed with error #",GetLastError());
             return(0);
            }
         }//if(BBUSize>0
        }//if(OrderSelect(i
         
    }// for( i=1;
return(0);
}


extern int stoplossbars = 7;
extern int stoploss_deviation = 10;

double StopLoss(int type) {
    
    if (Digits == 5 || Digits == 3) {
        int s_otstup = stoploss_deviation * 10;
    }

    double stoploss;
    if (type == 0) {
        stoploss = High[iHighest(NULL, 0, MODE_HIGH, stoplossbars, 1)] + s_otstup * Point;
        return (stoploss);
    }
    if (type == 1) {
        stoploss = Low[iLowest(NULL, 0, MODE_LOW, stoplossbars, 1)] - s_otstup * Point;
        return (stoploss);
    }
return 0;
}  

extern int takeprofitbars = 40;
double TakeProfit(int type) {

    int x = 0, stop = 0;
    double takeprofit;

    if (type == 0) {
        while (stop == 0) {
            takeprofit = Low[iLowest(NULL, 0, MODE_LOW, takeprofitbars, x)];
            if (takeprofit > Low[iLowest(NULL, 0, MODE_LOW, takeprofitbars, x + takeprofitbars)]) {
                takeprofit = Low[iLowest(NULL, 0, MODE_LOW, takeprofitbars, x + takeprofitbars)];
                x = x + takeprofitbars;
            } else {
                stop = 1;
                return (takeprofit);
            }
        }
    }

    if (type == 1) {
        while (stop == 0) {
            takeprofit = High[iHighest(NULL, 0, MODE_HIGH, takeprofitbars, x)];
            if (takeprofit < High[iHighest(NULL, 0, MODE_HIGH, takeprofitbars, x + takeprofitbars)]) {
                takeprofit = High[iHighest(NULL, 0, MODE_HIGH, takeprofitbars, x + takeprofitbars)];
                x = x + takeprofitbars;
            } else {
                stop = 1;
                return (takeprofit);
            }
        }
    }
return 0;
}