How can we design EA with indicator wich have differents timeframeS ?

 

MACD_H1 which is an MACD with a period=60 does'nt have any action in this EA .I need your help please

Here is my EA code :

#define MAGICMA 1970
//--- Inputs
input double Lots =0.01;
input double TakeProfit =5000;
input double StopLoss=1000;
input int TrailingStop=1000;
input string Choose_Lot="0.Manual/1.Automatic";
input bool Mode_Lot=0;
input string MODE_MANUAL="";
input double Volume_Lot=0.01;
input string MODE_AUTOMATIC="MaxiRisk=0.1 : Lot=10,000 si Marge=1,000";
input double MaxiRisk=0.1;
input string Period_MA="";
input int FastPeriod=25;
input int SlowPeriod =100;
input string Period_MACD_H1="";
input int FastPeriod2=12;
input int SlowPeriod2 =26;
input int signalPeriod2=9;
input string Period_AC_ou_MACDHisto="";
input int FastPeriod3=24;
input int SlowPeriod3 =52;
input int signalPeriod3=18;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick(void)
{
double ma25,ma100,MACD_H1,MACD,S_MACD,AC;



//--- go trading only for first tiks of new bar

//--- get Moving Average
ma25=iMA(NULL,0,FastPeriod,0,MODE_LWMA,PRICE_CLOSE,1);
ma100=iMA(NULL,0,SlowPeriod,0,MODE_LWMA,PRICE_CLOSE,1);
MACD_H1=iMA(NULL,60,FastPeriod2,0,MODE_EMA,PRICE_CLOSE,1)-
iMA(NULL,60,SlowPeriod2,0,MODE_EMA,PRICE_CLOSE,1);
MACD=iMACD(NULL,0,FastPeriod3,SlowPeriod3,signalPeriod3,PRICE_CLOSE,MODE_MAIN,1);
S_MACD=iMACD(NULL,0,FastPeriod3,SlowPeriod3,signalPeriod3,PRICE_CLOSE,MODE_SIGNAL,1);
AC=MACD-S_MACD;
int cnt,ticket,total;
//---
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
//---
if(Bars<100)
{
Print("bars less than 100");
return;
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return;
}
//--- to simplify the coding and speed up access data are put into internal variables

total=OrdersTotal();
if(total<1)
{
//--- no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ",AccountFreeMargin());
return;
}
//--- check for long position (BUY) possibility
if(ma25>ma100 && MACD_H1>0 && AC>0)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"3Winners",MAGICMA,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else
Print("Error opening BUY order : ",GetLastError());
return;
}
//--- check for short position (SELL) possibility
if(ma25<ma100 && MACD_H1<0 && AC<0)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"3Winners",MAGICMA,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
}
else
Print("Error opening SELL order : ",GetLastError());
}
//--- exit from the "no opened orders" block
return;
}
//--- it is important to enter the market correctly, but it is more important to exit it correctly...
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
//--- long position is opened
if(OrderType()==OP_BUY)
{
//--- should it be closed?
if(ma25<ma100 && MACD_H1<0 && AC<0)
{
//--- close order and exit
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return;
}
//--- check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
//--- modify order and exit
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return;
}
}
}
}
else // go to short position
{
//--- should it be closed?
if(ma25>ma100 && MACD_H1>0 && AC>0)
{
//--- close order and exit
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return;
}
//--- check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
//--- modify order and exit
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return;
}
}
}
}
}
}
//---
}
//+------------------------------------------------------------------+
Files:
3winners.mq4  7 kb
 

Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.

 
osirel:

MACD_H1 which is an MACD with a period=60 does'nt have any action in this EA .I need your help please

Here is my EA code :

MACD_H1=iMA(NULL,60,FastPeriod2,0,MODE_EMA,PRICE_CLOSE,1)-
iMA(NULL,60,SlowPeriod2,0,MODE_EMA,PRICE_CLOSE,1);

I don't see you coded a MACD....

 
//--- check for long position (BUY) possibility
if(ma25>ma100 && MACD_H1>0 && AC>0)

i think AC can never be > 0

Print("Error opening BUY order : ",GetLastError());

u have to ResetLastError() first & then check what err u get

 
qjol:

i think AC can never be > 0

...

Why are you thinking that, AC being the difference between main and signal buffer it can be positive or negative, not ?
 
angevoyageur:
Why are you thinking that, AC being the difference between main and signal buffer it can be positive or negative, not ?


u right my mistake, (i didn't notice it's a double, i thought it's n int)
 
deVries:
MACD_H1=iMA(NULL,60,FastPeriod2,0,MODE_EMA,PRICE_CLOSE,1)-
iMA(NULL,60,SlowPeriod2,0,MODE_EMA,PRICE_CLOSE,1);
I don't see you coded a MACD....
Perhaps you should look at the MACD code
Moving Average Convergence/Divergence, MACD - MQL4 Code Base
for(int i=0; i<limit; i++)
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,​i)-iMA(NULL,​0,​SlowEMA,​0,​MODE_EMA,​PRICE_CLOSE,​i);
//---- signal line counted in the 2-nd buffer
for(i=0; i<limit; i++)
SignalBuffer[i]=iMAOnArray(MacdBuffer,​Bars,​SignalSMA,​0,​MODE_SMA,​i);
I see a MACD coded.
 
WHRoeder:
Perhaps you should look at the MACD code
Moving Average Convergence/Divergence, MACD - MQL4 Code Base
for(int i=0; i<limit; i++)
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,​i)-iMA(NULL,​0,​SlowEMA,​0,​MODE_EMA,​PRICE_CLOSE,​i);
//---- signal line counted in the 2-nd buffer
for(i=0; i<limit; i++)
SignalBuffer[i]=iMAOnArray(MacdBuffer,​Bars,​SignalSMA,​0,​MODE_SMA,​i);
I see a MACD coded.

Allright i see now,thanks.
 

In tester it works

but on account it can fail if there are other trades not from this EA open

total=OrdersTotal();
if(total<1)

or if your broker is ECN then you're not allowed to open buy or sell with directly stoploss and/or takeprofit level

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"3Winners",MAGICMA,0,Green);

also slippage is not set to value of 5 digit account

did you check terminal to see

what this line

Print("Error opening BUY order : ",GetLastError());

was telling ??

 
deVries:

or if your broker is ECN then you're not allowed to open buy or sell with directly stoploss and/or takeprofit level

Is this true? I though it was something that was fixed in the MT4 terminal sometime last year?
 
ydrol:
Is this true? I though it was something that was fixed in the MT4 terminal sometime last year?

It's not more true since Build 5xx from May 2013 (I didn't check, it's from memory). You can't place a SL/TP directly with MT4, but you can do it by code with mql4.