//+------------------------------------------------------------------+
//|                                                  Set Zero SL.mq4 |
//|                                      Copyright © 2006, Eli Hayun |
//|                                          http://www.elihayun.com |
//+------------------------------------------------------------------+
//| 2011-04-15, Xaphod - Added BE+1 with auto digit size setting
//+------------------------------------------------------------------+
//| 2011-05-02, gspe - code cleaning
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Eli Hayun"
#property link      "http://www.elihayun.com"

#define SCRIPT_NAME "Set BE+1" 

double BEPlus = 1.0;	// BreakEven Pip Plus
double nPoint, nDigits;


//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
{
	bool   result;
	int    error;
	
	GetPairDigits();
      
	for (int i = OrdersTotal()-1; i >= 0; i--)
	{
		OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
		
		if ((OrderSymbol() == Symbol()) && (OrderType() == OP_BUY) || (OrderType() == OP_SELL))
		{
			double sl = 0;
			if((OrderType() == OP_BUY) && (OrderStopLoss() <= OrderOpenPrice() || OrderStopLoss() == 0))
			{
				sl = NormalizeDouble(OrderOpenPrice() + (BEPlus*nPoint), Digits);
			}
			if((OrderType() == OP_SELL) && (OrderStopLoss() >= OrderOpenPrice() || OrderStopLoss() == 0))
			{
				sl = NormalizeDouble(OrderOpenPrice() - (BEPlus*nPoint), Digits);
			}
			
			//Comment("BreakEven value is: ", sl);
			//Print("BreakEven value is: ", sl);
			if(sl != 0)
			{
				result=OrderModify(OrderTicket(), 0, sl, OrderTakeProfit(), 0, CLR_NONE);
				if(result != TRUE) {error = GetLastError(); Print("LastError = ", error);}
			}
		}
	}   
	//----
	return(0);
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// GetPairDigits                                                     |
//+------------------------------------------------------------------+
void GetPairDigits()
{
	nPoint = MarketInfo(Symbol(), MODE_POINT);		//get Pair Points
	nDigits = MarketInfo(Symbol(), MODE_DIGITS);	//get Pair Digits
	
	if(nDigits == 2 || nDigits == 4)	{nPoint = nPoint;		nDigits = 0;}
	if(nDigits == 3 || nDigits == 5)	{nPoint = nPoint*10;	nDigits = 1;}
	if(nDigits == 6)					{nPoint = nPoint*100;	nDigits = 2;} 
	if(nDigits == 7)					{nPoint = nPoint*1000;	nDigits = 3;}

	return(0);
} // End void GetPairDigits()


