HELP WITH CODE

 

Hello everybody,

It took time to me to learn good old mql4, then stayed some time away from it, and now that i come back i find it all changed. Far from discouraging, I am trying to use this changes to learn a little bit more than what i used to know.. Thus, i before only programmed few indicators and easy EA that displayed signals for me to manual trade. Now i want to automate everything, and I am trying to learn how functions work so that i can write long and extent codes and use them in multitimeframes and multi-asset EAs.


The code I am trying to buld now is the function that gives me the buy condition. But before i need to calculate the value of momentum over a custom indicator data array. So i am writing this: 

//-----Calculate MOMENTUM on KAMA data
double MomentumKAMA(int MK_Shift){       //----------as i will be comparing different shifts from this momentum this is the only parameter
   double KAMA[] = iCustom(NULL,0,"kaufman-adaptive-moving-average",5,0);   //-----store KAMA data in an array
   bool ArraySetAsSeries(KAMA , true);                                      //-----set that array as a timeseries
   return(iMomentumOnArray(KAMA,MK_Period+30,MK_Period,MK_Shift));}         //-----return my desired value

But it does not work. All variables are declared, some as input parameters, , and the errors I get from the MetaEditor are:

'KAMA' - invalid array access    prueba1.mq4    33    11
'ArraySetAsSeries' - function can be declared only in the global scope    prueba1.mq4    34    9
'KAMA' - declaration without type    prueba1.mq4    34    26
'true' - declaration without type    prueba1.mq4    34    33
'true' - comma expected    prueba1.mq4    34    33
5 error(s), 0 warning(s)        6    1


Any help will be appreciated. The aim is to get a value for the momentum in order to be compared in my BUY Condition function. Thanks!

 
i can post the whole code i have at this moment if needed, please help!
 
xavilin: It took time to me to learn good old mql4, then stayed some time away from it, and now that i come back i find it all changed.
double KAMA[] = iCustom(NULL,0,"kaufman-adaptive-moving-average",5,0);
bool ArraySetAsSeries(KAMA , true);
return(iMomentumOnArray(KAMA,MK_Period+30,MK_Period,MK_Shift));}   
  1. Hasn't changed, only enhanced with OOP, and now trying to be helpful about some wrong things.
  2. None of your problems are related to the new. Your code would still fail to compile with the old. learn to code
  3. Detailed explanation of iCustom - MQL4 forum ICustom returns a single double, how can you assign that to a zero length array?
  4. ArraySetAsSeries is known function. You appear to declaring something as a bool that takes a type KAMA and a type true? Just call it (no bool.)
 
WHRoeder:
xavilin: It took time to me to learn good old mql4, then stayed some time away from it, and now that i come back i find it all changed.
  1. Hasn't changed, only enhanced with OOP, and now picky about some wrong things.
  2. None of your problems are related to the new. Your code would still fail to compile with the old.
  3. Detailed explanation of iCustom - MQL4 forum ICustom returns a single double, how can you assign that to a zero length array?
  4. ArraySetAsSeries is known function. You appear to declaring something as a bool that takes a type KAMA and a type true?

I know my issues are with old version too. As I said, I wanted now to learn things I didn't learn before. And besides OOP, also init, deinnit and start (ontick) functions changed as well. Your answer was very useful, I do not want anybody to code this for me, I wanted exactly what you did, to put my eyes on what I did wrong. I understand the problem you answered on point 3., I'll take a look at it. However, I do not understand point 4.: from the F1 I thought ArraySetAsSeries is a bool function, what are you trying to point that I cannot see?


Thanks again, I'll try to read on what you pointed. If I have any more doubts I cannot solve by myself I'll ask here again!

 

xavilin: I  know my issues are with old version too.

also init, deinnit and start (ontick) functions changed as well.

I thought ArraySetAsSeries is a bool function, what are you trying to point that I cannot see?

  1. Then why did you complain that "it all changed."
  2. They haven't changed, you can still use the old functions or the new.
  3. It is a bool. Stop declaring it and call it. You responded while I was still editing. Just call it (no bool.)
 
WHRoeder:

xavilin: I  know my issues are with old version too.

also init, deinnit and start (ontick) functions changed as well.

I thought ArraySetAsSeries is a bool function, what are you trying to point that I cannot see?

  1. Then why did you complain that "it all changed."
  2. They haven't changed, you can still use the old functions or the new.
  3. It is a bool. Stop declaring it and call it. You responded while I was still editing. Just call it (no bool.)

1.I did not complain, I stated it from my ignorance trying to make kind of a joke. Do not be in defensive mode please, I find this community really helpful for people willing to learn, and if there's been people complaining about it i have nothing to do with them. I understand this forum is to find this kind of help, if i want my code programmed I'll just hire someone, and if I want to complain, I know I should do it to mql5 admins. Peace man!

2.Thanks, I did not know that.

3.Thanks again!

 

If anyone is interested, I found the solution writing this:


//-----Calculate MOMENTUM on KAMA data
double MomentumKAMA(int MK_Shift){
   int i,                           // Bar index
       Counted_bars;                // Number of counted bars
   double KAMA[],MK[];
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {KAMA[i] = iCustom(NULL,0,"kaufman-adaptive-moving-average",K_Period,K_Fast,K_Slow,5,i);   //array containing KAMA data. KAMA parameters are EA input int variables      
      MK[MK_Shift] = iMomentumOnArray(KAMA,50,MK_Period,MK_Shift);       //array containing MomentumKAMA data
      i--; }                        // Calculating index of the next bar;}
   return(MK[MK_Shift]);}           //returns the exact value of my indicator over KAMA for the given function parameter(shift) so i can compare different time periods (NOT FRAMES!)


Hope this helps somebody.

 
xavilin: I found the solution writing this:
   double KAMA[],MK[];
:
     {KAMA[i] = iCustom(NULL,0 ...
Trying to write to a zero length array isn't a solution.
 
WHRoeder:
xavilin: I found the solution writing this:
Trying to write to a zero length array isn't a solution.



then please give me some tip instead of trolling, i know you know a lot about arrays as i've seen you on some of the documentation i've been looking for ( http://www.forexfactory.com/showthread.php?t=165411 ), i've done my homework, investigated, read and find out how to write a code which i thought was going to work as it gives no error when compiled (haven't tested yet, maybe i should add some print statement and test it to see what it returns. could you at least help me in this point, please?). if i am in this forum is in search for help, and i've had little 'til the moment, sincerely.



back to the problem, the only documentation i've found on the internet of someone trying to do what i am aiming to do is seen in this link (if it is spam please ask and i'll remove):

http://www.metatrader4.com/forum/378

But unfortunately i do not fully understand what it is done there, so i tryed to adapt this code to something i more less understand. But it happens to be wrong and i get no help...

 
anyone: could my mistake be somehow solved with arrayresize or similar? thanks?
 
i finally came with this, hope now works. at least it gives no compilation error. i decided to put together both MK calculation function and trade-decision function. if anyone sees a major mistake please comment. print statements are still missing, i'll write them when start testing the EA. thanks to everybody!
//-----decide wether to trade or not and which kind of trade (B/S)
void Trade_Condition(){
   int i,                           // Bar index,
       Counted_bars;                // Number of counted bars
   double KAMA[],MK[];
   ArrayResize(KAMA,MK_Period+5);
   ArrayResize(MK,5);
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {KAMA[i] = iCustom(NULL,0,"kaufman-adaptive-moving-average",K_Period,K_Fast,K_Slow,5,i);   //array containing KAMA data      
      MK[i] = iMomentumOnArray(KAMA,50,MK_Period,i);       //array containing MomentumKAMA data
      i--; }                        // Calculating index of the next bar;}
      
//----Now check for Buy or Sell conditions
      static bool BUY = False;             //---define the variable that will give the go for a buy
      static bool SELL = False;            //---define the variable that will give the go for a sell
   if (MK[1]>MK_Bands && MK[2]<=MK_Bands){ //---Buy condition 
      BUY = True;}
   if (MK[1]<MK_Bands && MK[2]>=-MK_Bands){ //---Sell condition
      SELL = True;}
}                                   
one doubt that i have is wether i should call ArrayResize in the init function (with array declarations in the general part) or leave it this way. also, if static bool BUY and SELL should be declared in the function or outside it.
Reason: