ATR Trailer

ritusahu

Trader
Oct 20, 2011
5
0
17
MQL4:
 

Hi my ea on ATR trailer is not placing any order. please sole the problem. find attachment.:)
 

Attachments

  • ATR EA.txt
    5.3 KB · Views: 115
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
18,535
1,355
144
Odesa
www.earnforex.com
There's no point in attaching the EA's code (especially in a text file) here.

What version do you use - MT4 or MT5?
What settings do you use?
Does the smiling face appear in the top-right corner of the chart when you attach it?
Are there any errors in the Experts or Journal tabs of your terminal?
 

ritusahu

Trader
Oct 20, 2011
5
0
17
ea atr trailer is unable to place order

  • I am using MT4(demo account). I am attaching this EA with EURUSD, M1 with
  • ATR Period=10, Multiplier = 3.75,
  • Yes the smiling face is coming on chart top right corner.
  • No I am not getting any kind of error on both tab .
  • When I am testing it using strategy tester it showing me the result, but its not trading.

Please find the attachment of ea with my previous thread.
 

ritusahu

Trader
Oct 20, 2011
5
0
17
MQL4:
//+------------------------------------------------------------------+
//|                                                  ATR-Trailer.mq4 |
//|                                                    Andriy Moraru |
//|                                         [url]http://www.earnforex.com[/url] |
//|            							                            2011 |
//+------------------------------------------------------------------+
#property copyright "www.EarnForex.com, 2011"
#property link      "http://www.earnforex.com"
 
// Plain trailing stop EA with ATR-based stop-loss.
 
#define LONG 1
#define SHORT 2
 
extern int ATR_Period = 10;
extern double ATR_Multiplier = 3.75;
extern int StartWith = 1; // 1 - Short, 2 - Long
extern int Slippage = 100; 	// Tolerated slippage in pips
extern double Lots = 0.1;
 
extern int Magic = 123123123;
 
// Global variables
bool HaveLongPosition;
bool HaveShortPosition;
double StopLevel;
int LastPosition = 0;
 
int init()
{
   StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD);
   LastPosition = 3 - StartWith;
   return(0);
}
 
//+------------------------------------------------------------------+
//| Expert Every Tick Function                                       |
//+------------------------------------------------------------------+
int start()
{
	if (IsTradeAllowed() == false) return;
 
   // Getting the ATR values
   double ATR = iATR(NULL, 0, ATR_Period, 0);
   ATR *= ATR_Multiplier;
 
   if (ATR <= (MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD)) * Point) ATR = (MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD)) * Point;
 
  	// Check what position is currently open
 	GetPositionStates();
 
 	// Adjust SL and TP of the current position
 	if ((HaveLongPosition) || (HaveShortPosition)) AdjustSLTP(ATR);
   else
	{
   	// Buy condition
   	if (LastPosition == SHORT)
   	{
  			for (int i = 0; i < 10; i++)
  			{
  			   RefreshRates();
  				// Bid and Ask are swapped to preserve the probabilities and decrease/increase profit/loss size
  		   	 double SL = Bid - ATR;//NormalizeDouble(Bid - ATR, Digits);
  		   	 if (SL < StopLevel) SL = StopLevel;
  				int result = OrderSend(Symbol(), OP_BUY, Lots, Ask, 100, SL,0, "ATR-Trader", Magic,0,Blue); 
  				Sleep(1000);
            if (result == -1)
            {
               int e = GetLastError();
               Print(e);
            }
 
  				else return(0);
  			}
   	}
   	// Sell condition
   	else if (LastPosition == LONG)
   	{
  			for (i = 0; i < 10; i++)
  			{
  			   RefreshRates();
  				// Bid and Ask are swapped to preserve the probabilities and decrease/increase profit/loss size
  		      SL = Ask + ATR;//NormalizeDouble(Ask + ATR, Digits);
  		      if (SL < StopLevel) SL = StopLevel;
  				result = OrderSend(Symbol(), OP_SELL, Lots, Bid, 100, SL, 0, "ATR-Trader", Magic,0,Red); 
  				Sleep(1000);
            if (result == -1)
            {
               e = GetLastError();
               Print(e);
            }
 
  				else return(0);
  			}
 
   	}
   	 Print(MarketInfo(Symbol(), MODE_STOPLEVEL));
	}
 
	return(0);
}
 
//+------------------------------------------------------------------+
//| Check What Position is Currently Open										|
//+------------------------------------------------------------------+
void GetPositionStates()
{
   int total = OrdersTotal();
   for (int cnt = 0; cnt < total; cnt++)
   {
      if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false) continue;
      if (OrderMagicNumber() != Magic) continue;
      if (OrderSymbol() != Symbol()) continue;
 
      if (OrderType() == OP_BUY)
      {
			HaveLongPosition = true;
			HaveShortPosition = false;
		}
      else if (OrderType() == OP_SELL)
      {
			HaveLongPosition = false;
			HaveShortPosition = true;
		}
   	if (HaveLongPosition) LastPosition = LONG;
   	else if (HaveShortPosition) LastPosition = SHORT;
   	return;
	}
 
   HaveLongPosition = false;
	HaveShortPosition = false;
}
 
//+------------------------------------------------------------------+
//| Adjust Stop-Loss and TakeProfit of the Open Position					|
//+------------------------------------------------------------------+
void AdjustSLTP(double SLparam)
{
   int total = OrdersTotal();
   for (int cnt = 0; cnt < total; cnt++)
   {
      if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false) continue;
      if (OrderMagicNumber() != Magic) continue;
      if (OrderSymbol() != Symbol()) continue;
 
      if (OrderType() == OP_BUY)
		{
		   RefreshRates();
			double SL =Bid - SLparam;
			if (SL < StopLevel) SL = StopLevel;
			if (SL > OrderStopLoss())
			{
				for (int i = 0; i < 10; i++)
				{
      		   bool result = OrderModify(OrderTicket(), OrderOpenPrice(), SL, 0, 0);
      		   if (result) return;
				}
			}
		}
      else if (OrderType() == OP_SELL)
		{ 
		   RefreshRates();
			SL = Ask + SLparam;
			 if (SL < StopLevel) SL = StopLevel;
			if (SL < OrderStopLoss())
			{
				for (i = 0; i < 10; i++)
				{
      		   result = OrderModify(OrderTicket(), OrderOpenPrice(), SL, 0, 0);
      		   if (result) return;
				}
			}
		}
	}
}
//+------------------------------------------------------------------+

this is my complete EA ...
pleas check this
 
Last edited by a moderator:

ritusahu

Trader
Oct 20, 2011
5
0
17
Changes made by me to the original EA are:

1. ATR period has changed from int to double .
2. double SL = Bid - ATR; instead of NormalizeDouble(Bid - ATR, Digits);//in Buy Condition.
3. SL = Ask + ATR; instead of NormalizeDouble(Ask + ATR, Digits);//in sell condition.
4. result = OrderSend(Symbol(), OP_SELL, Lots, Bid, 100, SL, 0, "ATR-Trader", Magic,0,Red);
instead of
result = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, SL, 0, "ATR-Trader", Magic);
5. In AdjustSLTP() function I have change
double SL = NormalizeDouble(Bid - SLparam, Digits);
to
double SL =Bid - SLparam;// for buy
SL = Ask + SLparam;// for sell
6. and lastly If condition used before OrderModify changes made is
if (SL < OrderStopLoss()) instead of if (SL < NormalizeDouble(OrderStopLoss(), Digits))

Thanks in advance.
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,535
1,355
144
Odesa
www.earnforex.com
Heh... You forgot to mention two important things:

1. Your EA gives OrderSend Error 130.
2. Very important changes of yours:

MQL4:
StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD);

and

MQL4:
if (SL < StopLevel) SL = StopLevel;

The first piece of code sets StopLevel to something like 30 (at least with my broker), because you forget to multiply it with Point.

The second piece compares the valid stop-loss of about 1.3xxx with 30. Of course 30 is greater than the valid stop-loss and SL becomes = 30.

I think you know how to fix that now :).
 
Last edited:

nix

Trader
Oct 9, 2011
5
0
12
hi guys, i new using atr trailer. cud anyone add in a TP at 60 pips in the ea, much appreciated , cheers
 

nix

Trader
Oct 9, 2011
5
0
12
hi Enivid,
where is the location to download the latest version of ATR cos i download the Atr from the main page but the partial TP is not observed, pls advice, tks
 

nix

Trader
Oct 9, 2011
5
0
12
Hi Enivid,
i did download on the instructed page tks. But as we speak the ea has made abt 70 pips but it still did not take partial profit?! I tot the intention is to take partial profit at 60 pips. Tks and pls adivce, cheers
 

nix

Trader
Oct 9, 2011
5
0
12
Hi Enivid,
tks for quick response. i saw the TP in the INPUT column but if i set it , then the position is totally out when it reach 60 pips rite?
How do i set a partial TP example, if 0.1 position, it ll take 0.05 out when it reach 60 pips profit? and the rest will be on trailing. pls advice and cheers
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,535
1,355
144
Odesa
www.earnforex.com
You've asked for a take-profit in your first post. I didn't know that you want a partial stop-loss. It would be a bit hard to implement one.
Furthermore, there's no point in a partial profit-taking. If the price reverses you don't get any profit on the second half, and if it continues in your direction, you lose profit you'd gain from the previously closed part.
 

nix

Trader
Oct 9, 2011
5
0
12
Hi Enivid,
it may be not so profitable when partial profit going my way but when the position is on the wrong end, it cud reduce the loss if the market goes my way for a partial profit of 60 ( which most of the time it goes there and then turn around) and goes to a stop loss.
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,535
1,355
144
Odesa
www.earnforex.com
Hi Enivid,
it may be not so profitable when partial profit going my way but when the position is on the wrong end, it cud reduce the loss if the market goes my way for a partial profit of 60 ( which most of the time it goes there and then turn around) and goes to a stop loss.
If most of the time it goes there and turns around, it's better to use a full take-profit.