Coding help - page 262

 

Hi mladen,

I think I am going crazy, here is my changed code from the PeriodConverter.

//+------------------------------------------------------------------+

//| PeriodConverter.mq4 |

//| Copyright 2006-2014, MetaQuotes Software Corp. |

//| http://www.metaquotes.net |

//+------------------------------------------------------------------+

#property copyright "2006-2014, MetaQuotes Software Corp."

#property link "http://www.mql4.com"

#property description "Period Converter to updated format of history base"

#property strict

#property show_inputs

#include

input int StartHour = 9;

input int StartMinute = 0;

input int CloseHour = 17;

input int CloseMinute = 30;

int ExtHandle=-1;

//+------------------------------------------------------------------+

//| script program start function |

//+------------------------------------------------------------------+

void OnStart()

{

datetime time0;

ulong last_fpos=0;

long last_volume=0;

int i,start_pos;

int hwnd=0,cnt=0;

//---- History header

int file_version=401;

string c_copyright;

string c_symbol=Symbol();

int i_period=Period();

int i_digits=Digits;

int i_unused[13];

MqlRates rate;

//---

ExtHandle=FileOpenHistory("TT_"+c_symbol+(string)i_period+".hst",FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_ANSI);

if(ExtHandle<0)

return;

c_copyright="(C)opyright 2003, MetaQuotes Software Corp.";

ArrayInitialize(i_unused,0);

//--- write history file header

FileWriteInteger(ExtHandle,file_version,LONG_VALUE);

FileWriteString(ExtHandle,c_copyright,64);

FileWriteString(ExtHandle,c_symbol,12);

FileWriteInteger(ExtHandle,i_period,LONG_VALUE);

FileWriteInteger(ExtHandle,i_digits,LONG_VALUE);

FileWriteInteger(ExtHandle,0,LONG_VALUE);

FileWriteInteger(ExtHandle,0,LONG_VALUE);

FileWriteArray(ExtHandle,i_unused,0,13);

//--- write history file

start_pos=Bars-1;

rate.open=Open[start_pos];

rate.low=Low[start_pos];

rate.high=High[start_pos];

rate.tick_volume=(long)Volume[start_pos];

rate.spread=0;

rate.real_volume=0;

//--- normalize open time

rate.time=Time[start_pos];

for(i=start_pos-1; i>=0; i--)

{

if(IsStopped())

break;

time0=Time;

//--- history may be updated

if(i==0)

{

//--- modify index if history was updated

if(RefreshRates())

i=iBarShift(NULL,0,time0);

}

//---

if((time0>=rate.time || i==0) && MainTime(time0)==true)

{

if(i==0)

{

rate.tick_volume+=(long)Volume[0];

if(rate.low>Low[0])

rate.low=Low[0];

if(rate.high<High[0])

rate.high=High[0];

rate.close=Close[0];

}

last_fpos=FileTell(ExtHandle);

last_volume=(long)Volume;

FileWriteStruct(ExtHandle,rate);

cnt++;

if(time0>=rate.time)

{

rate.time=time0;

rate.open=Open;

rate.low=Low;

rate.high=High;

rate.close=Close;

rate.tick_volume=last_volume;

}

}

else if(MainTime(time0)==true)

{

rate.tick_volume+=(long)Volume;

if(rate.low>Low)

rate.low=Low;

if(rate.high<High)

rate.high=High;

rate.close=Close;

}

}

FileFlush(ExtHandle);

Print(cnt," record(s) written");

//--- collect incoming ticks

datetime last_time=LocalTime()-5;

while(!IsStopped())

{

datetime cur_time=LocalTime();

//--- check for new rates

if(RefreshRates())

{

time0=Time[0];

FileSeek(ExtHandle,last_fpos,SEEK_SET);

//--- is there current bar?

if(time0<rate.time && MainTime(time0)==true)

{

rate.tick_volume+=(long)Volume[0]-last_volume;

last_volume=(long)Volume[0];

if(rate.low>Low[0])

rate.low=Low[0];

if(rate.high<High[0])

rate.high=High[0];

rate.close=Close[0];

}

else if(MainTime(time0)==true)

{

//--- no, there is new bar

rate.tick_volume+=(long)Volume[1]-last_volume;

if(rate.low>Low[1])

rate.low=Low[1];

if(rate.high<High[1])

rate.high=High[1];

//--- write previous bar remains

FileWriteStruct(ExtHandle,rate);

last_fpos=FileTell(ExtHandle);

//----

rate.time=time0;

rate.open=Open[0];

rate.low=Low[0];

rate.high=High[0];

rate.close=Close[0];

rate.tick_volume=(long)Volume[0];

last_volume=rate.tick_volume;

}

//----

FileWriteStruct(ExtHandle,rate);

FileFlush(ExtHandle);

//---

if(hwnd==0)

{

hwnd=WindowHandle(Symbol(),i_period);

if(hwnd!=0)

Print("Chart window detected");

}

//--- refresh window not frequently than 1 time in 2 seconds

if(hwnd!=0 && cur_time-last_time>=2)

{

PostMessageA(hwnd,WM_COMMAND,33324,0);

last_time=cur_time;

}

}

Sleep(50);

}

//---

}

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

//---

if(ExtHandle>=0)

{

FileClose(ExtHandle);

ExtHandle=-1;

}

//---

}

//+------------------------------------------------------------------+

bool MainTime(datetime time0)

{

datetime temp = time0 - 1 * 60 *60;

if(((TimeHour(temp) == StartHour && TimeMinute(temp) >= StartMinute) ||

TimeHour(temp) > StartHour) &&

(TimeHour(temp)< CloseHour ||

(TimeHour(temp) == CloseHour && TimeMinute(temp) < CloseMinute)))

{

Print(".......return (true) - Time0 = "+TimeToString(time0) + " tempTime = "+TimeToString(temp));

return(true);

}

else

{

Print("+++++++false - Time0 = "+TimeToString(time0) + " tempTime = "+TimeToString(temp));

return(false);

}

}

I added the function MainTime to select the bars I want to show in my new offline chart.

(I also moved the time for an hour but that's not important).

Now when I test this on a chart with 15046 bars (M5) from 0:00 (Starttime) - 12:00 (Closetime)

I am getting correct print-messages and in the end the info, that 7596 bars were written.

So it is about a half from the original chart and so I think everything worked fine!

But when I open the corresponding offline chart it looks exactly as the original chart with all the bars.

I can't really understand that behaviour?!?!

 
sunshineh:
Hi mladen,

I think I am going crazy, here is my changed code from the PeriodConverter.

//+------------------------------------------------------------------+

//| PeriodConverter.mq4 |

//| Copyright 2006-2014, MetaQuotes Software Corp. |

//| http://www.metaquotes.net |

//+------------------------------------------------------------------+

#property copyright "2006-2014, MetaQuotes Software Corp."

#property link "http://www.mql4.com"

#property description "Period Converter to updated format of history base"

#property strict

#property show_inputs

#include

input int StartHour = 9;

input int StartMinute = 0;

input int CloseHour = 17;

input int CloseMinute = 30;

int ExtHandle=-1;

//+------------------------------------------------------------------+

//| script program start function |

//+------------------------------------------------------------------+

void OnStart()

{

datetime time0;

ulong last_fpos=0;

long last_volume=0;

int i,start_pos;

int hwnd=0,cnt=0;

//---- History header

int file_version=401;

string c_copyright;

string c_symbol=Symbol();

int i_period=Period();

int i_digits=Digits;

int i_unused[13];

MqlRates rate;

//---

ExtHandle=FileOpenHistory("TT_"+c_symbol+(string)i_period+".hst",FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_ANSI);

if(ExtHandle<0)

return;

c_copyright="(C)opyright 2003, MetaQuotes Software Corp.";

ArrayInitialize(i_unused,0);

//--- write history file header

FileWriteInteger(ExtHandle,file_version,LONG_VALUE);

FileWriteString(ExtHandle,c_copyright,64);

FileWriteString(ExtHandle,c_symbol,12);

FileWriteInteger(ExtHandle,i_period,LONG_VALUE);

FileWriteInteger(ExtHandle,i_digits,LONG_VALUE);

FileWriteInteger(ExtHandle,0,LONG_VALUE);

FileWriteInteger(ExtHandle,0,LONG_VALUE);

FileWriteArray(ExtHandle,i_unused,0,13);

//--- write history file

start_pos=Bars-1;

rate.open=Open[start_pos];

rate.low=Low[start_pos];

rate.high=High[start_pos];

rate.tick_volume=(long)Volume[start_pos];

rate.spread=0;

rate.real_volume=0;

//--- normalize open time

rate.time=Time[start_pos];

for(i=start_pos-1; i>=0; i--)

{

if(IsStopped())

break;

time0=Time;

//--- history may be updated

if(i==0)

{

//--- modify index if history was updated

if(RefreshRates())

i=iBarShift(NULL,0,time0);

}

//---

if((time0>=rate.time || i==0) && MainTime(time0)==true)

{

if(i==0)

{

rate.tick_volume+=(long)Volume[0];

if(rate.low>Low[0])

rate.low=Low[0];

if(rate.high<High[0])

rate.high=High[0];

rate.close=Close[0];

}

last_fpos=FileTell(ExtHandle);

last_volume=(long)Volume;

FileWriteStruct(ExtHandle,rate);

cnt++;

if(time0>=rate.time)

{

rate.time=time0;

rate.open=Open;

rate.low=Low;

rate.high=High;

rate.close=Close;

rate.tick_volume=last_volume;

}

}

else if(MainTime(time0)==true)

{

rate.tick_volume+=(long)Volume;

if(rate.low>Low)

rate.low=Low;

if(rate.high<High)

rate.high=High;

rate.close=Close;

}

}

FileFlush(ExtHandle);

Print(cnt," record(s) written");

//--- collect incoming ticks

datetime last_time=LocalTime()-5;

while(!IsStopped())

{

datetime cur_time=LocalTime();

//--- check for new rates

if(RefreshRates())

{

time0=Time[0];

FileSeek(ExtHandle,last_fpos,SEEK_SET);

//--- is there current bar?

if(time0<rate.time && MainTime(time0)==true)

{

rate.tick_volume+=(long)Volume[0]-last_volume;

last_volume=(long)Volume[0];

if(rate.low>Low[0])

rate.low=Low[0];

if(rate.high<High[0])

rate.high=High[0];

rate.close=Close[0];

}

else if(MainTime(time0)==true)

{

//--- no, there is new bar

rate.tick_volume+=(long)Volume[1]-last_volume;

if(rate.low>Low[1])

rate.low=Low[1];

if(rate.high<High[1])

rate.high=High[1];

//--- write previous bar remains

FileWriteStruct(ExtHandle,rate);

last_fpos=FileTell(ExtHandle);

//----

rate.time=time0;

rate.open=Open[0];

rate.low=Low[0];

rate.high=High[0];

rate.close=Close[0];

rate.tick_volume=(long)Volume[0];

last_volume=rate.tick_volume;

}

//----

FileWriteStruct(ExtHandle,rate);

FileFlush(ExtHandle);

//---

if(hwnd==0)

{

hwnd=WindowHandle(Symbol(),i_period);

if(hwnd!=0)

Print("Chart window detected");

}

//--- refresh window not frequently than 1 time in 2 seconds

if(hwnd!=0 && cur_time-last_time>=2)

{

PostMessageA(hwnd,WM_COMMAND,33324,0);

last_time=cur_time;

}

}

Sleep(50);

}

//---

}

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

//---

if(ExtHandle>=0)

{

FileClose(ExtHandle);

ExtHandle=-1;

}

//---

}

//+------------------------------------------------------------------+

bool MainTime(datetime time0)

{

datetime temp = time0 - 1 * 60 *60;

if(((TimeHour(temp) == StartHour && TimeMinute(temp) >= StartMinute) ||

TimeHour(temp) > StartHour) &&

(TimeHour(temp)< CloseHour ||

(TimeHour(temp) == CloseHour && TimeMinute(temp) < CloseMinute)))

{

Print(".......return (true) - Time0 = "+TimeToString(time0) + " tempTime = "+TimeToString(temp));

return(true);

}

else

{

Print("+++++++false - Time0 = "+TimeToString(time0) + " tempTime = "+TimeToString(temp));

return(false);

}

}

I added the function MainTime to select the bars I want to show in my new offline chart.

(I also moved the time for an hour but that's not important).

Now when I test this on a chart with 15046 bars (M5) from 0:00 (Starttime) - 12:00 (Closetime)

I am getting correct print-messages and in the end the info, that 7596 bars were written.

So it is about a half from the original chart and so I think everything worked fine!

But when I open the corresponding offline chart it looks exactly as the original chart with all the bars.

I can't really understand that behaviour?!?!

It is because you are using the original name ad time frame of the symbol. You have to change either the symbol name or the time frame (better to change the time frame, that way all your code should recognize the correct symbol name)

If you change the time frame, make sure that you do not use some "legal" time frames (1,5,15,30,60,...)

 

Hi,

This ea is used for equity protection in combination with other ea. You can input profit and loss limits of equity and when limit is hit ea will close all orders and close other EA.

The problem with ea equity guard is that it works only when mt4 is maximized. If its minimized or mt4 is running on VPS, when limit is hit EA will not start closing orders until I maximize EA or login on VPS. The moment i login or maximize ea start closing.

Thnx.

Files:
 
DarkForex33:
Hi,

This ea is used for equity protection in combination with other ea. You can input profit and loss limits of equity and when limit is hit ea will close all orders and close other EA.

The problem with ea equity guard is that it works only when mt4 is maximized. If its minimized or mt4 is running on VPS, when limit is hit EA will not start closing orders until I maximize EA or login on VPS. The moment i login or maximize ea start closing.

Thnx.

Does it behave the same way on your PC (not the VPS)?

If it works OK on your PC in same conditions then you have a problem with your VPS

 
mladen:
It is because you are using the original name ad time frame of the symbol. You have to change either the symbol name or the time frame (better to change the time frame, that way all your code should recognize the correct symbol name) If you change the time frame, make sure that you do not use some "legal" time frames (1,5,15,30,60,...)

Thanks, I have changed

ExtHandle=FileOpenHistory("TT_"+c_symbol+(string)3+".hst",FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_ANSI);

and

FileWriteInteger(ExtHandle,3,LONG_VALUE);

But if I open the offline chart ...,M3 it only shows "Waiting for Updates"... and nothing happens

 
sunshineh:
Thanks, I have changed

ExtHandle=FileOpenHistory("TT_"+c_symbol+(string)3+".hst",FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_ANSI);

and

FileWriteInteger(ExtHandle,3,LONG_VALUE);

But if I open the offline chart ...,M3 it only shows "Waiting for Updates"... and nothing happens

Interesting

Use this one as a basis for your indicator. This one works (seems that the version you used as a basis has errors in it)

Files:
 

Help me! for this ea

Greetings

Please experts

set in EA stop lose and take profit

gaps.ex4

gaps.mq4

Files:
gaps.ex4  6 kb
gaps.mq4  4 kb
 

Hello,

Can you help add a DEMA option to the attached indicator?

Currently the options are:

0= sma

1=ema

2=smma

3=lwma

Would like to add:

4= DEMAIs this possible?Thank you much!

 
MrWigglesworth:
Hello,

Can you help add a DEMA option to the attached indicator?

Currently the options are:

0= sma

1=ema

2=smma

3=lwma

Would like to add:

4= DEMAIs this possible?Thank you much!

MrWigglesworth

Here is a version with dema added as type 4 : ma__dema_crossover_with_arrow_and_email.mq4

 
amirreza132:
Greetings

Please experts

set in EA stop lose and take profit

gaps.ex4

gaps.mq4

amirreza132

That EA is designed to catch gaps and it already has take profit in cases when the gap happens

Reason: