| |
 |
Views:
3079 |
| Added: November 16, 2008 |
| |
| |
|
| 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
all formulas for MetaTrader
all formulas
|