I am new here, but have been reading for a few weeks. I am attempting to get into MQL coding in order to write my own EA for my trade ideas. I paid for someone to write one previously but wish to understand how to do this myself, and therefore would much appreciate some guidance relating to the following issue.
I wish for an order to be placed when 2 conditions are met at the same time: the first is that the bar crosses above the the MA line, and the second is that the elapsed number of seconds in the current bar is above a certain number.
As mentioned, I am extremely new to this, however I have been searching through various example EAs and looking at the very helpful documentation. It seems that I am looking for a PeriodSeconds function, as detailed below:
From this, I gather that I could specify that the PeriodSeconds function must be above the number of seconds that I specify. Is this correct?
My next question is this: I am using an offline M10 chart, and according to the documentation, PeriodSeconds must be used on ENUM_TIMEFRAMES. However, if period is set to "current" then would it still be able to operate on the M10 chart?
I hope I have made sense! Any responses much appreciated.
PeriodSeconds() only reports the number of seconds in one bar of the current char. On M1 chart, it will return 60, on M5 chart, it will return 300.
You have to look documentation for TimeCurrent(), iTime() or Time array and properties of the datetime type.
To get the number of seconds you should subtract current server time from the beginning time of the current bar. Something like this:
int secs = TimeCurrent() - Time;
datetime thisBar = Time;
int secondsElapsed = TimeCurrent() - thisBar;
datetime nextBar = thisBar + Period_Seconds(PERIOD_CURRENT);
int secondsRemaining = nextBar - TimeCurrent();
The section starting with "//" is a comment so you are basically just adding more comments.
This might be what you are looking for:
RefreshRates(); if( Cross(0, Bid > iMA(NULL, PERIOD_CURRENT, 14, 0, MODE_SMA, PRICE_CLOSE, 0)) && ((TimeCurrent() - Time) < 30) ) // Price crosses above Moving Average and Custom Bar Timer Code
However, that original code is somewhat too "Stringy". Clean it up a bit and give it more readable structure.
(TimeCurrent() - Time) < 30)
To add comments, please log in or register