//+------------------------------------------------------------------+
//|                                               Fluid Blue 3.3.mq4 |
//|                                                   Matt Pavlovich |
//|                                                  fibofx@gmail.com|
//+------------------------------------------------------------------+



#property copyright "Matt Pavlovich"
#property link      "fibofx@gmail.com"

extern double    MinTime = 120;
extern double    drawdown1 = 10;
extern double    drawdown2 = 20;
extern double    drawdown3 = 30;
double    riskm = 75;//micro=75, mini=7.5, standard=.75//
double    riskM = 7.5;//micro=75, mini=7.5, standard=.75//
double    riskS = 0.75;//micro=75, mini=7.5, standard=.75//
extern double    MaxLots = 100;
extern double    stop = 1;//1 - 4hr//
extern double    profit = 3;//3 - 4hr//
extern double    trailer = 0.5;
extern bool      DoubleDown = true;
extern bool      TrippleDown = true;
extern bool      QuadDown = true;
extern bool      Reverse = false;
extern bool      micro = true;
extern bool      mini = false;
extern bool      standard = false;
double           Lots;
int              MagicNumber=333;

//This is a reverse Martingale system.  It trades with the trend, doubling down as the position goes against you.  It is an "Always in Play"
//system as well, so be ready to place a lot of trades!//

//PLACE ON EURUSD 4HR CHART.  YOU MUST HAVE AT LEAST $500 TO START WITH A MICRO ACCOUNT, $5000 FOR A MINI, AND $50,000 FOR A STANDARD ACCOUNT.
//THIS SYSTEM HAS NOT YET BEEN TESTED.  AS SUCH, DEMO TEST BEFORE GOING LIVE.  AS ALWAYS, SPECULATING IN FOREX AND ANY OTHER MARKETS IS RISKY.
//YOU COULD LOOSE EVERY CENT YOU HAVE.  BE SMART!  ALSO, USE ONLY WITH A BROKER WITH A 2 PIP SPREAD ON THE EURUSD, AS WELL AS A VOLATILE FEED.
//THE LATTER DOES NOT INCLUDE THE LIKES OF INTERBANK FX, ALPARI, ETC.  THERE IS NOTHING WRONG WITH THESE BROKERS, BUT THEIR FEEDS TEND TO BE LESS
//VOLATILE THAN THEIR COMPETITORS.  I PERSONALLY RECOMMEND VELOCITY4X.  I AM NOT AN IB.  HAPPY TRADING!//


//+---------+
//| Start   |
//+---------+
int start()
  {

   int cnt, ticket, total;
   double SL,TP,TrailinStop;
   
  
   if (Lots>=100)
   Alert("Take your profits and run!!  Your broker will not allow more than 100 lots!  You cannot double down from here!  Stop trading!");
   
   if (micro)
   Lots=(MathCeil(AccountEquity() * riskm / 100000)/100);
     if (Lots>MaxLots) Lots=MaxLots;
   
   if (mini)
   Lots=(MathCeil(AccountEquity() * riskM / 100000)/10);
     if (Lots>MaxLots) Lots=MaxLots;
   
   if (standard)
   Lots=(MathCeil(AccountEquity() * riskS / 100000));
     if (Lots>MaxLots) Lots=MaxLots;
  
     

   double TrailingStop,msd,myrsi,myao;
   TrailingStop = iATR(Symbol(),0,14,1)*trailer;
   myao = iAC(Symbol(),0,0);
   if (!IsTesting()) Comment(" TrailingStop=",DoubleToStr(TrailingStop,4));
   total = 0; 
   for(cnt=OrdersTotal()-1; cnt>=0; cnt--)
     {
      OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
      if ( OrderSymbol()      != Symbol())     continue;
      if ( OrderMagicNumber() != MagicNumber)  continue;
      if(OrderType() == OP_BUY )  total++;
      if(OrderType() == OP_SELL ) total++;
     }
   
 
     {
      if(OrdersTotal()==0 && Ask-Bid<=3*Point && myao>0)
        {
         SL=Ask-iATR(Symbol(),0,14,0)*stop;
         TP=Ask+iATR(Symbol(),0,14,0)*profit;
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL,TP,"",MagicNumber,0,Blue);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
              {
               Print("BUY order opened : ",OrderOpenPrice());
              }
           }
         else 
           {
            Print("Error opening BUY order : ",GetLastError()); 
           }
         }
           
      if(OrdersTotal()==0 && Ask-Bid<=3*Point && myao<0)
        {
         SL=Bid+iATR(Symbol(),0,14,0)*stop;
         TP=Bid-iATR(Symbol(),0,14,0)*profit;
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL,TP,"",MagicNumber,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
              {
               Print("SELL order opened : ",OrderOpenPrice());
              }
           }
         else 
           {
            Print("Error opening SELL order : ",GetLastError());  
           }
         }
      if (DoubleDown)
      if(OrdersTotal()==1 && Ask-Bid<=3*Point && OrderOpenPrice()-Ask>=drawdown1*Point && myao>0)
        {
         SL=Ask-iATR(Symbol(),0,14,0)*stop;
         TP=Ask+iATR(Symbol(),0,14,0)*profit;
         ticket=OrderSend(Symbol(),OP_BUY,Lots*2,Ask,1,SL,TP,"",MagicNumber,0,Blue);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
              {
               Print("BUY order opened : ",OrderOpenPrice());
              }
           }
         else 
           {
            Print("Error opening BUY order : ",GetLastError()); 
           }
         }
         
      if (DoubleDown)   
      if(OrdersTotal()==1 && Ask-Bid<=3*Point && Bid-OrderOpenPrice()>=drawdown1*Point && myao<0)
        {
         SL=Bid+iATR(Symbol(),0,14,0)*stop;
         TP=Bid-iATR(Symbol(),0,14,0)*profit;
         ticket=OrderSend(Symbol(),OP_SELL,Lots*2,Bid,1,SL,TP,"",MagicNumber,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
              {
               Print("SELL order opened : ",OrderOpenPrice());
              }
           }
         else 
           {
            Print("Error opening SELL order : ",GetLastError());  
           }
         }
      if (TrippleDown)
      if(OrdersTotal()==2 && Ask-Bid<=3*Point && OrderOpenPrice()-Ask>=drawdown2*Point && myao>0)
        {
         SL=Ask-iATR(Symbol(),0,14,0)*stop;
         TP=Ask+iATR(Symbol(),0,14,0)*profit;
         ticket=OrderSend(Symbol(),OP_BUY,Lots*4,Ask,1,SL,TP,"",MagicNumber,0,Blue);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
              {
               Print("BUY order opened : ",OrderOpenPrice());
              }
           }
         else 
           {
            Print("Error opening BUY order : ",GetLastError()); 
           }
         }
         
      if (TrippleDown)   
      if(OrdersTotal()==2 && Ask-Bid<=3*Point && Bid-OrderOpenPrice()>=drawdown2*Point && myao<0)
        {
         SL=Bid+iATR(Symbol(),0,14,0)*stop;
         TP=Bid-iATR(Symbol(),0,14,0)*profit;
         ticket=OrderSend(Symbol(),OP_SELL,Lots*4,Bid,1,SL,TP,"",MagicNumber,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
              {
               Print("SELL order opened : ",OrderOpenPrice());
              }
           }
         else 
           {
            Print("Error opening SELL order : ",GetLastError());  
           }
         }
      if (QuadDown)
      if(OrdersTotal()==3 && Ask-Bid<=3*Point && OrderOpenPrice()-Ask>=drawdown3*Point && myao>0)
        {
         SL=Ask-iATR(Symbol(),0,14,0)*stop;
         TP=Ask+iATR(Symbol(),0,14,0)*profit;
         ticket=OrderSend(Symbol(),OP_BUY,Lots*8,Ask,1,SL,TP,"",MagicNumber,0,Blue);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
              {
               Print("BUY order opened : ",OrderOpenPrice());
              }
           }
         else 
           {
            Print("Error opening BUY order : ",GetLastError()); 
           }
         }
         
      if (QuadDown)   
      if(OrdersTotal()==3 && Ask-Bid<=3*Point && Bid-OrderOpenPrice()>=drawdown3*Point && myao<0)
        {
         SL=Bid+iATR(Symbol(),0,14,0)*stop;
         TP=Bid-iATR(Symbol(),0,14,0)*profit;
         ticket=OrderSend(Symbol(),OP_SELL,Lots*8,Bid,1,SL,TP,"",MagicNumber,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
              {
               Print("SELL order opened : ",OrderOpenPrice());
              }
           }
         else 
           {
            Print("Error opening SELL order : ",GetLastError());  
           }
         }
     }
       
 for(cnt=total-1; cnt>=0; cnt--)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         TrailingStop = iATR(Symbol(),0,14,1)*trailer;
         if (TrailingStop<=0.0005) TrailingStop=0.0006;
         if(OrderType()==OP_BUY)   // long position is opened //
           {
            if ((Bid - OrderOpenPrice() >=4*Point && CurTime()-OrderOpenTime()>=MinTime))
                {
                 RefreshRates();
                 OrderClose(OrderTicket(),OrderLots(),Bid,1,Violet); // close position
                 return(0); // exit
               }
            if (Reverse)
            if (myao<0)
                {
                 RefreshRates();
                 OrderClose(OrderTicket(),OrderLots(),Bid,1,Violet); // close position
                 return(0); // exit
                }
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-TrailingStop)
                    {
                      RefreshRates();
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop,OrderTakeProfit(),0,Blue);
                    }
                 }
              }
           }

         if(OrderType()==OP_SELL)   // short position is opened //
           {
            if ((OrderOpenPrice()- Ask >=4*Point && CurTime()-OrderOpenTime()>=MinTime))
                {
                 RefreshRates();
                 OrderClose(OrderTicket(),OrderLots(),Ask,1,Violet); // close position
                 return(0); // exit
                }
            if (Reverse)
            if (myao>0)
                {
                 RefreshRates();
                 OrderClose(OrderTicket(),OrderLots(),Ask,1,Violet); // close position
                 return(0); // exit
                }
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+TrailingStop)) || (OrderStopLoss()==0))
                    {
                      RefreshRates();
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop,OrderTakeProfit(),0,Red);
                    }
                 }
              }
           }
        }
        
        
     }
  }
  