ExpertCLOR_2MA&StATR_v01
Download scriptThis script is a management tool designed to automate specific actions for existing trades on the MetaTrader 4 platform. It is important to note that this tool is not designed to open new trades; rather, it acts as a "manager" for positions you have already opened manually or through other means.
The logic of the script can be broken down into three primary functions:
1. Dynamic Stop-Loss Management
The script constantly monitors your active "Buy" and "Sell" positions to manage risk automatically using two different methods:
- Trailing Stop (ATR Based): It integrates with a specific external indicator (
StopATR_auto) to calculate a dynamic stop-loss level. As the market moves, the script adjusts your stop-loss price to follow the price action, helping to lock in profits or protect against sudden reversals based on market volatility. - Break-Even Protection: The script includes a "Break-Even" feature. If a trade moves into a significant profit (defined by a user-set number of points), the script will automatically move the stop-loss to your entry price. This ensures that once a trade reaches a certain level of success, you are no longer at risk of losing money on that position.
2. Automated Exit Signals
The script uses "Moving Averages" (MA) as a primary signal for when it is time to exit a trade. It monitors two different moving averages—a "Fast" one and a "Slow" one—and looks for specific crossovers:
- For Buy Positions: If the Fast Moving Average crosses below the Slow Moving Average, the script recognizes this as a downward trend signal and initiates a close command.
- For Sell Positions: If the Fast Moving Average crosses above the Slow Moving Average, the script recognizes an upward trend signal and initiates a close command.
3. Automation Workflow
The internal logic follows a continuous cycle:
- Scan: It first identifies which trades are currently open on your specific trading chart (checking for Buy or Sell types).
- Protect: It checks if the current stop-loss needs to be moved higher (for trailing) or pulled back to the entry point (for break-even).
- Evaluate: It checks the Moving Average data to see if the "Close" conditions have been met.
- Execute: If a close signal is triggered, it automatically sends the command to shut down the position at the current market price.
Summary of Key Parameters
To control how these rules behave, the script allows you to toggle specific features (on/off), define the speed and type of the Moving Averages, set the profit threshold for the Break-Even point, and configure the sensitivity of the ATR-based trailing stop.
//+----------------------------------------------------------------------------+
//| ExpertCLOR_2MA&StATR_v01.mq4 |
//| Ñåðãåé Çàèêèí as Vuki |
//| f_kombi@mail.ru |
//+----------------------------------------------------------------------------+
//| Ñîâåòíèê êîòîðûé: |
//| 1. Çàêðûâàåò îðäåð ïî ïåðåñå÷åíèþ 2-õ ÌÀ (5 è 7 ïî óìîë÷àíèþ); |
//| 2. Àâòîìàòè÷åñêè äâèãàåò ñòîï ïî èíäèêàòîðó StopATR_auto; |
//| 3. Ïåðåâîäèò îòêðûòóþ ïîçèöèþ â áåçóáûòîê ïðè äîñòèæåíèè çàäàííîãî óðîâíÿ|
//| |
//| ÂÍÈÌÀÍÈÅ!!! Ñîâåòíèê òîëüêî ÇÀÊÐÛÂÀÅÒ îòêðûòûå îðäåðà!!! |
//+----------------------------------------------------------------------------+
// Âõîäíûå ïàðàìåòðû:
// MA_CloseOnOff - âêëþ÷åíèå (1) èëè âûêëþ÷åíèå (0) ðåæèìà çàêðûòèÿ îðäåðà ïî ïåðåñå÷åíèþ ÌÀ
// StATR_CloseOnOff - âêëþ÷åíèå (1) èëè âûêëþ÷åíèå (0) óñòàíîâêè è èçìåíåíèÿ ñòîïà ïî èíäèêàòîðó StopATR_auto
// MA_Fast_Pe - ïåðèîä áûñòðîé ÌÀ
// MA_Fast_Ty - òèï áûñòðîé ÌÀ (0-SMA, 1-EMA, 2-SMMA(ñãëàæåííàÿ), 3-LWMA(âçâåøåííàÿ))
// MA_Fast_Pr - öåíà áûñòðîé ÌÀ (0-Close, 1-Open, 2-High, 3-Low, 4-HL/2, 5-HLC/3, 6-HLCC/4)
// MA_Slow_Pe - ïåðèîä ìåäëåííîé ÌÀ
// MA_Slow_Ty - òèï ìåäëåííîé ÌÀ (0-SMA, 1-EMA, 2-SMMA(ñãëàæåííàÿ), 3-LWMA(âçâåøåííàÿ))
// MA_Slow_Pr - öåíà ìåäëåííîé ÌÀ (0-Close, 1-Open, 2-High, 3-Low, 4-HL/2, 5-HLC/3, 6-HLCC/4)
// TimeFrame - ðàáî÷èé òàéìôðåéì (1-Ì1, 5-Ì5, 15-Ì15á 60-Í1, 240-Í4)
// BezUb - óðîâåíü ïðîôèòà â ïóíêòàõ, ïðè êîòîðîì îðäåð ïåðåâîäèòñÿ â áåçóáûòîê
// CountBarsForShift - ïàðàìåòð èí-ðà StopATR_auto - ðàññòîÿíèå â áàðàõ äëÿ âûâîäà óðîâíÿ ñòîïà íà ýêðàí
// CountBarsForAverage - ïàðàìåòð èí-ðà StopATR_auto - êîëè÷åñòâî áàðîâ óñðåäíåíèÿ äëÿ ðàññ÷åòà ñòîïà
// Target - ïàðàìåòð èí-ðà StopATR_auto - êýôôèöèåíò óâåëè÷åíèÿ çíà÷åíèÿ ñðåäíåãî áàðà äëÿ ðàññ÷åòà ñòîïà
//-----
// Èíäèêàòîð StopATR_auto ÎÁßÇÀÒÅËÜÍÎ äîëæåí áûòü â ïàïêå Indicators âàøåãî ÌÒ4
// Èíäèêàòîð StopATR_auto íà ãðàôèê ìîæíî íå âåøàòü, åãî ñîâåòíèê ñàì ïîâåñèò
//-----
#property copyright "Ñåðãåé Çàèêèí as Vuki"
#property link "f_kombi@mail.ru"
//-----
extern int MA_CloseOnOff =1;
extern int StATR_CloseOnOff =1;
extern int MA_Fast_Pe =5;
extern int MA_Fast_Ty =MODE_EMA;
extern int MA_Fast_Pr =PRICE_CLOSE;
extern int MA_Slow_Pe =7;
extern int MA_Slow_Ty =MODE_EMA;
extern int MA_Slow_Pr =PRICE_OPEN;
extern int TimeFrame =PERIOD_M5;
extern int BezUb =15;
extern int CountBarsForShift =7;
extern int CountBarsForAverage=12;
extern double Target =2.0;
//----
int Opened=0;
int OpenedBuy=0;
int OpenedSell=0;
int OpenedBuyTicket=0;
int OpenedSellTicket=0;
int CloseBuy=0;
int CloseSell=0;
int Closed=0;
double StopL;
//expert initialization function
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//expert deinitialization function
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int cnt;
bool errbool=true;
int errint=0;
//----
CloseBuy=0; CloseSell=0;
// ïðîâåðêà, åñòü ëè îòêðûòûå ðûíî÷íûå èëè îòëîæåííûå îðäåðà
OpenedBuy=0;
OpenedSell=0;
for(cnt=0; cnt < OrdersTotal(); cnt++)
{
errbool=OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(errbool!=true) Comment("Îøèáêà âûáîðà îðäåðà ïî ïîçèöèè!","\n","\n");
if(OrderType()==OP_BUY && OrderSymbol()==Symbol()) {OpenedBuy=1; OpenedBuyTicket=OrderTicket();}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol()) {OpenedSell=1; OpenedSellTicket=OrderTicket();}
}
//åñëè åñòü ðûíî÷íûé áàé, òî èçìåíÿåì ñòîïëîññ â áó è ïî ñòîïàòð
if(OpenedBuy==1)
{
StopL=iCustom(NULL,TimeFrame,"StopATR_auto",Blue,Brown,false,CountBarsForShift,CountBarsForAverage,Target,2,0);
StopL=NormalizeDouble(StopL,Digits);
errbool=OrderSelect(OpenedBuyTicket, SELECT_BY_TICKET, MODE_TRADES);
if(errbool!=true) Comment("Îøèáêà âûáîðà îðäåðà ïî ïîçèöèè!","\n","\n");
// Ïåðåâîä â áåçóáûòîê îðäåðà Áàé
if(BezUb!=0 && (Bid - OrderOpenPrice())>=(BezUb*Point) && OrderStopLoss() < OrderOpenPrice())
{
errbool=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Point,OrderTakeProfit(),0,White);
errint=GetLastError();
if(errbool!=true&&errint>1) Comment(errint," Îøèáêà ìîäèôèêàöèè òåêóùåãî îðäåðà Áàé ïî áåçóáûòêó!","\n","\n");
else Comment("Ñðàáîòàë ïåðåâîä òåêóùåãî îðäåðà Áàé â áåçóáûòîê!\n");
return(0);
}
// Ìîäèôèêàöèÿ ñòîïà ïî ÑòîïÀÒÐ îðäåðà Áàé
if(OrderStopLoss() < StopL && StATR_CloseOnOff==1)
{
errbool=OrderModify(OrderTicket(),OrderOpenPrice(),StopL,OrderTakeProfit(),0,White);
errint=GetLastError();
if(errbool!=true&&errint>1) Comment(errint," Îøèáêà ìîäèôèêàöèè òåêóùåãî îðäåðà Áàé ïî ÑòîïÀÒÐ!","\n","\n");
return(0);
}
}
//åñëè åñòü ðûíî÷íûé ñåëë, òî èçìåíÿåì ñòîïëîññ â áó è ïî ñòîïàòð
if(OpenedSell==1)
{
StopL=iCustom(NULL,TimeFrame,"StopATR_auto",Blue,Brown,false,CountBarsForShift,CountBarsForAverage,Target,1,0);
StopL=NormalizeDouble(StopL,Digits);
errbool=OrderSelect(OpenedSellTicket, SELECT_BY_TICKET, MODE_TRADES);
if(errbool!=true) Comment("Îøèáêà âûáîðà îðäåðà ïî ïîçèöèè!","\n","\n");
// Ïåðåâîä â áåçóáûòîê îðäåðà Ñåëë
if(BezUb!=0 && (OrderOpenPrice() - Ask)>=(BezUb*Point) && OrderStopLoss() > OrderOpenPrice())
{
errbool=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-Point,OrderTakeProfit(),0,White);
errint=GetLastError();
if(errbool!=true&&errint>1) Comment(errint," Îøèáêà ìîäèôèêàöèè òåêóùåãî îðäåðà Ñåëë ïî áåçóáûòêó!","\n","\n");
else Comment("Ñðàáîòàë ïåðåâîä òåêóùåãî îðäåðà Ñåëë â áåçóáûòîê!\n");
return(0);
}
// Ìîäèôèêàöèÿ ñòîïà ïî ÑòîïÀÒÐ îðäåðà Ñåëë
if(StATR_CloseOnOff==1)
{
// Comment("çäåñü ðàáîòàåò! StopL = ",StopL,"\n");
if(OrderStopLoss()==0 || OrderStopLoss() > StopL) {
errbool=OrderModify(OrderTicket(),OrderOpenPrice(),StopL,OrderTakeProfit(),0,White);
errint=GetLastError();
if(errbool!=true&&errint>1) Comment(errint," Îøèáêà ìîäèôèêàöèè òåêóùåãî îðäåðà Ñåëë ïî ÑòîïÀÒÐ!","\n","\n");
return(0);
}
}
}
if(MA_CloseOnOff==1) FunctionSignalClose();
// Åñëè åñòü ñèãíàë íà çàêðûòèå, çàêðûâàåì
if((OpenedBuy==1&&OpenedSell==0&&CloseBuy==1&&CloseSell==0)||(OpenedBuy==0&&OpenedSell==1&&CloseBuy==0&&CloseSell==1))
{
if(OpenedBuy==1)
{
errbool=OrderSelect(OpenedBuyTicket, SELECT_BY_TICKET, MODE_TRADES);
if(errbool!=true) Comment("Îøèáêà âûáîðà îðäåðà ïî ïîçèöèè!","\n","\n");
}
if(OpenedSell==1)
{
errbool=OrderSelect(OpenedSellTicket, SELECT_BY_TICKET, MODE_TRADES);
if(errbool!=true) Comment("Îøèáêà âûáîðà îðäåðà ïî ïîçèöèè!","\n","\n");
}
Closed=FunctionCloseTrade(OrderTicket(),OrderType(),OrderLots());
if(Closed==1) OpenedBuy=0;
if(Closed==2) OpenedSell=0;
return(0);
}
return(0);
}
// Ôóíêöèÿ ðàññ÷åòà ñèãíàëîâ Çàêðûòü Áàé, Çàêðûòü Ñåëë
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void FunctionSignalClose()
{
double maFast1,maSlow1,maFast2,maSlow2;
maFast1=iMA(NULL,TimeFrame,MA_Fast_Pe,0,MA_Fast_Ty,MA_Fast_Pr,1);
maSlow1=iMA(NULL,TimeFrame,MA_Slow_Pe,0,MA_Slow_Ty,MA_Slow_Pr,1);
maFast2=iMA(NULL,TimeFrame,MA_Fast_Pe,0,MA_Fast_Ty,MA_Fast_Pr,2);
maSlow2=iMA(NULL,TimeFrame,MA_Slow_Pe,0,MA_Slow_Ty,MA_Slow_Pr,2);
if(OpenedBuy==1)
{
if(maFast1<=maSlow1 && maFast2 > maSlow2) {CloseBuy=1; CloseSell=0;}
}
if(OpenedSell==1)
{
if(maFast1>=maSlow1 && maFast2 < maSlow2) {CloseSell=1; CloseBuy=0;}
}
return;
}
// Ôóíêöèÿ çàêðûòèÿ ïîçèöèé Áàé èëè Ñåëë. Âîçâðàùàåò 1 åñëè çàêðûë Áàé, 2 åñëè çàêðûë Ñåëë, 0 åñëè îøèáêà
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int FunctionCloseTrade(int Tic, int Type, double Lot)
{
int Ret;
double PrCl;
color ArCo;
bool errbool;
//----
if(Type==OP_BUY)
{
PrCl=Bid;
ArCo=Aqua;
Ret=1;
}
if(Type==OP_SELL)
{
PrCl=Ask;
ArCo=Aqua;
Ret=2;
}
errbool=OrderClose(Tic,Lot,PrCl,10,ArCo);
if(errbool!=true)
{
Comment("Îøèáêà ",GetLastError()," çàêðûòèÿ ðûíî÷íîãî îðäåðà!","\n","\n");
return(0);
}
return(Ret);
}
//+------------------------------------------------------------------+
Comments