mmdel
mmdel

Reputation: 1279

How to get OHLC prices for a historic bar in MQL4?

I'm getting a bit stuck with a piece of mql4 code. Esentially, I need to access and store the OHLC prices of a historic bar back in time maximum 30 days back from current date. This is how I am currently doing it.

input  int    referenceDay=1;
static double reference;
if ( Day() == referenceDay ) reference = Open[0];

This is working fine until the point I either add to the code which, it then resets the reference back to 0. I am looking for a way to be able to access the history on each new candle and store the price for the referenceDay.

The objective is that whenever the EA is loaded to the chart, it automatically goes into the history and updates the reference price to be used on this trading day without having to wait for the entire month's iteration in real time.

Upvotes: 0

Views: 2117

Answers (1)

user3666197
user3666197

Reputation: 1

The objective is that whenever the EA is loaded to the chart, it automatically goes into the history and updates the reference price

So far so good.

Once EA gets loaded,
the definition of static double reference; sets the initial value of reference == 0.;
next, the call to Day() sets the rules:
int Day(); returns the current day of the month, which is not the "today"'s-day, but the day of month of the so called last known server time. Given the EA was loaded on Sunday afternoon, or Monday morning, before the Market was opened, the "last known server time" is still Friday ... remember, the Server-side datetime rules ( so, if trading on APAC or Australia / New Zealand TimeZone servers, additional gymnastics are in place ).

So, as the code continues - in all such cases, when the input int referenceDay value will accidentally != the last known server time Day(), your ( just ) initialised variable reference will remain == 0.

Surprised?

May test it:

static double reference = EMPTY;                     // .SET EXPLICIT INITIALISER

if ( Day() == referenceDay )
{             reference = Open[0];
       print( reference,
             "AFTER Day() MATCHED referenceDay"      // THIS NEED NOT HAPPEN
              );
}
else
{     print(  reference,
             "ON( ",Day(), " ) ",
             "AFTER Day() DID NOT MATCH referenceDay = ",
              referenceDay
}

May redefine the assignment strategy, using:

input  int    referenceDAYinput     = 1;
       int    referenceDAYnDaysBACK = max( 0,                       // FUSED
                                           Day() - referenceDAYinput
                                           );
static double referenceLEVEL        = iOpen( _Symbol,
                                             PERIOD_D1,
                                             referenceDAYnDaysBACK
                                             );

This code snippet certainly does not strive to be the complete solution, but shows the mechanics for achieving the desired objective.

Your actual code will have to solve how many Trading days - which is needed to address the TimeSeries-indexing logic of MetaTrader Terminal 4 platofrm -- ( not just the Calendar days ) -- there were in between the referenceDAYinput and the "last known server time"-Day(), but your are this close to have this done.

Upvotes: 0

Related Questions