Download MetaTrader 5

Order problems

To add comments, please log in or register
Hundreds of articles will help you to create a trading robot. Come and read them!
Franco
374
Franco 2010.06.13 00:03 

Hey there,

I'm experiencing some order problems.  Let me explain.  When a condition is met, a order is placed.  Lets say it is a sell order.  Now, when a buy condition is met, while the previous order is still open, and a buy order is executed, it cancels out the previous order(it closes the previous order, and does not open a new buy order.  It acts like the stop loss or take profit for the previousorder.)

I don't know why this is happening, but I would really love to find a solution to this problem.  Here is my code:

The Ctrade trade function is imported from the included file that comes with MT5, called Trade.mqh 

 Would appreciate any input into this issue.   

SymbolInfoTick(_Symbol,latest_price);
CTrade trade;
                          
double bsl = NormalizeDouble(latest_price.ask - 800*_Point,_Digits);
double btp = NormalizeDouble(latest_price.ask + 200*_Point,_Digits);     

double ssl = NormalizeDouble(latest_price.bid + 800*_Point,_Digits);
double stp = NormalizeDouble(latest_price.bid - 200*_Point,_Digits); 

  
if(EMA_Cross(emafast,emaslow,emashift,tf) == "CrossedUp") 
{
trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.1,latest_price.ask,bsl,btp,"");
bars = Bars(_Symbol,tf);
}    
                               
if(EMA_Cross(emafast,emaslow,emashift,tf) == "CrossedDown") 
{
trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,0.1,latest_price.bid,ssl,stp,"");
bars = Bars(_Symbol,tf);
}
 
Franco
374
Franco 2010.06.13 00:16  

 Great example look at the pic, those orders placed did not have a stop loss or take profit, every time a condition is met they open and close the order.

Look all the orders are connected 

Franco
374
Franco 2010.06.13 00:42  
I see there is no orderclose function in mt5, I'm really confused here how on earth does this work?
Samuel Olowoyo
2241
Samuel Olowoyo 2010.06.13 14:07  

Hi Saidar,

From my little understanding, when you place a Buy trade and you want to close the trade (as in MQL4), what you are doing is selling back what you bought earlier(remember you are trading a currency pair - buying one and selling the order). So to close a Buy trade, you place a sell without stop loss or take profit. That is, sp=0, and tp=0; 

It is true that there is no separate orderclose() function in MQL5 as you have in MQL4. But as I have explained, to close an opened SELL position, place a BUY (with sl=0 and tp=0) and to close an opened BUY position, place a SELL (with sl=0 and tp=0).

Concerning your code, I want you to consider the advice you were given in the earlier thread you opened. Try and revert to checking MA crossing on bars 1 and 2. That is the best way to be accurate.

Take care. 

Franco
374
Franco 2010.06.13 15:26  

Thanks for the advice...

This new method does not make much sense to me but I will see what I can do to make this work

Franco
374
Franco 2010.06.13 15:29  

What is happening now is it does not matter if I have an order with a sl and tp, if I open an order that is the opposite of the previous order it is cancelled out.

At some instances where two buy orders are open, and a sell order goes though, it sells both the buy orders, where as I never programmed it to buy or sell more than 0.1 lots. 

Samuel Olowoyo
2241
Samuel Olowoyo 2010.06.13 16:48  
Saidar:

What is happening now is it does not matter if I have an order with a sl and tp, if I open an order that is the opposite of the previous order it is cancelled out.

At some instances where two buy orders are open, and a sell order goes though, it sells both the buy orders, where as I never programmed it to buy or sell more than 0.1 lots. 

Hi Saidar,

The issue here is the management of opened positions. Your code does not have a code to check for the presence of an opened position and what to do with it if a new position is discovered. That is the case here. 

I am actually putting together a comprehensive article on order and position management will will be available very soon. But before then, using your past experience in MQL4, see how you can introduce  an order management code that will decide what to do when a position is already opened and other position is set (especially when your code allows opening of multiple positions.)

Maybe if you do this, you will find a way out of the present problem.

Take care 

Franco
374
Franco 2010.06.13 20:21  

Thanks olowsam, think I will wait for that article because I am utterly confused.

I cannot find any example that can help me so I am stuck atm.

 I have looked that MA ADX EA that you uploaded, exactly the same is happening there that is the problem I am experiencing.

What I get is that you need to find an open order, but how do you send a new order without cancelling out the previous one ? 

Vladimir
5905
Vladimir 2010.06.14 01:43  
Saidar:

Hey there,

I'm experiencing some order problems.  Let me explain.  When a condition is met, a order is placed.  Lets say it is a sell order.  Now, when a buy condition is met, while the previous order is still open, and a buy order is executed, it cancels out the previous order(it closes the previous order, and does not open a new buy order.  It acts like the stop loss or take profit for the previousorder.)

I don't know why this is happening, but I would really love to find a solution to this problem.  Here is my code:

The Ctrade trade function is imported from the included file that comes with MT5, called Trade.mqh 

 Would appreciate any input into this issue.   

I had a similar experience trying to work with individual orders in MQL5. MQL5 considers all orders to be part of a position. If you send a BUY order, a long position is opened. When you send a sell order, you sell out of your long position. If your sell size is the same as your buy size, the position is closed. It is a big change for those who used to program in MQL4. For example, I like to open several BUY orders at different times, with their individual stoplosses. In MQL5 these orders become a part of a single long position with common SL and TP. I am yet to find out how the SL and TP of the combined position are calculated based on the SL and TP of individual orders. May be somebody who knows this stuff can enlighten us. I was told that the only way to track individual orders is by using the Virtual Order Manager:

https://www.mql5.com/en/articles/88

I haven't used it yet. So, please, let me know if it does what you want.  

A Virtual Order Manager to track orders within the position-centric MetaTrader 5 environment
  • 2010.05.13
  • Paul
  • www.mql5.com
This class library can be added to an MetaTrader 5 Expert Advisor to enable it to be written with an order-centric approach broadly similar to MetaTrader 4, in comparison to the position-based approach of MetaTrader 5. It does this by keeping track of virtual orders at the MetaTrader 5 client terminal, while maintaining a protective broker stop for each position for disaster protection.
Franco
374
Franco 2010.06.14 12:14  
gpwr:

I had a similar experience trying to work with individual orders in MQL5. MQL5 considers all orders to be part of a position. If you send a BUY order, a long position is opened. When you send a sell order, you sell out of your long position. If your sell size is the same as your buy size, the position is closed. It is a big change for those who used to program in MQL4. For example, I like to open several BUY orders at different times, with their individual stoplosses. In MQL5 these orders become a part of a single long position with common SL and TP. I am yet to find out how the SL and TP of the combined position are calculated based on the SL and TP of individual orders. May be somebody who knows this stuff can enlighten us. I was told that the only way to track individual orders is by using the Virtual Order Manager:

https://www.mql5.com/en/articles/88

I haven't used it yet. So, please, let me know if it does what you want.  

Thanks gpwr,

I don't understand why they changed this really, but anyways, there must be a way to do it like we used to do it.  Will check it out thanks 

To add comments, please log in or register