Unnecessary "0" at the beginning of as the result of the iCustom function

 

Hi !

So I created a simple Itchimoku strategy, with 5 iCustom functions. 

Unfortunately, although all values printed from iCustom functions work correctly, one "Itchim4", which is supposed to print a certain value of S&P500, prints "0" at the beginning. ( I have attached screenshot if these values below in the "Przechwytywanie.PNG" file).


Could you please help me with removing the 0 from the result? 


double Itchim0,Itchim1,Itchim2, Itchim3,Itchim4,Itchim5,Itchim6,Itchim7; 



double ItchimBuffer0[],ItchimBuffer1[],ItchimBuffer2[],ItchimBuffer3[],ItchimBuffer4[],ItchimBuffer5[],ItchimBuffer6[],ItchimBuffer7[];

double LastHigh; 
double LastLow; 

int OnInit() 
   {
   Print("Version 1.15");

   return(INIT_SUCCEEDED);
   }                   
int start()
  {  
  LastHigh = iHigh (NULL,0,1);
  LastLow = iLow(NULL,0,1);
  
  Print("Previous High/Low =" ,  iTime(NULL,0,1),",",
                                 iHigh(NULL, 0,1),",",
                                 iLow(NULL,0,1));
                                
   Itchim0=iCustom(NULL,0,"Ichimoku",7,24,46,0,0);
   Itchim1=iCustom(NULL,0,"Ichimoku",7,24,46,1,0);
   Itchim2=iCustom(NULL,0,"Ichimoku",7,24,46,2,0);
   Itchim3=iCustom(NULL,0,"Ichimoku",7,24,46,3,0);
   Itchim4=iCustom(NULL,0,"Ichimoku",7,24,46,4,0);
   Itchim5=iCustom(NULL,0,"Ichimoku",7,24,46,5,0);       
   Print("Ichimoku = ",Itchim0," ",Itchim1," ",Itchim2," ",Itchim3," ",Itchim4,"",Itchim5);  


   CheckForOpen();
   Print ("InsideStartAfterCheckForOpen");
   return (0);
   }
void CheckForOpen()
{
   int res;
   Print ("InsideCheckForOpen");   
   if(Volume[0]>1) return;
   if (( Itchim0 > Itchim1) &&  ((iClose(NULL,PERIOD_CURRENT,0) > Itchim3)) && ((iClose(NULL,PERIOD_CURRENT,0) > Itchim4)) && (OrdersTotal() < 1 ) && ((iClose(NULL,PERIOD_CURRENT,0) > Itchim0)))
   {
      res=OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-50000*Point,Ask+100000*Point,"SEMA",0,0,Blue);
     // CloseSellPositions();
   }                          
   if (( Itchim0 < Itchim1) && ((iClose(NULL,PERIOD_CURRENT,0)) < Itchim2) && ((iClose(NULL,PERIOD_CURRENT,0) < Itchim3)) && ((iClose(NULL,PERIOD_CURRENT,0) < Itchim4)) )
   {  
   //   res=OrderSend(Symbol(),OP_SELL,1,Bid,3,Ask+50000*Point,Ask-100000*Point,"SEMA",0,0,Blue);
      CloseBuyPositions();
   }
}      
void CloseBuyPositions()   
{ 
   int res;  
   for (int i=OrdersTotal()-1; i >= 0; i--)
   {    
      res=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);      
      string CurrencyPair=OrderSymbol();
      if ((OrderType ()==OP_BUY)&& (_Symbol== CurrencyPair))       
      {
         res=OrderClose(OrderTicket(), OrderLots(),Bid,3,NULL);
         Print ("Pozycje Buy Zamknięte");
      }
   }
}  
void CloseSellPositions()   
{ 
   int res;  
   for (int i=OrdersTotal()-1; i >= 0; i--)
   {
      res=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);      
      string CurrencyPair=OrderSymbol();   
      if ((OrderType ()==OP_SELL)&& (_Symbol== CurrencyPair))    
      {
         res=OrderClose(OrderTicket(), OrderLots(),Ask,3,NULL);
         Print ("Pozycje Sell Zamknięte");   
      } 
   }
} 

Files:
 
Sebastian Woźniczka:

Hi !

So I created a simple Itchimoku strategy, with 5 iCustom functions. 

Unfortunately, although all values printed from iCustom functions work correctly, one "Itchim4", which is supposed to print a certain value of S&P500, prints "0" at the beginning. ( I have attached screenshot if these values below in the "Przechwytywanie.PNG" file).


Could you please help me with removing the 0 from the result? 


I did not run your code but it looks like the line which prints is:

   Print("Ichimoku = ",Itchim0," ",Itchim1," ",Itchim2," ",Itchim3," ",Itchim4,"",Itchim5);  

I suggest you use PrintFormat - more control over decimals, field width, leading 0's, sign etc. It is all in the documentation but here is an example:

#define def_decimalPlaces 3
   PrintFormat("Ichimoku = %+.*f %+.*f %+.*f %+.*f %+.*f %+.*f",
               def_decimalPlaces, Itchim0,
               def_decimalPlaces, Itchim1,
               def_decimalPlaces, Itchim2,
               def_decimalPlaces, Itchim3,
               def_decimalPlaces, Itchim4,
               def_decimalPlaces, Itchim5
               );

 
R4tna C #: I suggest you use PrintFormat - more control over decimals, field width, leading 0's, sign etc. It is all in the documentation but here is an example:
Asterisk has been broken in MT4 for a very long time.
PrintFormat("<%.*f>", 4, 0.1234567); // testscr: < .*f>
Asterisk work around.
      string format=StringFormat("<%%.%if>", 4);
  PrintFormat(format, 0.1234567); // testscr: <0.1235> 
or
     string format="<%."+string(Digits())+"f>";
  PrintFormat(format, 0.1234567); // testscr: <0.12346>
 
William Roeder #:
Asterisk has been broken in MT4 for a very long time.
Asterisk work around.
or

I use MQL5 and now see this is a MQL4 question so thanks for pointing that out

 
William Roeder #:
Asterisk has been broken in MT4 for a very long time.
Asterisk work around.

Thank you for your reply!


I applied your advice and it works correctly in every case, apart from the "Itchim4" function, so the value which I have been struggling with from the beginning. 

Here, the PrintFormat prints value: "0.0000".


Do you have maybe any other ideas on how to delete the 0, which is printed before the correct value? 

 
Sebastian Woźniczka:

Hi !

So I created a simple Itchimoku strategy, with 5 iCustom functions. 

Unfortunately, although all values printed from iCustom functions work correctly, one "Itchim4", which is supposed to print a certain value of S&P500, prints "0" at the beginning. ( I have attached screenshot if these values below in the "Przechwytywanie.PNG" file).


Could you please help me with removing the 0 from the result? 


Print("Ichimoku = ",Itchim0," ",Itchim1," ",Itchim2," ",Itchim3," ",Itchim4,"",Itchim5);  

It prints 0 for Itchim4, then it prints 4069.775 for  Itchim5. There is no space between the 2 numbers.

 
Keith Watford #:

It prints 0 for Itchim4, then it prints 4069.775 for  Itchim5. There is no space between the 2 numbers.

Oh yes indeed, thank you !

 
Keith Watford #:

It prints 0 for Itchim4, then it prints 4069.775 for  Itchim5. There is no space between the 2 numbers.

Wow - well spotted!

 
Sebastian Woźniczka #:

Oh yes indeed, thank you !

Can I just add the importance of using meaningful variable names in your codes, especially when you want other people's help.

   Itchim0=iCustom(NULL,0,"Ichimoku",7,24,46,0,0);
   Itchim1=iCustom(NULL,0,"Ichimoku",7,24,46,1,0);
   Itchim2=iCustom(NULL,0,"Ichimoku",7,24,46,2,0);
   Itchim3=iCustom(NULL,0,"Ichimoku",7,24,46,3,0);
   Itchim4=iCustom(NULL,0,"Ichimoku",7,24,46,4,0);
   Itchim5=iCustom(NULL,0,"Ichimoku",7,24,46,5,0);       
   Print("Ichimoku = ",Itchim0," ",Itchim1," ",Itchim2," ",Itchim3," ",Itchim4,"",Itchim5);  

These variable names are not helpful as you are unlikely to remember what they represent especially if they are used later in your code.

Usually when I see something like this, I will read no further.


With itchimoku you should use the obvious variable names like kijun, tenkan, spanA  etc.

That way you know instantly what they represent and the code is much easier to read.

Also, when printing it is better to do 

Print("kijun=",kijun," tenkan=",tenkan); //etc

You would have spotted your error immediately.