//+------------------------------------------------------------------+
//|                                                       Martin.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

extern double Lots       = 0.1;
extern int    TakeProfit = 50;
extern int    Step       = 50;
extern double Multipler  = 2;
extern int    Magic      = 123;
extern int    MA_Period  = 14;
extern int    Slippege   = 5;

int ticket;
double price, TP, lastlot, order_lots;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
   if (Digits == 3 || Digits == 5)
   {
       TakeProfit *= 10;
       Step       *= 10;  
       Slippege   *= 10;  
   }
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{

   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   if (CountTrades() == 0)
   {
       double ima = iMA(Symbol(), 0, MA_Period, 0, MODE_EMA, PRICE_CLOSE, 0);
       
       if(Ask > ima)
       {
          ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippege, 0, 0, "", Magic, 0,Green);
          if(ticket>0)
          {
              TP = NormalizeDouble(Ask + TakeProfit * Point, Digits);
              OrderModify(ticket, OrderOpenPrice(), 0, TP, 0);
          } 
       }
       else if(Bid < ima)
       {
           ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippege, 0, 0, "", Magic, 0,Red);
          if(ticket>0)
          {
              TP = NormalizeDouble(Bid - TakeProfit * Point, Digits);
              OrderModify(ticket, OrderOpenPrice(), 0, TP, 0);
          } 
       }
   } 
   else
   {
       int order_type = FindLastOrderType();
       if (order_type == OP_BUY)
       {
           price = FindeLastPrice(OP_BUY);
           if(Ask <= price - Step * Point)
           {
               if(Ask > iMA(Symbol(), 0, MA_Period, 0, MODE_EMA, PRICE_CLOSE, 0))
               {
               lastlot = FindLastLots(OP_BUY);
               lastlot = NormalizeDouble(lastlot * Multipler, 2);
               ticket = OrderSend(Symbol(), OP_BUY, lastlot, Ask, Slippege, 0, 0, "", Magic, 0, Green);
               if (ticket > 0)
                   ModifiOrders(OP_BUY);
               }
               else if(Bid < iMA(Symbol(), 0, MA_Period, 0, MODE_EMA, PRICE_CLOSE, 0)) 
               {
                
               lastlot = FindLastLots(OP_SELL);
               lastlot = NormalizeDouble(lastlot * Multipler, 2);
               
               ticket = OrderSend(Symbol(), OP_SELL, lastlot, Bid, Slippege, 0, 0, "", Magic, 0, Red);
               if (ticket > 0)
              
                   ModifiOrders(OP_SELL);
               }   
              
           }
       }
       else if (order_type == OP_SELL)
       {
           price = FindeLastPrice(OP_SELL);
           if(Bid >= price + Step * Point)
           {
               if(Bid > iMA(Symbol(), 0, MA_Period, 0, MODE_EMA, PRICE_CLOSE, 0))
               {
               lastlot = FindLastLots(OP_BUY);
               lastlot = NormalizeDouble(lastlot * Multipler, 2);
               ticket = OrderSend(Symbol(), OP_BUY, lastlot, Ask, Slippege, 0, 0, "", Magic, 0, Green);
               if (ticket > 0)
                   ModifiOrders(OP_BUY);
               } 
               if(Bid < iMA(Symbol(), 0, MA_Period, 0, MODE_EMA, PRICE_CLOSE, 0))
               {        
               lastlot = FindLastLots(OP_SELL);
               lastlot = NormalizeDouble(lastlot * Multipler, 2);
               ticket = OrderSend(Symbol(), OP_SELL, lastlot, Bid, Slippege, 0, 0, "", Magic, 0, Red);
               if (ticket > 0)
                   ModifiOrders(OP_SELL);
              }
           }
       }
   }
   
   return(0);
}
//+------------------------------------------------------------------+
void ModifiOrders(int otype)
{
    double avgprice = 0;
         order_lots = 0;
    
    price = 0;
     for(int i = OrdersTotal()-1; i>=0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
            {
                price += OrderOpenPrice() * OrderLots();
                order_lots += OrderLots();
            }
        }
    }
    
    avgprice = NormalizeDouble(price / order_lots, Digits);
    
    if (otype == OP_BUY)  TP = NormalizeDouble(avgprice + TakeProfit*Point, Digits);
    if (otype == OP_SELL) TP = NormalizeDouble(avgprice - TakeProfit*Point, Digits);
    
    for (i=OrdersTotal()-1; i>=0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
               OrderModify(OrderTicket(), OrderOpenPrice(), 0, TP, 0);
        }    
    }
}
//+------------------------------------------------------------------+
double FindLastLots(int otype)
{
    double oldlots;
    int    oldticket;
    
    ticket = 0;
    for(int i = OrdersTotal()-1; i>=0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
            {
                oldticket = OrderTicket();
                if(oldticket > ticket)
                {
                    oldlots = OrderLots();
                    ticket = oldticket;
                }
            }
        }
    }
    return(oldlots);    
}
//+------------------------------------------------------------------+
double FindeLastPrice(int otype)
{
    double oldopenprice;
    int    oldticket;
    
    ticket = 0;
    
    for( int i=OrdersTotal()-1; i>=0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
            {
                oldticket = OrderTicket();
                if(oldticket > ticket)
                {
                    oldopenprice = OrderOpenPrice();
                    ticket = oldticket;
                }
            }
        }
    }
    return(oldopenprice);
}
//+------------------------------------------------------------------+
int FindLastOrderType()
{
    for (int i=OrdersTotal()-1; i>=0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
                return(OrderType());
        }
    }
    return(-1);
}
//+------------------------------------------------------------------+
int CountTrades()
{
    int count = 0;
    for (int i=OrdersTotal()-1; i>=0; i--)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
                count++;
              
        }
    }
    return(count);
}
//+------------------------------------------------------------------+