I have a semi-auto EA which will open a new order if the user's manual order open profit >= e.g.10 pip, but the EA did not open the order even though my open order is already profiting +50 pips. Can anyone help and identify what is wrong with the code? Thanks in advance
Below are some snippets:
voidOnTick()
{
....
// ------- OPEN ORDER -------// 3. Check if LAST ORDER BY USER (same pair) OPEN PROFIT >= Open_Profit. Note: manual open order magic number = 0if(LastOrderOpenProfit(0) >= Open_Profit * myPoint)
{
ticket = GetLastOrder(0);
myAlert("print", "Last order by user is profittable. OpenProfit*mypoint:" + (Open_Profit * myPoint) +" .Attempting to copy open order ticket: " + ticket);
if(OrderSelect(ticket, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType() % 2 == 0) //buy
{
RefreshRates();
price = Ask;
SL = StopLoss * myPoint; //Stop Loss = value in points (relative to price)
TP = TakeProfit * myPoint; //Take Profit = value in points (relative to price) if(UseRiskMM) TradeSize = MM_Size(SL);
else TradeSize = LotSize;
if(IsTradeAllowed())
{
ticket = myOrderSend(OP_BUY, price, TradeSize, "");
if(ticket <= 0) return;
}
else//not autotrading => only send alert
{
myAlert("order", "");
}
myOrderModifyRel(ticket, 0, TP);
myOrderModifyRel(ticket, SL, 0);
}
else//sell
{
RefreshRates();
price = Bid;
SL = StopLoss * myPoint; //Stop Loss = value in points (relative to price)
TP = TakeProfit * myPoint; //Take Profit = value in points (relative to price) if(UseRiskMM) TradeSize = MM_Size(SL);
else TradeSize = LotSize;
if(IsTradeAllowed())
{
ticket = myOrderSend(OP_SELL, price, TradeSize, "");
if(ticket <= 0) return;
}
else//not autotrading => only send alert
{
myAlert("order", "");
}
myOrderModifyRel(ticket, 0, TP);
myOrderModifyRel(ticket, SL, 0);
}
}
}
.....
}
double LastOrderOpenProfit(int magic)
{
double profit = 0.0;
datetime time = 0;
myAlert("print", "In LastOrderOpenProfit() function...");
for(int i=OrdersTotal()-1; i>=0; --i)
{
if(OrderSelect(i, SELECT_BY_POS))
{
if(OrderSymbol() == Symbol()
&& OrderMagicNumber() == magic
&& OrderOpenTime() > time )
{
time = OrderOpenTime();
profit = OrderProfit();
myAlert("print", "Checked selected order same symbol/magicnumber and opentime> previous. MagicNumber:" + magic + ". Ticket: " +OrderTicket());
}
}
}
return profit;
}
int GetLastOrder(int magic)
{
double profit = 0.0;
datetime time = 0;
for(int i=OrdersTotal()-1; i>=0; --i)
{
if(OrderSelect(i, SELECT_BY_POS))
{
if(OrderSymbol() == Symbol()
&& OrderMagicNumber() == magic
&& OrderOpenTime() > time )
{
returnOrderTicket();
}
}
}
return -1;
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Hello everyone,
I have a semi-auto EA which will open a new order if the user's manual order open profit >= e.g.10 pip, but the EA did not open the order even though my open order is already profiting +50 pips. Can anyone help and identify what is wrong with the code? Thanks in advance
Below are some snippets: