//+------------------------------------------------------------------+
//|                                      smLazyHedging TrendChannel_vX
//+------------------------------------------------------------------+
#property copyright "Copyright 14.02.2021 SwingMan"
//#property link      "https://www.forexfactory.com/thread/1058948-lazyhedging-trading-ideas"
#property strict
#property indicator_chart_window

/*+------------------------------------------------------------------+
14.02.2021  - v2.0   Alerts
//+-----------------------------------------------------------------*/

#property indicator_buffers 3

#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 3

#property indicator_color1 clrMediumOrchid
#property indicator_color2 clrMediumOrchid
#property indicator_color3 clrDodgerBlue

enum TRADING_DIRECTION
  {
   Main_Trend,
   Both_Directions
  };
enum RANGE_CALCULATION
  {
   Channel_Range,
   ATR_Range
  };

enum ENTRY_VALUE
  {
   Open_CurrentBar,
   HiLo_PreviousBar
  };

//-- inputs
//+------------------------------------------------------------------+
input TRADING_DIRECTION Trading_Direction=Main_Trend;
sinput string ____Averages="---------------------------------------";
input int Average_Highs_Period         =10;
input int Average_Lows_Period          =8;
input int Average_Shift                =1;
input ENUM_MA_METHOD Average_Method    =MODE_SMA;
input bool Draw_ChannelLines           =true;
input bool Draw_MiddleLine             =false;
sinput string ____Entrys="---------------------------------------";
extern bool Enable_UpperTrendFilter    =false;
input ENTRY_VALUE Entry_Value             =Open_CurrentBar;
input RANGE_CALCULATION Range_Calculation =Channel_Range;
input int Smoothing_Period                =14;
input int ATR_Period                      =14;
sinput string Money_Manageemnt="---------------------------------------";
input bool Enable_MoneyManagement      =true;
input double NumberOfLots              =0.01;
input double RiscPercents_PerTrade     =1;

sinput string Alerts="---------------------------------------";
input bool Send_Alert_Signals          = false;
input bool Send_Email_Signals          = false;
input bool Send_Notification_Signals   = false;
sinput string ____Areas="---------------------------------------";
input bool Draw_Areas                  =true;
input color Color_UP_Trend             =clrDarkGreen;
input color Color_DOWN_Trend           =clrIndigo;

input bool Show_IndicatorName          =true;
input bool Show_Comment                =false;

input bool Write_Results               =false;
input bool Draw_BigBar_Entrys          =false;
sinput string Entry_Levels="---------------------------------------";
extern color ArrowColorEntry_UP        =clrAqua;
extern color ArrowColorEntry_DN        =clrYellow;
extern color ArrowColorEntry_UP2       =clrLime;
extern color ArrowColorEntry_DN2       =clrRed;
extern int ArrowWidthEntry             =2;

extern color clrTextColor              =clrLimeGreen;

input double Factor_L1                 =1.0;
input double Factor_L2                 =2.0;
//+------------------------------------------------------------------+

//---- constants
string CR="\n";
string sObj="arHedge_";
string sObjLine="Line_";

string sIndicatorName="LazyHedging";

//---- buffers
double avgHighs[],avgLows[],avgMiddle[];
double trendUP[],trendDN[];
double channelMedian[],channelAll[];
double channelMinim[];
double avgHighsUpper[],avgLowsUpper[];
double trendUPUpper[],trendDNUpper[];

//---- variables
int limit;
int firstBarUP,lastBarUP;
int firstBarDN,lastBarDN;
int upperTimeFrame;
datetime thisTime,oldTime;
bool newBar;
bool newAlert;
int AlertSignal;

bool signalOK=false;

int nSignals;
int sumSignals;

int fileHandle;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(Write_Results==true)
      fileHandle=FileOpen("Hedge_"+Symbol()+".csv",FILE_CSV|FILE_WRITE,";");
   ChartSetInteger(0,CHART_SHOW_GRID,0,false);
//---

   signalOK=false;
   nSignals=0;
   sumSignals=0;
   oldTime=0;
   newAlert=false;
   AlertSignal=0;

//--- upper time frame
   switch(Period())
     {
      case PERIOD_M1:
         upperTimeFrame=PERIOD_H1;
         break;
      case PERIOD_M5:
         upperTimeFrame=PERIOD_H4;
         break;
      case PERIOD_M15:
         upperTimeFrame=PERIOD_H4;
         break;
         break;
      case PERIOD_M30:
         upperTimeFrame=PERIOD_D1;
         break;
      case PERIOD_H1:
         upperTimeFrame=PERIOD_D1;
         break;
      case PERIOD_H4:
         upperTimeFrame=PERIOD_W1;
         break;
      case PERIOD_D1:
         upperTimeFrame=PERIOD_MN1;
         break;
      case PERIOD_W1:
         upperTimeFrame=PERIOD_MN1;
         break;
      default:
         upperTimeFrame=Period();
         Enable_UpperTrendFilter=false;
     }

////......................
//   Draw_Symbol_Timeframe();
////......................
   int iBuff=-1;
//---
   iBuff++;
   SetIndexBuffer(iBuff,avgHighs);
   if(Draw_ChannelLines==false)
      SetIndexStyle(iBuff,DRAW_NONE);
   SetIndexLabel(iBuff,"AverageHighs"+"("+(string)Average_Highs_Period+")");

   iBuff++;
   SetIndexBuffer(iBuff,avgLows);
   if(Draw_ChannelLines==false)
      SetIndexStyle(iBuff,DRAW_NONE);
   SetIndexLabel(iBuff,"AverageLows"+"("+(string)Average_Lows_Period+")");

   iBuff++;
   SetIndexBuffer(iBuff,avgMiddle);
   if(Draw_MiddleLine==false)
      SetIndexStyle(iBuff,DRAW_NONE);
   SetIndexLabel(iBuff,"AverageMiddle");

//--- more buffers --------------------------------------------
   IndicatorBuffers(iBuff+1+2+3+4);

   iBuff++;
   SetIndexBuffer(iBuff,trendUP);
   iBuff++;
   SetIndexBuffer(iBuff,trendDN);
//---
   iBuff++;
   SetIndexBuffer(iBuff,channelMedian);
   iBuff++;
   SetIndexBuffer(iBuff,channelAll);
   iBuff++;
   SetIndexBuffer(iBuff,channelMinim);
//---
   iBuff++;
   SetIndexBuffer(iBuff,avgHighsUpper);
   iBuff++;
   SetIndexBuffer(iBuff,avgLowsUpper);
   iBuff++;
   SetIndexBuffer(iBuff,trendUPUpper);
   iBuff++;
   SetIndexBuffer(iBuff,trendDNUpper);

//---
   string sName=WindowExpertName()+ "  ("+
                (string)Average_Highs_Period+","+(string)Average_Lows_Period+","+(string)Average_Shift+") ";

//double dSpread=MarketInfo(Symbol(),MODE_SPREAD);
//string sSpread=CR+"Spread="+DoubleToString(dSpread,Digits)+"   "+DoubleToString(dSpread*Point,Digits)+"   "+DoubleToString(Point,Digits);

   if(Show_IndicatorName==true)
      Comment(CR,"... ",sName," ...");
//Comment(CR,"... ",sName," ..."+sSpread);
//---

   int firstBar=Average_Highs_Period;
   SetIndexDrawBegin(0,firstBar);
   SetIndexDrawBegin(1,firstBar);
   SetIndexDrawBegin(2,firstBar);
//---
   IndicatorDigits(Digits);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(Show_Comment==true)
      Comment("");
   ObjectsDeleteAll(0,sObj);
   ObjectsDeleteAll(0,sObjLine);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],
                const double &open[],const double &high[],const double &low[],const double &close[],
                const long &tick_volume[],const long &volume[],const int &spread[])
  {
   int counted_bars=IndicatorCounted();
   if(counted_bars < 0)
      return(-1);
   if(counted_bars>0)
      counted_bars--;
   limit=Bars-counted_bars;
   if(counted_bars==0)
      limit-=Average_Highs_Period+1;

//--- check new Bar
   thisTime=time[0];
   newBar=(thisTime!=oldTime);
   if(newBar==true)
     {
      oldTime=thisTime;
     }


//--- Moving Averages 10/8 (Jack Bernstein)
   for(int i=limit; i>=0; i--)
     {
      avgHighs[i]=iMA(Symbol(),Period(),Average_Highs_Period,Average_Shift,Average_Method,PRICE_HIGH,i);
      avgLows[i] =iMA(Symbol(),Period(),Average_Lows_Period,Average_Shift,Average_Method,PRICE_LOW,i);
      avgMiddle[i]=0.5*(avgHighs[i]+avgLows[i]);
     }

//--- Channel
   for(int i=limit; i>=0; i--)
      channelAll[i]=(avgHighs[i]-avgLows[i]);

   for(int i=limit; i>=0; i--)
     {
      channelMedian[i]=iMAOnArray(channelAll,0,Smoothing_Period,0,MODE_SMA,i);
      if(newBar==true && i==0)
         //......................
         Draw_Symbol_Timeframe();
      //......................
     }

   double minValue=0;
   for(int i=limit; i>=0; i--)
     {
      minValue=MathMin(channelAll[i],channelMedian[i]);
      channelMinim[i]=minValue;

      ////--### TEST
      //if(Time[i]==D'2020.11.05 03:00')
      //   Comment("Channel Minim= ",DoubleToString(channelMinim[i],Digits));
     }

//--- Trend Channel / .........  for new Bar .......######### zu machen
   int currentTrend=0,upperTrend=0;

   Get_TrendChannel(Symbol(),Period(),currentTrend);

//--- Upper TimeFrame trends --------------------------------------------------
   Get_UpperTrendChannel(Symbol(),upperTimeFrame,upperTrend);

//--- Draw Entrys ---------------------------------------------------
   Draw_TradingObjects();

   FileClose(fileHandle);

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string Get_TrendString(int iTrend)
  {
   string sBars="  ("+(string)MathAbs(iTrend)+")";

   if(iTrend>0)
     {
      return("UP"+sBars);
     }
   else
      if(iTrend<0)
        {
         return("DOWN"+sBars);
        }
      else
         return("NONE");
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Get_TrendChannel(string symbol,int timeFrame,int channelTrend)
  {
   bool bodyAbove=false;
   bool bodyBelow=false;

   for(int i=limit; i>=0; i--)
     {
      trendUP[i]=EMPTY_VALUE;
      trendDN[i]=EMPTY_VALUE;

      bodyAbove=(Open[i]>avgHighs[i] && Close[i]>avgHighs[i]);
      bodyBelow=(Open[i]<avgLows[i] && Close[i]<avgLows[i]);

      //=============================================================
      //--- UP-Trend ------------------------------------------------
      if(bodyAbove==true && trendUP[i+1]==EMPTY_VALUE)
        {
         trendUP[i]=1;
         trendDN[i]=EMPTY_VALUE;
        }
      else
         if(bodyAbove==true && trendUP[i+1]!=EMPTY_VALUE)
           {
            trendUP[i]=trendUP[i+1]+1;
            trendDN[i]=EMPTY_VALUE;
           }

      //=============================================================
      //--- DOWN-Trend ----------------------------------------------
      if(bodyBelow==true && trendDN[i+1]==EMPTY_VALUE)
        {
         trendDN[i]=-1;
         trendUP[i]=EMPTY_VALUE;
        }
      else
         if(bodyBelow==true && trendDN[i+1]!=EMPTY_VALUE)
           {
            trendDN[i]=trendDN[i+1]-1;
            trendUP[i]=EMPTY_VALUE;
           }

      //=============================================================
      //--- CONTINUATION-Trend --------------------------------------
      if(bodyAbove==false && bodyBelow==false)
        {
         if(trendUP[i+1]!=EMPTY_VALUE && trendDN[i+1]==EMPTY_VALUE)
            trendUP[i]=trendUP[i+1]+1;
         else
            if(trendUP[i+1]==EMPTY_VALUE && trendDN[i+1]!=EMPTY_VALUE)
               trendDN[i]=trendDN[i+1]-1;
        }
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Get_UpperTrendChannel(string symbol,int timeFrame,int& channelTrend)
  {
   bool bodyAbove=false;
   bool bodyBelow=false;
   int iBar=0;

   int upperLimit=100;

   for(iBar=limit; iBar>=0; iBar--)
     {
      avgHighsUpper[iBar]=iMA(symbol,timeFrame,Average_Highs_Period,Average_Shift,Average_Method,PRICE_HIGH,iBar);
      avgLowsUpper[iBar] =iMA(symbol,timeFrame,Average_Lows_Period,Average_Shift,Average_Method,PRICE_LOW,iBar);
     }

   for(iBar=upperLimit; iBar>=0; iBar--)
     {
      //if(timeFrame==Period())

      trendUPUpper[iBar]=EMPTY_VALUE;
      trendDNUpper[iBar]=EMPTY_VALUE;

      bodyAbove=(iOpen(symbol,timeFrame,iBar)>avgHighsUpper[iBar] && iClose(symbol,timeFrame,iBar)>avgHighsUpper[iBar]);
      bodyBelow=(iOpen(symbol,timeFrame,iBar)<avgLowsUpper[iBar] && iClose(symbol,timeFrame,iBar)<avgLowsUpper[iBar]);

      //bodyAbove=(Open[iBar]>avgHighs[iBar] && Close[iBar]>avgHighs[iBar]);
      //bodyBelow=(Open[iBar]<avgLows[iBar] && Close[iBar]<avgLows[iBar]);

      //=============================================================
      //--- UP-Trend ------------------------------------------------
      if(bodyAbove==true && trendUPUpper[iBar+1]==EMPTY_VALUE)
        {
         trendUPUpper[iBar]=1;
         trendDNUpper[iBar]=EMPTY_VALUE;
        }
      else
         if(bodyAbove==true && trendUPUpper[iBar+1]!=EMPTY_VALUE)
           {
            trendUPUpper[iBar]=trendUPUpper[iBar+1]+1;
            trendDNUpper[iBar]=EMPTY_VALUE;
           }

      //=============================================================
      //--- DOWN-Trend ----------------------------------------------
      if(bodyBelow==true && trendDNUpper[iBar+1]==EMPTY_VALUE)
        {
         trendDNUpper[iBar]=-1;
         trendUPUpper[iBar]=EMPTY_VALUE;
        }
      else
         if(bodyBelow==true && trendDNUpper[iBar+1]!=EMPTY_VALUE)
           {
            trendDNUpper[iBar]=trendDNUpper[iBar+1]-1;
            trendUPUpper[iBar]=EMPTY_VALUE;
           }

      //=============================================================
      //--- CONTINUATION-Trend --------------------------------------
      if(bodyAbove==false && bodyBelow==false)
        {
         if(trendUPUpper[iBar+1]!=EMPTY_VALUE && trendDNUpper[iBar+1]==EMPTY_VALUE)
            trendUPUpper[iBar]=trendUPUpper[iBar+1]+1;
         else
            if(trendUPUpper[iBar+1]==EMPTY_VALUE && trendDNUpper[iBar+1]!=EMPTY_VALUE)
               trendDNUpper[iBar]=trendDNUpper[iBar+1]-1;
        }
     }

//--- result
   iBar=1;
   if(trendUPUpper[iBar]!=EMPTY_VALUE)
      channelTrend=(int)trendUPUpper[iBar];
   else
      if(trendDNUpper[iBar]!=EMPTY_VALUE)
         channelTrend=(int)trendDNUpper[iBar];

////---### TEST
//Comment(
//   CR,
//   "Trend= ",channelTrend,CR,
//   "Highs= ",DoubleToString(avgHighsUpper[iBar],Digits),CR,
//   "Lows= ",DoubleToString(avgLowsUpper[iBar],Digits)
//);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw_TradingObjects()
  {
   for(int i=limit; i>=0; i--)
     {
      //=============================================================
      //--- Areas ---------------------------------------------------
      if(Draw_Areas==true)
         Draw_TrendArea(i);


      //=============================================================
      //--- Trading Levels ------------------------------------------
      //--- Distance Values
      double dATR=0;
      if(Range_Calculation==ATR_Range)
         dATR=iATR(Symbol(),Period(),ATR_Period,i+1);
      else
         if(Range_Calculation==Channel_Range)
            dATR=channelMinim[i+1];

      //===============
      //=      1      =
      //===============
      //=============================================================
      //.............................................................
      //--- Entry Level = Body above/below channel
      double dEntry=0;
      if(Entry_Value==Open_CurrentBar)
         dEntry=Open[i];
      else
         if(Entry_Value==HiLo_PreviousBar)
           {
            if(trendUP[i+1]==1)
               dEntry=High[i+1];
            else
               if(trendDN[i+1]==-1)
                  dEntry=Low[i+1];
           }



      //.............................................................
      //--- Filter Upper Trend = Current Trend
      int iBarUpper=iBarShift(Symbol(),upperTimeFrame,Time[i+1],false);

      double dTrendUP_UpperTF=trendUPUpper[iBarUpper+1];
      double dTrendDN_UpperTF=trendDNUpper[iBarUpper+1];

      bool filterUP_UpperTF=(trendUP[i+1]== 1 && dTrendUP_UpperTF!=EMPTY_VALUE);
      bool filterDN_UpperTF=(trendDN[i+1]==-1 && dTrendDN_UpperTF!=EMPTY_VALUE);

      if(Enable_UpperTrendFilter==false)
        {
         filterUP_UpperTF=true;
         filterDN_UpperTF=true;
        }
      ////### TEST
      //if(Time[i]==D'2020.09.18 21:00')
      //   int uu=0;

      //.............................................................
      double winLoss;
      string sWinLoss;
      double dSpread=MarketInfo(Symbol(),MODE_SPREAD);
      //--- Draw Levels above/below Entry
      //int iWidth=5;

      //===================================================
      //--- LONG Trend
      if(trendUP[i+1]==1 && filterUP_UpperTF==true)
        {
         int testBars=5000;
         string sTime= ";  / "+TimeToString(Time[i+1]);

         double nLots=Get_NumberLots(Symbol(),dATR,winLoss);
         string sLots=CR+"Risk= "+DoubleToString(RiscPercents_PerTrade,2)+"%"+"   Lots= "+DoubleToString(nLots,2);
         sWinLoss=CR+"WinLoss= "+DoubleToString(winLoss,2)+" "+AccountCurrency();

         if(signalOK==false)
           {
            nSignals++;
            //...........
            Check_WinLoss("buy",i,sumSignals,dEntry+dATR*Factor_L1,dEntry-dATR*Factor_L1,dEntry+dATR*Factor_L2,dEntry-dATR*Factor_L2);
            //...........

            //#############################
            if(newBar==true && i==0)
              {
               AlertSignal=1;
               newAlert = true;

               string sAlertPrice;
               SendAlerts(AlertSignal,dEntry,dATR,sAlertPrice);
               //Print("  i=",i," PriceB ",sAlertPrice);
               Comment(CR,
                       "Last signal: ",TimeToString(Time[0]),CR,
                       sAlertPrice,CR,
                       " ChannelRange= ",DoubleToString(dATR/Point,0),CR,
                       " Spread= ",DoubleToString(dSpread,0));
               //"BUY");
              }
            //if(Show_Comment==true)
            //  {
            //   Comment(CR,Time[i],CR,
            //           CR,"n Signals= ",nSignals,
            //           CR," sum Signals= ",sumSignals,
            //           CR,"Bar Range= ",DoubleToString(dATR/Point,0),sTime,sLots,sWinLoss);
            //  }
            ;
            if(nSignals==testBars)
               signalOK=true;
           }

         if(Trading_Direction==Both_Directions)
           {
            Draw_ShortTrendLine(i,dEntry,ArrowColorEntry_UP,ArrowWidthEntry*2);
            Draw_ShortTrendLine(i,dEntry+dATR*Factor_L1,ArrowColorEntry_UP,ArrowWidthEntry);
            Draw_ShortTrendLine(i,dEntry+dATR*Factor_L2,ArrowColorEntry_UP,ArrowWidthEntry);
            Draw_ShortTrendLine(i,dEntry-dATR*Factor_L1,ArrowColorEntry_DN,ArrowWidthEntry);
            Draw_ShortTrendLine(i,dEntry-dATR*Factor_L2,ArrowColorEntry_DN,ArrowWidthEntry);
           }
         else
            if(Trading_Direction==Main_Trend)
              {
               Draw_ShortTrendLine(i,dEntry,ArrowColorEntry_UP,ArrowWidthEntry*2);
               Draw_ShortTrendLine(i,dEntry+dATR*Factor_L1,ArrowColorEntry_UP,ArrowWidthEntry);
               Draw_ShortTrendLine(i,dEntry+dATR*Factor_L2,ArrowColorEntry_UP,ArrowWidthEntry);
              }
         //Draw_EntryCircle(i,dEntry,ArrowColorEntry_UP,iWidth);
        }
      else
         //===================================================
         //--- SHORT Trend
         if(trendDN[i+1]==-1 && filterDN_UpperTF==true)
           {
            int testBars=5000;
            string sTime= ";  / "+TimeToString(Time[i+1]);

            double nLots=Get_NumberLots(Symbol(),dATR,winLoss);
            string sLots=CR+"Risk= "+DoubleToString(RiscPercents_PerTrade,2)+"%"+"   Lots= "+DoubleToString(nLots,2);
            sWinLoss=CR+"WinLoss= "+DoubleToString(winLoss,2)+" "+AccountCurrency();

            if(signalOK==false)
              {
               nSignals++;
               //...........
               Check_WinLoss("sell",i,sumSignals,dEntry+dATR*Factor_L1,dEntry-dATR*Factor_L1,dEntry+dATR*Factor_L2,dEntry-dATR*Factor_L2);
               //...........

               //#############################
               if(newBar==true && i==0)
                 {
                  AlertSignal=-1;
                  newAlert = true;

                  string sAlertPrice;
                  SendAlerts(AlertSignal,dEntry,dATR,sAlertPrice);
                  Comment(CR,
                          "Last signal: ",TimeToString(Time[0]),CR,
                          sAlertPrice,CR,
                          " BarRange= ",DoubleToString(dATR/Point,0),CR,
                          " Spread= ",DoubleToString(dSpread,0));
                  //Print("  i=",i," PriceS ",sAlertPrice);
                  //Comment(CR,
                  //        TimeToString(Time[0]),CR,
                  //        sAlertPrice);
                  //"SELL");
                 }
               //if(Show_Comment==true)
               //  {
               //   Comment(CR,Time[i],CR,
               //           CR,"n Signals= ",nSignals,
               //           CR," sum Signals= ",sumSignals,
               //           CR,"Bar Range= ",DoubleToString(dATR/Point,0),sTime,sLots,sWinLoss);
               //  }
               if(nSignals==testBars)
                  signalOK=true;
              }

            if(Trading_Direction==Both_Directions)
              {
               Draw_ShortTrendLine(i,dEntry,ArrowColorEntry_DN,ArrowWidthEntry*2);
               Draw_ShortTrendLine(i,dEntry+dATR*Factor_L1,ArrowColorEntry_UP,ArrowWidthEntry);
               Draw_ShortTrendLine(i,dEntry+dATR*Factor_L2,ArrowColorEntry_UP,ArrowWidthEntry);
               Draw_ShortTrendLine(i,dEntry-dATR*Factor_L1,ArrowColorEntry_DN,ArrowWidthEntry);
               Draw_ShortTrendLine(i,dEntry-dATR*Factor_L2,ArrowColorEntry_DN,ArrowWidthEntry);
              }
            else
               if(Trading_Direction==Main_Trend)
                 {
                  Draw_ShortTrendLine(i,dEntry,ArrowColorEntry_DN,ArrowWidthEntry*2);
                  Draw_ShortTrendLine(i,dEntry-dATR*Factor_L1,ArrowColorEntry_DN,ArrowWidthEntry);
                  Draw_ShortTrendLine(i,dEntry-dATR*Factor_L2,ArrowColorEntry_DN,ArrowWidthEntry);
                 }
            //Draw_EntryCircle(i,dEntry,ArrowColorEntry_DN,iWidth);
           }


      //===============
      //=      2      =
      //===============
      //=============================================================
      //.............................................................
      if(Draw_BigBar_Entrys==true)
        {
         //--- Big Bars
         if(trendUP[i+1]!=EMPTY_VALUE)
           {
            if(Open[i+1]>avgHighs[i+1] && Close[i+1]<avgLows[i+1])
               Draw_ShortTrendLine(i,dEntry,ArrowColorEntry_DN2,ArrowWidthEntry*4);
           }
         else
            if(trendDN[i+1]!=EMPTY_VALUE)
              {
               if(Open[i+1]<avgLows[i+1] && Close[i+1]>avgHighs[i+1])
                  Draw_ShortTrendLine(i,dEntry,ArrowColorEntry_UP2,ArrowWidthEntry*4);
              }
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Check_WinLoss(string sCode,int iBar,int& sumSignalsX,double firstLevelUP,double firstLevelDN,double secondLevelUP,double secondLevelDN)
  {
   int crossLevelUP=0;
   int crossLevelDN=0;

   for(int i=iBar; i>=iBar-30; i--)
     {
      if(i<0)
         return;

      if(High[i]>firstLevelUP)
         crossLevelUP=1;
      if(Low[i]<firstLevelDN)
         crossLevelDN=1;

      //==================================================
      //--- Loss
      if(crossLevelUP==1 && crossLevelDN==1)
        {
         sumSignalsX=sumSignalsX-2;
         if(Write_Results==true)
            FileWrite(fileHandle,"Loss","-2",sumSignals,Time[iBar]);
         //Print(" ");
         //Print(" #####  Time: ",Time[iBar],"    Loss ",Time[i]);
         return;
        }

      //==================================================
      //--- Win
      //--- buy trade is OK
      if(High[i]>secondLevelUP && crossLevelDN==0)
        {
         sumSignalsX=sumSignalsX+1;
         if(Write_Results==true)
            FileWrite(fileHandle,"Win","1",sumSignals,Time[iBar]);
         //Print(" ");
         //Print(" #####  Time: ",Time[iBar],"    Win-UP ",Time[i]);
         return;
        }

      //--- sell trade is OK
      if(Low[i]<secondLevelDN && crossLevelUP==0)
        {
         sumSignalsX=sumSignalsX+1;
         if(Write_Results==true)
            FileWrite(fileHandle,"Win","1",sumSignals,Time[iBar]);
         //Print(" #####  Time: ",Time[iBar],"    Win-DN ",Time[i]);
         return;
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw_TrendArea(int iBar)
  {
   string sTrend="";
   if(trendUP[iBar]!=EMPTY_VALUE)
     {
      sTrend="UP";
      lastBarUP=iBar;
     }
   else
      if(trendDN[iBar]!=EMPTY_VALUE)
        {
         sTrend="DOWN";
         lastBarDN=iBar;
        }

//--- trend UP --------------------------------------------
   if(sTrend=="UP")
     {
      while(trendUP[iBar]!=EMPTY_VALUE)
        {
         iBar++;
        }
      firstBarUP=iBar-1;
      Draw_ColoredArea(firstBarUP,lastBarUP,Color_UP_Trend,"UP");
      return;
     }
   else
      //--- trend DOWN --------------------------------------------
      if(sTrend=="DOWN")
        {
         while(trendDN[iBar]!=EMPTY_VALUE)
           {
            iBar++;
           }
         firstBarDN=iBar-1;
         Draw_ColoredArea(firstBarDN,lastBarDN,Color_DOWN_Trend,"DOWN");
         return;
        }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw_ColoredArea(int firstBar,int lastBar,color dColor,string sCode)
  {
   datetime time1 =Time[firstBar];
   datetime time2 =Time[lastBar];
   double price1=EMPTY_VALUE;
   double price2=0;

   string sObjUpDown=sObj+sCode+"_";
   string objName=sObjUpDown+TimeToString(Time[firstBar]);

//--- new Object
   if(ObjectFind(0,objName)>=0)
      ObjectDelete(objName);

   ObjectCreate(0,objName,OBJ_RECTANGLE,0,time1,price1,time2,price2);
   ObjectSetInteger(0,objName,OBJPROP_COLOR,dColor);

   ObjectSetInteger(0,objName,OBJPROP_COLOR,dColor);
   ObjectSetInteger(0,objName,OBJPROP_SELECTABLE,true);
   ObjectSetInteger(0,objName,OBJPROP_HIDDEN,true);
  }
//+------------------------------------------------------------------+

//                **********************************
//                         FUNCTIONS
//                **********************************
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw_ShortTrendLine(int iBar,double value,color dColor,int iWidth)
  {
   datetime time1=Time[iBar];
   datetime time2=time1;
   int ip=1,nPeriods=3;

   if(iBar>=nPeriods)
      time2=Time[iBar-nPeriods];
   else
      time2=time1+nPeriods*Period()*60;

////### TEST
//   if(iBar==26)
//     {
//      iBar=2;
//      time1=Time[iBar];
//      //time2=Time[iBar-nPeriods];
//      time2=time1+nPeriods*Period()*60;
//     }
////--- loop on weekends!
//   if(Period()<=PERIOD_D1)
//     {
//      do
//        {
//         time2=time2+Period()*60;
//         if(TimeDayOfWeek(time2)!=SATURDAY && TimeDayOfWeek(time2)!=SUNDAY)
//            ip++;
//        }
//      while(ip<=nPeriods);
//     }
//   else
//time2=time2+nPeriods*Period()*60;
//---

   string objName=sObjLine+TimeToString(time1)+"_"+DoubleToString(value,Digits);

   ObjectDelete(0,objName);
   ObjectCreate(0,objName,OBJ_TREND,0,time1,value,time2,value);

   ObjectSetInteger(0,objName,OBJPROP_COLOR,dColor);
   ObjectSetInteger(0,objName,OBJPROP_WIDTH,iWidth);
   ObjectSetInteger(0,objName,OBJPROP_RAY,false);
   ObjectSetInteger(0,objName,OBJPROP_HIDDEN,true);
  }
////+------------------------------------------------------------------+
////|                                                                  |
////+------------------------------------------------------------------+
//void Draw_EntryCircle(int iBar,double value,color dColor,int iWidth)
//  {
//   datetime time1=Time[iBar];
//   //datetime time2=time1;
//   //int ip=1,nPeriods=3;
//   //uchar arrowCode=59; //159; //164;
//   uchar arrowCode=164;
//
////---
//
//   string objName=sObj+TimeToString(time1)+"_E_"+DoubleToString(value,Digits);
//
//   ObjectDelete(0,objName);
//   ObjectCreate(0,objName,OBJ_ARROW_UP,0,time1,value);
//   //ObjectCreate(0,objName,OBJ_ARROW_BUY,0,time1,value);
//   //ObjectCreate(0,objName,OBJ_ARROW_LEFT_PRICE,0,time1,value);
//   //ObjectCreate(0,objName,OBJ_ARROW,0,time1,value);
//
//   //ObjectSetInteger(0,objName,OBJPROP_ARROWCODE,arrowCode);
//   ObjectSetInteger(0,objName,OBJPROP_COLOR,dColor);
//   ObjectSetInteger(0,objName,OBJPROP_WIDTH,iWidth);
//   //ObjectSetInteger(0,objName,OBJPROP_HIDDEN,true);
//  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string Get_PeriodName(int period)
  {
   string sPeriod="";

   switch(period)
     {
      case PERIOD_M1:
         sPeriod="M1";
         break;
      case PERIOD_M5:
         sPeriod="M5";
         break;
      case PERIOD_M15:
         sPeriod="M15";
         break;
      case PERIOD_M30:
         sPeriod="M30";
         break;
      case PERIOD_H1:
         sPeriod="H1";
         break;
      case PERIOD_H4:
         sPeriod="H4";
         break;
      case PERIOD_D1:
         sPeriod="D1";
         break;
      case PERIOD_W1:
         sPeriod="W1";
         break;
      case PERIOD_MN1:
         sPeriod="MN1";
         break;
     }
   return(sPeriod);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw_Symbol_Timeframe()
  {
   color dColor=clrTextColor;
//color dColor=clrDodgerBlue;
   int iWidth=12;

   string sUpperTF=" / "+Get_PeriodName(upperTimeFrame);
//string sUpperTF="  ["+Get_PeriodName(upperTimeFrame)+"]";
//string sText=Symbol()+", "+Get_PeriodName(Period())+sUpperTF; //upper timeframe
//string sText=Symbol()+"  ["+Get_PeriodName(Period())+"]";
   double dSlope=0;
   string sText="";
   if(avgMiddle[2]!=0)
      dSlope=10000*(avgMiddle[0]-avgMiddle[2])/avgMiddle[2];
   string sSlope=" /slp= "+DoubleToString(dSlope,2);
   if(Enable_UpperTrendFilter==true)
      sText=Symbol()+", "+Get_PeriodName(Period())+sUpperTF+sSlope; // + slope
   else
      sText=Symbol()+", "+Get_PeriodName(Period())+sSlope; // + slope

   string objName=sObj+"Symbol";

   ObjectDelete(0,objName);
   bool bObjCreate=ObjectCreate(0,objName,OBJ_LABEL,0,0,0);

   ObjectSetInteger(0,objName,OBJPROP_CORNER,CORNER_LEFT_LOWER);
   ObjectSetInteger(0,objName,OBJPROP_ANCHOR,ANCHOR_LEFT_LOWER);
   ObjectSetInteger(0,objName,OBJPROP_XDISTANCE,5);
   ObjectSetInteger(0,objName,OBJPROP_YDISTANCE,5);

   ObjectSetString(0,objName,OBJPROP_TEXT,sText);
   ObjectSetString(0,objName,OBJPROP_FONT,"Arial Black");

   ObjectSetInteger(0,objName,OBJPROP_FONTSIZE,iWidth);

   ObjectSetInteger(0,objName,OBJPROP_COLOR,dColor);
   ObjectSetInteger(0,objName,OBJPROP_WIDTH,iWidth);

   ObjectSetInteger(0,objName,OBJPROP_HIDDEN,true);
  }
//             **************************************
//             *         MONEY MANAGEMENT           *
//             **************************************
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Get_NumberLots(string symbol,double stopLossDistanceX,double& winLossX)
  {
   double Trading_KapitalValue=AccountBalance();
//if(Trading_Kapital>0)
//   Trading_KapitalValue=Trading_Kapital;
//else
//   Trading_KapitalValue=AccountBalance();

   double lot=0;
//double minLot,maxLot;
//int idMessage;
//string sMessage;
   if(Enable_MoneyManagement==false)
      return(NumberOfLots);

//double stoploss=stopLossPoints*MarketInfo(symbol,MODE_POINT);
   double stoploss=stopLossDistanceX;
   double risk=RiscPercents_PerTrade;
   int lotdigits=2;

   double ticketSize=MarketInfo(symbol,MODE_TICKSIZE);
   double tickValue=MarketInfo(symbol,MODE_TICKVALUE);

//lot=100*(risk/100)/(stoploss/MarketInfo(symbol,MODE_TICKSIZE)*MarketInfo(symbol,MODE_TICKVALUE));
   if(ticketSize!=0 && tickValue!=0 && stoploss!=0)
      lot=Trading_KapitalValue*(risk/100)/(stoploss/ticketSize*tickValue);
   else
     {
      int zz=5;
     }

   lot=NormalizeDouble(lot,2);
   winLossX=lot*(stoploss/MarketInfo(symbol,MODE_TICKSIZE)*MarketInfo(symbol,MODE_TICKVALUE));

   return(lot);
  }

//####################################################################
//+------------------------------------------------------------------+
//    function SendAlerts
//+------------------------------------------------------------------+
void SendAlerts(int AlertSignalX,double dEntryX,double dATRX,string &sPriceX)
  {
   if(Send_Alert_Signals == false && Send_Email_Signals == false && Send_Notification_Signals == false)
      return;

   if(newAlert == false)
      return;
   if(AlertSignal == 0)
      return;

//string st,
   string sAlert,sMail;
   string sSymbol = Symbol();

   string sPeriod=Get_PeriodName(Period());
   string sTime  = TimeToStr(TimeCurrent(),TIME_MINUTES|TIME_SECONDS);

//--- SPREAD
   double dSpread=MarketInfo(Symbol(),MODE_SPREAD)*Point;

//int iOrderType = AlertSignal;
//string sPrice = DoubleToStr(iClose(sSymbol,Period(),0), Digits);
   string sPrice;

   switch(AlertSignalX)
     {
      case  1:
         //st = "BUY ";
         sPrice=" BUY="+DoubleToString(dEntryX+dATRX*Factor_L1+dSpread,Digits)+
                " TP="+DoubleToString(dEntryX+dATRX*Factor_L2,Digits);

         //Print("");
         //Print("Entry=",DoubleToString(dEntryX+dATRX*Factor_L1,Digits),"   Spread=",DoubleToString(dSpread,Digits));
         //Print(sPrice);

         break;
      case -1:
         //st = "SELL ";
         sPrice=" SELL="+DoubleToString(dEntryX-dATRX*Factor_L1,Digits)+
                " TP="+DoubleToString(dEntryX-dATRX*Factor_L2+dSpread,Digits);
         //Print(sPrice);
         break;
     }

   sAlert = sSymbol + "  " + sPrice + "   (" + sPeriod + ")  " + sIndicatorName + "  " + sTime;
   sMail  = sSymbol + "  " + sPrice + "   (" + sPeriod + ")  " + sTime;

   if(Send_Alert_Signals == true)
      Alert(sAlert);
   if(Send_Email_Signals == true)
      SendMail(sIndicatorName, sMail);
   if(Send_Notification_Signals == true)
      SendNotification(sMail);

   Print(sMail);

   AlertSignal = 0;
   newAlert = false;

   sPriceX=sPrice;

   return;
  }
//+------------------------------------------------------------------+
