OrderClose

 

I would like this code to close trades when the trailing stop reaches 100 points. 

Can anyone help?

 

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

     {

      if(OrderSelect(i,SELECT_BY_POS)==True)

        {

         if(OrderSymbol()==_Symbol && OrderType()==OP_BUY)

           {

            double sl = OrderStopLoss() >=100 * _Point;            

              {

               Print("Hey new tick buy sl");

               result = OrderClose(OrderTicket(), OrderLots(), sl, 2 );

              }

           }

         if(OrderSymbol()==_Symbol && OrderType()==OP_SELL)

           {

            double sl = OrderStopLoss() <=100 * _Point;            

              {

               Print("Hey new tick buy sl");

               result = OrderClose(OrderTicket(), OrderLots(), sl, 2 );

              }

           }



        }

     }
 

I am not sure what you mean by " When the trailing stop loss reaches 100 pips.    Normally, you would add a take  profit at 100 pips.  Then the order will close.

A trailing stop loss of 100 pips would mean the price would need to increase by 100 pips.  When it does, The stop loss would now be at the original "buy"  or "sell" price.

It is a simple thing to add a take profit to your order when you open a position.

Here is the page.  The sixth element in the position open is the take profit point.

https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade/ctradepositionopen
Documentation on MQL5: Standard Library / Trade Classes / CTrade / PositionOpen
Documentation on MQL5: Standard Library / Trade Classes / CTrade / PositionOpen
  • www.mql5.com
PositionOpen(const string,ENUM_ORDER_TYPE,double,double,double,double,const string) - CTrade - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Thank you for responding  Chris Pinter.  I have a code that moves the stopLoss and TakeProfit with an amount of ticks up or down.  When the market makes a tick in the upword direction on a buy trade the stop moves up. When the market makes a tick in the downword direction the tp moves down.  What I want is for when the stop gets to an amount say 100 points from the open price the order will close the position.
Chris Pinter
Chris Pinter
  • 2023.02.22
  • www.mql5.com
Trader's profile
 

As per Forum rules and recommendations, please edit your post (don't create a new post) and replace your code properly (with "</>" or Alt-S), or attach the original file directly with the "+ Attach file" button below the text box.

NB! Very important! DO NOT create a new post. EDIT your original post.

 
Well I got it working by myself with this code.
for(int i=OrdersTotal()-1; i >= 0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS)==True)
        {
         if(OrderSymbol()==_Symbol && OrderType()==OP_BUY)
           {
           double sl = OrderOpenPrice() - StopOut * _Point;
           if(OrderStopLoss()>sl)
                                     
              {
               result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
              }
           }
           if(OrderSymbol()==_Symbol && OrderType()==OP_SELL)
           {
           double sl = OrderOpenPrice() + StopOut * _Point;
           if(OrderStopLoss()<sl)
                                     
              {
               result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
              }
          }
       }
     }
Reason: