//=============================================================================
//                                                     Fluid Turquoise 3.4.mq4 
//                                                                 Derk Wehler 
//                                                            fibofx@gmail.com
//
// Taken from FB3.3 by Matt Pavlovich: fibofx@gmail.com.  
// One change made by Derk Wehler, suggested by TradeForexFx: The use of the 
// difference of two moving averages instead of the accelorator oscillator.
// Search for "TradeForexFx" to find changes.
//
// Robert Hill
//  1/11/2007
// Added automatic calculation of MagicNumber using Symbol and Timeframe of chart 
// so that this can be tested on any currency pair and timeframe
//  1/12/07
// added code to select AC or MA indicator  
// Changed code where OrdersTotal() was used instead of total
//=============================================================================



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

// Robert Added UseAC to determine which works better AC or MA
extern int       UseAC        = 1;   // 1 use AC, 0 use MA    
extern double    MinTime 		= 120;
extern double    drawdown1 		= 10;
extern double    drawdown2 		= 20;
extern double    drawdown3 		= 30;
extern double    MaxLots		= 100;
extern double    stop 			= 1;//1 - 4hr//
extern double    profit 		= 3;//3 - 4hr//
extern double    trailer 		= 0.5;
// Line introduced to have desirable profit target for each trade
extern double    PipProfit 		= 4;
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    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 //


double           Lots;
// int              MagicNumber = 333; - original line
// Modified to calculate MagicNumber in Init by Robert
extern int              MagicBase = 3000; 
int MagicNumber;

//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!

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
   MagicNumber = MagicBase + func_Symbol2Val(Symbol())*100 + func_TimeFrame_Const2Val(Period());
}
   

//=============================================================================
// Start
//=============================================================================
int start()
{

	int cnt, ticket, total, test;
	double SL, TP, TrailinStop;

	if (Lots > MaxLots) 
		Lots = MaxLots;
	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 (mini)
		Lots = (MathCeil(AccountEquity() * riskM / 100000) / 10);

	if (standard)
		Lots = (MathCeil(AccountEquity() * riskS / 100000));
   
  
     

	double TrailingStop, msd, myrsi, myao;
	TrailingStop = iATR(Symbol(), 0, 14, 1) * trailer;
	
	// ------------------------------------------------------------------------
	// Change made for TradeForexFx to use moving average instead of iAC:
	// myao = iAC(Symbol(), 0, 0);	// original line
	// Change made to select either AC or MA by Robert
	
   if (UseAC == 1)
      myao = iAC(Symbol(), 0, 0);
   else
	   myao = (iMA(NULL, 0, 5, 0, MODE_LWMA, PRICE_CLOSE, 0) - 
			iMA(NULL, 0, 8, 0, MODE_LWMA, PRICE_CLOSE, 0)) / Point;
	// ------------------------------------------------------------------------
			
	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) - Line changed
//   if (OrdersTotal() == 0 && Ask - Bid <= 3 * Point && myao > 1) -  line changed
   if (UseAC == 1) test = 0; else test = 1;
//   if (OrdersTotal() == 0 && Ask - Bid <= 3 * Point && myao > test)
   if (total == 0 && Ask - Bid <= 3 * Point && myao > test)
	{
		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)   line changed      
 //	if (OrdersTotal() == 0 && Ask - Bid <= 3 * Point && myao < -1)  line changed
   if (UseAC == 1) test = 0; else test = -1;
//	if (OrdersTotal() == 0 && Ask - Bid <= 3 * Point && myao < test)
	if (total == 0 && Ask - Bid <= 3 * Point && myao < test)
	{
		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 && 
		if (total == 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 && 
		if (total == 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 && 
		if (total == 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 && 
		if (total == 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 && 
		if (total == 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)
		if (total==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)) - original line
				if ((Bid - OrderOpenPrice() >= PipProfit * 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))- original line
				if ((OrderOpenPrice() - Ask >=  PipProfit * 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);
						}
					}
				}
			}
		}
	}
}

//+------------------------------------------------------------------+
//| Time frame interval appropriation  function                      |
//+------------------------------------------------------------------+
int func_TimeFrame_Const2Val(int Constant ) {
   switch(Constant) {
      case 1:  // M1
         return(1);
      case 5:  // M5
         return(2);
      case 15:
         return(3);
      case 30:
         return(4);
      case 60:
         return(5);
      case 240:
         return(6);
      case 1440:
         return(7);
      case 10080:
         return(8);
      case 43200:
         return(9);
   }
}

int func_Symbol2Val(string symbol) {
   string mySymbol = StringSubstr(symbol,0,6);
	if(mySymbol=="AUDCAD") return(1);
	if(mySymbol=="AUDJPY") return(2);
	if(mySymbol=="AUDNZD") return(3);
	if(mySymbol=="AUDUSD") return(4);
	if(mySymbol=="CHFJPY") return(5);
	if(mySymbol=="EURAUD") return(6);
	if(mySymbol=="EURCAD") return(7);
	if(mySymbol=="EURCHF") return(8);
	if(mySymbol=="EURGBP") return(9);
	if(mySymbol=="EURJPY") return(10);
	if(mySymbol=="EURUSD") return(11);
	if(mySymbol=="GBPCHF") return(12);
	if(mySymbol=="GBPJPY") return(13);
	if(mySymbol=="GBPUSD") return(14);
	if(mySymbol=="NZDUSD") return(15);
	if(mySymbol=="USDCAD") return(16);
	if(mySymbol=="USDCHF") return(17);
	if(mySymbol=="USDJPY") return(18);
	Comment("unexpected Symbol");
	return(19);
}

  