<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Forex Software — Move Trades in Profit to Break Even]]></title>
	<link rel="self" href="https://forexsb.com/forum/feed/atom/topic/7518/" />
	<updated>2018-10-04T06:30:07Z</updated>
	<generator>PunBB</generator>
	<id>https://forexsb.com/forum/topic/7518/move-trades-in-profit-to-break-even/</id>
		<entry>
			<title type="html"><![CDATA[Re: Move Trades in Profit to Break Even]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/52461/#p52461" />
			<content type="html"><![CDATA[<p>You can easily do that:</p><p>Put your BreakEven call in the OnTick event handler.</p><p>Modify:<br /></p><div class="codebox"><pre><code>void OnTick()
  {
   if(Time[0]&gt;barTime)
     {
      barTime=Time[0];
      OnBar();
     }
  }</code></pre></div><p>to:</p><div class="codebox"><pre><code>void OnTick()
  {
   if(Time[0]&gt;barTime)
     {
      barTime=Time[0];
      OnBar();
     }
   else
    {
      UpdatePosition();
      ManageBreakEven();
    }
  }</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Popov]]></name>
				<uri>https://forexsb.com/forum/user/2/</uri>
			</author>
			<updated>2018-10-04T06:30:07Z</updated>
			<id>https://forexsb.com/forum/post/52461/#p52461</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Move Trades in Profit to Break Even]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/52458/#p52458" />
			<content type="html"><![CDATA[<p>Now i have tried to implement from this to ea studio..</p><br /><div class="codebox"><pre><code>//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   void ActionTrade::SetBreakEvenStop()
     {
      if(SetAggregatePosition(position)&lt;=0)
         return;

      double breakeven=stopLevel;
      if(breakeven&lt;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&gt;=_Point*breakeven)
           {
            if(position.StopLossPrice&lt;breakprice)
              {
               if(WriteLogFile)
                  logger.WriteLogRequest(&quot;SetBreakEvenStop&quot;,
                                         &quot;BreakPrice=&quot;+DoubleToString(breakprice,_Digits));

               ModifyPosition(breakprice,position.TakeProfitPrice);

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

               ModifyPosition(breakprice,position.TakeProfitPrice);

               Print(&quot;SetBreakEvenStop(&quot;,breakEven,&quot;) set StopLoss to &quot;,
                     DoubleToString(breakprice,_Digits),
                     &quot;, Ask=&quot;,DoubleToString(ask,_Digits));
              }
           }
        }
     }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+</code></pre></div><p>from<a href="https://github.com/PopovMP/FSB_Expert_Advisor_Code/blob/master/Forexsb.com/ActionTrade4.mqh">https://github.com/PopovMP/FSB_Expert_A … Trade4.mqh</a></p><br /><p>ADDED now that into eastudio</p><div class="codebox"><pre><code>  //+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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&gt;=_Point*breakeven)
           {
            if(OrderStopLoss()&lt;breakprice)
              {
               posStopLoss=breakprice;
               posTakeProfit=OrderTakeProfit();
               ModifyPosition();

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

               Print(&quot;SetBreakEvenStop(&quot;,breakeven,&quot;) set StopLoss to &quot;,
                     DoubleToString(breakprice,_Digits),
                     &quot;, Ask=&quot;,DoubleToString(ask,_Digits));
              }
           }
        }
     }  
 //+------------------------------------------------------------------+
 //|                                                                  |
 //+------------------------------------------------------------------+</code></pre></div><p>dont know what this with commission is but at first check it is working.</p><p>Added this in OnBar()</p><div class="codebox"><pre><code>     if(posType!=OP_FLAT &amp;&amp; useBreakeven)
     {
      ManageBreakEven();
      UpdatePosition();
     }  </code></pre></div><p>but what to do that it works in on tick? put it in ontick?</p><p>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?</p>]]></content>
			<author>
				<name><![CDATA[deadlef]]></name>
				<uri>https://forexsb.com/forum/user/9894/</uri>
			</author>
			<updated>2018-10-03T17:04:16Z</updated>
			<id>https://forexsb.com/forum/post/52458/#p52458</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Move Trades in Profit to Break Even]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/52456/#p52456" />
			<content type="html"><![CDATA[<p>Nobody an idea?</p><p>I have tried this.</p><div class="codebox"><pre><code>   if(posType!=OP_FLAT &amp;&amp; useBreakeven)
     {
      UpdateBE();
      UpdatePosition();
     }   </code></pre></div><p>added this to onbar</p><p>and added this function.</p><div class="codebox"><pre><code>void UpdateBE()
{
      bool modified;
      int lastError=0;
 for(int i = OrdersTotal() - 1; i &gt;= 0; i--)
   {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
       if(OrderType() == OP_BUY &amp;&amp; OrderOpenPrice() &gt; OrderStopLoss() &amp;&amp; Bid - OrderOpenPrice() &gt;= Breakeven * _Point)
       {
        modified=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0);
         lastError=GetLastError();
       }
           
      if(OrderType() == OP_SELL &amp;&amp; OrderOpenPrice() &lt; OrderStopLoss() &amp;&amp; OrderOpenPrice() - Ask &gt;= Breakeven * _Point)
       {
        modified=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0);
        lastError=GetLastError();
       }
      }
    }
}</code></pre></div><p>but it seems not working..what wrong here?</p><p>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.</p>]]></content>
			<author>
				<name><![CDATA[deadlef]]></name>
				<uri>https://forexsb.com/forum/user/9894/</uri>
			</author>
			<updated>2018-10-03T13:14:00Z</updated>
			<id>https://forexsb.com/forum/post/52456/#p52456</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Move Trades in Profit to Break Even]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/52333/#p52333" />
			<content type="html"><![CDATA[<p>Hi,</p><p>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?</p>]]></content>
			<author>
				<name><![CDATA[deadlef]]></name>
				<uri>https://forexsb.com/forum/user/9894/</uri>
			</author>
			<updated>2018-09-22T12:19:27Z</updated>
			<id>https://forexsb.com/forum/post/52333/#p52333</id>
		</entry>
</feed>
