int TotalOrdersCount() { int result = 0; for(int i=0;i < OrdersTotal();i++) { int check8 = OrderSelect(i, SELECT_BY_POS , MODE_TRADES); if (OrderMagicNumber() == MagicNumber) result++; } return (result); }If you are using the same magic number for all charts then you also have to check OrderSymbol().
I've done some research by reading numerous posts but I'm struggling to understand where OrderSymbol() needs to be inserted into my code and what the line would look like. Would you mind showing me, please?
You can't have done much research. There must be several thousand posts with such examples and I am not exaggerating!
You know how to check for the magic number
if (OrderMagicNumber() == MagicNumber)
So you should be able to work out how to check if OrderSymbol() is Symbol().
Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
PositionClose is not working - MQL5 programming forum (2020)
MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
This is the first EA that I've written, borrowing bits from others, too.
I have it attached to 12 charts on my Demo account, but it seems to only allow one trade at a time across all 12 forex pairs.
My account and broker aren't restricting me in this way, so is my code restricting me to only one order at a time?
Any other feedback is welcome.
input string Money="======Money Management======"; //============================
extern double Risk_P = 0.2; //Percentage Risk Per Trade (Zero = off)
extern double Min_Lots = 0.05; //Manual Lot Size (if above = 0)
extern double Max_Lots = 7.0;
extern double Lot_Decimal_Digits = 2.0;
extern double StopLoss = 15.0; //Stop Loss in Pips (default 15.0)
extern double TakeProfit = 0; //Take Profit in Pips (Zero = off)
extern double TrailingStop = 7.0; //Trailing Stop in Pips (default 7.0)
extern int Slippage = 3;
input string Magic="========Magic Number========"; //============================
extern int MagicNumber = 142710; //Magic Number
//+------------------------------------------------------------------+
// expert start function
//+------------------------------------------------------------------+
int start()
{
double MyPoint=Point;
if(Digits == 3 || Digits == 5) MyPoint = Point * 10;
double TheStopLoss = 0;
double TheTakeProfit = 0;
if( TotalOrdersCount() == 0 )
{
int result = 0;
if(iMA(NULL, 0, 13, 0, MODE_SMA, PRICE_TYPICAL, 2) > iMA(NULL, 0, 13, 0, MODE_SMA, PRICE_CLOSE, 3)) //Moving Average 2 bars ago > Moving Average 3 bars ago
{
result = OrderSend(Symbol(), OP_BUY, GetLots(), Ask, Slippage, 0, 0, "EagleWatch EA - AngryCCI", MagicNumber, 0, Blue);
if(result > 0)
{
TheStopLoss = 0;
TheTakeProfit = 0;
if(TakeProfit > 0) TheTakeProfit = Ask + TakeProfit * MyPoint;
if(StopLoss > 0) TheStopLoss = Ask - StopLoss * MyPoint;
int check1 = OrderSelect(result, SELECT_BY_TICKET);
int check2 = OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(TheStopLoss, Digits), NormalizeDouble(TheTakeProfit, Digits), 0, Green);
}
return(0);
}
if(iMA(NULL, 0, 13, 0, MODE_SMA, PRICE_TYPICAL, 0) < iMA(NULL, 0, 13, 0, MODE_SMA, PRICE_CLOSE, 1)) //Moving Average < Moving Average 1 bar ago
{
result = OrderSend(Symbol(), OP_SELL, GetLots(), Bid, Slippage, 0, 0, "EagleWatch EA - AngryCCI", MagicNumber, 0, Red);
if(result > 0)
{
TheStopLoss = 0;
TheTakeProfit = 0;
if(TakeProfit > 0) TheTakeProfit = Bid - TakeProfit * MyPoint;
if(StopLoss > 0) TheStopLoss = Bid + StopLoss * MyPoint;
int check3 = OrderSelect(result, SELECT_BY_TICKET);
int check4 = OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(TheStopLoss, Digits), NormalizeDouble(TheTakeProfit, Digits), 0, Green);
}
return(0);
}
}
for(int cnt = 0;cnt < OrdersTotal();cnt++)
{
int check5 = OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL &&
OrderSymbol() == Symbol() &&
OrderMagicNumber() == MagicNumber
)
{
if(OrderType() == OP_BUY)
{
if(TrailingStop > 0)
{
if(Bid-OrderOpenPrice() > MyPoint * TrailingStop)
{
if(OrderStopLoss() < Bid - MyPoint * TrailingStop)
{
int check6 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MyPoint, OrderTakeProfit(), 0, Green);
return(0);
}
}
}
}
else
{
if(TrailingStop>0)
{
if((OrderOpenPrice() - Ask) > (MyPoint * TrailingStop))
{
if((OrderStopLoss() > (Ask+MyPoint * TrailingStop)) || (OrderStopLoss() == 0))
{
int check7 = OrderModify(OrderTicket(), OrderOpenPrice(), Ask + MyPoint * TrailingStop, OrderTakeProfit(), 0, Red);
return(0);
}
}
}
}
}
}
return(0);
}
int TotalOrdersCount()
{
int result = 0;
for(int i=0;i < OrdersTotal();i++)
{
int check8 = OrderSelect(i, SELECT_BY_POS , MODE_TRADES);
if (OrderMagicNumber() == MagicNumber) result++;
}
return (result);
}
//+----------------------Incremental Lot Sizing----------------------+
double Lots = 0.05; //Lot Size If Not Incremental
double GetLots()
{
Lots=NormalizeDouble( (AccountBalance()*(Risk_P/(1000*StopLoss))),2 );
if(Lots==0.0) Lots = Min_Lots;
if(Lots>Max_Lots) Lots = Max_Lots;
return (Lots);
}
//+------------------------------------------------------------------+