Help me please about code MT4 Button

 

Help me

I'm writing code. Create a button to close all orders and Close only Profit order  Button

 close all button is working but  close only profit not working.



void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

   int _ticket=0;

   if(id== CHARTEVENT_OBJECT_CLICK && sparam=="closeall") // Close all has been pressed

     {

      int total = OrdersTotal();

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

  {

    OrderSelect(i, SELECT_BY_POS);

    int type   = OrderType();


    bool result = false;

    

    switch(type)

    {

      //Close opened long positions

      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Blue );

                          break;

      

      //Close opened short positions

      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

                          break;


      //Close pending orders

      case OP_BUYLIMIT  :

      case OP_BUYSTOP   :

      case OP_SELLLIMIT :

      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );

    }

    

    if(result == false)

    {

      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );

      Sleep(1000);

    }  

  }

  

   return ;   

   

      if(id== CHARTEVENT_OBJECT_CLICK && sparam=="closeprofit") // Close profit has been pressed

     {

           int total = OrdersTotal();

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

          bool res=OrderSelect(i, SELECT_BY_POS,MODE_TRADES);

            if(OrderProfit()>0)

          bool tic =OrderClose(OrderTicket(),OrderLots(),OrderOpenPrice(),5,clrWhite);

    

   }

   }

  }


I doing code . One button to change Take Profit Price every order. help me make it.

Thank you.

 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

Thank you.

 
Take a look at your brackets, for instance the one after the second forloop.. it's not there.
 
  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. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  3.  for(int i=total-1;i>=0;i--){
        OrderSelect(i, SELECT_BY_POS);
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
      For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.

  4. for(int i=OrdersTotal()-1;i>=0;i--)
              bool res=OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
                if(OrderProfit()>0)
  5.  bool tic =OrderClose(OrderTicket(),OrderLots(),OrderOpenPrice(),5,clrWhite);
    You close at the close price not the open price. #3.3