Topic: Move Trades in Profit to Break Even

Hi,

can someone help me out how can i modding the eas that when a open order is xx pips in profit than modify the stop loss to break even?

Re: Move Trades in Profit to Break Even

Nobody an idea?

I have tried this.

   if(posType!=OP_FLAT && useBreakeven)
     {
      UpdateBE();
      UpdatePosition();
     }   

added this to onbar

and added this function.

void UpdateBE()
{
      bool modified;
      int lastError=0;
 for(int i = OrdersTotal() - 1; i >= 0; i--)
   {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
       if(OrderType() == OP_BUY && OrderOpenPrice() > OrderStopLoss() && Bid - OrderOpenPrice() >= Breakeven * _Point)
       {
        modified=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0);
         lastError=GetLastError();
       }
           
      if(OrderType() == OP_SELL && OrderOpenPrice() < OrderStopLoss() && OrderOpenPrice() - Ask >= Breakeven * _Point)
       {
        modified=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0);
        lastError=GetLastError();
       }
      }
    }
}

but it seems not working..what wrong here?

also input useBreakeven and int Breakeven was added as input parameters... so if an open order have reached xx pips( Breakeven variable) than set SL to OrderOpenprice.

Re: Move Trades in Profit to Break Even

Now i have tried to implement from this to ea studio..


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   void ActionTrade::SetBreakEvenStop()
     {
      if(SetAggregatePosition(position)<=0)
         return;

      double breakeven=stopLevel;
      if(breakeven<breakEven)
         breakeven=breakEven;

      double breakprice = 0; // Break Even price including commission.
      double commission = 0; // Commission in points.
      if(position.Commission!=0)
         commission=MathAbs(position.Commission)/MarketInfo(_Symbol,MODE_TICKVALUE);

      double bid = MarketInfo(_Symbol, MODE_BID);
      double ask = MarketInfo(_Symbol, MODE_ASK);

      if(position.PosType==OP_BUY)
        {
         breakprice=NormalizeEntryPrice(position.OpenPrice+
                                        _Point*commission/position.Lots);
         if(bid-breakprice>=_Point*breakeven)
           {
            if(position.StopLossPrice<breakprice)
              {
               if(WriteLogFile)
                  logger.WriteLogRequest("SetBreakEvenStop",
                                         "BreakPrice="+DoubleToString(breakprice,_Digits));

               ModifyPosition(breakprice,position.TakeProfitPrice);

               Print("SetBreakEvenStop(",breakEven,
                     ") set StopLoss to ",DoubleToString(breakprice,_Digits),
                     ", Bid=",DoubleToString(bid,_Digits));
              }
           }
        }
      else if(position.PosType==OP_SELL)
        {
         breakprice=NormalizeEntryPrice(position.OpenPrice -
                                        _Point*commission/position.Lots);
         if(breakprice-ask>=_Point*breakeven)
           {
            if(position.StopLossPrice==0 || position.StopLossPrice>breakprice)
              {
               if(WriteLogFile)
                  logger.WriteLogRequest("SetBreakEvenStop","BreakPrice="+
                                         DoubleToString(breakprice,_Digits));

               ModifyPosition(breakprice,position.TakeProfitPrice);

               Print("SetBreakEvenStop(",breakEven,") set StopLoss to ",
                     DoubleToString(breakprice,_Digits),
                     ", Ask=",DoubleToString(ask,_Digits));
              }
           }
        }
     }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

fromhttps://github.com/PopovMP/FSB_Expert_A … Trade4.mqh


ADDED now that into eastudio

  //+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double NormalizeEntryPrice(double price)
     {
      double tickSize=MarketInfo(_Symbol,MODE_TICKSIZE);
      if(tickSize!=0)
         return (NormalizeDouble(MathRound(price / tickSize) * tickSize, _Digits));
      return (NormalizeDouble(price, _Digits));
     }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+  
  void ManageBreakEven()
     {
      double breakeven=Breakeven;

      double breakprice = 0; // Break Even price including commission.
   [color=red]   double commission = OrderCommission(); // Commission in points.[/color]
   [color=red]      commission=MathAbs(commission)/MarketInfo(_Symbol,MODE_TICKVALUE);[/color]

      double bid = MarketInfo(_Symbol, MODE_BID);
      double ask = MarketInfo(_Symbol, MODE_ASK);

      if(OrderType()==OP_BUY)
        {
[color=red]         breakprice=NormalizeEntryPrice(OrderOpenPrice()+
                                        _Point*commission/Entry_Amount);[/color]
         if(bid-breakprice>=_Point*breakeven)
           {
            if(OrderStopLoss()<breakprice)
              {
               posStopLoss=breakprice;
               posTakeProfit=OrderTakeProfit();
               ModifyPosition();

               Print("SetBreakEvenStop(",breakeven,
                     ") set StopLoss to ",DoubleToString(breakprice,_Digits),
                     ", Bid=",DoubleToString(bid,_Digits));
              }
           }
        }
      else if(OrderType()==OP_SELL)
        {
         breakprice=NormalizeEntryPrice(OrderOpenPrice() -
                                        _Point*commission/Entry_Amount);
         if(breakprice-ask>=_Point*breakeven)
           {
            if(OrderStopLoss()==0 || OrderStopLoss()>breakprice)
              {
               posStopLoss=breakprice;
               posTakeProfit=OrderTakeProfit();
               ModifyPosition();

               Print("SetBreakEvenStop(",breakeven,") set StopLoss to ",
                     DoubleToString(breakprice,_Digits),
                     ", Ask=",DoubleToString(ask,_Digits));
              }
           }
        }
     }  
 //+------------------------------------------------------------------+
 //|                                                                  |
 //+------------------------------------------------------------------+

dont know what this with commission is but at first check it is working.

Added this in OnBar()

     if(posType!=OP_FLAT && useBreakeven)
     {
      ManageBreakEven();
      UpdatePosition();
     }  

but what to do that it works in on tick? put it in ontick?

Cause i dont know if it is really good idea..BE is 50pips away from entry point than the BE will be only modifiy on next bar. isnt it better to do with ontick?

Re: Move Trades in Profit to Break Even

You can easily do that:

Put your BreakEven call in the OnTick event handler.

Modify:

void OnTick()
  {
   if(Time[0]>barTime)
     {
      barTime=Time[0];
      OnBar();
     }
  }

to:

void OnTick()
  {
   if(Time[0]>barTime)
     {
      barTime=Time[0];
      OnBar();
     }
   else
    {
      UpdatePosition();
      ManageBreakEven();
    }
  }