Author: Copyright 2019, Baskakov Vladimir

This script is an automated trading system (Expert Advisor) designed to trade the correlation between two specific currency pairs: EURUSD and GBPUSD. It focuses on identifying momentum shifts between these two markets to make trading decisions.

Here is a detailed breakdown of the logic used by the script:

Core Trading Strategy

The bot operates on a "Relative Momentum" concept. Instead of looking at one currency in isolation, it constantly compares the MACD (Moving Average Convergence Divergence) indicators of EURUSD and GBPUSD.

  • Buy Signal: The bot looks for a specific crossover where the EURUSD momentum becomes significantly weaker than the GBPUSD momentum in one timeframe, while simultaneously showing a stronger trend in another. Essentially, it triggers a "Buy" when the relationship between these two pairs reaches a specific divergence point.
  • Sell Signal: Conversely, it triggers a "Sell" when the GBPUSD momentum falls below the EURUSD momentum according to its internal comparison rules.

Key Functional Components

1. Dynamic Environment Adaptation

  • Auto-Digits: The script includes a smart detection system to recognize different broker formats (e.g., 3-digit vs. 5-digit brokers). This ensures that price calculations remain accurate regardless of which platform you use.
  • Lot Normalization: Before entering a trade, the script automatically checks your broker's rules for minimum, maximum, and step sizes for trade volumes. If your requested trade size is invalid, it corrects it to the nearest allowed value.

2. Trade Management & Execution

  • Margin Safety: Before placing any order, the bot performs a "Free Margin Check." If your account balance is insufficient to open a trade, it will print a warning and skip the action to prevent unnecessary errors.
  • Slippage Handling: The script is designed to be resilient against "slippage" (price changes between the moment a signal appears and the order is filled). It features a retry mechanism that attempts to execute orders multiple times if the initial attempt fails.
  • Automatic Protection: Once a trade is opened, the bot automatically calculates and attaches:
    • Stop Loss (SL): To limit potential losses.
    • Take Profit (TP): To secure gains at specific targets.
  • Smart Closing: It includes a dedicated function to close existing positions based on specific "Magic Numbers," ensuring it doesn't accidentally interfere with other manual trades or different bots running on the same account.

3. Operational Rules

  • Single Position Limit: You can configure the bot to only allow one open trade at a time, preventing "over-trading" and helping manage risk exposure.
  • Time Tracking: The script keeps track of the specific bar times for the last buy and sell actions. This prevents it from opening multiple duplicate trades on the exact same candle.
  • Error Reporting: If a trade fails or an order cannot be modified, the bot translates complex technical error codes into human-readable text (e.g., "Not enough money," "Trade is already closed," etc.) and alerts the user.

Summary for the User

This script is a correlation-based momentum bot. It ignores general market noise and focuses strictly on the mathematical relationship between EURUSD and GBPUSD. It handles all the "heavy lifting" of professional trading—such as margin checks, broker digit corrections, slippage retries, and profit protection—allowing it to run autonomously based on its internal signal logic.

Price Data Components
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt Closes Orders by itself
Indicators Used
MACD Histogram
Miscellaneous
It issuies visual alerts to the screen
4 Views
0 Downloads
0 Favorites
EurGbp EA
ÿþ//+------------------------------------------------------------------+

//|                                                    EurGbp EA.mq4 |

//|                                Copyright 2019, Baskakov Vladimir |

//|                        https://www.mql5.com/ru/users/kalipso1979 |

//+------------------------------------------------------------------+

#property copyright "Copyright 2019, Baskakov Vladimir"

#property link      "https://www.mql5.com/ru/users/kalipso1979"

#property version   "1.4"

#property strict

// 1JQ< A45;:8

input double _Lots = 0.01;//Lots



// Stop Loss (2 ?C=:B0E)

input int _SL = 75;//Stop Loss



// Take Profit (2 ?C=:B0E)

input int _TP = 46;//Take Profit



// Magic Number (<5B:0 "A2>8E" A45;>: 4;O @>1>B0)

input int _MagicNumber = 46226;//Magic



// ><<5=B0@89 4;O A45;>:

input string _Comment = "EurGbp EA";//Comments



// >?CAB8<>5 ?@>A:0;L7K20=85

 int _Slippage = 5;



// 4=>2@5<5==> =5 1>;55 >4=>9 A45;:8

bool _OnlyOneOpenedPos = true;



// 2B>CGQB G8A;0 7=0:>2 F5=K

 bool _AutoDigits = true;



// --- 

// MACD_eur1

 int fast_ema = 12;

 int slow_ema = 26;

 int macd_sma = 9;

 int apply = 0;

// --- 

// MACD_gbp1

 int fast_ema_2 = 12;

 int slow_ema_2 = 26;

 int macd_sma_2 = 9;

 int apply_2 = 0;

// --- 

// MACD_eur

 int fast_ema_3 = 12;

 int slow_ema_3 = 26;

 int macd_sma_3 = 9;

 int apply_3 = 0;

// --- 

// MACD_gbp

 int fast_ema_4 = 12;

 int slow_ema_4 = 26;

 int macd_sma_4 = 9;

 int apply_4 = 0;



// ---

double OP_LOTS = 0.0;

// ---

// MQL4 | :;0AA 02B>>?@545;=8O G8A;0 7=0:>2 ?>A;5 70?OB>9 C B5:CI53> 8=AB@C<5=B0 | ENSED Team, http://ensed.org

class CKDig

{

   public:

      CKDig(const bool useAutoDigits)

      {

         Set(useAutoDigits);

      }

      

      ~CKDig(void)

      {

      }

      

      uint Get(void)

      {

         return m_value;

      }

      

   private:      

      uint m_value;      

      

      void Set(const bool useAutoDigits)

      {

         m_value = 1;

         if (!useAutoDigits)

         {

            return;

         }

         

         if (Digits() == 3 || Digits() == 5)

         {

            m_value = 10;

         }

      }

};



CKDig *KDig;

#define K_DIG (KDig.Get())



datetime LAST_BUY_BARTIME = 0;

datetime LAST_SELL_BARTIME = 0;



// ---

void OnInit()

{

	// ---

	get_lots_by_input();

	// ---

	KDig  = new CKDig(_AutoDigits);

}



// ---

void OnDeinit(const int reason)

{

	// ---

	// ---

	if(CheckPointer(KDig))

	{

	   delete KDig;

	}

}



// ---

void OnTick()

{

	// ---

	// 70:@KB85 A45;:8

	if(find_orders(_MagicNumber))

	{

		if(cl_buy_sig())

		{

			cbm(_MagicNumber, _Slippage, OP_BUY);

		}

		if(cl_sell_sig())

		{

			cbm(_MagicNumber, _Slippage, OP_SELL);

		}

	}

	

	// >B:@KB85 A45;:8

	// ---

	

	// ---

	if(!find_orders(_MagicNumber, (_OnlyOneOpenedPos ? -1 : OP_BUY)))

	{

		if(op_buy_sig() && LAST_BUY_BARTIME != iTime(Symbol(), Period(), 0))

		{

			LAST_BUY_BARTIME = iTime(Symbol(), Period(), 0);

			open_positions(OP_BUY, OP_LOTS);	

		}

	}

	// ---

	if(!find_orders(_MagicNumber, (_OnlyOneOpenedPos ? -1 : OP_SELL)))

	{

		if(op_sell_sig() && LAST_SELL_BARTIME != iTime(Symbol(), Period(), 0))

		{

			LAST_SELL_BARTIME = iTime(Symbol(), Period(), 0);

			open_positions(OP_SELL, OP_LOTS);	

		}

	}

}



// ---



// ---

// ---

void get_lots_by_input() 

{

//MQL4 | ?@8A2>5=85 >1JQ<0 ?> 2E>4=><C ?0@0<5B@C | ENSED Team

  OP_LOTS = _Lots;

   double MinLot = MarketInfo(Symbol(),MODE_MINLOT);

  double MaxLot = MarketInfo(Symbol(),MODE_MAXLOT);

  double LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);



  if (OP_LOTS < MinLot) OP_LOTS = MinLot;

  if (OP_LOTS > MaxLot) OP_LOTS = MaxLot;

}



// ---

// ---

bool find_orders(int magic = -1, int type = -1, int time = -1, string symb = "NULL", double price = -1, double lot = -1)

{

	// MQL4 | DC=:F8O ?>8A:0 >B:@KBKE >@45@>2 | ENSED Team, http://ensed.org

	// 2>72@0I05B 8AB8=C, 5A;8 =0945= E>BO 1K >48= >@45@ A ?>4E>4OI8<8 ?0@0<5B@0<8

	for (int i = OrdersTotal() - 1; i >= 0; i--)

	{

		if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

			break;

		if (((OrderType() == type) || (type == -1))

			&& ((OrderMagicNumber() == magic) || (magic == -1))

			&& ((OrderSymbol() == symb) || (symb == "NULL" && OrderSymbol() == Symbol()))

			&& ((OrderOpenTime() >= time) || (time == -1))

			&& ((OrderLots() == lot) || (lot == -1))

			&& ((NormalizeDouble(OrderOpenPrice(), Digits) == NormalizeDouble(price, Digits)) || (price == -1)))

		{

			return (true);

			break;

		}

	}

	return (false);

}



// ---

string Market_Err_To_Str(int errCode)

{

	// MQL4 | DC=:F8O @0AH8D@>2:8 :>4>2 >H81>: | ENSED Team, http://ensed.org

	// DC=:F8O >E20BK205B B>;L:> :>4K >H81>: B>@3>2KE >?5@0F89

	string errText;

	switch (errCode)

	{

	case 0:

		errText = "5B >H81>:";

		break;

	case 1:

		errText = "5B >H81:8, => @57C;LB0B =58725AB5=";

		break;

	case 2:

		errText = "1I0O >H81:0";

		break;

	case 3:

		errText = "5?@028;L=K5 ?0@0<5B@K";

		break;

	case 4:

		errText = "">@3>2K9 A5@25@ 70=OB";

		break;

	case 5:

		errText = "!B0@0O 25@A8O :;85=BA:>3> B5@<8=0;0";

		break;

	case 6:

		errText = "5B A2O78 A B>@3>2K< A5@25@><";

		break;

	case 7:

		errText = "54>AB0B>G=> ?@02";

		break;

	case 8:

		errText = "!;8H:>< G0ABK5 70?@>AK";

		break;

	case 9:

		errText = "54>?CAB8<0O >?5@0F8O =0@CH0NI0O DC=:F8>=8@>20=85 A5@25@0";

		break;

	case 64:

		errText = "!G5B 701;>:8@>20=";

		break;

	case 65:

		errText = "5?@028;L=K9 =><5@ AG5B0";

		break;

	case 128:

		errText = "AB5: A@>: >6840=8O A>25@H5=8O A45;:8";

		break;

	case 129:

		errText = "5?@028;L=0O F5=0";

		break;

	case 130:

		errText = "5?@028;L=K5 AB>?K";

		break;

	case 131:

		errText = "5?@028;L=K9 >1J5<";

		break;

	case 132:

		errText = " K=>: 70:@KB";

		break;

	case 133:

		errText = "">@3>2;O 70?@5I5=0";

		break;

	case 134:

		errText = "54>AB0B>G=> 45=53 4;O A>25@H5=8O >?5@0F88";

		break;

	case 135:

		errText = "&5=0 87<5=8;0AL";

		break;

	case 136:

		errText = "5B F5=";

		break;

	case 137:

		errText = "@>:5@ 70=OB";

		break;

	case 138:

		errText = ">2K5 F5=K";

		break;

	case 139:

		errText = "@45@ 701;>:8@>20= 8 C65 >1@010BK205BAO";

		break;

	case 140:

		errText = " 07@5H5=0 B>;L:> ?>:C?:0";

		break;

	case 141:

		errText = "!;8H:>< <=>3> 70?@>A>2";

		break;

	case 145:

		errText = ">48D8:0F8O 70?@5I5=0, B0: :0: >@45@ A;8H:>< 1;87>: : @K=:C";

		break;

	case 146:

		errText = ">4A8AB5<0 B>@3>2;8 70=OB0";

		break;

	case 147:

		errText = "A?>;L7>20=85 40BK 8AB5G5=8O >@45@0 70?@5I5=> 1@>:5@><";

		break;

	case 148:

		errText = ">;8G5AB2> >B:@KBKE 8 >B;>65==KE >@45@>2 4>AB83;> ?@545;0, CAB0=>2;5==>3> 1@>:5@><.";

		break;

	case 4000:

		errText = "5B >H81:8";

		break;

	case 4001:

		errText = "5?@028;L=K9 C:070B5;L DC=:F88";

		break;

	case 4002:

		errText = "=45:A <0AA820 - 2=5 480?07>=0";

		break;

	case 4003:

		errText = "5B ?0<OB8 4;O AB5:0 DC=:F89";

		break;

	case 4004:

		errText = "5@5?>;=5=85 AB5:0 ?>A;5 @5:C@A82=>3> 2K7>20";

		break;

	case 4005:

		errText = "0 AB5:5 =5B ?0<OB8 4;O ?5@540G8 ?0@0<5B@>2";

		break;

	case 4006:

		errText = "5B ?0<OB8 4;O AB@>:>2>3> ?0@0<5B@0";

		break;

	case 4007:

		errText = "5B ?0<OB8 4;O 2@5<5==>9 AB@>:8";

		break;

	case 4008:

		errText = "58=8F80;878@>20==0O AB@>:0";

		break;

	case 4009:

		errText = "58=8F80;878@>20==0O AB@>:0 2 <0AA825";

		break;

	case 4010:

		errText = "5B ?0<OB8 4;O AB@>:>2>3> <0AA820";

		break;

	case 4011:

		errText = "!;8H:>< 4;8==0O AB@>:0";

		break;

	case 4012:

		errText = "AB0B>: >B 45;5=8O =0 =>;L";

		break;

	case 4013:

		errText = "5;5=85 =0 =>;L";

		break;

	case 4014:

		errText = "58725AB=0O :><0=40";

		break;

	case 4015:

		errText = "5?@028;L=K9 ?5@5E>4";

		break;

	case 4016:

		errText = "58=8F80;878@>20==K9 <0AA82";

		break;

	case 4017:

		errText = "K7>2K DLL =5 @07@5H5=K";

		break;

	case 4018:

		errText = "52>7<>6=> 703@C78BL 181;8>B5:C";

		break;

	case 4019:

		errText = "52>7<>6=> 2K720BL DC=:F8N";

		break;

	case 4020:

		errText = "K7>2K 2=5H=8E 181;8>B5G=KE DC=:F89 =5 @07@5H5=K";

		break;

	case 4021:

		errText = "54>AB0B>G=> ?0<OB8 4;O AB@>:8, 2>72@0I05<>9 87 DC=:F88";

		break;

	case 4022:

		errText = "!8AB5<0 70=OB0";

		break;

	case 4050:

		errText = "5?@028;L=>5 :>;8G5AB2> ?0@0<5B@>2 DC=:F88";

		break;

	case 4051:

		errText = "54>?CAB8<>5 7=0G5=85 ?0@0<5B@0 DC=:F88";

		break;

	case 4052:

		errText = "=CB@5==OO >H81:0 AB@>:>2>9 DC=:F88";

		break;

	case 4053:

		errText = "H81:0 <0AA820";

		break;

	case 4054:

		errText = "5?@028;L=>5 8A?>;L7>20=85 <0AA820-B09<A5@88";

		break;

	case 4055:

		errText = "H81:0 ?>;L7>20B5;LA:>3> 8=48:0B>@0";

		break;

	case 4056:

		errText = "0AA82K =5A>2<5AB8<K";

		break;

	case 4057:

		errText = "H81:0 >1@01>B:8 3;>10;L=K5E ?5@5<5==KE";

		break;

	case 4058:

		errText = ";>10;L=0O ?5@5<5==0O =5 >1=0@C65=0";

		break;

	case 4059:

		errText = "$C=:F8O =5 @07@5H5=0 2 B5AB>2>< @568<5";

		break;

	case 4060:

		errText = "$C=:F8O =5 @07@5H5=0";

		break;

	case 4061:

		errText = "H81:0 >B?@02:8 ?>GBK";

		break;

	case 4062:

		errText = "68405BAO ?0@0<5B@ B8?0 string";

		break;

	case 4063:

		errText = "68405BAO ?0@0<5B@ B8?0 integer";

		break;

	case 4064:

		errText = "68405BAO ?0@0<5B@ B8?0 double";

		break;

	case 4065:

		errText = " :0G5AB25 ?0@0<5B@0 >68405BAO <0AA82";

		break;

	case 4066:

		errText = "0?@>H5==K5 8AB>@8G5A:85 40==K5 2 A>AB>O=88 >1=>2;5=8O";

		break;

	case 4067:

		errText = "H81:0 ?@8 2K?>;=5=88 B>@3>2>9 >?5@0F88";

		break;

	case 4099:

		errText = ">=5F D09;0";

		break;

	case 4100:

		errText = "H81:0 ?@8 @01>B5 A D09;><";

		break;

	case 4101:

		errText = "5?@028;L=>5 8<O D09;0";

		break;

	case 4102:

		errText = "!;8H:>< <=>3> >B:@KBKE D09;>2";

		break;

	case 4103:

		errText = "52>7<>6=> >B:@KBL D09;";

		break;

	case 4104:

		errText = "5A>2<5AB8<K9 @568< 4>ABC?0 : D09;C";

		break;

	case 4105:

		errText = "8 >48= >@45@ =5 2K1@0=";

		break;

	case 4106:

		errText = "58725AB=K9 A8<2>;";

		break;

	case 4107:

		errText = "5?@028;L=K9 ?0@0<5B@ F5=K 4;O B>@3>2>9 DC=:F88";

		break;

	case 4108:

		errText = "525@=K9 =><5@ B8:5B0";

		break;

	case 4109:

		errText = "">@3>2;O =5 @07@5H5=0. 5>1E>48<> 2:;NG8BL >?F8N  07@5H8BL A>25B=8:C B>@3>20BL 2 A2>9AB20E M:A?5@B0.";

		break;

	case 4110:

		errText = ";8==K5 ?>78F88 =5 @07@5H5=K. 5>1E>48<> ?@>25@8BL A2>9AB20 M:A?5@B0.";

		break;

	case 4111:

		errText = ">@>B:85 ?>78F88 =5 @07@5H5=K. 5>1E>48<> ?@>25@8BL A2>9AB20 M:A?5@B0.";

		break;

	case 4200:

		errText = "1J5:B C65 ACI5AB2C5B";

		break;

	case 4201:

		errText = "0?@>H5=> =58725AB=>5 A2>9AB2> >1J5:B0";

		break;

	case 4202:

		errText = "1J5:B =5 ACI5AB2C5B";

		break;

	case 4203:

		errText = "58725AB=K9 B8? >1J5:B0";

		break;

	case 4204:

		errText = "5B 8<5=8 >1J5:B0";

		break;

	case 4205:

		errText = "H81:0 :>>@48=0B >1J5:B0";

		break;

	case 4206:

		errText = "5 =0945=> C:070==>5 ?>4>:=>";

		break;

	default:

		errText = "H81:0 ?@8 @01>B5 A >1J5:B><";

	}

	// ---

	return (errText);

}



// ---

void open_positions(int signal, double lot, double price = 0.0, string symb = "NONE", int mode = 0)

{

	// MQL4 | DC=:F8O >B:@KB8O >@45@>2 | ENSED Team, http://ensed.org

	RefreshRates();

	// ---

	int symbDigits = 0;

	string _symb = symb;

	// ---

	if (_symb == "NONE")

	{

		symbDigits = Digits;

		_symb = Symbol();

	}

	else

		symbDigits = int(MarketInfo(_symb, MODE_DIGITS));

	// ---

	//---

	    if((AccountFreeMarginCheck(_symb,OP_BUY,lot)<=0) || (GetLastError()==134))

      {

         Print(_symb," ",lot," Not enough money. ");

         return;

      }

	if (signal == OP_BUY)

		price = NormalizeDouble(MarketInfo(_symb, MODE_ASK), symbDigits); // F5=0 >B:@KB8O 4;O ?>:C?>:

		 if((AccountFreeMarginCheck(_symb,OP_SELL,lot)<=0) || (GetLastError()==134))

      {

         Print(_symb," ",lot," Not enough money. ");

         return;

      }

	if (signal == OP_SELL)

		price = NormalizeDouble(MarketInfo(_symb, MODE_BID), symbDigits); // F5=0 >B:@KB8O 4;O ?@>406

	// ---

	int err = 0;

	for (int i = 0; i <= 5; i++)

	{

	   RefreshRates();

	   // ---

		int ticket = OrderSend(_symb, // A8<2>;

			signal, // B8? >@45@0

			lot, // >1J5<

			NormalizeDouble(price, symbDigits), // F5=0 >B:@KB8O

			_Slippage * KDig.Get(), // C@>25=L 4>?CAB8<>3> @5:2>B0

			0, // Stop Loss

			0, // Take Profit

			_Comment, // :><<5=B0@89 >@45@0

			_MagicNumber, // <038G5A:>5 G8A;>

			0, // A@>: 8AB5G5=8O (8A?>;L7C5BAO 2 >B;>65==KE >@45@0E)

			CLR_NONE); // F25B >B>1@0605<>9 AB@5;:8 =0 3@0D8:5 (CLR_NONE - AB@5;:0 =5 @8AC5BAO)

		// ---

		if (ticket != -1)

		{

			err = 0;

			// ---

			if (!IsTesting())

				Sleep(1000);

			// ---

			RefreshRates();

			// ---

			if(_SL != 0 || _TP != 0)

			{

				for (int tryModify = 0; tryModify <= 5; tryModify++)

				{

					RefreshRates();

					// ---

					if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

					{

						double sl = NormalizeDouble(get_sl(_SL * KDig.Get(), signal, price, _symb), symbDigits);

						double tp = NormalizeDouble(get_tp(_TP * KDig.Get(), signal, price, _symb), symbDigits);

						// ---

						if (sl != 0 || tp != 0)

   						if (OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp, OrderExpiration()))

   							break;

						// ---

						err = GetLastError(); // ?>;CG05< :>4 >H81:8 <>48D8:0F88

					}

					// ---

					if (!IsTesting())

						Sleep(tryModify*1000);

				}

				// ---

				if (err != 0)

					Alert("H81:0 2KAB02;5=8O SL/TP: " + Market_Err_To_Str(err));

			}

			// ---

			break;

		}

		else

		{

			err = GetLastError(); // ?>;CG05< :>4 >H81:8 >B:@KB8O

			// ---

			if (err == 0)

				break;

			// ---

			i++;

			// ---

			if (!IsTesting())

				Sleep(i*500); // 2 A;CG05 >H81:8 45;05< ?0C7C ?5@54 =>2>9 ?>?KB:>9

		}

	}

	// ---

	if (err != 0)

	{

		if(signal == OP_BUY)

			LAST_BUY_BARTIME = 0;

		if(signal == OP_SELL)

			LAST_SELL_BARTIME = 0;

		Alert("H81:0 >B:@KB8O: "  + Market_Err_To_Str(err)); // 5A;8 5ABL >H81:0 - 2K2>48< A>>1I5=85

	}

}



// ---

double get_tp(int tp_value, int type, double price = 0.0, string symb = "NONE")

{

	// MQL4 | DC=:F8O @0AG5B0 25;8G8=K Take Profit 4;O >@45@>2 | ENSED Team, http://ensed.org

	double _price = price;

	string _symb = symb;

	// ---

	if (_symb == "NONE")

		_symb = Symbol();

	int symbDigits = int(MarketInfo(_symb, MODE_DIGITS));

	// ---

	if (_price == 0)

	{

		if (type == OP_BUY)

			_price = NormalizeDouble(MarketInfo(_symb, MODE_ASK), symbDigits);

		// ---

		if (type == OP_SELL)

			_price = NormalizeDouble(MarketInfo(_symb, MODE_BID), symbDigits);

	}

	// ---

	if (tp_value > 0)

	{

		if (type == OP_BUY || type == OP_BUYLIMIT || type == OP_BUYSTOP)

			return NormalizeDouble(_price + tp_value * MarketInfo(_symb, MODE_POINT), symbDigits);

		// ---

		if (type == OP_SELL || type == OP_SELLLIMIT || type == OP_SELLSTOP)

			return NormalizeDouble(_price - tp_value *  MarketInfo(_symb, MODE_POINT), symbDigits);

	}

	// ---

	return 0.0;

}



// ---

double get_sl(int sl_value, int type, double price = 0.0, string _symb = "NONE")

{

	// MQL4 | DC=:F8O @0AG5B0 25;8G8=K Stop Loss 4;O >@45@>2 ?> D8:A8@>20==><C 7=0G5=8N SL | ENSED Team, http://ensed.org

	if (_symb == "NONE")

		_symb = Symbol();

	int symbDigits = int(MarketInfo(_symb, MODE_DIGITS));

	double symbPoint = MarketInfo(_symb, MODE_POINT);

	// ---

	if (price == 0.0)

	{

		if (type == OP_BUY)

			price = NormalizeDouble(MarketInfo(_symb, MODE_ASK), symbDigits);

		if (type == OP_SELL)

			price = NormalizeDouble(MarketInfo(_symb, MODE_BID), symbDigits);

	}

	// ---

	if (sl_value > 0)

	{

		if (type == OP_BUY || type == OP_BUYLIMIT || type == OP_BUYSTOP)

			return NormalizeDouble(price - sl_value * symbPoint, symbDigits);

		if (type == OP_SELL || type == OP_SELLLIMIT || type == OP_SELLSTOP)

			return NormalizeDouble(price + sl_value * symbPoint, symbDigits);

	}

	// ---

	return 0.0;

}



// ---

bool close_by_ticket(const int ticket, const int slippage)

{

	/*

	 MQL4 | DC=:F8O 70:@KB8O A45;:8 ?> 5Q =><5@C (B8:5BC) | ENSED Team

	 @8 70:@KB88 @K=>G=>3> >@45@0 CG8BK205BAO C@>25=L <0:A8<0;L=> 4>?CAB8<>3> ?@>A:0;L7K20=8O (slipage)

	 */

	if (!OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)) // 2K18@05< >@45@ ?> B8:5BC

	{

	   return false;

	}

	

	int err = 0;

	

	for (int i = 0; i < 5; i++)

	{

	   ResetLastError();

	   

		RefreshRates();

		

		double price = 0.0;

		

		if (OrderType() == OP_BUY)

		{

			price = NormalizeDouble(SymbolInfoDouble(OrderSymbol(), SYMBOL_BID), (int)SymbolInfoInteger(OrderSymbol(), SYMBOL_DIGITS));

		}

		if (OrderType() == OP_SELL)

		{

			price = NormalizeDouble(SymbolInfoDouble(OrderSymbol(), SYMBOL_ASK), (int)SymbolInfoInteger(OrderSymbol(), SYMBOL_DIGITS));

		}

				   

	   // 5A;8 @K=>G=K9 >@45@ - 70:@K205< 53>, 5A;8 >B;>65==K9 - C40;O5<

	   bool result = false;

	   

		if (OrderType() <= OP_SELL) 

		{

			result = OrderClose(OrderTicket(), OrderLots(), price, slippage * KDig.Get(), clrNONE);

	   }

		else

		{

			result = OrderDelete(OrderTicket());

		}

		

		if (result) // 5A;8 70:@KB85 8;8 C40;5=85 ?@>H;> CA?5H=> - 2>72@0I05< true 8 2KE>48< 87 F8:;0

		{

			return (true);

		}

		

		err = GetLastError();

		

		if (err != 0)

		{

			Print("Error of close_by_ticket() #" + (string)err + ": " + Market_Err_To_Str(err)); // 5A;8 5ABL >H81:0 - 40Q< @0AH8D@>2:C 5Q :>40 2 6C@=0;

		}

		

		Sleep(300 * i);

	}

	return (false);

}



// ---

bool cbm(int magic, int slippage, int type)

{

	/*

	 close by magic (70:@KB85 2A5E >@45@>2 40==>3> B8?0 A 40==K< MagicNumber)

	 #G8BK205BAO <0:A8<0;L=> 4>?CAB8<>5 ?@>A:0;L7K20=85 (slipage)

	 A?>;L7C5BAO DC=:F8O close_by_ticket.

	 */

	int n = 0;

	RefreshRates();

	for (int i = OrdersTotal() - 1; i >= 0; i--)

	{

		if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

			break;

		if ((OrderType() == type) && (OrderMagicNumber() == magic) && (Symbol() == OrderSymbol()))

		{

			close_by_ticket(OrderTicket(), slippage); // 70:@K205< A45;:C

			n++; // =0@0I8205< AGQBG8: 70:@KBKE A45;>: (=0 A0<>< 45;5 - ?>?KB>: 70:@KB8O)

		}

	}

	if (n > 0) // 5A;8 ?>?KB>: 70:@KB8O 1K;> 1>;LH5 0, B> DC=:F8O 2>72@0I05B true

			return (true);

	return (false);

}



double e_MACD_eur1()

{

	return iMACD("EURUSD", PERIOD_H1, fast_ema, slow_ema, macd_sma, apply, 0, 2);

}

double e_MACD_gbp1()

{

	return iMACD("GBPUSD", PERIOD_H1, fast_ema_2, slow_ema_2, macd_sma_2, apply_2, 0, 2);

}

double e_MACD_eur()

{

	return iMACD("EURUSD", PERIOD_H1, fast_ema_3, slow_ema_3, macd_sma_3, apply_3, 0, 1);

}

double e_MACD_gbp()

{

	return iMACD("GBPUSD", PERIOD_H1, fast_ema_4, slow_ema_4, macd_sma_4, apply_4, 0, 1);

}

// ---

bool op_buy_sig()

{

	if((e_MACD_eur1() < e_MACD_gbp1()) && (e_MACD_eur() > e_MACD_gbp()))

		return true;

	// ---

	return false;

}

// ---

bool op_sell_sig()

{

	if((e_MACD_gbp1() < e_MACD_eur1()) && (e_MACD_gbp() > e_MACD_eur()))

		return true;

	// ---

	return false;

}

// ---

bool cl_buy_sig()

{

	return false;

}

// ---

bool cl_sell_sig()

{

	return false;

}



Comments