Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

votes: 16
2012.10.10 13:00
2016.11.22 07:32

The Expert Advisor moves Stop Loss of the open position along the border of the channel built using ATR_Trailing. Stop Loss is modified only when the bar is changed based on the newly closed bar's data in case Stop Loss will be eventually pinned to the current price.

The Expert Advisor provides a possibility to display the indicator used in it on the current chart directly from the Expert Advisor. There is no need to install the indicator on the chart manually. This is quite convenient, as the indicator is displayed on the chart automatically after the Expert Advisor is installed on the chart. This indicator always has input parameters identical to the Expert Advisor's ones.

Below is the code allowing users to implement such a feature:

//| Inclusion of CChart class in expert                              |
#include <Charts\Chart.mqh>
//---- declaration of a global variable as CChart type
CChart cchart;
//| Expert initialization function                                   |
int OnInit()
//---- getting handle of the ATR_Trailing indicator
   if(InpInd_Handle==INVALID_HANDLE) Print(" Failed to get handle of ATR_Trailing indicator");

//--- resetting error code to zero

//--- cchart object works with the current chart (ID=0) , the expert is attached to

//---- adding of ATR_Trailing indicator on the chart  
   if(!cchart.IndicatorAdd(0,InpInd_Handle)) Print(" Failed to add ATR_Trailing indicator on the chart");


Expert Advisor input parameters:

//| Expert Advisor input parameters              |
input int Period_ATR=14;  //АТR period
input double Sell_Factor=2.0;
input double Buy_Factor=2.0;
input uint Deviation=10;  //slippage


Exp_ATR_Trailing working chart

Exp_ATR_Trailing working chart 

Translated from Russian by MetaQuotes Software Corp.
Original code:

i-AnyRange2 i-AnyRange2

The indicator of two unspecified time intervals.

TimeSeries - Function Library for Working with Time Series TimeSeries - Function Library for Working with Time Series

Function Library for Working with Time Series: iTime, iOpen, iHigh, iLow, iClose, iHighest, iLowest, iBarshift. Short invocation version is available for all functions (with the current chart's symbol and period).

PositionInfo PositionInfo

The simple indicator for displaying some relevant information about a position right in the chart window.

ColorStochastic_HTF ColorStochastic_HTF

Standard Stochastic oscillator displayed as a cloud that can be located by setting the indicator timeframe value different from the chart one.