Why So Serious
Why So Serious

Reputation: 1

mql4 envelope EA opens two trades on same bar?

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");
  }
//+------------------------------------------------------------------+

Upvotes: 0

Views: 316

Answers (2)

Ssempala Ibrahim
Ssempala Ibrahim

Reputation: 1

first the data here should be of a boolean data type int trade_opened = false; // To check that there are no open trades. Some times the code compiles but the code might no work as intended

Upvotes: 0

nyuletony
nyuletony

Reputation: 1

If I understood your query I think you are intending to have the program open one trade per bar. There are many approaches to it and I have applied this one which seem to be working. I don't know why the Stoploss is not working though.

//+------------------------------------------------------------------+
//|                                                      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

int trade_opened = false; // To check that there are no open trades(on 
the bar)
double bar_index; //To check the bar number

double ticket; //To check OrderSend function

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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
      if (!trade_opened && bar_index == 1)
      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);
      trade_opened = true;
     }

   if(signal=="buy"&&OrdersTotal()==0)
     {
      //send a buy order
      if (!trade_opened && bar_index == 1)      
      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);
      trade_opened = true;
     }

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

Upvotes: 0

Related Questions