A piece of code to remember an entry signal

 

Hello all,

I have an EA that has a number of different types of entry signals. I can toggle them on and off with true/false inputs. Meaning, if I have #1 and #5 set to true, entry signals will only be generated when both #1 and #5's conditions are met to open a trade and all other signals (or lack thereof) will be ignored.

The problem is, The entry signal has to be generated from both signals on the exact same bar to get an entry. How can I get the EA to remember one of the entry signals for a period of bars while waiting for a possible signal from the other. In other words, if I had an entry signal from #1 in the last "X" bars and am now getting an entry signal from #5 on the last bar (but the last bar didn't give a signal for #1), open a trade anyway. If the signal from #1 was too many bars ago, don't open a trade.

Here is the coding I used for one of the signals. All the others are in the same basic format. I want the time (bar) stipulation to be usable no matter which combination of signals I may or may not be using. Example; I am using signals #1,#4, and #9. I get an entry signal only if all three generated a signal sometime within the last X bars.

//Signal1

Bool BS1= false, SS1=false;

if(Use1)

{

double MA1=iMA(NULL,0,MA1Period,0,0,0,1);

double MA2=iMA(NULL,0,MA2Period,0,0,0,1);

if(MA1>MA2) BS1=true;

if(MA1<MA2) SS1=true;

}

else

{

BS1=true;

SS1=true;

}

//Signal2

bool BS2=false, SS2= false;

if(Use2)

{

double BUpper= iBands(.........................);

double BLower= iBands(..........................);

if(..........) BS2=true;

if(..........) SS2=true;

}

else

{

BS2=true;

SS2=true;

}

if((BS1==true) && (BS2==true)) return(1);

if((SS1==true) && (SS2==true)) return(2);

I am pretty sure that I need to use the "for" operator like this in some way;

int i;

for(i=0;i<=MaxBars-1;i++)

But, I am not sure how to put it in. If someone could show me what they would add to the first signal, I can use the same format for all the others I have added.

Thank you all for your help in this.

Ron

 

You could user a timer, basically setting a trade time variable and comparing current time to it. Or you can use iBarShift ( http://docs.mql4.com/series/iBarShiftwhich returns the bar with the exact or closest time of a given date/time given.

ex:

datetime lastTradeTime = --whatever the last trade time was--;

int theBarIWant = iBarShift(Symbol(), PERIOD_H1, lastTradeTime, false);

Open[theBarIWant];

Close[theBarIWant];

...whatever...

 

You have me stumped.

I can't seem to figure out how to incorporate it into my existing signals and work correctly.

I'll keep trying.

 

I don't really understand your problem (because of my English), but I think you can use:

double MA1=iMA(NULL,0,MA1Period,0,0,0,X);

for X bar.

 
Roger09:
I don't really understand your problem (because of my English), but I think you can use:

double MA1=iMA(NULL,0,MA1Period,0,0,0,X);

for X bar.

No, that will just allow me to adjust the shift of the bar that is used to do the calculations. What I want, is for an entry signal from one of the different signals I use to remain valid for a certain period of bars. That way, I can get an entry signal if both signals are generated within a given number of bars, not just if they both happen on the same bar.

I think I have it though. I can declare a global variable for each signal, set it to a high number and then add conditions in each individual signal part. If the global is greater than the span of bars I want to cover (which will always be true the first time around), the calculations are done, if not, the entry signal is set to true. If the calculations are done and trigger an entry signal, the global variable is reset to the current bar (using iBarShift) and an entry signal is given.

The first time through, calculations will always be done. If an entry signal is generated, the global will be reset to 0 and the next time through, it will bypass the calculations (because it is now lower than the span of bars I want to cover) and keep the entry signal as true. After each bar is generated, the global will increase by one. When the global value exceeds the span of bars I want to cover, it will be just like it was at the start (the global being greater than said span of bars) and it will go back to doing calculations. If another signal is triggered, it will repeat the process.

I think it should work but, I could be missing something (wouldn't be the first time, nor the last).

Thanks for the help guys. It would have taken me a while to find ibarShift. I am still very wet behind the ears when it comes to programming.

 

Well, either my idea is right and my coding is wrong or my idea is wrong and my coding is right or both my idea and my coding is wrong. Either way, it doesn't appear to be working.

I'll keep at it.