Download MetaTrader 5

what is the problem occuring when i run my ea on alpari MT5?

To add comments, please log in or register
Format text in the messages editor. It is convenient!
enbo lu
Moderator
1947
enbo lu 2012.08.13 16:40 

what i am doing  is : open a position and modify the tp ,there is no problem when i am backtesting, but when i run my ea on the demo account  of alpari mt5, it shows this error. 

where is the problem,any one knows?   why did PositionModify  happen  before PositionOpen ? What is 'Exchange buy'?

 

        if(!mtrade.PositionOpen(_Symbol,ORDER_TYPE_BUY,volum[0],mtick.ask,0,0,NULL))

              {

               Print("PositionOpen failed with error #",GetLastError());

               ResetLastError();

              }

            else

              {               

                  mtrade.PositionModify(_Symbol,0,mtick.ask+mtp[0]*_Point);

              } 

enbo lu
Moderator
1947
enbo lu 2012.08.14 03:00  
luenbo:

what i am doing  is : open a position and modify the tp ,there is no problem when i am backtesting, but when i run my ea on the demo account  of alpari mt5, it shows this error. 

where is the problem,any one knows?   why did PositionModify  happen  before PositionOpen ? What is 'Exchange buy'?

 

        if(!mtrade.PositionOpen(_Symbol,ORDER_TYPE_BUY,volum[0],mtick.ask,0,0,NULL))

              {

               Print("PositionOpen failed with error #",GetLastError());

               ResetLastError();

              }

            else

              {               

                  mtrade.PositionModify(_Symbol,0,mtick.ask+mtp[0]*_Point);

              } 

Who can help me , thanks a lot~!
Andriy Moraru
1160
Andriy Moraru 2012.08.14 09:00  

TP too close to Bid?

Or you should probably try passing PositionInfo.StopLoss() instead of 0 as stop-loss in your Modify order.

Alexey Da
Moderator
6606
Alexey Da 2012.08.14 09:23  

The reason is trying to modify position which has not opened yet (placed for execution doesn't mean done).

 

onewithzachy
2271
onewithzachy 2012.08.14 09:55  

Hi luenbo,

I read your topic before, but I forget that I actually got the answer for that 3 months ago, thanks to alexvd, now I remember (https://www.mql5.com/en/forum/6674#comment_193370) .

Luenbo, what's the error code ?, you check the error code for order send and you should do the same with order modify.

I think before modifying you should Sleep for about 50 ms. Try like this 

 if(!mtrade.PositionOpen(_Symbol,ORDER_TYPE_BUY,volum[0],mtick.ask,0,0,NULL))
   {
   Print("PositionOpen failed with error #",GetLastError());
   ResetLastError();
   }
   else
   {
   Sleep (50); 
   ResetLastError();              
   if (!mtrade.PositionModify(_Symbol,0,mtick.ask+mtp[0]*_Point))
       {
        Print("Position Modify failed with error #",GetLastError());
       }
   } 

 And please use SRC button to post code

 

 

ecn stops and 4756 error
ecn stops and 4756 error
  • www.mql5.com
deal, ", ", "Volume: ", Result.
enbo lu
Moderator
1947
enbo lu 2012.08.14 13:37  
onewithzachy:

Hi luenbo,

I read your topic before, but I forget that I actually got the answer for that 3 months ago, thanks to alexvd, now I remember (https://www.mql5.com/en/forum/6674#comment_193370) .

Luenbo, what's the error code ?, you check the error code for order send and you should do the same with order modify.

I think before modifying you should Sleep for about 50 ms. Try like this 

 And please use SRC button to post code

 

 

Thank you very much, onewithzachy! 
enbo lu
Moderator
1947
enbo lu 2012.08.14 13:54  

But i still have a question ,Why they don't accept sl/tp when the position is opening by OrderSend() or mtrade.PositonOpen() functon?

We have to set tp/sl by modifing the postion or sending a pending order

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
Alexey Petrov
Admin
1220
Alexey Petrov 2012.08.14 14:36  
luenbo:

But i still have a question ,Why they don't accept sl/tp when the position is opening by OrderSend() or mtrade.PositonOpen() functon?

We have to set tp/sl by modifing the postion or sending a pending order

You need to learn the difference between the execution modes. In the MqlTradeRequest section of help there is a description of how to fill this structure depending on the execution mode of the symbol.

When the execution mode is Market or Exchange, an exact price is not specified when performing market operations (i.e. the market orders are filled using the current market price). So in this case you cannot specify SL or TP since you do not know the exact price, at which the position will be opened.

If the execution mode is Instant or Request, the SL and TP can be specified along with opening a position.

enbo lu
Moderator
1947
enbo lu 2012.08.14 17:03  
onewithzachy:

Hi luenbo,

I read your topic before, but I forget that I actually got the answer for that 3 months ago, thanks to alexvd, now I remember (https://www.mql5.com/en/forum/6674#comment_193370) .

Luenbo, what's the error code ?, you check the error code for order send and you should do the same with order modify.

I think before modifying you should Sleep for about 50 ms. Try like this 

 And please use SRC button to post code

 

 

 Sleep for about 50 ms or more does not always work......

 I run my ea on three currency pairs, sometimes it works but sometimes does't . 

 Is there any other way to solve this problem ?

 

onewithzachy
2271
onewithzachy 2012.08.15 00:21  
luenbo:

 Sleep for about 50 ms or more does not always work......

 I run my ea on three currency pairs, sometimes it works but sometimes does't . 

 Is there any other way to solve this problem ?

Hi luenbo,

I guess different broker has different situation.

1. Check the error code for that order modify, see if we can do something about it. Some suggest to modify after 1 minute later, which I think not funny anymore.

2. Send 2 limit and stop opposite pending orders. For example, if we have open buy, we also open sell stop as stop loss and sell limit as take profit, all with same lot size. When one pending order is touched and opened, the open buy and that pending order will be both closed, however we have to delete the other pending order.

I prefer that because MT5 is position based and not ticket based (MT4 is ticket based). Meaning if we open 1 lot buy with SL and TP and 5, 7 pips later - or whatever - we open another buy with SL and TP, the opened buy price will be averaged and all SL and TP will be zero again, With MT5 we always have 1 open buy not 2 or 7 and the opened price will be averaged. Try that by manual trade. So, instead open position with SL and TP, we better open position with 2 opposite pending orders as SL and TP.

3. I haven't read all the documentation of MT5, but I hope MT5 support OCO (Orders cancel the Other), coz that exchange brokerage usually allow OCO.

So far this is I can think of.

 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
To add comments, please log in or register