Determine highest price of each M1-Bar (Urgent help needed)

 

Hello everybody,

I want to determine the highest price of each M1-bar every minute.

My aim is make a calculation meaning adding each higheste price per M1-bar

For example:


at 09:00 am M1-Bar Highest price = 1 sum: 1

at 09:01 am M1-Bar Highest price = 2 sum: 3( sum of 09:00 and 09:01)

at 09:02 am M1-Bar Highest price = 3 sum: 6 ( sum o 09:00 and 09:02, 09:02)


Thanks for a code example for my problem.


Kind regards,

Hoschie

 

double highs[];

int timeframe = PERIOD_M1;

ArrayCopySeries(highs, MODE_HIGH, NULL, timeframe);

double priceSum = 0;

for(int i = 0; i < ArraySize(highs); i++){

priceSum += highs[i];

}

Note that if your chart is already on the correct timeframe, you can use the array High[] (which is already filled with data) and completely forgo the ArrayCopySeries. The code would look like:

double priceSum = 0;

int history = Bars; // or whatever number of bars(candles) you want, enter any number here as long as it's less than the number Bars.

for(int i = 0; i < history; i++){

priceSum += High[i];

}

 

Hello forexCoder,


thanks for this code sample. I test this with following code:

int timeframe = PERIOD_M1;
ArrayCopySeries(highs, MODE_HIGH, Symbol(), timeframe);
handle=FileOpen("TestFile", FILE_CSV|FILE_WRITE, ';');
if(handle>0)
{
FileWrite(handle, "#","HIGH-Values in the M1-Chart");
for(int i=0;i<ArraySize(highs);i++)
{
FileWrite(handle, i+1,highs[i]);
}
FileFlush(handle);
FileClose(handle);

}

But the High-values in the file "TestFile" are not the same "Highs" like the One in the Backtest-Data of MetaTrader4. I do not understand where this values come from? I also attach the Backtest-File from MetaTrader4 and the generated TestFile.

Is a Sleep-Method necessary?


Thanks in avance for a hint.

 

I see no attachments.

Why would you need Sleep()?

To test by date as well add

datetime opens[];

ArrayCopySeries(opens, MODE_TIME, Symbol(), timeframe);

Then in the for loop change the write method to

FileWrite(handle, i+1, highs[i], opens[i]);

Check by date to make sure you're looking at the same data. Paste results and let us see what the disrepancies are.


Also press F2 for history center and compare with the data there (easier than chart reading).

 

Hello forexCoder,


I thought that with Sleep the program wait 60 seconds to read only the bar of M1-Chart.

Nevertheless I test it with "date". That means I wanted to backtest the 3th may 2011 "EUR/USD" with reading every M1-Candle but

in the test-file which I add to this post (TestFile2) I also see the values of the previos day 2th may 2011. Why?

Wha should I have to implement to see only the values of 1 day? In the first glance I thought I choose the wrong date,

but the choice of begin and start date was correct.

Kind regards,

Hoschie

 

Ahh.

If you're trying to backtest this code (using the strategy tester) then you'll have a problem....

You can set the dates in the dropdowns for strategy tester, but when you run the code I wrote for you above, you dno't limit it to that day only, but to the number of bars in history.

I'm not sure what you're trying to do, could you explain what data you want? all M1 for the current day only?

 

Hi forexCoder,


I will try to explain what my aim is to do:

My EA buys 1 position EUR/USD for example at 08:00 am 1.43

Then my EA should collect also from 08:00 am beginning the M1 from current chart

and make following calculation:

08:00 Highest value 1.43 sum: 1.43 average : 1.43/1 ( calculation of the average)

08:01 Highest values 1.4280 sum: 1.43+1.4280 = 2.858 average: 2.858 /2 Minutes or values = 1.4290

etc..

at 09:00 my EA should make a comparison:

the highest value at 09:00 is 1.4340 the average of calculated highest values at this point is 1.4310. Then the position should be closed,

otherwise it continues with a new calculation beginning at 09 am et...

So my question: Is this logic implementable in MQ4? and if so, how can I do this, to avoid historical values and getting only the current values.


Thanks in advance for your support.

 

Addition:

"all M1 for the current day only?"

When I run the EA on live-trading, how can my program read the M1 of the current day only?


In that way?

for(int i=0;i<ArraySize(opens);i++)
{
if ( TimeToStr(current,TIME_DATE) == TimeToStr(opens[i],TIME_DATE) )
{
if( TimeToStr(opens[i],TIME_MINUTES) >= "08:00" )
{

FileWrite(handle, i+1,TimeToStr(opens[i]),highs[i]);
}
}

}

This is more a harakiri-Code, or?


Kind regards,

Hoschie

Reason: