Change SL when is in profit?

To add comments, please log in or register
Mitch Zanting
42
Mitch Zanting  
void OnTick()
  {
       
      double SlowMovingAverage = iMA(NULL,0,90,0,MODE_SMA,PRICE_CLOSE,0);
      
      double LastSlowMovingAverage = iMA(NULL,0,90,0,MODE_SMA,PRICE_CLOSE,1);
      
      double FastMovingAverage = iMA(NULL,0,15,0,MODE_SMA,PRICE_CLOSE,0);
      
      double LastFastMovingAverage = iMA(NULL,0,15,0,MODE_SMA,PRICE_CLOSE,1);
      
      //Crossing
      if ((LastFastMovingAverage < LastSlowMovingAverage)&&(FastMovingAverage > SlowMovingAverage) && OrdersTotal()<3)
      {
         int buyticket = OrderSend
   (
      Symbol(),
      OP_BUY,
      0.02,
      Ask,
      5,
      Ask-400*_Point,
      Ask+250*_Point,
      NULL,
      0,
      0,
      Green
   );
      }
      
      if ((LastFastMovingAverage > LastSlowMovingAverage)&&(FastMovingAverage < SlowMovingAverage) && OrdersTotal()<3)
      {
         int Sellticket = OrderSend
   (
      Symbol(),
      OP_SELL,
      0.02,
      Bid,
      5,
      Bid-400*_Point,
      Bid+250*_Point,
      NULL,
      0,
      0,
      Blue
   );
      }
      
  }

Hello everyone,

Im looking for a way to modify the SL if the trade is for example in 100 points profit.

Can someone help with the code i need..

Petr Nosek
3051
Petr Nosek  
Mitch Zanting:

Hello everyone,

Im looking for a way to modify the SL if the trade is for example in 100 points profit.

Can someone help with the code i need..

If nobody wants to help you, send me a PM and at the beginning of the next week I should have some time.
Oksana Machok
735
Oksana Machok  
Mitch Zanting:

Hello everyone,

Im looking for a way to modify the SL if the trade is for example in 100 points profit.

Can someone help with the code i need..

If you're talking about TrailingStop - code from MT4 Samples:

double TrailingStop=100; 
 for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         //--- long position is opened
         if(OrderType()==OP_BUY)
           {
            //--- should it be closed?
           
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
         else // go to short position
           {
            //--- should it be closed?
           
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
        }
     }
GrumpyDuckMan
882
GrumpyDuckMan  

Hello Friend,

I did something like this. I have not tested it yet. I hope it helps a little.

      //    Modify long
      Trade=OrderSelect(ClosePosition,SELECT_BY_TICKET);
      ask=Ask-OrderOpenPrice(); //
      TS=TrailingStop*Point;    //
      TP=ask+TS;

You need something like this too.

   double StopLevel=MarketInfo(NULL,MODE_STOPLEVEL)*_Point;
To add comments, please log in or register