log in     sign up for free
 
Combine your enthusiasm, energy, focus, devotion, and discipline to becoming the best trader you can be, but once you have done that, there is no point in agonizing over the details.
Jack D. Schwager
 
 
Home
systems (285)
 
 
Special offer: buy MetaStock (try for free)   |   Reuters QuoteCenter Real-Time Data (get a free month)   |   EOD Data
Formula Library
 
AmiBroker
MetaTrader
MetaStock
TradeStation
Wealth-Lab
NinjaTrader
 
add your formula
System Library
 
 
Account login
Your email:
Password:
 
Log in
New to s4t? Sign up.
If you forgot your password click here.
Search the Web
 
Custom Search

ATR stops

Formula for: MetaTrader

mql expert

 

 

Views:  3079

Added: November 16, 2008
 
Rate this code:  Rate: 1 Rate: 2 Rate: 3 Rate: 4 Rate: 5
This formula has not been rated yet
 

email this link

 
 
Tags: MetaTrader, mql expert
 


Stops based on ATR. There's a Trailing Stop, a Big Profit Stop, and Protective Stop

 

 



Code:

/*[[
Name := ATR stops
Author := Loren Gordon
Link := n/a
Lots := 1.00
Stop Loss := 0
Take Profit := 0
Trailing Stop := 0
]]*/
Defines: ATRLength(10),ProtectiveATRs(3);
Defines: TrailingATRs(4);
Defines: BigProfitATRs(7),ExitBarLen(3);

Var: i(0),sl(0);
Var: PosHigh(0),PosLow(0),PosHL(0),ATRVal(0);

//Exit if TrailingStop and TrailingATRs > 0
If TrailingStop > 0 and TrailingATRs > 0 then
{
Alert("TrailingStop and TrailingATRs cannot both be greater than 0. Set one, and set the other to 0.");
Exit;
};

//Big Profit ATR Stop and ATR Trailing Stop
For i=1 to TotalTrades
{
If Ord(i,VAL_SYMBOL) = Symbol Then
{
If Ord(i,VAL_TYPE) = OP_BUY Then
{
sl = 0;
//Big Profit ATR Stop
If BigProfitATRs > 0 Then
{
ATRVal = iATR(ATRLength,1)*BigProfitATRs; //Calculate ATR Big Profit value
If Curtime - OrderValue(i,VAL_OPENTIME) < Period*60 then PosHL = Close[1]; //Set PosHL if new order
If Close[1] > PosHL then PosHL = Close[1]; //Update PosHL for rising closed bars
If PosHL > OrderValue(i,VAL_OPENPRICE) + ATRVal then
sl = Low[Lowest(MODE_LOW,ExitBarLen,ExitBarLen)]; //If Big Profit (OpenPrice + ATR*BigProfitATRs), set S/L to Low of last ExitBarLen bars
};
//ATR Trailing Stop
If TrailingATRs > 0 and sl = 0 Then
{
ATRVal = iATR(ATRLength,1)*TrailingATRs; //Calculate ATR TrailingStop value
If Curtime - OrderValue(i,VAL_OPENTIME) < Period*60 then PosHigh = High; //Set PosHigh if new order
If High > PosHigh then PosHigh = High; //Update PosHigh if higher High reached
If OrderValue(i,VAL_STOPLOSS) < PosHigh - ATRVal Then
sl = PosHigh - ATRVal; //If current S/L is less than new S/L, set new S/L
};
//Standard Trailing Stop
If TrailingStop > 0 and sl = 0 Then
{
If Bid-Ord(i,VAL_OPENPRICE) > TrailingStop*Point and
Bid-Ord(i,VAL_STOPLOSS) > TrailingStop*Point Then
sl = Bid - TrailingStop*Point;
};
If sl > 0 then
ModifyOrder(Ord(i,VAL_TICKET),
Ord(i,VAL_OPENPRICE),
sl,
Ord(i,VAL_TAKEPROFIT),
White);
Continue;
};
If Ord(i,VAL_TYPE) = OP_SELL Then
{
sl = 0;
//BigProfit ATR Stop
If BigProfitATRs > 0 Then
{
ATRVal = iATR(ATRLength,1)*BigProfitATRs; //Calculate ATR Big Profit value
If Curtime - OrderValue(i,VAL_OPENTIME) < Period*60 then PosHL = Close[1]; //Set PosHL if new order
If Close[1] < PosHL then PosHL = Close[1]; //Update PosHL for decling closed bars
If PosHL < OrderValue(i,VAL_OPENPRICE) - ATRVal then
sl = High[Highest(MODE_HIGH,ExitBarLen,ExitBarLen)]; //If Big Profit (OpenPrice - ATR*BigProfitATRs), set S/L to High of last ExitBarLen bars
};
//ATR Trailing Stop
If TrailingATRs > 0 and sl = 0 Then
{
ATRVal = iATR(ATRLength,1)*TrailingATRs; //Calculate ATR TrailingStop value
If Curtime - OrderValue(i,VAL_OPENTIME) < Period*60 then PosLow = Low; //Set PosLow if new order
If Low < PosLow then PosLow = Low; //Update PosLow if lower Low reached
If OrderValue(i,VAL_STOPLOSS) > PosLow + ATRVal Then
sl = PosLow + ATRVal; //If current S/L is greater than new S/L, set new S/L
};
//Standard Trailing Stop
If TrailingStop > 0 and sl = 0 Then
{
If OrderValue(i,VAL_OPENPRICE)-Ask > Point*TrailingStop and
(OrderValue(i,VAL_STOPLOSS)-Ask > TrailingStop*Point or
OrderValue(i,VAL_STOPLOSS) = 0) Then
sl = Ask + TrailingStop*Point;
};
If sl > 0 then
ModifyOrder(Ord(i,VAL_TICKET),
Ord(i,VAL_OPENPRICE),
sl,
Ord(i,VAL_TAKEPROFIT),
Gold);
Continue;
};
};
};

//ATR Protective Stop Example
If ProtectiveATRs > 0 Then
{
ATRVal = iATR(ATRLength,1)*ProtectiveATRs; //Calculate ATR Protective Stop
sl = Ask - ATRVal; //Set ATR stop
}
else sl = Low[1] -1 * Point;
SetOrder(OP_BUY,
Lots,
Ask,
3,
sl,
Ask+TakeProfit*Point,
BLUE);
Exit;


If ProtectiveATRs > 0 Then
{
ATRVal = iATR(ATRLength,1)*ProtectiveATRs; //Calculate ATR Protective Stop
sl = Bid + ATRVal; //Set ATR Stop
}
else sl = High[1] + 1 * Point;
SetOrder(OP_SELL,
Lots,
Bid,
3,
sl,
Bid-TakeProfit*Point,
RED);
Exit;



 





Code to difficult? Find somebody to help you with coding here.

 



Author: Loren Gordon
Source: www.xeatrade.com

 

View similar (mql expert for MetaTrader):

ATR stops
...
 
 
all formulas for MetaTrader
all formulas

 

 

Email to friend

Top

 

     
However we try to maintain hiqhest possible level of service - most formulas, oscillators, indicators
and systems are submitted by anonymous users.
Therefore S4T™ does not take any responsibility for it's quality.
If you use any of this information, use it at your own risk. You are responsible for your own trading decisions.
Be sure to verify that any information you see on these pages is correct, and is applicable to your particular trade.
In no case will S4T™ be responsible for your trading gains or losses.
PayPalSecure payments by PayPal S4T™ is a part of TAURO EDUCATION NETWORK
 
Privacy note | (c) copyrights systems4trading.com 2006-2009