//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright   "Copyright 2022, A3migo Project."
#property link        "https://no-website.yet"
#property version     "443.776"
#property description "RSI D "
#property description "modified from 24.0321"
#include "TradeAlt.mqh"//       CTradeAlt         trade;
#resource "IND5.rRSI(oma)(ds)22.1212.ex5"
#resource "Macd_Tema.ex5"
#resource "IND5.rRsi2Macd25.1111.ex5"
//#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006
#define buy ORDER_TYPE_BUY
#define sel ORDER_TYPE_SELL
#define none -1
#define sym (string)Symbol()
#define dig (uchar)Digits()
#define total (uchar)PositionsTotal()
#define ask (double)SymbolInfoDouble(sym,SYMBOL_ASK)
#define bid (double)SymbolInfoDouble(sym,SYMBOL_BID)
#define bal (double)AccountInfoDouble(ACCOUNT_BALANCE)
#define equ (double)AccountInfoDouble(ACCOUNT_EQUITY)
#define apft (double)AccountInfoDouble(ACCOUNT_PROFIT)
#define pnt (double)SymbolInfoDouble(sym,SYMBOL_POINT)
#define mlvl (double)AccountInfoDouble(ACCOUNT_MARGIN_LEVEL)

//#define fmlvl (double)AccountInfoDouble(ACCOUNT_MARGIN_FREE)
//input double   iEquityStop    = 18.0;    // Profit Target (Percent of Start Lot)
//input double
input double   iStartLots     = 0.15;   // Trade Lot
//input double   i2ndLot    = 0.35;   // Smaller Lot
input double   i2ndLot    = 0.06;   // Smaller Lot
input double   iStart3Lots     = 0.35;   // Trade Lot
//input int      iStopLoss      = 900;    // Stop Loss (Initial)
input int      iMagicNumber   = 227;    // Magic Number
input int          iSlippage           = 1;       // Slippage (in pips)
input int          Spread              = 7;       // Spread (in pips)
//static int totalbuy, totalsel;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class AutoA3m
  {
private:
public:
   //   bool              openNewTrade(bool int typ);
   bool              openNewTrade(bool buySignal, bool selSignal, int buyCount, int sellCount);
//   void              CloseTrades(int no2close = 999);
   bool              buffers();
   bool              trendy();
   void              accounts();
   bool              IsHourTrue();
   bool              osd();
   bool              release();
   bool              checkGV();
   double            GetLastHistoryPositions();
   double            CalculateHedgeAveragePrice();
   //   int               Trades; //              = 13;     // Goal to keep #Trades Maximum
   //   int               iPointOrderStep; //     = 45;      // Group Step (in pnts)
   //ENUM_TIMEFRAMES Higher1TF         = PERIOD_M6; // Filter - Higher TF
   ENUM_TIMEFRAMES   Higher2TF; //         = PERIOD_M20; // Filter - Higher TF
   ENUM_TIMEFRAMES   Higher3TF; //         = PERIOD_M30; // Filter - Higher TF
   ENUM_TIMEFRAMES   Higher4TF; //         = PERIOD_H1; // Filter - Higher TF
   //   ENUM_TIMEFRAMES   macdTF3; //         = PERIOD_M5; // Filter - Higher TF
   ENUM_TIMEFRAMES   macdTF2; //         = PERIOD_M5; // Filter - Higher TF
   ENUM_TIMEFRAMES   macdTF; //         = PERIOD_M12; // Filter - Higher TF
   double            iStartLot, iStartLot2, tsla, tslb, mlvl0; //, qq0e, qq1e, qq2e; // MinLoss,
   ulong             worsTics[];
   int               b4, s4, rsiMacd_handle, tapMa_handle, TF1_RSO_handle, macd4_handle, macd3_handle, macd2_handle, TF2_RSO_handle;
   double            prevFullLotPrice[2], tap[], rsimacd40[], rsimacd41[], TF1_RSO[], macd40[], macd41[], macd20[], macd21[], TF2_RSO[], macd30[], macd31[];
   datetime          dt[2], Time[], hi1Time[], hi2Time[], hi3Time[], hi4Time[]; //, ot; // , hi1Time[]
   datetime          currTime, new3htfBuy, new3htfSel;
   double tsltotal, temaDistance, ask0, bid0, pnt0, bal0, equ0, apft0, trend[2], MaxEqu[3], Last[2], LastLot[2], tsl[2], pft, sw, ltv, totalLosses, net, minLoss, maxLoss
   , lever, pftTotal, pft2Total, iMinimalProfit, xcm //, pntStep, xcm
   , trailPft, dista, maxLot, buyMng, selMng, buyMxg, selMxg;
   int               totalbuy, totalsel, ccount, LastError, ps3, ps1, b, s, bs, b2, s2;
   bool              testing, osd_to_be_called, marg, check;
  };
CPositionInfo     position;
CTradeAlt         trade;
AutoA3m           auto;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//   auto.Trades              = 13;     // Goal to keep #Trades Maximum
//   auto.iPointOrderStep     = 45;      // Group Step (in pnts)
   auto.iStartLot = iStartLots;
   auto.iStartLot2 = i2ndLot;
//   auto.iStartLot3 = iStartLots3;
//ENUM_TIMEFRAMES Higher1TF         = PERIOD_M6; // Filter - Higher TF
   auto.Higher2TF         = PERIOD_M20; // Filter - Higher TF
   auto.Higher3TF         = PERIOD_M30; // Filter - Higher TF
   auto.Higher4TF         = PERIOD_H1; // Filter - Higher TF
//   auto.macdTF3         = PERIOD_H2; // Filter - Higher TF
   auto.macdTF2         = PERIOD_M5; // Filter - Higher TF
   auto.macdTF         = PERIOD_M12; // Filter - Higher TF
   auto.testing = MQLInfoInteger(MQL_TESTER);
   auto.macd4_handle = auto.rsiMacd_handle = auto.TF1_RSO_handle = auto.macd2_handle = auto.TF2_RSO_handle = auto.macd3_handle = INVALID_HANDLE; //, m
   auto.minLoss = auto.iStartLot * 1000 * 0.35;
   auto.maxLoss = -auto.iStartLot * 1000 * 8.24;
   auto.lever = (double)AccountInfoInteger(ACCOUNT_LEVERAGE);
   auto.iMinimalProfit = 6 * auto.iStartLot * 1.618033;
//   auto.pntStep = (auto.iPointOrderStep * pnt);
   auto.xcm = -auto.iStartLot * 27;
//   auto.eqplus = iEquityStop * iStartLots * 10;
   auto.dista = NormalizeDouble(2.618034 / pow(10, _Digits - 1), _Digits + 2);
   auto.temaDistance = 90 * Point();
   auto.maxLot = SymbolInfoDouble(NULL, SYMBOL_VOLUME_MAX);
   auto.ccount = 3;
   auto.LastError = sel;
   auto.ps3 = PeriodSeconds(auto.Higher3TF);
   auto.ps1 = PeriodSeconds(auto.macdTF);
   auto.b = auto.s = auto.b4 = auto.s4 = auto.bs = auto.b2 = auto.s2 = 0;
   auto.osd_to_be_called = auto.marg = auto.check = false;
   auto.testing = MQLInfoInteger(MQL_TESTER);
   auto.ask0 = ask;
   auto.bid0 = bid;
   auto.pnt0 = pnt;
   auto.ps1 = PeriodSeconds(auto.macdTF);
   auto.ps3 = PeriodSeconds(auto.Higher3TF);
//   auto.pntStep = (auto.iPointOrderStep * pnt);
   Comment("");
   auto.xcm = -auto.iStartLot * 27;
   auto.maxLot = SymbolInfoDouble(NULL, SYMBOL_VOLUME_MAX);
   auto.dista = NormalizeDouble(2.618034 / pow(10, _Digits - 1), _Digits + 2);
   auto.lever = (int)AccountInfoInteger(ACCOUNT_LEVERAGE); // info.Leverage()*1.618033;
   TesterHideIndicators(true);
   if(auto.testing != false)
      trade.LogLevel(LOG_LEVEL_NO);
   else
      trade.LogLevel(LOG_LEVEL_ALL);
   trade.SetExpertMagicNumber(iMagicNumber);
   trade.SetDeviationInPoints(iSlippage);
   trade.SetMarginMode();
   trade.SetTypeFillingBySymbol(Symbol());
   if(!auto.testing)
      trade.SetAsyncMode(true);
   ArraySetAsSeries(auto.tap, true);
   ArraySetAsSeries(auto.TF1_RSO, true);
   ArraySetAsSeries(auto.TF2_RSO, true);
   ArraySetAsSeries(auto.Time, true);
   ArraySetAsSeries(auto.hi1Time, true);
   ArraySetAsSeries(auto.hi2Time, true);
   ArraySetAsSeries(auto.hi3Time, true);
   ArraySetAsSeries(auto.hi4Time, true);
   ArraySetAsSeries(auto.macd20, true);
   ArraySetAsSeries(auto.macd21, true);
   ArraySetAsSeries(auto.macd30, true);
   ArraySetAsSeries(auto.macd31, true);
//   ArraySetAsSeries(auto.macd40, true);
//   ArraySetAsSeries(auto.macd41, true);
   ArraySetAsSeries(auto.rsimacd40, true);
   ArraySetAsSeries(auto.rsimacd41, true);
   ArrayResize(auto.tap, 5, 5);
   ArrayResize(auto.TF1_RSO, 5, 5);
   ArrayResize(auto.TF2_RSO, 5, 5);
   ArrayResize(auto.Time, 5, 5);
   ArrayResize(auto.hi1Time, 5, 5);
   ArrayResize(auto.hi2Time, 5, 5);
   ArrayResize(auto.hi3Time, 5, 5);
   ArrayResize(auto.hi4Time, 5, 5);
   ArrayResize(auto.macd20, 5, 5);
   ArrayResize(auto.macd21, 5, 5);
   ArrayResize(auto.macd30, 5, 5);
   ArrayResize(auto.macd31, 5, 5);
//   ArrayResize(auto.macd40, 5, 5);
//   ArrayResize(auto.macd41, 5, 5);
   ArrayResize(auto.rsimacd40, 5, 5);
   ArrayResize(auto.rsimacd41, 5, 5);
   auto.tapMa_handle = iTEMA(NULL, auto.macdTF, 9, 0, PRICE_MEDIAN);
   if(auto.tapMa_handle < 0)
     {
      Print("The creation of tapMa has failed: tapMa_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   auto.rsiMacd_handle = iCustom(NULL, auto.macdTF2, "::IND5.rRsi2Macd25.1111.ex5", 12, 34, 9, PRICE_CLOSE, false, false, false);
   if(auto.rsiMacd_handle < 0)
     {
      Print("The creation of rsiMacd has failed: rsiMacd_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   auto.TF1_RSO_handle = iCustom(NULL, auto.macdTF, "::IND5.rRSI(oma)(ds)22.1212.ex5", 5, 8, MODE_EMA, PRICE_CLOSE);
   if(auto.TF1_RSO_handle < 0)
     {
      Print("The creation of TF1_RSO has failed: TF1_RSO_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   auto.macd2_handle = iCustom(NULL, auto.macdTF, "::Macd_Tema.ex5", 12, 26, 9, PRICE_WEIGHTED, false, false, false);
   if(auto.macd2_handle < 0)
     {
      Print("The creation of Hi4TF_Macd has failed: Hi4TF_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   auto.TF2_RSO_handle = iCustom(NULL, auto.macdTF2, "::IND5.rRSI(oma)(ds)22.1212.ex5", 5, 8, MODE_EMA, PRICE_CLOSE);
   if(auto.TF2_RSO_handle < 0)
     {
      Print("The creation of TF1_RSO has failed: TF1_RSO_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   auto.macd3_handle = iCustom(NULL, auto.macdTF2, "::Macd_Tema.ex5", 12, 26, 9, PRICE_WEIGHTED, false, false, false);
   if(auto.macd3_handle < 0)
     {
      Print("The creation of Hi4TF_Macd has failed: Hi4TF_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     } /*
   auto.macd4_handle = iCustom(NULL, auto.macdTF3, "::Macd_Tema.ex5", 12, 34, 9, PRICE_WEIGHTED, false, false, false);
   if(auto.macd4_handle < 0)
     {
      Print("The creation of Hi4TF_Macd has failed: Hi4TF_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     } */
   int total0 = total;
   auto.bal0 = bal;
   auto.currTime = iTime(NULL, auto.macdTF, 0);
   auto.new3htfBuy = iTime(NULL, auto.Higher3TF, 0) + 33 * 60; //auto.ps3 + auto.ps3;
   auto.new3htfSel = iTime(NULL, auto.Higher3TF, 0) + 33 * 60; //auto.ps3 + auto.ps3;
   auto.selMxg = auto.buyMxg = 0;
   auto.buyMng = auto.selMng = DBL_MAX; /*
   if(total >= 1)
     {
      for(int k = total - 1; k >= 0; k--)
         if(position.SelectByIndex(k))
           {
            int otyp = position.Type();
            if(otyp != POSITION_TYPE_SELL && otyp != POSITION_TYPE_BUY)
               continue;
            if(_Symbol != position.Symbol())
               continue;
            if(position.Volume()<=i2ndLot)
               continue;
            if(otyp == POSITION_TYPE_BUY)
               auto.bigbuy = true;
            if(otyp == POSITION_TYPE_SELL)
               auto.bigsel = true;
           }
     } */
   auto.accounts();
   auto.osd_to_be_called = true;
   auto.ccount = 3;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//   if(auto.testing)
//      OnDeinit(REASON_CHARTCLOSE);
   auto.accounts();
//   gapBuy = gapSel = false;
   static int ototal; // remember open positions total
   int p = PositionsTotal();
   if(p != ototal)
      auto.ccount = 3;
   if(auto.ccount > 0)
     {
      auto.b4 = auto.s4 = auto.totalbuy = auto.totalsel = 0;
      auto.Last[sel] = auto.Last[buy] = 0;
      auto.selMxg = auto.buyMxg = 0;
      auto.buyMng = auto.selMng = DBL_MAX;
      int total0 = total;
      for(int k = total0 - 1; k >= 0; k--)
         if(position.SelectByIndex(k))
           {
            int otyp = position.Type();
            if(otyp != POSITION_TYPE_SELL && otyp != POSITION_TYPE_BUY)
               continue;
            if(_Symbol != position.Symbol())
               continue;
            auto.ltv = position.Volume();
            if(otyp == POSITION_TYPE_SELL)
              {
               ++auto.s4;
               if(auto.ltv >= iStartLots)
                 {
                  auto.totalsel+=1;
                  if(position.PriceOpen() > auto.selMxg)
                     auto.selMxg = position.PriceOpen();
                  if(position.PriceOpen() < auto.selMng)
                     auto.selMng = position.PriceOpen();
                 }
              }
            else
              {
               ++auto.b4;
               if(auto.ltv >= iStartLots)
                 {
                  auto.totalbuy+=1;
                  if(position.PriceOpen() > auto.buyMxg)
                     auto.buyMxg = position.PriceOpen();
                  if(position.PriceOpen() < auto.buyMng)
                     auto.buyMng = position.PriceOpen();
                 }
              }
            if(auto.testing)
               if(position.Profit()>i2ndLot *1000)
                  trade.PositionClose(position.Ticket(),-1);
           }
      auto.ccount = auto.ccount - 1;
      ototal = PositionsTotal();
      auto.CalculateHedgeAveragePrice();
     }
   if(!(CopyTime(NULL, PERIOD_M12, 0, 5, auto.Time) > 4)) // || (Time[0]<=new3htfSel && Time[0]<=new3htfBuy)) // && !gapBuy && !gapSel))
      return;
   datetime prevTime = auto.currTime;
   auto.currTime = auto.Time[0];
   if(prevTime == auto.currTime) // && (Time[0]<=new3htfSel && Time[0]<=new3htfBuy && !gapBuy && !gapSel))
      return;
   if(!auto.buffers())
      return;
   auto.currTime = auto.Time[0];
   if((auto.currTime > auto.new3htfBuy || auto.currTime > auto.new3htfSel) && auto.ask0 - auto.bid0 <= Spread * 10 * auto.pnt0)
     {
      static double rsi40a, rsi40b, rsi41a;
      rsi40a = auto.rsimacd40[1];
      rsi40b = auto.rsimacd40[2];
      rsi41a = auto.rsimacd41[1];
      if(fabs(rsi40b - rsi40a) <= 5 && (fabs(rsi40b - rsi40a) >= 4 || fabs(rsi40a - rsi40b) >= 4 || fabs(rsi40a - rsi41a) >= 4 || fabs(rsi41a - rsi40a) >= 4))
        {
         static bool sigBuy, sigSel;
         sigSel = auto.currTime > auto.new3htfSel ? true : false; //auto.openTradeType += 3;
         sigBuy = auto.currTime > auto.new3htfBuy ? true : false; //auto.openTradeType += (auto.openTradeType == 2) ? 2 : 4;
         if(auto.openNewTrade(sigBuy, sigSel, auto.totalbuy, auto.totalsel) >= true) //auto.openTradeType) >= true)
            auto.CalculateHedgeAveragePrice();
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool AutoA3m::openNewTrade(bool buySignal, bool selSignal, int buyCount, int sellCount)
  {
   static double h0, h01;
   ResetLastError();
//   if(fabs(rsimacd40[2] - rsimacd40[1]) < 2.65 || fabs(rsimacd40[1] - rsimacd40[2]) < 2.65 || fabs(rsimacd40[1] - rsimacd41[1]) < 2.65 || fabs(rsimacd41[1] - rsimacd40[1]) < 2.65)
//      return false;
   /*   if(fabs(rsimacd40[2] - rsimacd40[1]) > 3.65 || fabs(rsimacd40[1] - rsimacd40[2]) > 3.65 || fabs(rsimacd40[1] - rsimacd41[1]) > 3.65 || fabs(rsimacd41[1] - rsimacd40[1]) > 3.65)
         buySignalb = selSignalb = true;
         else
         buySignalb = selSignalb = true; */
//   const double mgdistance = NormalizeDouble(160 / pow(10,dig),dig);
   static double mgdistance;
//   double maxi = fmin(maxLot, fmax(iStartLot, (equ0 * 2 - bal0) / 1000)); //  && (s4 < 1 || lastSel < pnt0)
   if(selSignal)
     {
      h0 = TF1_RSO[0];
      h01 = TF2_RSO[0];
      if(trend[0] == sel || trend[1] == sel) //trend[3]==sel ||
         if(h01 < TF2_RSO[1] - 3 || h0 < TF1_RSO[1] - 3)
           {
            new3htfSel = Time[0];
            ccount = 5;
            //           LastLot[sel] = iStartLots; //!bigsel ? iStartLots : (selMng < DBL_MAX && Last[sel] == selMng && (selMxg - selMng) / (160 / pow(10,dig)) < totalsel) ? iStart3Lots : i2ndLot;
            //double lotSel;
            if(sellCount < 2)
               LastLot[sel] = iStartLots;
            else
               //                  LastLot[sel] = i2ndLot;
              {
               mgdistance = NormalizeDouble(160 / pow(10,dig),dig);
               double distSel = bid - selMxg;
               if(distSel > mgdistance) // && selMxg > 0 && sellCount > 1) // && s4 >= 2)
                  LastLot[sel] = iStart3Lots;
               else
                  LastLot[sel] = i2ndLot;
              }
            //            LastLot[sel] = lotSel;
            osd();
            Last[sel] = bid;
            if(!trade.Sell(NormalizeDouble(LastLot[sel], 2)))
              {
               LastError = GetLastError();
               Print("OrderSend error #", LastError);
               if(testing != false && LastError == 10019 && LastError != 0)
                  ExpertRemove();
               new3htfSel = iTime(NULL, PERIOD_M6, 0);
               ResetLastError();
              }
            else
               if(!buySignal)
                  return true;
           }
     }
   if(buySignal)
     {
      h0 = TF1_RSO[0];
      h01 = TF2_RSO[0];
      if(trend[0] == buy || trend[1] == buy) //trend[3]==sel ||
         if(h01 > TF2_RSO[1] + 3 || h0 > TF1_RSO[1] + 3)
           {
            new3htfBuy = Time[0];
            ccount = 5;
            //           LastLot[buy] = iStartLots; //!bigbuy ? iStartLots : (buyMxg > 0 && Last[buy] == buyMxg && (buyMxg - buyMng) / (160 / pow(10,dig)) < totalbuy) ? iStart3Lots : i2ndLot;
            //double lotBuy;
            if(buyCount < 2)
               LastLot[buy] = iStartLots;
            else
               //                  LastLot[buy] = i2ndLot;
              {
               mgdistance = NormalizeDouble(160 / pow(10,dig),dig);
               double distBuy = buyMng - ask;
               if(distBuy > mgdistance) // && buyMng < DBL_MAX && buyCount > 1) // && b4 >= 2)
                  LastLot[buy] = iStart3Lots;
               else
                  LastLot[buy] = i2ndLot;
               //LastLot[buy] = lotBuy;
              }
            osd();
            Last[buy] = ask;
            if(!trade.Buy(NormalizeDouble(LastLot[buy], 2)))
              {
               LastError = GetLastError();
               Print("OrderSend error #", LastError);
               if(testing != false && LastError == 10019 && LastError != 0)
                  ExpertRemove();
               new3htfBuy = iTime(NULL, PERIOD_M6, 0);
               ResetLastError();
              }
            else
               return true;
           }
     }
   return false;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
   auto.osd_to_be_called = true;
   auto.ccount = 5;
   auto.release();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool AutoA3m::buffers()
  {
   int i = 1, j = i - 1;
   double h0 = 999, h1 = 999, h02 = 999, h12 = 999;
   ArrayResize(tap, 5, 5);
   ArrayResize(TF1_RSO, 5, 5);
   ArrayResize(TF2_RSO, 5, 5);
   ArrayResize(Time, 5, 5);
   ArrayResize(macd20, 5, 5);
   ArrayResize(macd21, 5, 5);
   ArrayResize(macd30, 5, 5);
   ArrayResize(macd31, 5, 5);
//   ArrayResize(macd40, 5, 5);
//   ArrayResize(macd41, 5, 5);
   ArrayResize(hi1Time, 5, 5);
   ArrayResize(hi2Time, 5, 5);
   ArrayResize(hi3Time, 5, 5);
   ArrayResize(hi4Time, 5, 5);
   ArrayResize(rsimacd40, 5, 5);
   ArrayResize(rsimacd41, 5, 5);
   for(i = 1; i < 6; ++i)
     {
      if(!(CopyTime(NULL, Higher3TF, 0, 5, hi3Time) > j))
         continue;
      else
         if(!(CopyTime(NULL, macdTF2, 0, 5, hi1Time) > j))
            continue;
         else
            if(!(CopyTime(NULL, Higher2TF, 0, 5, hi2Time) > j))
               continue;
            else
               if(!(CopyTime(NULL, Higher4TF, 0, 5, hi4Time) > j))
                  continue; /*
               else
                  if(!(CopyBuffer(macd4_handle, 1, 0, 5, macd41) > j)) // || macd21[0]==EMPTY_VALUE)
                     continue;
                  else
                     if(!(CopyBuffer(macd4_handle, 0, 0, 5, macd40) > j))
                        continue; */
               else
                  if(!(CopyBuffer(tapMa_handle, 0, 0, 5, tap) > j))
                     continue;
                  else
                     if(!(CopyBuffer(macd3_handle, 1, 1, 5, macd31) > j)) // || macd21[0]==EMPTY_VALUE)
                        continue;
                     else
                        if(!(CopyBuffer(macd3_handle, 0, 1, 5, macd30) > j))
                           continue;
                        else
                           if(!(CopyBuffer(TF2_RSO_handle, 0, 1, 5, TF2_RSO) > j)) // || TF1_RSO[0]==EMPTY_VALUE || TF1_RSO[1]==EMPTY_VALUE || TF1_RSO[2]==EMPTY_VALUE)
                              continue;
                           else
                              if(!(CopyBuffer(rsiMacd_handle, 1, 1, 5, rsimacd41) > j)) // || macd21[0]==EMPTY_VALUE)
                                 continue;
                              else
                                 if(!(CopyBuffer(rsiMacd_handle, 0, 1, 5, rsimacd40) > j))
                                    continue;
                                 else
                                    if(!(CopyBuffer(macd2_handle, 1, 1, 5, macd21) > j)) // || macd21[0]==EMPTY_VALUE)
                                       continue;
                                    else
                                       if(!(CopyBuffer(macd2_handle, 0, 1, 5, macd20) > j))
                                          continue;
                                       else
                                          if(!(CopyBuffer(TF1_RSO_handle, 0, 1, 5, TF1_RSO) > j)) // || TF1_RSO[0]==EMPTY_VALUE || TF1_RSO[1]==EMPTY_VALUE || TF1_RSO[2]==EMPTY_VALUE)
                                             continue;
                                          else
                                            {
                                             h0 = TF1_RSO[0];
                                             h1 = TF1_RSO[1];
                                             h02 = TF2_RSO[0];
                                             h12 = TF2_RSO[1];
                                             if(i < 6 && ((h0 <= 100.00 && h1 <= 100.00 && h0 >= 0.00 && h1 >= 0.00 && (h0 < h1 - 3 || h0 > h1 + 3))
                                                          || (h02 <= 100.00 && h12 <= 100.00 && h02 >= 0.00 && h12 >= 0.00 && (h02 < h12 - 3 || h02 > h12 + 3))))
                                               {
                                                if(trendy())
                                                   return true;
                                               }
                                             else
                                                break;
                                            }
     }
   return false;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool AutoA3m::trendy()
  {
   datetime hi4t = Time[0];
   int newCandle = hi4t > new3htfSel ? hi4t > new3htfBuy ? 3 : sel : buy;
   int can1dle = hi4t == hi1Time[0] ? 1 : 0; // M20
   int can2dle = hi4t == hi2Time[0] ? 1 : 0; // M20
   int can3dle = hi2Time[0] == hi3Time[0] ? 1 : 0; // H1
   int can4dle = hi3Time[0] == hi4Time[0] ? 1 : 0; // H3
   if(newCandle == sel)
      return true;
//   if(macd40[can1dle + 0] < macd40[can1dle + 2] - (dista * 1.62) && (trend[1] != sel || trend[0] != sel))
   if(tap[0]<tap[1] && (trend[1] != sel || trend[0] != sel))
     {
      if(macd20[can2dle + 0] < macd20[can2dle + 2] - (dista * 1.62) && (macd20[can2dle + 0] < macd21[can2dle + 1] - dista || macd21[can2dle + 0] < macd20[can2dle + 1] - dista))
         trend[1] = sel;
      if(macd30[can1dle + 0] < macd30[can1dle + 2] - (dista * 1.62) && (macd30[can1dle + 0] < macd31[can1dle + 1] - dista || macd31[can1dle + 0] < macd30[can1dle + 1] - dista))
         trend[0] = sel;
     }
   else
      //      if(macd40[can1dle + 0] > macd40[can1dle + 2] + (dista * 1.62) && (trend[1] != buy || trend[0] != buy))
      if(tap[0]>tap[1] && (trend[1] != buy || trend[0] != buy))
        {
         if(macd20[can2dle + 0] > macd20[can2dle + 2] + (dista * 1.62) && (macd20[can2dle + 0] > macd21[can2dle + 1] + dista || macd21[can2dle + 0] > macd20[can2dle + 1] + dista))
            trend[1] = buy;
         if(macd30[can1dle + 0] > macd30[can1dle + 2] + (dista * 1.62) && (macd30[can1dle + 0] > macd31[can1dle + 1] + dista || macd31[can1dle + 0] > macd30[can1dle + 1] + dista))
            trend[0] = buy;
        }
   return true;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void AutoA3m::accounts()
  {
   bal0 = bal;
   equ0 = equ;
   apft0 = apft;
   ask0 = ask;
   bid0 = bid;
   pnt0 = pnt;
//   pntStep = (iPointOrderStep * pnt0);
   mlvl0 = mlvl;
   marg = mlvl0 > lever * 16.18034;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool AutoA3m::IsHourTrue()
  {
   MqlDateTime structTime;
   TimeCurrent(structTime);
   if(structTime.hour >= 2 && structTime.hour <= 21)
      return true;
//---
   return false;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool AutoA3m::osd()
  {
//   pntStep = (iPointOrderStep * pnt0);
   xcm = -iStartLot * 27;
//   if(PositionsTotal() < 1 || bs < 1 || check)
//     {
//      Print("GVs updated");
//     }
   osd_to_be_called = check = false;
   ccount = 3;
   return true;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool AutoA3m::release()
  {
   IndicatorRelease(TF1_RSO_handle);
   IndicatorRelease(TF2_RSO_handle);
   IndicatorRelease(macd2_handle);
   IndicatorRelease(macd3_handle);
   IndicatorRelease(rsiMacd_handle);
   IndicatorRelease(macd4_handle);
   macd4_handle = rsiMacd_handle = TF1_RSO_handle = macd2_handle = TF2_RSO_handle = macd3_handle = INVALID_HANDLE;
   return true;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double AutoA3m::CalculateHedgeAveragePrice() //bool end = true)
  {
   dt[0] = iTime(_Symbol, PERIOD_M1, 0);
   return 0;
  }
//+------------------------------------------------------------------+
