profit price problem

 

Hi I am new to mql4,

I am having a problem with this code It is to close an order by stop loss price or take profit price,.

Can someone tell me where I am going wrong here.

void CloseallOders17()
{

int total = OrdersTotal();
for(int i=total-1;i>=0;i--)

OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
int type = OrderType();

bool result = false;

( switch(type)

//Close opened long positions
case OP_BUY : if ( OrderProfit() >= Amaxtakeprofitprice) result = OrderClose( OrderTicket(),

//Close opened short positions
case OP_SELL : if ( OrderProfit() >= Amaxtakeprofitprice) result = OrderClose( OrderTicket(),

//Close opened long positions
case OP_BUY : if ( OrderProfit() >= Amaxstoplossprice) result = OrderClose( OrderTicket(),

//Close opened short positions
case OP_SELL : if ( OrderProfit() >= Amaxstoplossprice) result = OrderClose( OrderTicket(),
)
else
Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
break;
)

 

HI

you'r missing a lot of { and }...

here's an functon for closing all orders.

Hope this helps you in your programming! =)

void Fun_CloseAllOrders()
   {
      int      Total=OrdersTotal();
      int      Ticket;
      for (int i = 0; i < Total; i ++) 
      {
      
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) 
            {
               while(true)
               {
                  bool Ans;                                       //server response after closing
                  Ticket = OrderTicket();                         //Get ticket number
                  //------------Close buy
                                    
                  if(OrderType()==OP_BUY)
                     {
                        RefreshRates();
                        Ans = OrderClose(Ticket,OrderLots(), Bid, Slippage, Green);
                        if(Ans==True)
                           {
                              Print("CloseAll_Closed BUY ", Ticket);
                              break;
                           }
                        Print("Error: ", GetLastError());
                     }
                  if(OrderType()==OP_SELL)
                     {
                        RefreshRates();
                        Ans = OrderClose(Ticket,OrderLots(), Ask, Slippage, Green);
                        if(Ans==True)
                           {
                              Print("CloseAll_Closed SELL ", Ticket);
                              break;
                           }
                        Print("Error: ", GetLastError());
                        
                     }
                  break;   
               }
            }

      
      }
 
enotrek:

you'r missing a lot of { and }...

here's an functon for closing all orders.

Hope this helps you in your programming! =)

void Fun_CloseAllOrders()
   {
      int      Total=OrdersTotal();
      int      Ticket;
      for (int i = 0; i < Total; i ++) 

//...
Seems like u are missing quite a bit yourself. For the millionth time - the loop counter should decrement in a order closing loop. See here -> https://www.mql5.com/en/forum/119840/.
 
I guess until they fix the book, this issue will continue....

https://book.mql4.com/trading/orderclose


In order to demonstrate the method of using trade functions for closing of market orders, let's solve a problem:

Problem 28. Write a script that closes one of the market orders available on the account. The script execution must result in closing of the order closest to the location of the script attached to the symbol window with the mouse.
.
The books solution...


   for (int i=1; i<=OrdersTotal(); i++)       //Cycle for all orders..
     {                                        //displayed in the terminal
      if(OrderSelect(i-1,SELECT_BY_POS)==true)//If there is the next one
        {                                     
         // Order characteristics..
         // ..must be analyzed here 
        }
     }                                        //End of the cycle body

 
Viffer:
I guess until they fix the book, this issue will continue....

https://book.mql4.com/trading/orderclose

The books solution...


what book did you get that from? in my mql4 book the close all orders loop is like this:

for(int i=1; i<=Mas_Ord_New[0][0]; i++)// Cycle for live orders

I believe it works correctly like this because the total orders is held in that array index, which remains unaltered as each order is closed

Edit: I see now you were looking at a different section of the book to me it does it a different way in that section ..

 

I am having a problem with this code It is to close an order by stop loss price or take profit price,.

Can someone tell me where I am going wrong here.


  1. void CloseallOders17() {
    
    int total = OrdersTotal();
    for(int i=total-1;i>=0;i--)
    
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
    int type = OrderType();
    

    Always check return codes

    for(int pos = OrdersTotal() - 1; pos >= 0; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)             // Only my orders w/
    &&  OrderMagicNumber()  == Magic.Number         // my magic number
    &&  OrderSymbol()       == Symbol() ){          // and period and symbol
    
    And yes, you must count down when closing orders

  2.  //Close opened long positions
    case OP_BUY : if ( OrderProfit() >= Amaxtakeprofitprice) result = OrderClose( OrderTicket(),
    
    //Close opened short positions
    case OP_SELL : if ( OrderProfit() >= Amaxtakeprofitprice) result = OrderClose( OrderTicket(),
    
    //Close opened long positions
    case OP_BUY : if ( OrderProfit() >= Amaxstoplossprice) result = OrderClose( OrderTicket(),
    //Close opened short positions
    case OP_SELL : if ( OrderProfit() >= Amaxstoplossprice) result = OrderClose( OrderTicket(),
    
    Are there breaks between each case? OrderProfit() >= maxStop These are probably wrong, if you want to close when you get too far into negative profit. But isn't that the point of a stoploss?
 
SDC:


what book did you get that from? [...]

Same question to you...do you have the link to that section.


My link takes you to the page for " Closing and Deleting orders" where it describes function OrderClose(), function OrderDelete() and funtion OrderCloseby(). It refrences the following tutorial scripts,

https://c.mql5.com/mql4/book/mq4/scripts/closeorder.mq4

https://c.mql5.com/mql4/book/mq4/scripts/deleteorder.mq4

https://c.mql5.com/mql4/book/mq4/scripts/closeby.mq4

All 3 tutorials use...

for(int i=1; i<=OrdersTotal(); i++)


I have seen the method you posted but can't remember where it was in the book... I guess for this method, the critical part being "which remains unaltered as each order is close". Presumably that code section is demonstrating something other than order close. To create an array to replicate the existing order pool for the sole purpose of closing orders is unnecessary.

V