Metatrader 5 coding questions / issues - page 8

 

I actually meant - can you make fisher_m11.mq4 a multi-time-frame indicator. That is, M1, M5, M15, M30 etc can all be viewed on the same chart.

mladen:
funayot, This is a correct Ehlers fisher transform for metatrader 5 : https://www.mql5.com/en/forum/181297/page13
 

I got an expert advisor made by eatree (attached). Please help me on inserting this condition in the ea. If buy/Sell order hit takeprofit/stop loss/ trailing stop, while the entry condition is still true, don't enter another trade until the next signal. Example, MA1 crosses up MA2 Buy and if take profit/stop loss/ trailing stop is hit, wait for the next signal that is MA 1 crosses down MA2 before opening another position.

Thanks.

Files:
 

Can anyone please direct me to where I can locate multi timeframe expert advisor for mt5. I am planning to check the code in order to see how to include multi-timeframe in an ea that I'm trying to build. Thanks.

 
funayot:
Can anyone please direct me to where I can locate multi timeframe expert advisor for mt5. I am planning to check the code in order to see how to include multi-timeframe in an ea that I'm trying to build. Thanks.

You can try something like this (this one can be called from the OnTick() - no need to do it in in OnInit() - I prefer that way because this way it can find the indicator even in cases when for some reason it is not found in the OnInit() procedure - there were cases when an error that a time frame does not exist was raised for a perfectly legal time frame, just one example. This way you will try till it becomes available or otherwise it will simply prevent EA from working because the indicator is missing)

ENUM_TIMEFRAMES timeFrame = PERIOD_D1;

static int indicatorHandle = -1;

if (indicatorHandle == -1) indicatorHandle = iCustom(NULL,timeFrame,"customIndicator",parameter1,parameter2,...,paremetern);

if (indicatorHandle == -1) return(0);

From that point on, you can use the indicatorHandle to copy buffers values the usual way how it is done in metatrader 5

___________________

PS : used daily time frame in this example simply as an example

PPS: you can also check the Moving average EA from samples (it is a simple EA written in metatrader 5 without using the overcomplicated object orientated EA frame they are pushing - much easier to understand what is done in it)

 

Thank you mladen. I will try it and give you feedback.

mladen:
You can try something like this (this one can be called from the OnTick() - no need to do it in in OnInit() - I prefer that way because this way it can find the indicator even in cases when for some reason it is not found in the OnInit() procedure - there were cases when an error that a time frame does not exist was raised for a perfectly legal time frame, just one example. This way you will try till it becomes available or otherwise it will simply prevent EA from working because the indicator is missing)
ENUM_TIMEFRAMES timeFrame = PERIOD_D1;

static int indicatorHandle = -1;

if (indicatorHandle == -1) indicatorHandle = iCustom(NULL,timeFrame,"customIndicator",parameter1,parameter2,...,paremetern);

if (indicatorHandle == -1) return(0);

From that point on, you can use the indicatorHandle to copy buffers values the usual way how it is done in metatrader 5

___________________

PS : used daily time frame in this example simply as an example

PPS: you can also check the Moving average EA from samples (it is a simple EA written in metatrader 5 without using the overcomplicated object orientated EA frame they are pushing - much easier to understand what is done in it)
 

Mladen,

I plan creating an expert advisor that checks MACD in 5mins timeframe and 30 minutes timeframe. If some conditions are true in both timeframes, then execute trade. I copied below how my ontick, oninit and some classes look like in my code. I got confused how to use the codes you suggested. I used the ENUM_TIMEFRAMES timeFrame = PERIOD_H1 on MACDhandle but I couldn't use it on MACD2handle.

void OnTick()

{

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

if(ExtExpert2.Processing()) limit_time = TimeCurrent() + ExtTimeOut;

}

}

int OnInit()

{

string _s = Symbols;

StringTrimLeft(_s);

StringTrimRight(_s);

if(_s == ""){

_s = _Symbol;

}

StrSplit(_s, sa, "/");

ArrayResize(ExtExpert2, ArraySize(sa));

ArrayResize(limit_time, ArraySize(sa));

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

ExtExpert2 = new CMyExpert;//.init(sa)

if(!ExtExpert2.Init(sa)){

ExtExpert2.Deinit();

return(-1);

}

}

return(0);

}

bool CustomExpert::InitIndicators()

{

if(handleMACD == INVALID_HANDLE)

if((handleMACD = iCustom(m_symbol.Name(),0, "MACD", MACDFastEMA, MACDSlowEMA, MACDSignalSMA, MACDAppliedPrice)

) == INVALID_HANDLE){

printf("Error Creating MACD");

return(false);

}

if(MACD2handle == INVALID_HANDLE)

if((MACD2handle = iCustom(m_symbol.Name(),0, "MACD", MACDFastEMA2, SlowEMA2, MACDSignalSMA2, MACDAppliedPrice2)

) == INVALID_HANDLE){

printf("Error Creating MACD2");

return(false);

}

return(true);

}

void CustomExpert::Deinit()

{

if(MACDhandle != INVALID_HANDLE){

IndicatorRelease(MACDhandle);

MACDhandle = INVALID_HANDLE;

}

if(MACD2handle != INVALID_HANDLE){

IndicatorRelease(MACD2handle);

MACD2handle = INVALID_HANDLE;

}

}

mladen:
You can try something like this (this one can be called from the OnTick() - no need to do it in in OnInit() - I prefer that way because this way it can find the indicator even in cases when for some reason it is not found in the OnInit() procedure - there were cases when an error that a time frame does not exist was raised for a perfectly legal time frame, just one example. This way you will try till it becomes available or otherwise it will simply prevent EA from working because the indicator is missing)
ENUM_TIMEFRAMES timeFrame = PERIOD_D1;

static int indicatorHandle = -1;

if (indicatorHandle == -1) indicatorHandle = iCustom(NULL,timeFrame,"customIndicator",parameter1,parameter2,...,paremetern);

if (indicatorHandle == -1) return(0);

From that point on, you can use the indicatorHandle to copy buffers values the usual way how it is done in metatrader 5

___________________

PS : used daily time frame in this example simply as an example

PPS: you can also check the Moving average EA from samples (it is a simple EA written in metatrader 5 without using the overcomplicated object orientated EA frame they are pushing - much easier to understand what is done in it)
 
funayot:
Mladen,

I plan creating an expert advisor that checks MACD in 5mins timeframe and 30 minutes timeframe. If some conditions are true in both timeframes, then execute trade. I copied below how my ontick, oninit and some classes look like in my code. I got confused how to use the codes you suggested. I used the ENUM_TIMEFRAMES timeFrame = PERIOD_H1 on MACDhandle but I couldn't use it on MACD2handle.

void OnTick()

{

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

if(ExtExpert2.Processing()) limit_time = TimeCurrent() + ExtTimeOut;

}

}

int OnInit()

{

string _s = Symbols;

StringTrimLeft(_s);

StringTrimRight(_s);

if(_s == ""){

_s = _Symbol;

}

StrSplit(_s, sa, "/");

ArrayResize(ExtExpert2, ArraySize(sa));

ArrayResize(limit_time, ArraySize(sa));

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

ExtExpert2 = new CMyExpert;//.init(sa)

if(!ExtExpert2.Init(sa)){

ExtExpert2.Deinit();

return(-1);

}

}

return(0);

}

bool CustomExpert::InitIndicators()

{

if(handleMACD == INVALID_HANDLE)

if((handleMACD = iCustom(m_symbol.Name(),0, "MACD", MACDFastEMA, MACDSlowEMA, MACDSignalSMA, MACDAppliedPrice)

) == INVALID_HANDLE){

printf("Error Creating MACD");

return(false);

}

if(MACD2handle == INVALID_HANDLE)

if((MACD2handle = iCustom(m_symbol.Name(),0, "MACD", MACDFastEMA2, SlowEMA2, MACDSignalSMA2, MACDAppliedPrice2)

) == INVALID_HANDLE){

printf("Error Creating MACD2");

return(false);

}

return(true);

}

void CustomExpert::Deinit()

{

if(MACDhandle != INVALID_HANDLE){

IndicatorRelease(MACDhandle);

MACDhandle = INVALID_HANDLE;

}

if(MACD2handle != INVALID_HANDLE){

IndicatorRelease(MACD2handle);

MACD2handle = INVALID_HANDLE;

}

}

funayot

From the code you posted I can not see that you initialized any of the instances of MACD with time frame too. You have to specify time frame at a time of obtaining the handle to indicator when using iCustom() in metatrader 5

 

In iCustom(m_symbol.Name(),0, "MACD", MACDFastEMA, MACDSlowEMA, MACDSignalSMA, MACDAppliedPrice)

Zero should be replaced with timeFrame1 and timeFrame2 in the two MACD handles which would have been initialized separately in ENUM_TIMEFRAMES timeFrame1 = PERIOD_M5.

ENUM_TIMEFRAMES timeFrame2 = PERIOD_H1. Now I get it. Any indicator can be substituted in the code as appropriate. Thanks Mladen.

mladen:
funayot From the code you posted I can not see that you initialized any of the instances of MACD with time frame too. You have to specify time frame at a time of obtaining the handle to indicator when using iCustom() in metatrader 5
 
funayot:
In iCustom(m_symbol.Name(),0, "MACD", MACDFastEMA, MACDSlowEMA, MACDSignalSMA, MACDAppliedPrice)

Zero should be replaced with timeFrame1 and timeFrame2 in the two MACD handles which would have been initialized separately in ENUM_TIMEFRAMES timeFrame1 = PERIOD_M5.

ENUM_TIMEFRAMES timeFrame2 = PERIOD_H1. Now I get it. Any indicator can be substituted in the code as appropriate. Thanks Mladen.

Good Now go on with coding (and occasional hair pulling )

 

Mladen, how can I code ea that executes this conditions:

Conditions:

1. If buy order(from indicator signal buffer2 is above buffer 1)

=open position

2. While open position, if take profit or trailing stop is hit, while the buy condition is still intact, wait for another fresh signal, even though buy condition is still intact.

For example, EMA 5 crosses EMA 34 and RSI above 50, buy. If take profit or trailing stop is hit, and buy condition is still intact, wait for another signal that is, buy condition/sell condition.

Thanks.

Reason: