//+------------------------------------------------------------------+
//|                                  Expert for Fractals5+Signal.mq4 |
//+------------------------------------------------------------------+
#property copyright "© Maloma"

extern string _Parameters_Expert = "----- Параметры советника";
extern double Lots = 0.1;
extern int    SL   = 15;
extern int    TP   = 30;
extern int    magic=4802454;
extern string _Parameters_Tral = "----- Параметры трала";
extern bool   Tral;
extern int    TStOp=10;
extern int    TStEp=5;
extern int    Bezubitok=0;
extern string _Parameters_MM = "----- Параметры манимэнэджмента";
extern bool   MM;
extern double MaximumRisk        = 0.03;
extern double DecreaseFactor     = 3;
extern double hedg =2;

       double RLots;
       double SigUP=0, SigDN=0;
       int    CBars=0;
       bool   trend,old; 
       int    TotB,TotS;

double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/500,1);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>0) lot=NormalizeDouble(lot*hedg*losses,1);//NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
  
void Tral()
{
 for(int i=OrdersTotal()-1;i>=0;i--)
    {
     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
     if ((OrderSymbol()==Symbol()) && (OrderMagicNumber()==magic))
      {
       if (OrderType()==OP_BUY && Bid-OrderOpenPrice()>TStOp*Point)
        {
         if (Bezubitok!=0 && Bid-OrderOpenPrice()>=Bezubitok*Point && OrderStopLoss()<OrderOpenPrice())
          {OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+1*Point,OrderTakeProfit(),0,CLR_NONE);}
         if (Bid-OrderStopLoss()>=(TStOp+TStEp)*Point)
          {OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TStOp*Point,OrderTakeProfit(),0,CLR_NONE);}
        }
       if (OrderType()==OP_SELL && OrderOpenPrice()-Ask>TStOp*Point)
        {
         if (Bezubitok!=0 && OrderOpenPrice()-Ask>=Bezubitok*Point && OrderStopLoss()>OrderOpenPrice())
          {OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-1*Point,OrderTakeProfit(),0,CLR_NONE);}
         if (OrderStopLoss()-Ask>=(TStOp+TStEp)*Point)
          {OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TStOp*Point,OrderTakeProfit(),0,CLR_NONE);}
        }
      }
    }
 return(0);
}


void GetSignal(int i)
{

 if(High[3]>High[4] && High[3]>High[5] && High[3]>High[2] && High[3]>High[1])
  {SigUP=1;}
 else
  {SigUP=0;}
 
 if(Low[3]<Low[4] && Low[3]<Low[5] && Low[3]<Low[2] && Low[3]<Low[1])
  {SigDN=1;}
 else
  {SigDN=0;}
  
 return(0);
}

void Check4Close()
{
 int j=OrdersTotal();
 for (int i=j;i>=0;i--)
  {
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   if (Symbol()==OrderSymbol() && magic==OrderMagicNumber())
    if (OrderType()==OP_BUY && SigDN==1)
     OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE);
    if (OrderType()==OP_SELL && SigUP==1)
     OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE);
  }
 return(0);
}

void Total()
{
 TotB=0;
 TotS=0;
 int j=OrdersTotal();
 for (int i=j;i>=0;i--)
  {
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   if (Symbol()==OrderSymbol() && magic==OrderMagicNumber())
    if (OrderType()==OP_BUY) TotB++;
    if (OrderType()==OP_SELL) TotS++;
  }
 return(0); 
}

void start()
{
 if (Tral) Tral();
 if (Bars==CBars) {return(2);}
 GetSignal(1);
// if (SigUP==1 && SigDN==1) {} else Check4Close();
 if (MM) RLots=LotsOptimized(); else RLots=Lots;
 Total();
 if (SigUP==1 && TotB==0) OrderSend(Symbol(),OP_BUY,RLots,Ask,3,Ask-SL*Point,Ask+TP*Point,"e-4FATL",magic,0,Green);
 if (SigDN==1 && TotS==0) OrderSend(Symbol(),OP_SELL,RLots,Bid,3,Bid+SL*Point,Bid-TP*Point,"e-4FATL",magic,0,Red);
 CBars=Bars;
 return(0);
}