Bailing out with break-even detection

Hi,

I noticed that a some of my no-good trades usually start with some profit but then turn for the wrong direction. I want to add a break-even detection and bail out of those trades just before they go negative. This is the code I was thinking of:

If the profit is very small but still positive and the 1-minute trend is against my trade I simply close my trade (by signaling CR_BREAKEVEN to my main control loop).

This algorithm is a bit soft, because it does not include a hard stoploss but rather it looks at the 1-minute trend. So as long as the M1 trend is going with my order I accept that the signal can still go negative.

// Bail out with break even

      // Determine current profit in pips
      if (OrderType() == OP_BUY)
        double ProfitPips = (Bid - OrderOpenPrice()) / MarketInfo(_Symbol,MODE_POINT);
      if (OrderType() == OP_SELL)
        double ProfitPips = (OrderOpenPrice() - Ask) / MarketInfo(_Symbol,MODE_POINT);

      if (ProfitPips > 0 && ProfitPips < 6) {
         double be_sma = iMA(gChart.symbol,PERIOD_M1,8,0,MODE_EMA,PRICE_CLOSE,0);
         double be_lma = iMA(gChart.symbol,PERIOD_M1,16,0,MODE_EMA,PRICE_CLOSE,0);
        
         // Determine direction of M1 trend
         int be_type;
         if (be_sma > be_lma) be_type = OP_BUY;
         else if (be_sma < be_lma) be_type = OP_SELL;
         else be_type = -1;
        
         if (be_type != OrderType()) {
            /* logging code deleted */
            return CR_BREAKEVEN; // Signal to main loop that order can be closed
         }      
      }

Is this a method for break-even detection that someone else is using. Am I inventing a better wheel here?
Any comments are welcome.

Edit: After including this trick in my EA I updated the code in this post so it is more like real code instead of psuedo code. My first results after a week of testing are very promising.

  1. it should be return(CR_BREAKEVEN);
  2. So it returns CR_BREAKEVEN, now what? There is no reference to the closeorder code.

The method is okay, execution requires some tweaks.

What I use is, but depends on your strategy:
if(dir == 1 && Low[1] > _orderopen) _sl = _orderopen;

Ordermodify(<parameters>,_sl,<parameters>);

roughly putted. There needs to be a order select and a for function as well.

Ah yes, my EA uses a function that does a heap of tests to see if there is a CloseReason hence CR_xxxx. If there is a CloseReason then in the main “do_manage” function the open order will be closed. Here I was just posting the minimum snippet of code that would show my break-even detection method.

Question 1: Why should it be “return (CR_BREAKEVEN);”?

In your codesnippet (which I think is great) i am wondering if you would not need a check to make sure that the current value (Ask/Bid) is at least _spread pips away from the intended _sl. Otherwise the new _sl might not be accepted.

Pseudo code:

if (_dir == OPBUY) {
  double lv_sldist = market.stoploss;
  double lv_minsl = _orderopen + lv_sldist;
  if (dir == OPBUY && Low[1] > _orderopen && Bid > lv_minsl) _sl = _orderopen;
  Ordermodify(<parameters>,_sl,<parameters>);
}

And something similar for SELL orders.

There needs to be some sort of grace period that allows the others to either bail by Bail Bond or quickly switch seats make it an audbible warning that the craft is pilotless.

if (_dir == OPBUY) {
double lv_sldist = market.stoploss;
double lv_minsl = _orderopen + lv_sldist;
if (dir == OPBUY && Low[1] > _orderopen && Bid > lv_minsl) _sl = _orderopen;
Ordermodify(,_sl,);
}

i would like to kw wht is “_sl” here…is it OrderStopLoss or OrderStopLevel or StopLoss???

and can u plz write the complete code…with OrderModify as well…thank u