Please i need help with my EA !!!! it keeps opening trades whenever sl has been hit even if the conditions to trade are not meet

 

my strategy is based on the envelope indicator :

-for a buy order a candle has to close above the envelope line

-for a sell order the candle has to close below the envelope line

The EA seems to enter trades based on those rules but whenever a candle closes above the envelope line and the EA enters a buy trade if the sl has been it immediately opens another trade without checking the rules for a buy or sell order

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+

input double StopLoss=50.0;    // Stop Loss in pips
input double TakeProfit=100.0; // Take Profit in pips



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {

//create an empty string for the signal
   string signal="";

//symbol, period,14 candles,SMA,no shift,close price,deviation 0.10,buffer line,candle 0
   double LowerBand=iEnvelopes(_Symbol,_Period,14,MODE_SMA,0,PRICE_CLOSE,0.10,MODE_LOWER,0);
   double UpperBand=iEnvelopes(_Symbol,_Period,14,MODE_SMA,0,PRICE_CLOSE,0.10,MODE_UPPER,0);

//if close price is below lower band
   if(Close[1]<LowerBand)
     {
      signal="sell";
     }

//if close price is above upper band
   if(Close[1]>UpperBand)
     {
      signal="buy";
     }

   if(signal=="sell"&&OrdersTotal()==0)
     {
      //send a sell order
      double sellSL=Bid+StopLoss*_Point;
      double sellTP=Bid-TakeProfit*_Point;
      OrderSend(_Symbol,OP_SELL,0.10,Bid,3,sellSL,sellTP,NULL,0,0,Red);
     }

   if(signal=="buy"&&OrdersTotal()==0)
     {
      //send a buy order
      double buySL=Ask-StopLoss*_Point;
      double buyTP=Ask+TakeProfit*_Point;
      OrderSend(_Symbol,OP_BUY,0.10,Ask,3,buySL,buyTP,NULL,0,0,Green);
     }

// create a chart output
   Comment("BLZ TRADING EA");
  }
//+------------------------------------------------------------------+
 
Why So:
if(Close[1]<LowerBand && Open[1]>LowerBand)
     {
      signal="sell";
     }
if(Close[1]>UpperBand && Open[1]<UpperBand)
     {
      signal="buy";
     }

 
Catalin Zachiu #:
i updated the code but  still same problem !
 
Why So #:
i updated the code but  still same problem !

it happens because your stoploss is too small , with your current conditions , the stop or TP can be triggered more than once on the same candle , you can add another condition to the EA so that the orders are opened in an alternative way :

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+

input double StopLoss=50.0;    // Stop Loss in pips
input double TakeProfit=100.0; // Take Profit in pips

string lastSignal;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   int ticket;

//create an empty string for the signal
   string signal="";

//symbol, period,14 candles,SMA,no shift,close price,deviation 0.10,buffer line,candle 0
   double LowerBand=iEnvelopes(_Symbol,_Period,14,MODE_SMA,0,PRICE_CLOSE,0.10,MODE_LOWER,0);
   double UpperBand=iEnvelopes(_Symbol,_Period,14,MODE_SMA,0,PRICE_CLOSE,0.10,MODE_UPPER,0);

//if close price is below lower band
   if(Close[1]<LowerBand && Open[1]>LowerBand)
     {
      signal="sell";
     }

//if close price is above upper band
   if(Close[1]>UpperBand && Open[1]<UpperBand)
     {
      signal="buy";
     }

   if(signal=="sell"&&OrdersTotal()==0 && lastSignal!="sell")
     {
      lastSignal="sell";
      //send a sell order
      double sellSL=Bid+StopLoss*_Point;
      double sellTP=Bid-TakeProfit*_Point;
      ticket=OrderSend(_Symbol,OP_SELL,0.10,Bid,3,sellSL,sellTP,NULL,0,0,Red);
     }

   if(signal=="buy"&&OrdersTotal()==0 && lastSignal!="buy")
     {
      lastSignal="buy";
      //send a buy order
      double buySL=Ask-StopLoss*_Point;
      double buyTP=Ask+TakeProfit*_Point;
      ticket=OrderSend(_Symbol,OP_BUY,0.10,Ask,3,buySL,buyTP,NULL,0,0,Green);
     }

// create a chart output
   Comment("BLZ TRADING EA");
  }
//+------------------------------------------------------------------+
 
Catalin Zachiu #:

it happens because your stoploss is too small , with your current conditions , the stop or TP can be triggered more than once on the same candle , you can add another condition to the EA so that the orders are opened in an alternative way :

do u know a way to limit the EA to only take one trade per candle so to avoid been triggered more than once on the same candle !!
 
  1. Why So #: do u know a way to limit the EA to only take one trade per candle so to avoid been triggered more than once on the same candle !!

    If you had used this, you would have found this:
        How can I search for indicators and other elements in this forum? - MQL5 programming forum #1 (2017) #10 (2021)
        How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
              RTFM and STFW: How To Tell You've Seriously Screwed Up.

  2. You are looking at a signal. Act on a change of signal.
              Too many orders - MQL4 programming forum #1 (2017)

 
Why So #:
do u know a way to limit the EA to only take one trade per candle so to avoid been triggered more than once on the same candle !!

Yes , check the conditions only one time , when first tick arrives . Do you want to open a buy order, for example , even if the whole candle is above the evelope , or only if it crosses it ?

 
Catalin Zachiu #:

Yes , check the conditions only one time , when first tick arrives . Do you want to open a buy order, for example , even if the whole candle is above the evelope , or only if it crosses it ?

i want to open a buy order when the whole candle closes above not just cross it and closed back inside the lines it must close above do u get what i mean
 
Also… your “signal” variable, is changing in either “buy” or “sell”… there should be a third option where there is no buy or sell conditions … for example if buy condition is met, signal variable will change to “buy”, but then if the price will go beyond upperband, you should change that signal value to “none” , otherwise you will keep open buy trades 
 
Daniel Cioca #:
Also… your “signal” variable, is changing in either “buy” or “sell”… there should be a third option where there is no buy or sell conditions … for example if buy condition is met, signal variable will change to “buy”, but then if the price will go beyond upperband, you should change that signal value to “none” , otherwise you will keep open buy trades 

its seems that was he solution was looking for !! how can add that third option ??

 
Why So #:

its seems that was he solution was looking for !! how can add that third option ??

Add an “else “to your if statement … if(something) signal=“buy”; else if(something)signal=“sell”;else signal=“none”;
Reason: