Orders Execution
0
Views
0
Downloads
0
Favorites
Profitability Reports
GBP/USD
Oct 2024 - Jan 2025
53.00 %
Total Trades
3178
Won Trades
642
Lost trades
2536
Win Rate
0.20 %
Expected payoff
-0.15
Gross Profit
557.38
Gross Loss
-1046.00
Total Net Profit
-488.62
-100%
-50%
0%
50%
100%
NZD/USD
Oct 2024 - Jan 2025
16.00 %
Total Trades
3178
Won Trades
434
Lost trades
2744
Win Rate
0.14 %
Expected payoff
-0.34
Gross Profit
199.57
Gross Loss
-1281.48
Total Net Profit
-1081.91
-100%
-50%
0%
50%
100%
Divergence Xpert
//+------------------------------------------------------------------+
//| Divergence Xpert.mq4 |
//| Copyright © 2009, TheXpert |
//| theforexpert@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, TheXpert"
#property link "theforexpert@gmail.com"
extern string Group1 = "Ïàðàìåòðû äëÿ îðäåðîâ";
// magic number needed for filtering orders opened by certain EA
extern int Magic = 12345;
// lots size of opening orders. If 0 it is counted automatically
extern double TradeLots = 0;
// risk for automatic lots size counting
extern double RiskPercentage = 0;
// maximum acceptable slippage for the price
extern int Slippage = 3;
// maximum loop count for orders opening if failed due to requoting
extern int TimesToRepeat = 3;
extern string Group3 = "Ïàðàìåòðû äëÿ ñòðàòåãèè";
extern int i_fastEMA = 9;
extern int i_slowEMA = 21;
extern int i_signalMA = 5;
extern int Depth = 1;
extern int ShowHidden = 0;
extern int UseClose = 0;
extern int Target = 0;
extern int Loss = 500;
string symbol;
bool CriticalError = false;
/// \brief Equal to ternar operator
/// needed = Condition ? IfTrue : IfFalse;
/// \param IfTrue
/// \param IfFalse
/// \return matching value from parameters
double DoubleIf(bool Condition, double IfTrue, double IfFalse)
{
if (Condition) return (IfTrue);
else return (IfFalse);
}
void WaitForContext()
{
while (IsTradeContextBusy())
{
Sleep(100);
}
}
void OpenBuy(int MN, int Target, int Loss, double Lot)
{
int count = 0;
while (count < TimesToRepeat)
{
WaitForContext();
RefreshRates();
double TP = DoubleIf(Target > 0, Ask + Target*Point, 0);
double SL = DoubleIf(Loss > 0, Ask - Loss*Point, 0);
double LotsToBid = DoubleIf(Lot == 0, GetLotsToBid(RiskPercentage), Lot);
int res = OrderSend(Symbol(), OP_BUY, LotsToBid, Ask, Slippage, SL, TP, NULL, MN, 0, Blue);
if (res > 0) return;
count++;
}
}
void OpenSell(int MN, int Target, int Loss, double Lot)
{
int count = 0;
while (count < TimesToRepeat)
{
WaitForContext();
RefreshRates();
double TP = DoubleIf(Target > 0, Bid - Target*Point, 0);
double SL = DoubleIf(Loss > 0, Bid + Loss*Point, 0);
double LotsToBid = DoubleIf(Lot == 0, GetLotsToBid(RiskPercentage), Lot);
int res = OrderSend(Symbol(), OP_SELL, LotsToBid, Bid, Slippage, SL, TP, NULL, MN, 0, Red);
if (res > 0) return;
count++;
}
}
void CloseSells(int MagicNumber, int Slippage)
{
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
// already closed
if(OrderSelect(i, SELECT_BY_POS) == false) continue;
// not current symbol
if(OrderSymbol() != Symbol()) continue;
// order was opened in another way
if(OrderMagicNumber() != MagicNumber) continue;
if(OrderType() == OP_SELL)
{
int count = 0;
while (count < TimesToRepeat)
{
WaitForContext();
RefreshRates();
if (OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red))
{
break;
}
count++;
}
}
}
}
void CloseBuys(int MagicNumber, int Slippage)
{
for(int i = OrdersTotal(); i >= 0; i--)
{
// already closed
if(OrderSelect(i, SELECT_BY_POS) == false) continue;
// not current symbol
if(OrderSymbol() != Symbol()) continue;
// order was opened in another way
if(OrderMagicNumber() != MagicNumber) continue;
if(OrderType() == OP_BUY)
{
int count = 0;
while (count < TimesToRepeat)
{
WaitForContext();
RefreshRates();
if (OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue))
{
break;
}
count++;
}
}
}
}
bool GetActiveOrders(int MagicNumber = -1)
{
for(int i = 0; i < OrdersTotal(); i++)
{
// already closed
if(OrderSelect(i, SELECT_BY_POS) == false) continue;
// not current symbol
if(OrderSymbol() != Symbol()) continue;
// order was opened in another way
if(OrderMagicNumber() != MagicNumber && MagicNumber != -1) continue;
if(OrderType() == OP_SELL || OrderType() == OP_BUY)
{
return (true);
}
}
return (false);
}
int GetOrdersCount(int MagicNumber = -1, int Type = -1, string symb = "")
{
int count = 0;
for(int i = 0; i < OrdersTotal(); i++)
{
// already closed
if(OrderSelect(i, SELECT_BY_POS) == false) continue;
// not current symbol
if(OrderSymbol() != Symbol() && symb == "") continue;
// not specified symbol
if(OrderSymbol() != symb && symb != "" && symb != "all") continue;
// order was opened in another way
if(OrderMagicNumber() != MagicNumber && MagicNumber != -1) continue;
if(OrderType() == Type || Type == -1)
{
count++;
}
}
return (count);
}
double GetLotsToBid(double RiskPercentage)
{
double margin = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
double minLot = MarketInfo(Symbol(), MODE_MINLOT);
double maxLot = MarketInfo(Symbol(), MODE_MAXLOT);
double step = MarketInfo(Symbol(), MODE_LOTSTEP);
double account = AccountEquity();
double percentage = account*RiskPercentage/100;
if (margin*step == 0) return(minLot);
double lots = MathRound(percentage/margin/step)*step;
if(lots < minLot)
{
lots = minLot;
}
if(lots > maxLot)
{
lots = maxLot;
}
return (lots);
}
void Check()
{
double buy = iCustom(symbol, 0, "Divergence", i_fastEMA, i_slowEMA, i_signalMA, Depth, ShowHidden, 0, 2, 1);
double sell = iCustom(symbol, 0, "Divergence", i_fastEMA, i_slowEMA, i_signalMA, Depth, ShowHidden, 0, 1, 1);
if (buy != EMPTY_VALUE)
{
if (UseClose == 0) CloseSells(Magic, Slippage);
if (GetOrdersCount(Magic, OP_BUY) == 0)
{
OpenBuy(Magic, Target, Loss, TradeLots);
}
}
else if (UseClose == 1)
{
CloseBuys(Magic, Slippage);
}
if (sell != EMPTY_VALUE)
{
if (UseClose == 0) CloseBuys(Magic, Slippage);
if (GetOrdersCount(Magic, OP_SELL) == 0)
{
OpenSell(Magic, Target, Loss, TradeLots);
}
}
else if (UseClose == 1)
{
CloseSells(Magic, Slippage);
}
}
datetime LastTime;
int init()
{
symbol = Symbol();
LastTime = Time[0];
}
int start()
{
if (CriticalError) return(0);
if (!IsTradeAllowed()) return(0);
double margin = AccountFreeMargin();
double minBet = GetLotsToBid(0)*MarketInfo(symbol, MODE_MARGINREQUIRED);
if (!GetActiveOrders(Magic) && minBet > margin)
{
CriticalError = true;
return(0);
}
if (LastTime < Time[0])
{
LastTime = Time[0];
}
else
{
return(0);
}
Check();
return(0);
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---