Discussion of article "Better Programmer(Part 02): Stop doing these 5 things to become successful MQL5 programmer"

 

New article Better Programmer(Part 02): Stop doing these 5 things to become successful MQL5 programmer has been published:

This is the Must read article for anyone wanting to improve their programming career, This Article series is aimed at making you the best programmer you can possibly be no matter how experienced you are, the discussed ideas works for MQL5 programming newbies as well as Professionals

I use the word noob a lot in this Article series to describe someone with less experience in MQL5 programming (don't get offended by that) , It makes it easy to relate to , This doesn't mean that this article is only for noobs absolutely not , this is for everyone regardless of the programming experience you have because what makes you a noob or not is your coding habits and behaviors... not years of coding

noobs_vs_professional coder 


The first Article is here for those who haven't read it , so let me continue this mission to turn noobs to professionals

Author: Omega J Msigwa

 
Thanks for your article ...
 

Hi guys,

I'm a noob. I've got plenty programming experience but only starting to code in MQL5 - it's my first language similar to C++. I've been reading through the reference docs but cannot spot my error - the value that I expect to be returned from expoMA is getting printed/commented on my chart as 0.0 - am I making some sort of syntax error? I receive no issues/errors compiling. Any input you have would be great

void OnTick(
  {   
   int days = 3; 
   
   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
  
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   double movingAverageValues[];
   
   double bidAskAverage = (Ask + Bid)/2;
   
   int exponentialMovingAverage = iMA(_Symbol,_Period,MA_Period,0,MODE_EMA,PRICE_CLOSE);
      
   ArraySetAsSeries(movingAverageValues,true);
   
   CopyBuffer(exponentialMovingAverage,0,0,3,movingAverageValues);

   double alpha = expoMA(days);
   
   double dEMA = bidAskAverage - movingAverageValues[1]; // index 1 here represents the EMA of the t - 1 value
      
   Comment("The ask price is: " +     DoubleToString(Ask,5) + "\n"
           "The bid price is: " + DoubleToString(Bid,5) + "\n" 
           "The average price is: " + DoubleToString(bidAskAverage,5) + "\n"
           "The custom expoMA value is: " + DoubleToString(alpha,5) + "\n"
           "The dEMA value is: " + DoubleToString(dEMA,5)) ;   
   }

double expoMA(int days=2)
   {   
    double alpha = 2 / (days + 1);   
    NormalizeDouble(alpha,5);
    return(alpha);
   }
 
spinner461:

Hi guys,

I'm a noob. I've got plenty programming experience but only starting to code in MQL5 - it's my first language similar to C++. I've been reading through the reference docs but cannot spot my error - the value that I expect to be returned from expoMA is getting printed/commented on my chart as 0.0 - am I making some sort of syntax error? I receive no issues/errors compiling. Any input you have would be great

double expoMA(int days=2)
   {   
    double alpha = 2.0 / (days + 1);

    //...
   }

You're dividing 2/4 in integers (0), and then that is parsed into a double. Instead, you should have one of between dividend or divisor be a double (so the division is done in the "most detailed" data type)

Other options could be (being days an int):

double alpha = double(2) / (days + 1);
double alpha = 2 / (days + 1.0);
double alpha = 2 / double(days + 1);
That's probably a python-ish mistake heheh
 

Manuel Alejandro Cercos Perez:

You're dividing 2/4 in integers (0), and then that is parsed into a double. Instead, you should have one of between dividend or divisor be a double (so the division is done in the "most detailed" data type)

Other options could be (being days an int):

That's probably a python-ish mistake heheh


Hey Manuel,

Thanks so much! The above tip worked like a charm. You've saved me pouring many more hours into finding the root cause.

 
How can I make a function that only allow 1 trade or open position per candle not only at the beginning of the candle but until new candle forms. The newbar function only allows at beginning of new candle only 
 
There are a lot of scam bags
 
If it's MQL5, you should init the iMA in OnInit.

Else you will do it on every tick received.

Your last question: take the open time and add
Period seconds()/2 to it, then you are half way into the period.





 
Dominik Egert:
If it's MQL5, you should init the iMA in OnInit.

Else you will do it on every tick received.

Your last question: take the open time and add
Period seconds()/2 to it, then you are half way into the period.





Every question about coding help should be posted on the forum as an independent topic
 
You dsYoug Chee #:
How can I make a function that only allow 1 trade or open position per candle not only at the beginning of the candle but until new candle forms. The newbar function only allows at beginning of new candle only 

You should check open times of your positions and whether one already exists in the specified period of the candle. If it exists, then do nothing. If it does not exist,then open a new position.

Reason: