The biggest mistake on your MQL5 code:
void OnTick () { string signal= "" ; double myMovingAverafeArray1[],myMovingAverafeArray2[],myMovingAverafeArray3[]; int movingAverageDefinition1 = iMA ( _Symbol , _Period , 8 , 0 , MODE_EMA , PRICE_CLOSE ); int movingAverageDefinition2 = iMA ( _Symbol , _Period , 13 , 0 , MODE_EMA , PRICE_CLOSE ); int movingAverageDefinition3 = iMA ( _Symbol , _Period , 21 , 0 , MODE_EMA , PRICE_CLOSE );
You on EACH TICK create several indicator handles!
The correct example is from the link: iMA
- www.mql5.com
Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
How to call indicators in MQL5 - MQL5 Articles 12 March 2010
Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
How to call indicators in MQL5 - MQL5 Articles 12 March 2010
I'm getting really frustrated at this point.
So i put the iMA handler in OnInit() and it still doesnt really work. This time it tells its something with the Copybuffer function.
(undeclared identifier)
//+------------------------------------------------------------------+ //| oninit.mq5 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ void OnInit() { //--- int EMA8 = iMA(_Symbol,_Period,8,0,MODE_EMA,PRICE_CLOSE); int EMA13 = iMA(_Symbol,_Period,13,0,MODE_EMA,PRICE_CLOSE); int EMA21 = iMA(_Symbol,_Period,21,0,MODE_EMA,PRICE_CLOSE); //--- } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- double _ma1[],_ma2[],_ma3[]; ArraySetAsSeries(_ma1, true); ArraySetAsSeries(_ma2, true); ArraySetAsSeries(_ma3, true); if(CopyBuffer(EMA8,0,0,5,_ma1)!=5) return; if(CopyBuffer(EMA13,2,0,5,_ma2)!=5) return; if(CopyBuffer(EMA21,3,0,5,_ma3)!=5) return; } //+------------------------------------------------------------------+
Of course, it does. I said "you get that in OnInit." You did, and then through the values away.
In CopyBuffer you use an undeclared variable. So of course, that doesn't compile.
Put your handler variables outside of OnInit so you can pass them to CopyBuffer. Assign to the
variables in OnInit.
I'm getting really frustrated at this point.
So i put the iMA handler in OnInit() and it still doesnt really work. This time it tells its something with the Copybuffer function.
(undeclared identifier)
We read the help:
Call by the first position and the number of required elements
int CopyBuffer( int indicator_handle, // indicator handle int buffer_num, // indicator buffer number int start_pos, // start position int count, // amount to copy double buffer[] // target array to copy );
Code:
//+------------------------------------------------------------------+ //| oninit.mq5 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" //--- int handle_iMA8; int handle_iMA13; int handle_iMA21; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- handle_iMA8 = iMA(Symbol(),Period(),8,0,MODE_EMA,PRICE_CLOSE); //--- if the handle is not created if(handle_iMA8==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d", Symbol(), EnumToString(Period()), GetLastError()); //--- the indicator is stopped early return(INIT_FAILED); } //--- handle_iMA13 = iMA(Symbol(),Period(),13,0,MODE_EMA,PRICE_CLOSE); //--- if the handle is not created if(handle_iMA13==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d", Symbol(), EnumToString(Period()), GetLastError()); //--- the indicator is stopped early return(INIT_FAILED); } //--- handle_iMA21 = iMA(Symbol(),Period(),21,0,MODE_EMA,PRICE_CLOSE); //--- if the handle is not created if(handle_iMA21==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d", Symbol(), EnumToString(Period()), GetLastError()); //--- the indicator is stopped early return(INIT_FAILED); } //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- double array_ima8[],array_ima13[],array_ima21[]; ArraySetAsSeries(array_ima8, true); ArraySetAsSeries(array_ima13, true); ArraySetAsSeries(array_ima21, true); int start_pos=0,count=6; if(CopyBuffer(handle_iMA8,0,0,count,array_ima8)!=count) return; if(CopyBuffer(handle_iMA13,0,0,count,array_ima13)!=count) return; if(CopyBuffer(handle_iMA21,0,0,count,array_ima21)!=count) return; } //+------------------------------------------------------------------+
We read the help:
Call by the first position and the number of required elements
Code:
Cool. So now the EMAs work, but its still very broken. My if clauses still dont seem to work. Also, why do you set int start_pos and count on the
Ontick event handler and at the top where the int handles for the EMAs. These value are not suppose to change ontick, or am i missing out
something ?
However, heres my code. Everything beyond the OnTick function is identical to your example:
void OnTick() { //---- double array_ima50[],array_ima100[],array_ima150[]; ArraySetAsSeries(array_ima50, true); ArraySetAsSeries(array_ima100, true); ArraySetAsSeries(array_ima150, true); int start_pos=0,count=6; if(CopyBuffer(handle_iMA50,0,0,count,array_ima50)!=count){ return; } if(CopyBuffer(handle_iMA100,0,0,count,array_ima100)!=count){ return; } if(CopyBuffer(handle_iMA150,0,0,count,array_ima150)!=count){ return; } if (array_ima50[0] < array_ima100[0]){ if (array_ima100[0] < array_ima150[0]){ signal = "SELL"; } } if (array_ima50[0] > array_ima100[0]) { if (array_ima100[0] > array_ima150[0]) { signal = "BUY"; } } Comment("Signal:", signal ); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I have a problem with my EA.
It's calculateting the 21,13 and 8 eMA and it should give a Buy signal if the 8 - 13 - 21 are on top of each other in this sequence and a Sell signal if they are beneath each other.
I hope that you get what i am trying to say.
The thing is it works if i check for a Buy signal or a Sell signal individually, but when i try to check for both it wont load into the Tester.
Error messages:
MQL5 cannot open file 'Test.ex5'
Experts loading of Test failed
Here's my code:
Thanks for your time and help. :)