how to code this in MQL4 "if total opened positions = 2, set buy stop position" ?

 

Hi,

I need a help, I'm still learning MQL4, but I Stuck.

how to code this in MQL4  "if total opened positions = 2, set sell stop position" ?

I use this code but not working properly!

for(int i=OrdersTotal()-1; i>=0; i--)           
  {
  if(!OrderSelect(2,SELECT_BY_POS,MODE_TRADES))  continue;
  if(OrderType() !=OP_SELL+OP_SELL) continue;
  OrderSend (_Symbol,OP_BUYSTOP,0.02,Ask+400*_Point,3,Ask-400*_Point,Ask+800*_Point,NULL,0,0,Green);
  }
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The...
 
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2.   if(!OrderSelect(2,SELECT_BY_POS,MODE_TRADES))  continue;
    
    You are selecting the same order each time — use your loop variable i.
  3.   if(OrderType() !=OP_SELL+OP_SELL) continue;
    This make no sense. Check for a buy or a sell.

  4. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number 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
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  5. Count them. Then after the loop, test your count for your opening.

  6.   OrderSend (_Symbol,OP_BUYSTOP,0.02,Ask+400*_Point,3,Ask-400*_Point,Ask+800*_Point,NULL,0,0,Green);
    
    You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP is longer. Don't you want the same/specified amount for either direction?
    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25
    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (Control-O) → charts → Show ask line.)

  7. Check your return codes for errors, and report them including GLE/LE and your variable values. Don't look at GLE/LE unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

 
ar.ahmed:

Hi,

I need a help, I'm still learning MQL4, but I Stuck.

how to code this in MQL4  "if total opened positions = 2, set sell stop position" ?

I use this code but not working properly!

Use the Calculate Current Orders function from the Moving Average expert sample provided with mt4 and : if(CalculateCurrentOrders(Symbol())==2)
   OrderSend.....
 
William Roeder:
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. You are selecting the same order each time — use your loop variable i.
  3. This make no sense. Check for a buy or a sell.

  4. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number 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
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  5. Count them. Then after the loop, test your count for your opening.

  6. You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP is longer. Don't you want the same/specified amount for either direction?
    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25
    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (Control-O) → charts → Show ask line.)

  7. Check your return codes for errors, and report them including GLE/LE and your variable values. Don't look at GLE/LE unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

Thank you Mr. William
 
Catalin Zachiu:
Use the Calculate Current Orders function from the Moving Average expert sample provided with mt4 and : if(CalculateCurrentOrders(Symbol())==2)
   OrderSend.....

Thank you Mr. Catalin,  can you please share the code to get understand more?

 

I tried this code, but now the problem is >> it opens unlimited number of buy stop positions ! I want to open only one buy stop position!

///////////////////////////////////////////////
// if open orders =2 , set buy stop position //
///////////////////////////////////////////////
openCount=0;
for( pos = OrdersTotal()-1; pos >= 0 ; pos--) 
if (
    OrderSelect(pos, SELECT_BY_POS)            // Only my orders 
&&  OrderType()        <= OP_SELL              // Only open orders
&&  OrderMagicNumber() == 0                    // my magic number
&&  OrderSymbol()      == Symbol() )           // and period and symbol
   {openCount++;}
   
if (openCount == 2)    
for(pos = OrdersTotal()-1; pos >= 0 ; pos--)
if (
    OrderSelect(pos, SELECT_BY_POS)            // Only my orders 
&&  OrderMagicNumber() == 0                    // my magic number
&&  OrderSymbol()      == Symbol() )           // and period and symbol
    OrderSend (_Symbol,OP_BUYSTOP,0.02,Ask+400*_Point,3,Ask-400*_Point,Ask+800*_Point,NULL,0,0,Green);
 
ar.ahmed: I tried this code, but now the problem is >> it opens unlimited number of buy stop positions ! I want to open only one buy stop position!

You put OrderSend inside a loop, therefor it opens multiple positions.

 
William Roeder:

You put OrderSend inside a loop, therefor it opens multiple positions.

i put it outside the loop, but still opens multiple positions.

i think it's because the conditions are still reached ! but how to do order only one time even the conditions are reached !


///////////////////////////////////////////////
// if open orders =2 , set buy stop position //
///////////////////////////////////////////////
int openCount2=0;
for(pos = OrdersTotal()-1; pos >= 0 ; pos--) 
if (
    OrderSelect(pos, SELECT_BY_POS)            // Only my orders 
&&  OrderType()        <= OP_SELL              // Only open orders
&&  OrderMagicNumber() == 0                    // my magic number
&&  OrderSymbol()      == Symbol()             // and period and symbol
   )         
    {openCount2++;}
 
if  (openCount2 == 2)
    OrderSend (_Symbol,OP_BUYSTOP,0.02,Ask+400*_Point,3,Ask-400*_Point,Ask+800*_Point,NULL,0,0,Green);
 

You need to check the stop orders too, and only place a stop order when you got 2 market orders and 0 stop orders:

int openCount=0;
int openStopCount=0;

for(pos = OrdersTotal()-1; pos >= 0 ; pos--)
 {
   if (OrderSelect(pos, SELECT_BY_POS) &&         // Only my orders :
       OrderMagicNumber() == 0         &&         // my magic number
       OrderSymbol()      == _Symbol)             // and my symbol
    {
      if(OrderType() <= OP_SELL)                  // count market orders
         openCount++;
      else
      if(OrderType() >= OP_BUYSTOP)               // count stop orders
         openStopCount++;
    }
 }
if(openCount == 2 && openStopCount == 0)
    OrderSend (_Symbol,OP_BUYSTOP,0.02,Ask+400*_Point,3,Ask-400*_Point,Ask+800*_Point,NULL,0,0,Green);