//+------------------------------------------------------------------+
//| 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;
}
}
}
}
}
//+------------------------------------------------------------------+