I want to put TEMA and MACD together to use. But my code did not work well.
I set MACD buffer reverse then copied data into buffer. But after I test this code, I found there are two MACD but without signal line. As you can see from screencutting, the first MACD is right while the second MACD's data is actually the data of signal. And in the meantime, the signal data became 0. Then I cannot plot the signal line.
Why this happened? I only set one MACD buffer and one MACD indicator_label in code. By the way, the second MACD window is set by using MT5's insert indicator window funtion. I just want to use this window's data to check whether I plot right. The problem is in the blue circle's data......
COLOR_HISTOGRAM needs two buffers. It takes the signal line as color buffer for the previous histogram. check attached file.
Then I want to move MACD code from "int OnCalculate()" into "void OnTick()", and delete the "int OnCalculate()" . The code has been changed like this.
//+------------------------------------------------------------------+ //| TEMA+MACD testEA.mq5 | //+------------------------------------------------------------------+ #include <Trade\Trade.mqh> input int InpFastEMA=2; // Fast EMA period input int InpSlowEMA=5; // Slow EMA period input int InpSignalSMA=2; // Signal SMA period input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price input int fastTEMA_Period=5; input int slowTEMA_Period=30; input int EA_Magic=234508; input double Lot=0.01; int MACD_Handle; int fastTEMAHandle; int slowTEMAHandle; double MACDBuffer[]; double SignalBuffer[]; double fastTEMAVal[]; double slowTEMAVal[]; CTrade trade; //+------------------------------------------------------------------+ int OnInit() { SetIndexBuffer(0,MACDBuffer,INDICATOR_DATA); SetIndexBuffer(1,SignalBuffer,INDICATOR_DATA); SetIndexBuffer(2,fastTEMAVal,INDICATOR_DATA); SetIndexBuffer(3,slowTEMAVal,INDICATOR_DATA); ArraySetAsSeries(fastTEMAVal,true); ArraySetAsSeries(slowTEMAVal,true); MACD_Handle = iMACD(NULL,0,InpFastEMA,InpSlowEMA,InpSignalSMA,InpAppliedPrice); fastTEMAHandle=iTEMA(NULL,0,fastTEMA_Period,0,PRICE_CLOSE); slowTEMAHandle=iTEMA(NULL,0,slowTEMA_Period,0,PRICE_CLOSE); if(fastTEMAHandle<0 || slowTEMAHandle<0 || MACD_Handle<0) { Alert("Error in creating handles. Wrong:",GetLastError(),"!!"); } return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ void OnDeinit(const int reason) { IndicatorRelease(MACD_Handle); IndicatorRelease(fastTEMAHandle); IndicatorRelease(slowTEMAHandle); } //+------------------------------------------------------------------+ void OnTick() { MqlTick latest_tick; MqlTradeRequest mrequest; MqlTradeResult mresult; ZeroMemory(mrequest); double MACD[]; double Signal[]; ArraySetAsSeries(MACD,true); ArraySetAsSeries(Signal,true); ArraySetAsSeries(MACDBuffer,true); ArraySetAsSeries(SignalBuffer,true); CopyBuffer(MACD_Handle,0,0,3000,MACD); CopyBuffer(MACD_Handle,1,0,3000,Signal); for(int i=0; i<3000; i++) { MACDBuffer[i]=MACD[i]; SignalBuffer[i]=Signal[i]; } bool Buy_Condition; bool Sell_Condition; if (Buy_Condition) { } if (Sell_Condition){ } }
But when I put this code into back test, there is an "array out of range" error where is in the " MACDBuffer[i]" in for loop.
Why this happened? This for loop has been checked in "int OnCalculate()" where it worked well. But this for loop cannot work well in " "void OnTick()".....
Then I want to move MACD code from "int OnCalculate()" into "void OnTick()", and delete the "int OnCalculate()" . The code has been changed like this.
But when I put this code into back test, there is an "array out of range" error where is in the " MACDBuffer[i]" in for loop.
Why this happened? This for loop has been checked in "int OnCalculate()" where it worked well. But this for loop cannot work well in " "void OnTick()".....
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I want to put TEMA and MACD together to use. But my code did not work well.
I set MACD buffer reverse then copied data into buffer. But after I test this code, I found there are two MACD but without signal line. As you can see from screencutting, the first MACD is right while the second MACD's data is actually the data of signal. And in the meantime, the signal data became 0. Then I cannot plot the signal line.
Why this happened? I only set one MACD buffer and one MACD indicator_label in code. By the way, the second MACD window is set by using MT5's insert indicator window funtion. I just want to use this window's data to check whether I plot right. The problem is in the blue circle's data......