Download MetaTrader 5

My code is not checking every hour that the D+ is above 21.

To add comments, please log in or register
Kingkest
17
Kingkest  

I am working on an EA and this is not checking every hour on the open of the candle when the D+ is above the 21 in ADX. Can someone help me out here with this please.

 

In short I want this to alert me at every open candle when the D+ is above 21.  

 


bool           EnterOpenBar=true;
datetime       CurrentTime;

double DPlus=iADX(NULL,PERIOD_H1,14,PRICE_OPEN,MODE_PLUSDI,0);
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(EnterOpenBar=true)
     {
      if(CurrentTime!=Time[0])
        {
         if(iADX(NULL,PERIOD_H1,14,PRICE_OPEN,MODE_PLUSDI,0)>21)
           {
            Alert("D+ Above");
           }
        }
      CurrentTime=Time[0];
     }
//---
  }
//+------------------------------------------------------------------+
whroeder1
15071
whroeder1  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Print out your variables, and find out why.
  3.  if (EnterOpenBar = true)
    and that isn't doing what you think it is.
Keith Watford
Moderator
10064
Keith Watford  

I have edited your post, placing your code inside an SRC box and removed the huge white spaces.

The code does work, but as WHR says

 if (EnterOpenBar = true)

is not a boolean test

Kingkest
17
Kingkest  
GumRai:

I have edited your post, placing your code inside an SRC box and removed the huge white spaces.

The code does work, but as WHR says

is not a boolean test


Ok, I am new to this forum thank you for the help with adding code to the forum. Do you guys see a way I could make it work. It seems to be getting in on the start of some candles but not there is plenty of candles it should be checking and its not.
Fernando Carreiro
2231
Fernando Carreiro  
Kingkest:
Ok, I am new to this forum thank you for the help with adding code to the forum. Do you guys see a way I could make it work. It seems to be getting in on the start of some candles but not there is plenty of candles it should be checking and its not.

Both WHRoeder and GumRai already told you what is wrong (and needs to be corrected first, before looking at the rest):

if(EnterOpenBar=true) // This is incorrect! Assignment is not the same as comparison, which is not even needed in this case.

When a poster tells you what is wrong, it is up to you to go read the documentation and see how to correct it. When you don't do that, it tells us that you are not putting in the effort (or simply ignoring or not reading the answer).

So I will give you 2 hints (but it is up to you to go read the documentation):

  • "=" is not the same as "==" (assignment is not the same as comparison)
  • boolean "bool" variables, don't even need a comparison, because that is what the "if()" function already requires.
Keith Watford
Moderator
10064
Keith Watford  
Kingkest:
Ok, I am new to this forum thank you for the help with adding code to the forum. Do you guys see a way I could make it work. It seems to be getting in on the start of some candles but not there is plenty of candles it should be checking and its not.

Looking at

iADX(NULL,PERIOD_H1,14,PRICE_OPEN,MODE_PLUSDI,0)

you can be forgiven for assuming that the value of the indicator will remain the same throughout the life of the current bar.

The fact is that the value can change depending on whether the current bar makes new highs or lows compared to the previous bar.

Because of this you should only check after a bar has closed

iADX(NULL,PERIOD_H1,14,PRICE_OPEN,MODE_PLUSDI,1)

That way, when you look back over the indicator, you will see that you get the alerts correctly.



 if (EnterOpenBar = true)

should be corrected, but it will not affect the function of the current code, so much. As it will return true all the time,

bool           EnterOpenBar=false;

will still allow the alerts to be issued.

Fernando Carreiro
2231
Fernando Carreiro  
GumRai:

you can be forgiven for assuming that the value of the indicator will remain the same throughout the life of the current bar. The fact is that the value can change depending on whether the current bar makes new highs or lows compared to the previous bar.

iADX(NULL,PERIOD_H1,14,PRICE_OPEN,MODE_PLUSDI,0)

will still allow the alerts to be issued.

bool EnterOpenBar=false;

EDIT: Sorry to contradict you again, but as you have just pointed out, the OP is using the "Open" Price, not the "Close" price. The Open Price will never change during the life of the current bar, no matter what new highs or lows are printed. Your statement would only hold true if he were using anything other than the Open price. Correction on my part. I forgot that the ADX uses the Highs and Lows in its calculations, so your statement is correct and he should use the previous bar instead. Now I am left wondering why the iADX() function needs an "applied price" parameter anyway, but that is another matter.

Also, initialising EnterOpenBar to false, will also not correct the problem, as that will just make the EA do nothing at all. What the OP needs to do is correctly update the EnterOpenBar variable in accordance with the comparisons done for the CurrentTime and Time[].

Personally I was trying to refraining from giving the total solution, as that would not help him learn and understand what is wrong with his code logic, but in light of all this, I am sure it is now just confusing him even more.

So here is the full solution:

// Please note, this code is untested
// IMPORTANT! Please attach EA to an H1 Chart in order for it to work on H1 timing.

extern int    ADXPeriod      = 14;
extern double DPlusThreshold = 21.0;

bool     EnterOpenBar;
datetime CurrentTime;
double   DPlus;

int OnInit()
{
   CurrentTime = EMPTY; // Intialise here and not globally, in the case the EA is restated
   
   return(INIT_SUCCEEDED);
}

void OnTick()
{
   
   datetime BarTime = Time[ 0 ]; // Save variable, in case new tick arrives, causing a new Bar while executing
   
   if( CurrentTime != BarTime )
   {
      EnterOpenBar = ( CurrentTime != EMPTY );
      CurrentTime  = BarTime;
   }
   else
      EnterOpenBar = false;
   
   if( EnterOpenBar )
   {
     DPlus = iADX( NULL, PERIOD_CURRENT, ADXPeriod, PRICE_OPEN, MODE_PLUSDI, 1 ); // Using previous bar as suggested by GumRai
     if( DPlus > DPlusThreshold ) Alert( "D+ Above" );
   }
}
Keith Watford
Moderator
10064
Keith Watford  
FMIC:

Sorry to contradict you again, but as you have just pointed out, the OP is using the "Open" Price, not the "Close" price. The Open Price will never change during the life of the current bar, no matter what new highs or lows are printed. Your statement would only hold true if he were using anything other than the Open price.


Here is a test code (indicator) that I ran through the strategy tester

      static datetime CurrentTime=0;
      static double adx_at_open=0;
      double adx=iADX(NULL,PERIOD_H1,14,PRICE_OPEN,MODE_PLUSDI,0);
      if(CurrentTime!=Time[0])
        {
         adx_at_open=adx;
         Print("ADX at Bar Open=",DoubleToStr(adx,4));
         CurrentTime=Time[0];
         return(0);
        }
      if(adx!=adx_at_open)
        {
         Print("New ADX Value=",DoubleToStr(adx,4));
         adx_at_open=adx;
        }


Now, unless I have made some silly error, the log file proves that the value does indeed often change during the life of the current bar


0    09:45:28    2016.08.08 00:00  ADX test EURUSD,H1: ADX at Bar Open=13.5096
0    09:45:36    2016.08.08 00:27  ADX test EURUSD,H1: New ADX Value=16.6049
0    09:45:36    2016.08.08 00:30  ADX test EURUSD,H1: New ADX Value=16.6955
0    09:45:36    2016.08.08 00:30  ADX test EURUSD,H1: New ADX Value=17.0424
0    09:45:36    2016.08.08 00:30  ADX test EURUSD,H1: New ADX Value=17.1255
0    09:45:36    2016.08.08 00:30  ADX test EURUSD,H1: New ADX Value=17.5205
0    09:45:36    2016.08.08 00:30  ADX test EURUSD,H1: New ADX Value=17.8141
0    09:45:36    2016.08.08 01:00  ADX test EURUSD,H1: ADX at Bar Open=15.4389
0    09:45:36    2016.08.08 02:00  ADX test EURUSD,H1: ADX at Bar Open=13.3804
0    09:45:36    2016.08.08 02:00  ADX test EURUSD,H1: New ADX Value=13.6273
0    09:45:36    2016.08.08 02:00  ADX test EURUSD,H1: New ADX Value=13.8652
0    09:45:36    2016.08.08 02:01  ADX test EURUSD,H1: New ADX Value=14.0947
0    09:45:36    2016.08.08 02:01  ADX test EURUSD,H1: New ADX Value=14.3160
0    09:45:36    2016.08.08 02:02  ADX test EURUSD,H1: New ADX Value=14.5298
0    09:45:36    2016.08.08 02:02  ADX test EURUSD,H1: New ADX Value=14.7363
0    09:45:36    2016.08.08 02:04  ADX test EURUSD,H1: New ADX Value=14.9359
0    09:45:36    2016.08.08 02:04  ADX test EURUSD,H1: New ADX Value=15.1290
0    09:45:36    2016.08.08 02:59  ADX test EURUSD,H1: New ADX Value=15.1008
0    09:45:36    2016.08.08 02:59  ADX test EURUSD,H1: New ADX Value=15.0735
0    09:45:36    2016.08.08 02:59  ADX test EURUSD,H1: New ADX Value=15.0470
0    09:45:36    2016.08.08 03:00  ADX test EURUSD,H1: ADX at Bar Open=13.0408
0    09:45:36    2016.08.08 04:00  ADX test EURUSD,H1: ADX at Bar Open=11.3020
0    09:45:36    2016.08.08 04:28  ADX test EURUSD,H1: New ADX Value=11.5206
0    09:45:36    2016.08.08 04:28  ADX test EURUSD,H1: New ADX Value=11.9369
0    09:45:36    2016.08.08 04:28  ADX test EURUSD,H1: New ADX Value=12.1353
0    09:45:36    2016.08.08 04:30  ADX test EURUSD,H1: New ADX Value=12.3276
0    09:45:36    2016.08.08 04:30  ADX test EURUSD,H1: New ADX Value=12.5141

Keith Watford
Moderator
10064
Keith Watford  
FMIC:

Also, initialising EnterOpenBar to false, will also not correct the problem, as that will just make the EA do nothing at all.

You didn't fully understand what I said. I didn't say that it would correct his problem.

I said that

 if (EnterOpenBar = true)

will always return true, so no matter what EnterOpenBar is set to, true or false, the code following the if() will always be executed.

Fernando Carreiro
2231
Fernando Carreiro  
GumRai: Here is a test code (indicator) that I ran through the strategy tester. Now, unless I have made some silly error, the log file proves that the value does indeed often change during the life of the current bar

Yes, you are correct and I noticed my error a few minutes after the initial post, but I corrected my post soon afterwards, but you probably did not see the update. You probably guessed by now that I seldom use that Indicator (hence the lack of experience with it).

I am however now left wondering why the iADX() (and the chart indicator) requires the "applied price" parameter in the first place, as it is not used in the calculation! But that is besides the point and not relevant here.

Fernando Carreiro
2231
Fernando Carreiro  
GumRai:

You didn't fully understand what I said. I didn't say that it would correct his problem. I said that will always return true, so no matter what EnterOpenBar is set to, true or false, the code following the if() will always be executed.

I meant the following, but no worries, as today has been filled with misunderstandings and miscommunication on my part (I probably have mercury in retrograde, as they say).
bool EnterOpenBar=false;
12
To add comments, please log in or register