Executer_candles
Download scriptThis script is an automated trading system designed for the MetaTrader platform. It functions by scanning live price charts to identify specific candlestick patterns and manages trades automatically based on pre-defined risk and profit rules.
Core Trading Logic
The software operates as a "pattern recognition" engine. Instead of following simple trend lines, it looks for specific geometric shapes and movements in the candles (such as "Morning Stars," "Hammer" candles, or "Bull/Bear" formations).
When the market forms a pattern that matches its internal logic, the script will:
- Execute Buys: If a bullish pattern is detected (indicating potential upward movement), it opens a "Buy" position.
- Execute Sells: If a bearish pattern is detected (indicating potential downward movement), it opens a "Sell" position.
Dynamic Risk and Lot Management
One of the most sophisticated features of this script is its ability to adjust trade sizes automatically. Rather than trading the same amount every time, the system analyzes two main factors:
- Account Balance: It checks your available free margin to ensure you have enough funds to cover a trade.
- Trading History: It reviews your past performance. It looks at previous wins and losses to calculate an appropriate "Lot Size" (trade volume). This is intended to help balance risk, ensuring that the size of your next trade is proportional to your account's health and recent success rate.
Trade Protection and Management
Once a trade is opened, the script takes over the management of that position through several layers of protection:
- Stop Loss & Take Profit: Every trade is instantly assigned a "safety net" (Stop Loss) to limit potential losses and a target (Take Profit) to lock in gains.
- Trailing Stop: The script features a dynamic "Trailing Stop" mechanism. As a trade moves into profit, the system automatically slides the safety net along with it. This allows the trade to remain open as long as the price continues to move in your favor, while protecting the profits you have already accumulated.
- Safety Checks: Before opening any new trades, the script performs "sanity checks" to ensure there is sufficient data on the chart, that the stop loss values are realistic, and that the account has enough capital to sustain the trade.
User Experience Features
To keep the user informed without requiring them to constantly watch the charts, the script includes:
- Audio Alerts: It can play a sound file whenever a trade is opened, closed, or modified.
- Visual Coding: Different colors are assigned to various types of orders (Buy, Sell, Modifications) to make it easier to identify what the robot is doing at a glance.
//+------------------------------------------------------------------+
//| Executer_candles.mq4 |
//| Copyright © 2006, Alex Sidd (Executer) |
//| mailto:work_st@mail.ru |
//+------------------------------------------------------------------+
//| Íåêîòîðûå "îáùåïîëåçíûå" ôóíêöèè êîäà, òðåéëèíã ñòîï, íàïðèìåð, |
//| áûëè ëþáåçíî ïîçàèìñòâîâàíû èç äðóãèõ ýêñïåðòîâ. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Alex Sidd (Executer)"
#property link "mailto:work_st@mail.ru"
//----
#define MAGIC 612457
//----
extern double lStopLoss = 90;
extern double sStopLoss =60;
extern double lTakeProfit = 80;
extern double sTakeProfit = 100;
extern double lTrailingStop = 30;
extern double sTrailingStop = 100;
extern double Lots = 0.10;
extern double DecreaseFactor = 100;
extern double MaximumRisk = 0.025;
extern double MaximumLots = 100;
extern bool flag = true;
//----
double Limit = 400;
color clOpenBuy = Blue;
color clCloseBuy = Aqua;
color clOpenSell = Red;
color clCloseSell = Violet;
color clModiBuy = Blue;
color clModiSell = Red;
string Name_Expert = "Executer_Candles";
int Slippage = 0;
bool UseSound = True;
string NameFileSound = "alert.wav";
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetSizeLot()
{
double Lot = Lots;
int cycle;
int prof = 0;
int orders = HistoryTotal(); // history orders total
int losses = 0; // number of losses orders without a break
int vinn = 0;
int i = orders;
//---- select lot size
Lot = NormalizeDouble(AccountFreeMargin()*MaximumRisk / 100, 1);
if(AccountFreeMargin() <= 10000 && AccountFreeMargin() > 5000)
DecreaseFactor = NormalizeDouble(AccountFreeMargin() / 100, 1);
if(AccountFreeMargin() <= 5000 && AccountFreeMargin() > 3000)
DecreaseFactor = NormalizeDouble(AccountFreeMargin() / 200, 1);
if(AccountFreeMargin() <= 14)
DecreaseFactor = 14;
if(AccountFreeMargin() > 10000)
DecreaseFactor = 60;
if(DecreaseFactor > 0 && orders > DecreaseFactor)
{
for(cycle = 1; cycle < DecreaseFactor; cycle++)
{
i--;
if(OrderSelect(i, SELECT_BY_TICKET, MODE_HISTORY) == false)
{
Print("Error in history!");
break;
}
if(OrderCloseTime() > 0)
{
prof = prof + OrderProfit();
if(OrderProfit() <= 0 )
losses++;
else
vinn++;
}
}
if(prof <= 0 )
Lot = 0.1; // Lot - (0.1*losses); //Lot = 0.1;//&& losses > vinn
// if(prof <=0 && vinn>losses) Lot = Lot - (0.1*losses);
if(prof > 0 && losses > vinn)
{
Lot = Lot + (0.1*NormalizeDouble(vinn / 4, 0.1));
}
if(prof > 0 && losses <= vinn )
{
Lot = Lot + (0.1*NormalizeDouble(vinn / 2, 0.1));
}
}
if(AccountFreeMargin() < 300 || Lot < 0.1)
Lot = 0.1;
if(Lot*1275 >= AccountFreeMargin())
Lot = NormalizeDouble(AccountFreeMargin()*MaximumRisk / 100, 1);
if(MaximumLots != 0 && Lot > MaximumLots)
Lot = MaximumLots;
if(DecreaseFactor > orders)
Lot = Lots;
Lot = Lots;
return(Lot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsUp()
{
if(iOpen(NULL, 1440, 1) < iClose(NULL, 1440, 1))
return(false);
else
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void deinit()
{
Comment("");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsViselnik()
{
if((IsUp() == true) &&
(Open[1] - Close[1] > 0) &&
((High[1] - Open[1])*100 / (Open[1] - Close[1] + 0.0000001) < 15) &&
((Close[1] - Low[1])*100 / (Open[1] - Close[1] + 0.0000001) > 200) &&
(Open[2] - Close[2] < 0))
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsHummer()
{
if((IsUp() == false) &&
(Open[1] - Close[1] < 0) &&
((High[1] - Close[1])*100 / (Close[1] - Open[1] + 0.0000001) > 200) &&
((Open[1] - Low[1])*100 / (Close[1] - Open[1] + 0.0000001)<15) &&
(Open[2] - Close[2] > 0))
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsBear()
{
if((IsUp() == true) &&
(Open[1] - Close[1] > 0) &&
(Open[1] >= Close[2]) &&
(Close[1] <= Open[2]) &&
(Open[2] - Close[2] < 0) &&
((Open[1] - Close[1]) / (Close[2] - Open[2] + 0.0000001) > 1.5))
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsBull()
{
if((IsUp() == false) &&
(Open[1] - Close[1] < 0) &&
(Open[2] - Close[2] > 0) &&
(Close[1] >= Open[2]) &&
(Open[1] <= Close[2]) &&
((Close[1] - Open[1]) / (Open[2] - Close[2] + 0.0000001) > 1.5))
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool Zavesa_black()
{
if((IsUp() == true) &&
(Open[1] - Close[1] > 0) &&
(Open[2] - Close[2] < 0) &&
((Close[2] - Open[2]) / (High[2] - Low[2]) > 0.6) &&
(Open[1] > High[2]) &&
(Close[1] < (Open[2] + (Close[2] - Open[2]) / 2)))
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool Prosvet()
{
if((IsUp() == false) &&
(Open[1] - Close[1] < 0) &&
(Open[2] - Close[2] > 0) &&
((Open[2] - Close[2]) / (High[2] - Low[2]) > 0.6) &&
(Open[1] < Low[2]) &&
(Close[1] > (Close[2] + (Open[2] - Close[2]) / 2)))
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool morning_star()
{
if((Open[3] - Close[3] > 0) &&
(Close[2] - Open[2] > 0) &&
(Close[1] - Open[1] > 0) &&
(Close[2] < Close[3]) &&
(Open[1] > Close[2]) &&
(((MathAbs(Open[3] - Close[1]) + MathAbs(Open[1] - Close[3])) /
(Open[3] - Close[3] + 0.0000001)) < 0.1) &&
((Open[3] - Close[3]) / (High[3] - Low[3]) > 0.8) && ((Close[2] - Open[2]) /
(High[2] - Low[2]) < 0.3) &&
(Close[1] - Open[1]) / (High[1] - Low[1]) > 0.8)
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool evening_star()
{
if((Open[3] - Close[3] < 0) &&
(Close[2] - Open[2] < 0) &&
(Close[1] - Open[1] < 0) &&
(Close[2] > Close[3]) &&
(Open[1] < Close[2]) &&
(((MathAbs(Open[3] - Close[1]) + MathAbs(Open[1] - Close[3])) / (Close[3] -
Open[3] + 0.0000001)) < 0.1) &&
((Close[3] - Open[3]) / (High[3] - Low[3]) > 0.8) && ((Open[2] - Close[2]) /
(High[2] - Low[2]) < 0.3) &&
((Open[1] - Close[1]) / (High[1] - Low[1]) > 0.8))
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsEveningDodgiStar()
{
if((Open[3] - Close[3] < 0) &&
(Close[2] - Open[2] == 0) &&
(Close[1] - Open[1] <0) &&
(Close[2] >= Close[3]) &&
(Open[1] <= Close[2]) &&
(((MathAbs(Open[3] - Close[1]) + MathAbs(Open[1] -Close[3])) /
(Open[3] - Close[3] + 0.0000001)) < 0.1))
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsMorningDodgiStar()
{
if((Open[3] - Close[3] > 0) &&
(Close[2] - Open[2] == 0) &&
(Close[1] - Open[1] > 0) &&
(Close[2] <= Close[3]) &&
(Open[1] >= Close[2]) &&
(((MathAbs(Open[3] - Close[1]) + MathAbs(Open[1] - Close[3])) /
(Open[3] - Close[3] + 0.0000001)) < 0.1))
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
if(Bars < 100)
{
Print("bars less than 100");
return(0);
}
if(lStopLoss < 3)
{
Print("StopLoss less than 3");
return(0);
}
if(lTakeProfit < 3)
{
Print("TakeProfit less than 3");
return(0);
}
if(sStopLoss < 3)
{
Print("StopLoss less than 3");
return(0);
}
if(sTakeProfit < 3)
{
Print("TakeProfit less than 3");
return(0);
}
if(AccountFreeMargin() < (1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if(!ExistPositions())
{
if(IsHummer() == true)
{
OpenBuy();
return(0);
}
if(IsBull() == true)
{
OpenBuy();
return(0);
}
if(Prosvet() == true)
{
OpenBuy();
return(0);
}
if(morning_star() == true)
{
OpenBuy();
return(0);
}
if(IsMorningDodgiStar() == true)
{
OpenBuy();
return(0);
}
}
if(!ExistPositions())
{
if(IsViselnik() == true)
{
OpenSell();
return(0);
}
if(IsBear() == true)
{
OpenSell();
return(0);
}
if(Zavesa_black() == true)
{
OpenSell();
return(0);
}
if(evening_star() == true)
{
OpenSell();
return(0);
}
if(IsEveningDodgiStar() == true)
{
OpenSell();
return(0);
}
}
TrailingPositionsBuy(lTrailingStop);
TrailingPositionsSell(sTrailingStop);
return (0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool ExistPositions()
{
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC)
{
return(True);
}
}
}
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void TrailingPositionsBuy(int trailingStop)
{
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC)
{
if(OrderType() == OP_BUY)
{
if(Bid - OrderOpenPrice() > trailingStop*Point)
{
if(OrderStopLoss() < Bid - trailingStop*Point)
ModifyStopLoss(Bid - trailingStop*Point);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void TrailingPositionsSell(int trailingStop)
{
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC)
{
if(OrderType() == OP_SELL)
{
if(OrderOpenPrice() - Ask > trailingStop*Point)
{
if(OrderStopLoss() > Ask + trailingStop*Point || OrderStopLoss() == 0)
ModifyStopLoss(Ask + trailingStop*Point);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStopLoss)
{
bool fm;
fm = OrderModify(OrderTicket(), OrderOpenPrice(), ldStopLoss, OrderTakeProfit(),
0, CLR_NONE);
if(fm && UseSound)
PlaySound(NameFileSound);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseBuy()
{
bool fc;
fc = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clCloseBuy);
if(fc && UseSound)
PlaySound(NameFileSound);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseSell()
{
bool fc;
fc = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clCloseSell);
if(fc && UseSound)
PlaySound(NameFileSound);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OpenBuy()
{
double ldLot, ldStop, ldTake;
string lsComm;
ldLot = GetSizeLot();
ldStop = GetStopLossBuy();
ldTake = GetTakeProfitBuy();
lsComm = GetCommentForOrder();
OrderSend(Symbol(), OP_BUY, ldLot, Ask, Slippage, ldStop, ldTake, lsComm, MAGIC,
0, clOpenBuy);
if(UseSound)
PlaySound(NameFileSound);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OpenSell()
{
double ldLot, ldStop, ldTake;
string lsComm;
int i = 1;
int orders = HistoryTotal();
ldLot = GetSizeLot();
ldStop = GetStopLossSell();
ldTake = GetTakeProfitSell();
lsComm = GetCommentForOrder();
OrderSend(Symbol(), OP_SELL, ldLot, Bid, Slippage, ldStop, ldTake, lsComm, MAGIC,
0, clOpenSell);
if(UseSound)
PlaySound(NameFileSound);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string GetCommentForOrder()
{
return(Name_Expert);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetStopLossBuy()
{
return (Bid - lStopLoss*Point);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetStopLossSell()
{
return(Ask + sStopLoss*Point);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetTakeProfitBuy()
{
return(Ask + lTakeProfit*Point);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetTakeProfitSell()
{
return(Bid - sTakeProfit*Point);
}
//----
return(0);
//+------------------------------------------------------------------+
Comments