More trades than expected

 

Hi guys. I have a strange phenomenom. I recently build a simple ea with multiple indicators. One of them gives a value (for a sell position). The strange thing now is, when I test the ea the trades get executet proberly but for some reason sometimes they get send for no apparent reason. When I check the data windows the value data is empty, but somehow the trades get sent anyway. Can someone help me with this?


#property strict

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }

void OnTick()
  {
double MTF15 = iMA(_Symbol,PERIOD_M15,50,0,MODE_EMA,PRICE_CLOSE,0);  
double MTF1H = iMA(_Symbol,PERIOD_H1,50,0,MODE_EMA,PRICE_CLOSE,0);
Print(DoubleToString(MTF15),"M15");
Print(DoubleToString(MTF1H),"1H");

double MacdDivBy = iCustom(_Symbol,_Period,"MacdDiv",0,0); 
double MacdDivSl = iCustom(_Symbol,_Period,"MacdDiv",1,0);
double MacdSl = iCustom(_Symbol,_Period,"MacdDiv",2,0);
double MacdFs = iCustom(_Symbol,_Period,"MacdDiv",3,0);
Comment("MacdDiv" + DoubleToString(MacdDivBy));

double SlSl = MathMax(iHigh(_Symbol,_Period,0),iHigh(_Symbol,_Period,5)) + 20 * _Point;
SlSl = NormalizeDouble(SlSl,_Digits);

double SlBy = MathMin(iLow(_Symbol,_Period,0),iLow(_Symbol,_Period,5)) - 20 * _Point;
SlBy = NormalizeDouble(SlBy,_Digits);

double TpSl = Bid - ((SlSl - Bid) * 2);
TpSl = NormalizeDouble(TpSl,_Digits);

double TpBy = Ask + ((TpSl + Ask) *2); 
TpBy = NormalizeDouble(TpBy,_Digits);

for(int i=OrdersTotal()-1; i>=0; i--)
      { int ticket = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrdersTotal() >= 1)
         {return;}}
         
if( MTF15 < MTF1H)
   { if( MacdDivSl > 0 )
      {if( MacdSl && MacdFs > 0)
         {int ticket = OrderSend(_Symbol,_Period,0.01,Bid,10000,SlSl,TpSl,NULL,1,0,0);
          MacdDivSl = 0;}}}
  }


 //  1. 15MTF < 1MTF = sell & 15 MTF > 1MTF = buy
 //  2. Bearish divergence = sell & bullish divergence = buy
 //  3. macd value > 0 = sell & macd value < 0 = buy
 //  (4. Macd value > 0 for divergence / macd value < 0 for divergence)
 //  4. sl sell = SlSl & sl buy = SlBy
 //  5. tp sell = SlSl * 2 + Bid 

Two pictures to illustrate the problem:

1. No value in data window but trade got opened 2. Value is correct in data window and trade got correctly opened (cursor is over on of the red arrows / the  according bar)

no Value = no signal value correct and + signal

 
  1. Your code
     Documentation
    {int ticket = OrderSend(
    _Symbol,
    _Period,                
    0.01,
    Bid,
    10000,
    SlSl,
    TpSl,
    NULL,
    1,
    0,
    0
    );
    int  OrderSend(
       string   symbol,              // symbol
       int      cmd,                 // operation
       double   volume,              // volume
       double   price,               // price
       int      slippage,            // slippage
       double   stoploss,            // stop loss
       double   takeprofit,          // take profit
       string   comment=NULL,        // comment
       int      magic=0,             // magic number
       datetime expiration=0,        // pending order expiration
       color    arrow_color=clrNONE  // color
       );

    Invalid Trade operation

  2. if( MTF15 < MTF1H)
       { if( MacdDivSl > 0 )
          {if( MacdSl && MacdFs > 0)

    You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12

 
Martin Eberhardt:

Hi guys. I have a strange phenomenom. I recently build a simple ea with multiple indicators. One of them gives a value (for a sell position). The strange thing now is, when I test the ea the trades get executet proberly but for some reason sometimes they get send for no apparent reason. When I check the data windows the value data is empty, but somehow the trades get sent anyway. Can someone help me with this?


Two pictures to illustrate the problem:

1. No value in data window but trade got opened 2. Value is correct in data window and trade got correctly opened (cursor is over on of the red arrows / the  according bar)

Testing without these indicators can be difficult. (MacdDiv)


BUY and SELL are not clear in your Opening Order

(OrderSend(_Symbol,_Period,0.01,Bid,10000,SlSl,TpSl,NULL,1,0,0);)


Please Change As In This Example

OrderSend(Symbol(),OP_BUY,0.01,ASK,3,stoploss,takeprofit,"My order",16384,0,clrGreen);

//BUY and SELL are not clear in your Opening Order
//Order Type Is Not Certain In Your Code OP_BUY or OP_SELL
(OrderSend(_Symbol,_Period,0.01,Bid,10000,SlSl,TpSl,NULL,1,0,0);)

Please Change As In This Example
bool OrderSendResult=OrderSend(Symbol(),OP_BUY,0.01,ASK,3,stoploss,takeprofit,"My order",16384,0,clrGreen);
 
Martin Eberhardt:

Hi guys. I have a strange phenomenom. I recently build a simple ea with multiple indicators. One of them gives a value (for a sell position). The strange thing now is, when I test the ea the trades get executet proberly but for some reason sometimes they get send for no apparent reason. When I check the data windows the value data is empty, but somehow the trades get sent anyway. Can someone help me with this?


Two pictures to illustrate the problem:

1. No value in data window but trade got opened 2. Value is correct in data window and trade got correctly opened (cursor is over on of the red arrows / the  according bar)

#property strict

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }

void OnTick()
  {
double MTF15 = iMA(_Symbol,PERIOD_M15,50,0,MODE_EMA,PRICE_CLOSE,0);  
double MTF1H = iMA(_Symbol,PERIOD_H1,50,0,MODE_EMA,PRICE_CLOSE,0);
//Print(DoubleToString(MTF15),"M15");
//Print(DoubleToString(MTF1H),"1H");

double MacdDivBy = iCustom(_Symbol,_Period,"MacdDiv",0,0); 
double MacdDivSl = iCustom(_Symbol,_Period,"MacdDiv",1,0);
double MacdSl = iCustom(_Symbol,_Period,"MacdDiv",2,0);
double MacdFs = iCustom(_Symbol,_Period,"MacdDiv",3,0);
Comment("MacdDiv" + DoubleToString(MacdDivBy));

double SlSl = MathMax(iHigh(_Symbol,_Period,0),iHigh(_Symbol,_Period,5)) + 200 * _Point;
SlSl = NormalizeDouble(SlSl,_Digits);

double SlBy = MathMin(iLow(_Symbol,_Period,0),iLow(_Symbol,_Period,5)) - 200 * _Point;
SlBy = NormalizeDouble(SlBy,_Digits);

double TpSl = Bid - ((SlSl - Bid) * 2);
TpSl = NormalizeDouble(TpSl,_Digits);

double TpBy = Ask + ((TpSl + Ask) *2); 
TpBy = NormalizeDouble(TpBy,_Digits);

for(int i=OrdersTotal()-1; i>=0; i--)
      { int ticket = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrdersTotal() >= 1)
         {return;}}
         
if( MTF15 < MTF1H)
   { if( MacdDivSl > 0 )
      {if( MacdSl && MacdFs > 0)
         {int ticket = OrderSend(_Symbol,OP_SELL,0.01,Bid,30,SlSl,TpSl,NULL,1,0,0);
         // Print(GetLastError()," ",Bid," ",SlSl," ,",TpSl);
          MacdDivSl = 0;}}}
  }


 //  1. 15MTF < 1MTF = sell & 15 MTF > 1MTF = buy
 //  2. Bearish divergence = sell & bullish divergence = buy
 //  3. macd value > 0 = sell & macd value < 0 = buy
 //  (4. Macd value > 0 for divergence / macd value < 0 for divergence)
 //  4. sl sell = SlSl & sl buy = SlBy
 //  5. tp sell = SlSl * 2 + Bid 
 

Thanks for the help guys. Even though I had the false trade operation it still openend the trades without an error lol. 


I'm trying to change it now to a reaction to a signal change as William recommended. 

 
William Roeder:
  1. Your code
     Documentation

    Invalid Trade operation

  2. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12

Well I don't really understand how to do No. 2. I looked at the script you provided in the other post but I can't figure out how it can help me. (I cut the part about the delay) 

 static bool openCondition=false; 
 bool prevCondition = openCondition; openCondition = OPENCONDITION;
 if(openCondition){
    if(!prevCondition){
                 OrderSend();}

I changed it like this but it didn't change anything. Still to many sell signals from a (buffered?) signal.

void OnTick()
  {
  
for(int i=OrdersTotal()-1; i>=0; i--)
      {int ticket = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrdersTotal() >= 1)
         {return;}}
         
double MTF15 = iMA(_Symbol,PERIOD_M15,50,0,MODE_EMA,PRICE_CLOSE,0);  
double MTF1H = iMA(_Symbol,PERIOD_H1,50,0,MODE_EMA,PRICE_CLOSE,0);
Print(DoubleToString(MTF15),"M15");
Print(DoubleToString(MTF1H),"1H");

double MacdDivBy = iCustom(_Symbol,_Period,"MacdDiv",0,0); 
double MacdDivSl = iCustom(_Symbol,_Period,"MacdDiv",1,0);
double MacdSl = iCustom(_Symbol,_Period,"MacdDiv",2,0);
double MacdFs = iCustom(_Symbol,_Period,"MacdDiv",3,0);
Comment("MacdDiv" + DoubleToString(MacdDivBy));

double SlSl = MathMax(iHigh(_Symbol,_Period,0),iHigh(_Symbol,_Period,5)) + 20 * _Point;
SlSl = NormalizeDouble(SlSl,_Digits);

double SlBy = MathMin(iLow(_Symbol,_Period,0),iLow(_Symbol,_Period,5)) - 20 * _Point;
SlBy = NormalizeDouble(SlBy,_Digits);

double TpSl = Bid - ((SlSl - Bid) * 2);
TpSl = NormalizeDouble(TpSl,_Digits);

double TpBy = Ask + ((TpSl + Ask) *2); 
TpBy = NormalizeDouble(TpBy,_Digits);

string signal =""; 
         
if( MTF15 < MTF1H)
   { if( MacdDivSl > 0 )
      {if( MacdSl && MacdFs > 0)
      signal = "sell";}}
         
         
  
  
 static bool openCondition=false;
 bool prevCondition = openCondition; openCondition = signal=="sell";
 if(openCondition){
    
                 int ticket = OrderSend(_Symbol,OP_SELL,0.01,Bid,10000,SlSl,TpSl,NULL,1,0,0);
         }

}
 //  1. 15MTF < 1MTF = sell & 15 MTF > 1MTF = buy
 //  2. Bearish divergence = sell & bullish divergence = buy
 //  3. macd value > 0 = sell & macd value < 0 = buy
 //  (4. Macd value > 0 for divergence / macd value < 0 for divergence)
 //  4. sl sell = SlSl & sl buy = SlBy
 //  5. tp sell = SlSl * 2 + Bid 
  
 
Well I came to understand the logic and all and found a way to act on signal change. The problem now is that the trading signal virtually never changes lol. Will have to look someone else into it but Paypal has maintanance right now. Will keep this updated if I found a solution.