#property copyright "Rezus666"
#property link      ""



extern double  Lots              = 0.1;
extern int     Magic             = 123;
extern int     TakeProfit        = 50;
extern bool    TPtoND            = true;
extern int     MaxLoss           = 100;
extern int     Slippage          = 5;
extern string separator = "*** TrailingStop  Settings ***";
extern bool    ProfitTrailing    = TRUE;
extern double  TrailingStop      = 0.0;
extern double  TrailingStep      = 0.2;
extern double  Perevod_0         = 0.0;
extern string separator1 = "*** Martingeil  Settings ***";
extern int     MaxOrders         = 5;
extern int     StepOrd           = 50;
extern double  Multipler         = 2;
extern string separator2 = "*** Strateg  Settings ***";
extern int     MinBarHistori     = 1000;
extern int     MinColDip         = 5;
extern int     StepDip           = 50;
extern double  MinVer            = 0.0001;
extern bool    TrendH4D1         = true;

int    BarHistori=0;
int ticket;
double price, TP, lastlot, OrderLoss, TotalOrders;
double nextdiap;
int grad = 0;
double MatrixVer[2000][2000];
double dipArr[]; 
bool AllBar = true;
bool NoHistori = true;
string del = "";
int spred = 2;
double gd_100;
int count = 0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{  
    Delen();
    gd_100 = Point;
    if(Digits == 3 || Digits == 5)
    {
      TakeProfit *= 10;
      StepOrd       *= 10;
      StepDip       *= 10;
      Slippage   *= 10;
      MaxLoss    *= 10;
      spred      *= 10;
      TrailingStop *= 10;
      TrailingStep *= 10;
      Perevod_0  *= 10;
      gd_100 = 10.0 * gd_100;
      
     }
     return(0);
 }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{  
    TS();
    if (NevBar())
    { 
       if (DayOfWeek()==1&&Period()==1440)Delen();
       if (Hour()==4&&Period()==240)Delen();
       if (Hour()==1&&Period()==60)Delen();
       if (Hour()==1||Hour()==13&&Period()==30)Delen();
       if (Hour()==1&&Minute()==0&&Period()==15)Delen();
       if (Minute()==0&&Period()==5)Delen();
    }
   if(CountTrades() == 0)
     {
      if (Fn()==1)
        {
           ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, " ", Magic, 0, Blue);
           
           if(ticket > 0)
           {
              if(TPtoND)TP =  NormalizeDouble(nextdiap+StepDip*Point, Digits);
              if(!TPtoND)TP =  NormalizeDouble(Ask + TakeProfit * Point, Digits);
              OrderModify(ticket, OrderOpenPrice(), 0, TP, 0);
           }
         }         
        if (Fn()==-1)        
        {
         ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, " ", Magic, 0, Red);
           
           if(ticket > 0)
           {
              if(TPtoND)TP =  NormalizeDouble(nextdiap, Digits);
              if(!TPtoND)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 - StepOrd * 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 + StepOrd * 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)
                   {
                         if (OrderType() == OP_BUY)  OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Black);
                       if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, 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)
            if (NormalizeDouble(OrderTakeProfit(), Digits) != TP)
                 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);
}

//+------------------------------------------------------------------+
//////////////////////////////////////////////////////////////////////
bool NevBar() {
   static int PrevBar=0;
   if (PrevBar==Time[0]) return(false);
   PrevBar=Time[0];
   return(true);
}
void Delen()
{
   double MaxClose=Close[ArrayMaximum(Close)];
   double MinClose=Close[ArrayMinimum(Close)];
   double price=MinClose; 
   
   int ToState=0;
   int FromState=0;
   int Matrix[2000][2000];
   //double MatrixVer[2000][2000];
   double SummArr [];
   grad = 0;
   if(AllBar==true)BarHistori=Bars;
   if (BarHistori < MinBarHistori)
   {
      Comment ("Недостаточно истории...\n"+
               "Минимум "+MinBarHistori+"\n"+
               "Есть в истории "+BarHistori);
      NoHistori = true;
      return(0);
   }
   NoHistori = false;
   for (double z = MinClose; z< MaxClose;z=z+StepDip*Point)
   {
       grad+=1; 
   }
   ArrayResize(dipArr,grad);
   for (int i=0; i<grad; i++)
   {
      dipArr[i]=price;
      price = price + StepDip*Point;
   }
    for(int t = 0; t < BarHistori; t++)
    {
        for(ToState = 0; ToState < grad; ToState++)
        {
            if (ToState==grad-1) {
                if((dipArr[ToState-1] <= Close[t]) && (Close[t] < dipArr[ToState]))
                {
                    Matrix[FromState,ToState] = Matrix[FromState,ToState]+ 1;
                    FromState = ToState;
                }
            }
           else{
                if((dipArr[ToState] <= Close[t]) && (Close[t] < dipArr[ToState+1]))
                {
                    Matrix[FromState,ToState] = Matrix[FromState,ToState]+ 1;
                    FromState = ToState;
                }
            }

        }
    }
   ArrayResize(SummArr,grad); 
   ArrayInitialize(SummArr,0);
   int j;
   for(i = 0;i < grad;i++)
   {
      for(j =0;j < grad;j++)
      {
         SummArr[i]=SummArr[i]+Matrix[i,j];
      }
   }
   for(i = 0;i <grad;i++)
   {
      for(j =0;j <grad;j++)
      {
        if(SummArr[i]!=0)MatrixVer[i,j]= Matrix[i,j]/SummArr[i];
      }
   }

}

double Fn()
{
   int diap;
   double ArrVer[];
   if(NoHistori == true){nextdiap=0;return(0);}
   if (Bid>dipArr[1]&&Ask<dipArr[grad-2])
   {
      for(int i = 0;i <grad;i++)
      {
         if (dipArr[i]<=Bid&&Ask<=dipArr[i]+StepDip*Point)
            diap = i;
      }
      ArrayResize(ArrVer,grad); 
      for (i = 0;i<grad;i++)
      {
         if (MatrixVer[diap,i] == 1)
            ArrVer[i]=0.0001;
         else 
            ArrVer[i]=MatrixVer[diap,i];
      }
      if(ArrVer[ArrayMaximum(ArrVer)]>MinVer)
      {
         int index = ArrayMaximum(ArrVer);
         nextdiap = dipArr[index];
      }
      else nextdiap=0;
   }
   else nextdiap=0;
   int trend = Trend();
   if (trend==1)del="UP";
   if (trend==-1)del="DOWN";
   if (trend==0)del="NO";
  
    Comment("Цена стримиться к "+nextdiap+"\nВероятность: "+ArrVer[ArrayMaximum(ArrVer)]+"\nBars: "+Bars+"\nТренд: "+del+"\nКол-во периoдов: "+grad);
    if((nextdiap+StepDip*Point)>Ask&&nextdiap>0&&((((nextdiap+StepDip*Point)-Ask)/Point)/StepDip)<MinColDip&&Ask<((nextdiap+StepDip*Point)- spred* Point)&&trend==1)return (1);
    if(nextdiap<Bid&&nextdiap>0&&(((Bid-nextdiap)/Point)/StepDip)<MinColDip&&Bid>(nextdiap+ spred* Point)&&trend==-1)return (-1);
    if(nextdiap==0)return (0);
} 


int TS(){
 RealTrailOrder();
}

void RealTrailOrder() {
   double l_ord_open_price_20;
   double l_ord_stoploss_28;
   double l_price_36;
   double ld_0 = MarketInfo(Symbol(), MODE_STOPLEVEL) * Point / gd_100;
   double ld_8 = MathMax(TrailingStop, ld_0);
   double ld_88 = MathMax(Perevod_0, ld_0);
   for (int l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) {
      if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES) == TRUE) {
         if (OrderMagicNumber() == Magic || Magic < 0 && OrderSymbol() == Symbol()) {
            l_ord_open_price_20 = OrderOpenPrice();
            l_ord_stoploss_28 = OrderStopLoss();
            while (IsTradeContextBusy()) Sleep(500);
            RefreshRates();
            if (OrderType() == OP_BUY) {
               l_price_36 = ND(Bid - ld_8 * gd_100);
               if (TrailingStop>0){
               if (Bid > l_ord_open_price_20 + ld_8 * gd_100 || !ProfitTrailing && l_price_36 >= l_ord_stoploss_28 + TrailingStep * gd_100 && ld_8 * gd_100 > ld_0 * gd_100) {
                  if (!OrderModify(OrderTicket(), OrderOpenPrice(), l_price_36, OrderTakeProfit(), 0, Blue))
                     if (!IsOptimization()) Print("BUY OrderModify Error " + GetLastError());
               }}
               if (Perevod_0>0){
               if(Bid > l_ord_open_price_20 + ld_88 * gd_100){
               if(l_ord_open_price_20 >= l_ord_stoploss_28){
                  if (!OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice() + 2 * gd_100, OrderTakeProfit(), 0, Blue))
                     if (!IsOptimization()) Print("BUY OrderModify Error " + GetLastError());
               }}}
            }
            if (OrderType() == OP_SELL) {
               l_price_36 = ND(Ask + ld_8 * gd_100);
                if (TrailingStop>0){
               if (Ask < l_ord_open_price_20 - ld_8 * gd_100 || !ProfitTrailing && l_price_36 <= l_ord_stoploss_28 - TrailingStep * gd_100 || l_ord_stoploss_28 == 0.0 && ld_8 * gd_100 > ld_0 * gd_100) {
                  if (!OrderModify(OrderTicket(), OrderOpenPrice(), l_price_36, OrderTakeProfit(), 0, Red))
                     if (!IsOptimization()) Print("Sell OrderModify Error " + GetLastError());
               }}
               if (Perevod_0>0){
               if(l_ord_open_price_20 <= l_ord_stoploss_28){
               if ((Ask < l_ord_open_price_20 - ld_88 * gd_100) && (l_ord_open_price_20 < l_ord_stoploss_28)){
                  if (!OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()- 2 * gd_100, OrderTakeProfit(), 0, Red))
                     if (!IsOptimization()) Print("Sell OrderModify Error " + GetLastError());
               }}}
            }
         }
      }
   }
}
double ND(double ad_0) {
   return (NormalizeDouble(ad_0, Digits));
}
int Trend()
{
//-----------
int SumUpsM5=0;
int SumUpsM15=0;
int SumUpsM30=0;
int SumUpsH1=0;
int SumUpsH4D1=0;

if (TrendH4D1 == false){
//if (iMA(NULL,PERIOD_M5,20,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M5,20,0,MODE_SMA,PRICE_CLOSE,1))
//{SumUpsM5++;}
if (iMA(NULL,PERIOD_M15,20,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M15,20,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsM15++;}
if (iMA(NULL,PERIOD_M30,20,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M30,20,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsM30++;}
if (iMA(NULL,PERIOD_H1,20,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_H1,20,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iMA(NULL,PERIOD_H4,20,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_H4,20,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH4D1++;}
if (iMA(NULL,PERIOD_D1,20,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_D1,20,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH4D1++;}
}

if (TrendH4D1 == false){ 
//if (iMA(NULL,PERIOD_M5,50,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M5,50,0,MODE_SMA,PRICE_CLOSE,1))
//{SumUpsM5++;}
if (iMA(NULL,PERIOD_M15,50,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M15,50,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsM15++;}
if (iMA(NULL,PERIOD_M30,50,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M30,50,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsM30++;}
if (iMA(NULL,PERIOD_H1,50,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_H1,50,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH4D1++;}
if (iMA(NULL,PERIOD_D1,50,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_D1,50,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH4D1++;}
}

if (TrendH4D1 == false){
//if (iMA(NULL,PERIOD_M5,100,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M5,100,0,MODE_SMA,PRICE_CLOSE,1))
//{SumUpsM5++;}
if (iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsM15++;}
if (iMA(NULL,PERIOD_M30,100,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M30,100,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsM30++;}
if (iMA(NULL,PERIOD_H1,100,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_H1,100,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iMA(NULL,PERIOD_H4,100,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_H4,100,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH4D1++;}
if (iMA(NULL,PERIOD_D1,100,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_D1,100,0,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH4D1++;}
}

if (TrendH4D1 == false){
//-----Average Directional Movement Index
//if (iADX(NULL,PERIOD_M5,14,PRICE_CLOSE,MODE_MAIN,0)>iADX(NULL,PERIOD_M5,14,PRICE_CLOSE,MODE_MAIN,1))
//{SumUpsM5++;}
if (iADX(NULL,PERIOD_M15,14,PRICE_CLOSE,MODE_MAIN,0)>iADX(NULL,PERIOD_M15,14,PRICE_CLOSE,MODE_MAIN,1))
{SumUpsM15++;}
if (iADX(NULL,PERIOD_M30,14,PRICE_CLOSE,MODE_MAIN,0)>iADX(NULL,PERIOD_M30,14,PRICE_CLOSE,MODE_MAIN,1))
{SumUpsM30++;}
if (iADX(NULL,PERIOD_H1,14,PRICE_CLOSE,MODE_MAIN,0)>iADX(NULL,PERIOD_H1,14,PRICE_CLOSE,MODE_MAIN,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iADX(NULL,PERIOD_H4,14,PRICE_CLOSE,MODE_MAIN,0)>iADX(NULL,PERIOD_H4,14,PRICE_CLOSE,MODE_MAIN,1))
{SumUpsH4D1++;}
if (iADX(NULL,PERIOD_D1,14,PRICE_CLOSE,MODE_MAIN,0)>iADX(NULL,PERIOD_D1,14,PRICE_CLOSE,MODE_MAIN,1))
{SumUpsH4D1++;}
}

if (TrendH4D1 == false){
//-----Bears
//if (iBearsPower(NULL,PERIOD_M5, 13,PRICE_CLOSE,0)>iBearsPower(NULL,PERIOD_M5, 13,PRICE_CLOSE,1))
//{SumUpsM5++;}
if (iBearsPower(NULL,PERIOD_M15, 13,PRICE_CLOSE,0)>iBearsPower(NULL,PERIOD_M15, 13,PRICE_CLOSE,1))
{SumUpsM15++;}
if (iBearsPower(NULL,PERIOD_M30, 13,PRICE_CLOSE,0)>iBearsPower(NULL,PERIOD_M30, 13,PRICE_CLOSE,1))
{SumUpsM30++;}
if (iBearsPower(NULL,PERIOD_H1, 13,PRICE_CLOSE,0)>iBearsPower(NULL,PERIOD_H1, 13,PRICE_CLOSE,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iBearsPower(NULL,PERIOD_H4, 13,PRICE_CLOSE,0)>iBearsPower(NULL,PERIOD_H4, 13,PRICE_CLOSE,1))
{SumUpsH4D1++;}
if (iBearsPower(NULL,PERIOD_D1, 13,PRICE_CLOSE,0)>iBearsPower(NULL,PERIOD_D1, 13,PRICE_CLOSE,1))
{SumUpsH4D1++;}
}

if (TrendH4D1 == false){
//----Bulls
//if (iBullsPower(NULL,PERIOD_M5, 13,PRICE_CLOSE,0)>iBullsPower(NULL,PERIOD_M5, 13,PRICE_CLOSE,1))
//{SumUpsM5++;}
if (iBullsPower(NULL,PERIOD_M15, 13,PRICE_CLOSE,0)>iBullsPower(NULL,PERIOD_M15, 13,PRICE_CLOSE,1))
{SumUpsM15++;}
if (iBullsPower(NULL,PERIOD_M30, 13,PRICE_CLOSE,0)>iBullsPower(NULL,PERIOD_M30, 13,PRICE_CLOSE,1))
{SumUpsM30++;}
if (iBullsPower(NULL,PERIOD_H1, 13,PRICE_CLOSE,0)>iBullsPower(NULL,PERIOD_H1, 13,PRICE_CLOSE,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iBullsPower(NULL,PERIOD_H4, 13,PRICE_CLOSE,0)>iBullsPower(NULL,PERIOD_H4, 13,PRICE_CLOSE,1))
{SumUpsH4D1++;}
if (iBullsPower(NULL,PERIOD_D1, 13,PRICE_CLOSE,0)>iBullsPower(NULL,PERIOD_D1, 13,PRICE_CLOSE,1))
{SumUpsH4D1++;}
}

if (TrendH4D1 == false){
//------CCI
//if (iCCI(NULL,PERIOD_M5,14,PRICE_CLOSE,0)>iCCI(NULL,PERIOD_M5,14,PRICE_CLOSE,1))
//{SumUpsM5++;}
if (iCCI(NULL,PERIOD_M15,14,PRICE_CLOSE,0)>iCCI(NULL,PERIOD_M15,14,PRICE_CLOSE,1))
{SumUpsM15++;}
if (iCCI(NULL,PERIOD_M30,14,PRICE_CLOSE,0)>iCCI(NULL,PERIOD_M30,14,PRICE_CLOSE,1))
{SumUpsM30++;}
if (iCCI(NULL,PERIOD_H1,14,PRICE_CLOSE,0)>iCCI(NULL,PERIOD_H1,14,PRICE_CLOSE,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iCCI(NULL,PERIOD_H4,14,PRICE_CLOSE,0)>iCCI(NULL,PERIOD_H4,14,PRICE_CLOSE,1))
{SumUpsH4D1++;}
if (iCCI(NULL,PERIOD_D1,14,PRICE_CLOSE,0)>iCCI(NULL,PERIOD_D1,14,PRICE_CLOSE,1))
{SumUpsH4D1++;}
}

if (TrendH4D1 == false){
//----Dem
//if (iDeMarker(NULL,PERIOD_M5, 13, 0)>iDeMarker(NULL,PERIOD_M5, 13, 1))
//{SumUpsM5++;}
if (iDeMarker(NULL,PERIOD_M15, 13, 0)>iDeMarker(NULL,PERIOD_M15, 13, 1))
{SumUpsM15++;}
if (iDeMarker(NULL,PERIOD_M30, 13, 0)>iDeMarker(NULL,PERIOD_M30, 13, 1))
{SumUpsM30++;}
if (iDeMarker(NULL,PERIOD_H1, 13, 0)>iDeMarker(NULL,PERIOD_H1, 13, 1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iDeMarker(NULL,PERIOD_H4, 13, 0)>iDeMarker(NULL,PERIOD_H4, 13, 1))
{SumUpsH4D1++;}
if (iDeMarker(NULL,PERIOD_D1, 13, 0)>iDeMarker(NULL,PERIOD_D1, 13, 1))
{SumUpsH4D1++;}
}

if (TrendH4D1 == false){
//-----Force
//if (iForce(NULL,PERIOD_M5,13,MODE_SMA,PRICE_CLOSE,0)>iForce(NULL,PERIOD_M5,13,MODE_SMA,PRICE_CLOSE,1))
//{SumUpsM5++;}
if (iForce(NULL,PERIOD_M15,13,MODE_SMA,PRICE_CLOSE,0)>iForce(NULL,PERIOD_M15,13,MODE_SMA,PRICE_CLOSE,1))
{SumUpsM15++;}
if (iForce(NULL,PERIOD_M30,13,MODE_SMA,PRICE_CLOSE,0)>iForce(NULL,PERIOD_M30,13,MODE_SMA,PRICE_CLOSE,1))
{SumUpsM30++;}
if (iForce(NULL,PERIOD_H1,13,MODE_SMA,PRICE_CLOSE,0)>iForce(NULL,PERIOD_H1,13,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iForce(NULL,PERIOD_H4,13,MODE_SMA,PRICE_CLOSE,0)>iForce(NULL,PERIOD_H4,13,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH4D1++;}
if (iForce(NULL,PERIOD_D1,13,MODE_SMA,PRICE_CLOSE,0)>iForce(NULL,PERIOD_D1,13,MODE_SMA,PRICE_CLOSE,1))
{SumUpsH4D1++;}
}

//-----MACD
if (TrendH4D1 == false){
//if (iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,1))
//{SumUpsM5++;}
if (iMACD(NULL,PERIOD_M15,12,26,9,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,PERIOD_M15,12,26,9,PRICE_CLOSE,MODE_MAIN,1))
{SumUpsM15++;}
if (iMACD(NULL,PERIOD_M30,12,26,9,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,PERIOD_M30,12,26,9,PRICE_CLOSE,MODE_MAIN,1))
{SumUpsM30++;}
if (iMACD(NULL,PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_MAIN,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1))
{SumUpsH4D1++;}
if (iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,1))
{SumUpsH4D1++;}
}

//-----MOM
if (TrendH4D1 == false){
//if (iMomentum(NULL,PERIOD_M5,14,PRICE_CLOSE,0)>iMomentum(NULL,PERIOD_M5,14,PRICE_CLOSE,1))
//{SumUpsM5++;}
if (iMomentum(NULL,PERIOD_M15,14,PRICE_CLOSE,0)>iMomentum(NULL,PERIOD_M15,14,PRICE_CLOSE,1))
{SumUpsM15++;}
if (iMomentum(NULL,PERIOD_M30,14,PRICE_CLOSE,0)>iMomentum(NULL,PERIOD_M30,14,PRICE_CLOSE,1))
{SumUpsM30++;}
if (iMomentum(NULL,PERIOD_H1,14,PRICE_CLOSE,0)>iMomentum(NULL,PERIOD_H1,14,PRICE_CLOSE,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iMomentum(NULL,PERIOD_H4,14,PRICE_CLOSE,0)>iMomentum(NULL,PERIOD_H4,14,PRICE_CLOSE,1))
{SumUpsH4D1++;}
if (iMomentum(NULL,PERIOD_D1,14,PRICE_CLOSE,0)>iMomentum(NULL,PERIOD_D1,14,PRICE_CLOSE,1))
{SumUpsH4D1++;}
}

//-----RSI
if (TrendH4D1 == false){
//if (iRSI(NULL,PERIOD_M5,14,PRICE_CLOSE,0)>iRSI(NULL,PERIOD_M5,14,PRICE_CLOSE,1))
//{SumUpsM5++;}
if (iRSI(NULL,PERIOD_M15,14,PRICE_CLOSE,0)>iRSI(NULL,PERIOD_M15,14,PRICE_CLOSE,1))
{SumUpsM15++;}
if (iRSI(NULL,PERIOD_M30,14,PRICE_CLOSE,0)>iRSI(NULL,PERIOD_M30,14,PRICE_CLOSE,1))
{SumUpsM30++;}
if (iRSI(NULL,PERIOD_H1,14,PRICE_CLOSE,0)>iRSI(NULL,PERIOD_H1,14,PRICE_CLOSE,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iRSI(NULL,PERIOD_H4,14,PRICE_CLOSE,0)>iRSI(NULL,PERIOD_H4,14,PRICE_CLOSE,1))
{SumUpsH4D1++;}
if (iRSI(NULL,PERIOD_D1,14,PRICE_CLOSE,0)>iRSI(NULL,PERIOD_D1,14,PRICE_CLOSE,1))
{SumUpsH4D1++;}
}

//----STOCH
if (TrendH4D1 == false){
//if (iStochastic(NULL,PERIOD_M5,5,3,3,MODE_SMA,0,MODE_MAIN,0)>iStochastic(NULL,PERIOD_M5,5,3,3,MODE_SMA,0,MODE_MAIN,1))
//{SumUpsM5++;}
if (iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_MAIN,0)>iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_MAIN,1))
{SumUpsM15++;}
if (iStochastic(NULL,PERIOD_M30,5,3,3,MODE_SMA,0,MODE_MAIN,0)>iStochastic(NULL,PERIOD_M30,5,3,3,MODE_SMA,0,MODE_MAIN,1))
{SumUpsM30++;}
if (iStochastic(NULL,PERIOD_H1,5,3,3,MODE_SMA,0,MODE_MAIN,0)>iStochastic(NULL,PERIOD_H1,5,3,3,MODE_SMA,0,MODE_MAIN,1))
{SumUpsH1++;}
}
if (TrendH4D1 == true){
if (iStochastic(NULL,PERIOD_H4,5,3,3,MODE_SMA,0,MODE_MAIN,0)>iStochastic(NULL,PERIOD_H4,5,3,3,MODE_SMA,0,MODE_MAIN,1))
{SumUpsH4D1++;}
if (iStochastic(NULL,PERIOD_D1,5,3,3,MODE_SMA,0,MODE_MAIN,0)>iStochastic(NULL,PERIOD_D1,5,3,3,MODE_SMA,0,MODE_MAIN,1))
{SumUpsH4D1++;}
}
if (TrendH4D1 == true){
double UpPerH4D1=NormalizeDouble(SumUpsH4D1*100/26,1);

if (UpPerH4D1>50)return(1);
else if (UpPerH4D1<50)return(-1);
   else return(0);
}
//if (UpPerH4D1>DownPerH4D1)
//else
 
//double UpPerM5=NormalizeDouble(SumUpsM5*100/13,1);
//double DownPerM5=NormalizeDouble(100-UpPerM5,1);
if (TrendH4D1 == false){ 
double UpPerM15=NormalizeDouble(SumUpsM15*100/13,1);
double DownPerM15=NormalizeDouble(100-UpPerM15,1);
 
double UpPerM30=NormalizeDouble(SumUpsM30*100/13,1);
double DownPerM30=NormalizeDouble(100-UpPerM30,1);
 
double UpPerH1=NormalizeDouble(SumUpsH1*100/13,1);
double DownPerH1=NormalizeDouble(100-UpPerH1,1);
 
if (/*UpPerM5>50&&*/UpPerM15>50&&UpPerM30>50&&UpPerH1>50)return(1);
else if (/*UpPerM5<50&&*/UpPerM15<50&&UpPerM30<50&&UpPerH1<50)return(-1);
   else return(0);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////