if(!AccountInfoInteger(ACCOUNT_TRADE_EXPERT))
Alert("Automated trading is forbidden for the account ",AccountInfoInteger(ACCOUNT_LOGIN),
" at the trade server side");
and got no error message.
When the EA detects an Open position, (in demo) it correctly sets my SL to 7.5 and my TP to 15.0. It also places a Limit order 3.5 away to close half the position (trading futures). When price reaches that target, the EA then moves SL to Open +/- 0.25.
However, in Live account, the EA gives error 4756 and retcode 10016 and it seems to be trying to set the SL to 0.25 in profit and so far 3 trades have closed at 0.25 profit.
Okay, I have more info: The EA places the Limit order before modifying SL and TP. I also have a Global variable set to either 0 or 1, depending on if it finds the Limit order. On the demo, it places the Limit order and sets the Global variable to 1. After price reaches it, the variable is set back to 0. However, on Live account the Limit order disappears immediately (at least when placed using my original script; not sure if the EA places it), and the EA sets the Global variable to 1. This triggers my Break Even code to try to move the SL to 0.25 in profit. Hence, the error. I even opened the GV (F3) and set it to 0 and it immediately was reset to 1 with no Limit order visible on the screen or in the Trade tab.
Now, why would the code respond differently on a Live account than a Demo? And why does my Limit order seem to disappear? And how does the GV get set to 1 if the only code to change it, is if it finds the Limit order and the GV is 0, or after placing the Limit order only if the GV is 0?
int totalPend=OrdersTotal(); // total number of placed pending ordersint pendingTrades=0;
//--- iterate over all placed pending ordersfor(int j=0; j<totalPend; j++)
{
//--- parameters of the orderulong order_ticket=OrderGetTicket(j); // order ticketif(order_ticket>0)
pendingTrades++;
}
if(pendingTrades==1 && GlobalVariableGet("PartClosePlaced")==0)
GlobalVariableSet("PartClosePlaced",1);
if(pendingTrades==0 && GlobalVariableGet("PartClosePlaced")==0)
{
//--- zeroing the request and result valuesZeroMemory(request);
ZeroMemory(result);
//--- parameters to place a pending order
request.action =TRADE_ACTION_PENDING; // type of trade operation
request.symbol =Symbol(); // symbol
request.volume=pendingLots; // volume of 0.1 lot
request.deviation=2; // allowed deviation from the price//request.magic =EXPERT_MAGIC; // MagicNumber of the order//int offset = 50; // offset from the current price to place the order, in points//double price; // order triggering pricedouble point=SymbolInfoDouble(_Symbol,SYMBOL_POINT); // value of point//--- checking the type of operationif(type==POSITION_TYPE_BUY)
{
request.type=ORDER_TYPE_SELL_LIMIT; // order type
tradePrice=price+350*point;
//Print("tradePrice = ",tradePrice);//price=SymbolInfoDouble(Symbol(),SYMBOL_ASK)-offset*point; // price for opening
request.price=NormalizeDouble(tradePrice,digits); // normalized opening price
}
elseif(type==POSITION_TYPE_SELL)
{
request.type=ORDER_TYPE_BUY_LIMIT; // order type
tradePrice=price-350*point;
//Print("tradePrice = ",tradePrice);//price=SymbolInfoDouble(Symbol(),SYMBOL_ASK)+offset*point; // price for opening
request.price=NormalizeDouble(tradePrice,digits); // normalized opening price
}
elseAlert("This example is only for placing pending orders"); // if not pending order is selected//--- send the requestif(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code//--- information about the operationPrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
GlobalVariableSet("PartClosePlaced",1);
}
There is code to delete the Limit order but only if it finds no Open trades and the Limit order is only placed after there is an open trade.
if(openTrades==0)
{
ulong order_ticket=0;
int totalPend=OrdersTotal(); // total number of placed pending ordersint pendingTrades=0;
//--- iterate over all placed pending ordersfor(int j=0; j<totalPend; j++)
{
//--- parameters of the order
order_ticket=OrderGetTicket(j); // order ticket
typePend=(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE); // type of the orderif(order_ticket>0)// && (typePend==ORDER_TYPE_BUY_LIMIT || typePend==ORDER_TYPE_SELL_LIMIT))
pendingTrades++;
}
if((GlobalVariableGet("PartClosePlaced")==1 && pendingTrades==1 && (typePend==ORDER_TYPE_BUY_LIMIT || typePend==ORDER_TYPE_SELL_LIMIT)))
{
//--- zeroing the request and result valuesZeroMemory(request);
ZeroMemory(result);
//--- setting the operation parameters
request.action=TRADE_ACTION_REMOVE; // type of trade operation
request.order = order_ticket; // order ticket//--- send the requestif(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code//--- information about the operation PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
}
GlobalVariableSet("PartClosePlaced",0);
}// end if(openTrades==0)
I did run this script to check:
and got no error message.
When the EA detects an Open position, (in demo) it correctly sets my SL to 7.5 and my TP to 15.0. It also places a Limit order 3.5 away to close half the position (trading futures). When price reaches that target, the EA then moves SL to Open +/- 0.25.
However, in Live account, the EA gives error 4756 and retcode 10016 and it seems to be trying to set the SL to 0.25 in profit and so far 3 trades have closed at 0.25 profit.
Okay, I have more info: The EA places the Limit order before modifying SL and TP. I also have a Global variable set to either 0 or 1, depending on if it finds the Limit order. On the demo, it places the Limit order and sets the Global variable to 1. After price reaches it, the variable is set back to 0. However, on Live account the Limit order disappears immediately (at least when placed using my original script; not sure if the EA places it), and the EA sets the Global variable to 1. This triggers my Break Even code to try to move the SL to 0.25 in profit. Hence, the error. I even opened the GV (F3) and set it to 0 and it immediately was reset to 1 with no Limit order visible on the screen or in the Trade tab.
Now, why would the code respond differently on a Live account than a Demo? And why does my Limit order seem to disappear? And how does the GV get set to 1 if the only code to change it, is if it finds the Limit order and the GV is 0, or after placing the Limit order only if the GV is 0?
There is code to delete the Limit order but only if it finds no Open trades and the Limit order is only placed after there is an open trade.