//+------------------------------------------------------------------+
//|                                                       asdasd.mq4 |
//|                                                         Dimasyok |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Dimasyok"
#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    Slippage     = 5;
extern int    MATime       = 60;
extern int    MAPeriod     = 14;
extern int    MaxOrders    = 5;
extern int    MaxLoss      = 100;


int ticket;
double price, TP, lastlot, OrderLoss, TotalOrders;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
    if(Digits == 3 || Digits == 5)
    {
      TakeProfit *= 10;
      Step       *= 10;
      Slippage   *= 10;
      MaxLoss    *= 10;
      
     }
     return(0);
 }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   if(CountTrades() == 0)
     {
        double ima = iMA(Symbol(), MATime, MAPeriod, 0, MODE_SMA, PRICE_CLOSE, 0 );
        
        if(Ask > ima)
        {
           ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, " ", Magic, 0, Blue);
           
           if(ticket > 0)
           {
              TP =  NormalizeDouble(Ask + TakeProfit * Point, Digits);
              OrderModify(ticket, OrderOpenPrice(), 0, TP, 0);
           }
         } 
        if (Bid < ima)
        {
         ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, " ", Magic, 0, Red);
           
           if(ticket > 0)
           {
              TP =  NormalizeDouble(Bid - TakeProfit * Point, Digits);
              OrderModify(ticket, OrderOpenPrice(), 0, TP, 0);
           }   
        }          
     }
     if(CountTrades() > 0 && CountTrades() < MaxOrders)
     {
       int order_type = FindLastOrderType();
       if(order_type == OP_BUY)
       {
          price = FindLastPrice(OP_BUY);
          if(Ask <= price - Step * Point)
          {
             lastlot = FindLastLots(OP_BUY);
             if (lastlot <= 0) return;
             lastlot = NormalizeDouble(lastlot * Multipler, 2);
             ticket = OrderSend(Symbol(), OP_BUY, lastlot, Ask, Slippage, 0, 0, "", Magic,0, Blue);
             if(ticket > 0)
             ModifyOrders(OP_BUY);
              
           }
        }
       if (order_type == OP_SELL)
       {
           price = FindLastPrice(OP_SELL);
          if(Bid >= price + Step * Point)
          {
             lastlot = FindLastLots(OP_SELL);
             if (lastlot <= 0) return;
             lastlot = NormalizeDouble(lastlot * Multipler, 2);
             ticket = OrderSend(Symbol(), OP_SELL, lastlot, Bid, Slippage, 0, 0, "", Magic,0, Red);
          }   
          if(ticket > 0)
          ModifyOrders(OP_SELL);
       }
      }
      if(TotalLoss() < MaxLoss * (-1))
      {
          for (int i=OrdersTotal()-1; i >= 0; i--)
          {
               if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
               {
                   if (OrderMagicNumber() == Magic)
                   {
                        OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Black);
                         OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Black);
                   }
                }
          } 
      
      
  }    
    
   return(0);



}

//+------------------------------------------------------------------+
int TotalLoss()
{
       OrderLoss = 0;
       
    for(int i=OrdersTotal()-1; i>=0; i--)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
           if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
           {
           
            OrderLoss += OrderProfit() / OrderLots() ;
            
           }
        }
     }
     return(OrderLoss);
}
//+------------------------------------------------------------------+
 void ModifyOrders(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 && OrderType() == otype)
           OrderModify(OrderTicket(), OrderOpenPrice(), 0, TP, 0, 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 && OrderType() == otype)
             {
                oldticket = OrderTicket();
                if(oldticket > ticket)
                {
                   oldLots = OrderLots();
                   
                  ticket = oldticket;
                }
             }
          }
       }
       return(oldLots);
}
//+------------------------------------------------------------------+
double FindLastPrice(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 && OrderType() == otype)
             {  
                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);
}

//+------------------------------------------------------------------+


