Unexpected order - page 2

 
Fernando Carreiro #: Also you should detect for a non "EMPTY_VALUE" and not for 0.

Actually, zero is correct for iFractals.

void OnStart(){
  Print(iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,1)); // testscr USDSGD,Daily: 0.0
 
William Roeder #: Actually, zero is correct for iFractals.

It is?

You are correct. Just checked MetaQuotes example code for the indicator, and for MQL4 it does set the empty value to zero, while on the MQL5 they use EMPTY_VALUE.

I stand corrected. Thank you for pointing it out.

EDIT: I've correct my previous posts taking this correction into account, to make it easier for future readers.

 
Fernando Carreiro #: MQL4 it does set the empty value to zero, while on the MQL5 they use EMPTY_VALUE.

Good to know; brain update in progress.

 

The error still here

iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3)!= EMPTY_VALUE
iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3) == EMPTY_VALUE
iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3) != 0
iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3) == 0

Nothing from above works. I'm using MQL4

 
matrixyb #: The error still here. Nothing from above works. I'm using MQL4

If you are unable to fix it on your own, then you will have to show the full code. You are obviously implementing it incorrectly based on your previous posts, but we are unable to say what that is without analysing your code.

 
Fernando Carreiro #:

If you are unable to fix it on your own, then you will have to show the full code. You are obviously implementing it incorrectly based on your previous posts, but we are unable to say what that is without analysing your code.

//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
string signal ="";
   if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,1)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,1)==0)
      {signal="sell";}
   else if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,2)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,1)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,2)==0)
      {signal="sell";}
   else if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,3)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,1)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,2)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3)==0)
      {signal="sell";}
   else if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,4)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,1)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,2)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,4)==0)
      {signal="sell";}
   else if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,5)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,1)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,2)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,4)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,5)==0)
      {signal="sell";}

if (signal=="sell" && OrdersTotal()==0){
OrderSend(_Symbol,OP_SELL,0.10,Bid,5,0,Bid-(Point*100),NULL,0,0,Red);
}
  }
//+------------------------------------------------------------------+

This is the full code of a simple EA that tries to check if a fractal signal is on previous bar or on bar before previous but it has to be among last 5 candles max then a selling order is placed if the condition is satisfied. However, false sell orders are placed knowing that conditions are not met.

How can this code be fixed?


I tired to implment implement something out of this

Fernando Carreiro #:

You will never find a fractal at bar shift 0 nor 1. Sometimes you may find it at 2, but not always. The only correct shift to search for a fractal is at bar shift 3.

but I failed to do so.

 
matrixyb #: This is the full code of a simple EA that tries to check if a fractal signal is on previous bar or on bar before previous but it has to be among last 5 candles max then a selling order is placed if the condition is satisfied. However, false sell orders are placed knowing that conditions are not met. How can this code be fixed? I tired to implment implement something out of this but I failed to do so.

I will say the following just one more time, and if you don't comply, I will stop trying to help you.

  1. Remove all fractal verifications on bar shift 1 or 0. They will result in no useful information, so remove that part from your code.
  2. If you only want to react to confirmed fractals, then also remove verification of fractals at bar shift 2. For confirmed fractals, only verify from bar shift 3 onwards.

Fix these problems first, and repost your new code. Only then will I provide further help.

Also, as further guidance, please implement a new bar detection in your code as per the following example ...

Code Base

Detecting the start of a new bar or candle

Fernando Carreiro, 2022.04.24 00:46

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.
 
matrixyb #: How can this code be fixed?
string signal ="";
if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,1)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,1)==0)
   {signal="sell";}
else if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,2)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,1)==0
     && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,2)==0)
   {signal="sell";}
else if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,3)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,1)==0
     && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,2)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3)==0)
   {signal="sell";}
else if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,4)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,1)==0
     && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,2)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3)==0 
     && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,4)==0)
   {signal="sell";}
else if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,5)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,1)==0
     && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,2)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3)==0
     && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,4)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,5)==0)
   {signal="sell";}

Are your books one column but two feet wide? No because that is unreadable. They are 6 inches, sometimes two columns, so you can read it easily. So should be your code. I'm not going to go scrolling (or moving my eyes) back and forth trying to read it. Don't copy and paste code, write self documenting code.

double up[6], double dn[6];
for(int iBar=1, iBar<6;++iBar){
   up[iBar]     = iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,iBar);
   dn[iBar]     = iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,iBar);
}
   if(up[1]>0 && dn[1]==0)
      {signal="sell";}
   else if(up[2]>0 && dn[1]==0 && dn[2]==0)
      {signal="sell";}
   else if(up[3]>0 && dn[1]==0 && dn[2]==0 && dn[3]==0)
      {signal="sell";}
   else if(up[4]>0 && dn[1]==0 && dn[2]==0 && dn[3]==0 && dn[4]==0)
      {signal="sell";}
   else if(up[5]>0 && dn[1]==0 && dn[2]==0 && dn[3]==0 && dn[4]==0 && dn[5]==0)
      {signal="sell";}

Removing bars one and two since they can never be non-zero.
Now you can see and understand your code.
If you can state your condition in concrete terms, now you can fix it.
On the first new bar where you have up[3]>0 && dn[3]==0 you opened a new order. Do you really want to open second and third orders on subsequent bars?

double up[6], double dn[6];
for(int iBar=3, iBar<6;++iBar){
   up[iBar]     = iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,iBar);
   dn[iBar]     = iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,iBar);
}
   if(up[3]>0 && dn[3]==0)
   ||(up[4]>0 && dn[3]==0 && dn[4]==0)
   ||(up[5]>0 && dn[3]==0 && dn[4]==0 && dn[5]==0)
      {signal="sell";}
 
Fernando Carreiro #:

I will say the following just one more time, and if you don't comply, I will stop trying to help you.

  1. Remove all fractal verifications on bar shift 1 or 0. They will result in no useful information, so remove that part from your code.
  2. If you only want to react to confirmed fractals, then also remove verification of fractals at bar shift 2. For confirmed fractals, only verify from bar shift 3 onwards.

Fix these problems first, and repost your new code. Only then will I provide further help.

Also, as further guidance, please implement a new bar detection in your code as per the following example ...

you mean something like this

//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

datetime LastOrderTime = 0;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
string signal ="";
   if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,3)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3)==0)
      {signal="sell";}
   else if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,4)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,4)==0)
      {signal="sell";}
   else if(iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,5)>0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,3)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,4)==0 && iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,5)==0)
      {signal="sell";}

if (signal=="sell" && LastOrderTime != Time[0]){
OrderSend(_Symbol,OP_SELL,0.10,Bid,5,Bid+(Point*100),Bid-(Point*100),NULL,0,0,Red);
LastOrderTime = Time[0];
}
  }
//+------------------------------------------------------------------+
 
matrixyb #: you mean something like this

William has already given you improvements. Look at his post #18 ...

Removing bars one and two since they can never be non-zero.
Now you can see and understand your code.
If you can state your condition in concrete terms, now you can fix it.

double up[6], double dn[6];
for(int iBar=3, iBar<6;++iBar){
   up[iBar]     = iFractals(_Symbol,PERIOD_CURRENT,MODE_UPPER,iBar);
   dn[iBar]     = iFractals(_Symbol,PERIOD_CURRENT,MODE_LOWER,iBar);
}
   if(up[3]>0 && dn[3]==0)
   ||(up[4]>0 && dn[3]==0 && dn[4]==0)
   ||(up[5]>0 && dn[3]==0 && dn[4]==0 && dn[5]==0)
      {signal="sell";}
Reason: