Thanks for your guide, but i still get the same error with the following code,
int start()
{
int cnt=0;
double macd_histogram[3];
int barCount;
//---- check for history and trading
if (Bars<100 || IsTradeAllowed()==false) return;
if (barCount == Bars && Bars < 120) //new histograms
return;
for(int k = 1; k >= 0; k--)
macd_histogram[k] = iOsMA(NULL, 0, 5, 35, 5, PRICE_OPEN, k);
if (CalculateCurrentOrders(Symbol()) != 0)
{
for(cnt=0; cnt<OrdersTotal(); cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY)
{
if(macd_histogram[0]<macd_histogram[1])
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
}
if(OrderType()==OP_SELL)
{
if(macd_histogram[0]>macd_histogram[1])
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
}
}
}
}
Thanks for helping !!!
How to get earlier bars?
Macdbar0 = MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
Macdbar1 = MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
Macdbar2 = MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);
Macdbar3 = MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,3);
...
MacdbarX = MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,X);
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
How To write the Macd code for expert advisor that compare with previous 2 bar? Like the picture as shown below.
From the MACDSample ea, i get the following code,
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);
But it only can compare the current and the previous.
And 1 more thing is when i compare the current with previous, i backtest the ea and get the following problem, the problem is it straight close the order after it open an order.
with the following code,
double MacdCurrent = iMACD(Symbol(),NULL,dMacd1,dMacd2,dMacd3,PRICE_CLOSE,MODE_MAIN,0);
double MacdPrevious = iMACD(Symbol(),NULL,dMacd1,dMacd2,dMacd3,PRICE_CLOSE,MODE_MAIN,1);
if (CalculateCurrentOrders(Symbol()) != 0)
{
if(OrderType()==OP_BUY)
{
if(MacdCurrent>MacdPrevious)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
}
}
Anyone can help ? Thanks.