This expert advisor will scan through new trades and automatically apply a default stop loss to trades that have no stop loss

Click here for the MT4 version

Start by using the wizard to create the expert advisor template. I called my expert AutoStopLoss.

For MT5 include the Trade.mqh file to make use of the CTrade class and create an object of type CTrade.

#include <Trade/Trade.mqh>
CTrade	Trade;

This expert uses a simple point value to set the default stop loss. This needs a single input line

input	int	InpStopLossPoints		=	500;	//	Stop loss in points

No code is required in the OnInit or OnDeinit functions, all the work is done in the OnTick function. Begin by declaring a variable to set a time to look back for new trades. Trades placed earlier than the calculated time will not have a stop loss applied

datetime	checkTime	=	TimeCurrent()-30;							//	Only looking at trades in last 30 seconds

Then a standard loop to scan through trades

	int		cnt			=	PositionsTotal();							//	This won't see limit and stop orders
	for (int i=cnt-1; i>=0; i--) {
		ulong ticket	=	PositionGetTicket(i);
		if (ticket>0) {
			if (PositionGetInteger(POSITION_MAGIC)==0 && PositionGetDouble(POSITION_SL)==0) {
								//	magic 0 = manual entry, sl 0 means not set
				if (PositionGetInteger(POSITION_TIME)>checkTime) {					//	lets you override after 30 seconds
					string	symbol			=	PositionGetString(POSITION_SYMBOL);
					double	stopLoss			=	InpStopLossPoints*SymbolInfoDouble(symbol, SYMBOL_POINT);
					double	stopLossPrice	=	(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) ?
														PositionGetDouble(POSITION_PRICE_OPEN)-stopLoss :
														PositionGetDouble(POSITION_PRICE_OPEN)+stopLoss;
					stopLossPrice				=	NormalizeDouble(stopLossPrice, (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS));
					Trade.PositionModify(ticket, stopLossPrice, PositionGetDouble(POSITION_TP));
				}
			}
		}
	}

Line 1 just captures the total number of positions for the loop

Line 2 is the start of the loop counting down from cnt-1 to zero.

Line 3 uses the PositionGetTicket function to both load the position information into memory and return the ticket number. If this fails the ticket number returned will be zero and line 4 then checks that a valid ticket number was returned.

Line 5 checks that the selected order has magic number zero using PositionGetInteger and passing in POSITION_MAGIC which means it was placed manually through the terminal and has no stop loss already applied.

Line 7 then uses the earlier check time to compare open time and avoid any trades placed before the time interval. Without this the expert would repeatedly apply a stop loss if ti is removed. Using the timeout allows you to cancel the stop loss after waiting the selected numebr of seconds and the trade will then be ignored by the expert.

Line 8 loads the symbol for the position into a variable. I use this value more than once and prefer to only call the function one time.

Line 9 calculates the size of the stop loss by converting the points to a price value based on the symbol of the current order

Lines 10-13 then calculate the stop loss price, depending on buy or sell, and ensure that it is a valid normalized price

Finally line 14 applies the updated stop loss price using the CTrade class, by the Trade variable created at the top of the code.

 

 

Leave a Reply