Download MetaTrader 5
To add comments, please log in or register
Searching on MQL5.community is performed in all sections. Try it!
acecard
10
acecard 2016.04.06 13:54 

Hi all,

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:

https://docs.mql4.com/common/periodseconds

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.

AC 

Drazen Penic
2408
Drazen Penic 2016.04.06 18:00  

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[0];
whroeder1
13656
whroeder1 2016.04.06 18:43  
acecard:
the elapsed number of seconds in the current bar is above a certain number

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?
  1.    datetime thisBar  = Time[0];
    int secondsElapsed   = TimeCurrent() - thisBar;
    
       datetime nextBar  = thisBar + Period_Seconds(PERIOD_CURRENT);
    int secondsRemaining = nextBar - TimeCurrent();
    
  2. PeriodSeconds returns 60 * period requested. Since the period of the chart is embedded the chart history, PeriodSeconds(PERIOD_CURRENT) should return 600 and _Period should return 10 on the M10 chart. PeriodSeconds(PERIOD_M10) returns 600, but you shouldn't hard code the period.
acecard
10
acecard 2016.04.06 22:17  
Ok, I see. Having read these, this makes a bit more sense. So, putting this into my current code:
RefreshRates();  if(Cross(0, Bid > iMA(NULL, PERIOD_CURRENT, 14, 0, MODE_SMA, PRICE_CLOSE, 0)) //Price crosses above Moving Average    && TimeCurrent() - Time[0] < 30 // Bar timer //Custom Code    )

 This didn't seem to work. Do I have the time format written correctly? Thanks AC
Fernando Carreiro
1574
Fernando Carreiro 2016.04.06 22:39  

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[0]) < 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.

whroeder1
13656
whroeder1 2016.04.07 01:11  
acecard: This didn't seem to work.

  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. There are no mind readers here, we have no idea what cross(int, bool) does.
  3. (TimeCurrent() - Time[0]) < 30)
    Why are you checking if the bar started less then 30 seconds ago? During the Asian session, it could be many minutes before you get a new tick. Why don't you check for a new bar?
    Bars is unreliable (a refresh/reconnect can change number of bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
/
To add comments, please log in or register