Getting different values of Heiken Ashi candles in Data Window & in my code

 

Hello,

Why I am getting different values of Heiken Ashi candles in Data Window & in my code values I am getting from my code is mentioned in highlighted logs. Can anyone guide me where I have made the mistake?

// Here is he chunk of code
datetime H1,H4;
double OPEN,CLOSE,H1EMAprev,HAHigh,HALow,HAOpen,HAClose,H1iHighest;
int total=9999999999,i;

   OPEN=iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,2,1);
   CLOSE=iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,3,1);
   H1EMAprev=NormalizeDouble(iMA(_Symbol,PERIOD_H1,14,0,MODE_EMA,PRICE_CLOSE,1),Digits());

   if(H1!=iTime(Symbol(),PERIOD_H1,0)) // new candle on D1
     {
      if(OPEN<CLOSE)
        {
         for(i=0;i<total;i++)
           {
            OPEN=NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,2,i),Digits);
            CLOSE=NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,3,i),Digits);
            HAHigh= NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",0,i),Digits);
            HALow = NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",1,i),Digits);
            HAOpen= NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",2,i),Digits);
            HAClose=NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",3,i),Digits);
            H1iHighest=NormalizeDouble(iHigh(Symbol(),PERIOD_H1,iHighest(Symbol(),PERIOD_H1,MODE_HIGH,i-1,i)),Digits);
            if(OPEN<CLOSE)
              {
               Print("its White, i: ",i,", HAHigh: ",HAHigh,", HALow: ",HALow,", HAOpen: ",HAOpen,", HAClose: ",HAClose,
                     ", OPEN: ",OPEN,", CLOSE: ",CLOSE,", Symbol: ",_Symbol,", H1iHighest: ",H1iHighest);
               break;
              }
           }
        }
      else
        {
         Print("its Red");
        }
      //Do Something...
      H1=iTime(Symbol(),PERIOD_H1,0);    // overwrite old with new value
     }

Heikin Ashi calculations... how do I get the high/low/close/open
Heikin Ashi calculations... how do I get the high/low/close/open
  • 2010.12.06
  • www.mql5.com
Hello, Is there anything out there that can get me the current Heikin Ashi candles high/low/close/open...
 
You are using the same name "heiken ashi" to all your objects... Are your sure that your are comparing the same things???
 
Complicated
 
kumaillakhani:

Hello,

Why I am getting different values of Heiken Ashi candles in Data Window & in my code values I am getting from my code is mentioned in highlighted logs. Can anyone guide me where I have made the mistake?

Hi,

in your code you are reading data of HA on the bar open moment. During that time the value of HA close has not been formed yet (note that HA open value is the same). But in data window you observe the candle after it has already been formed. Thus the difference.

 
Minions Labs:
You are using the same name "heiken ashi" to all your objects... Are your sure that your are comparing the same things???

// Here is he chunk of code
datetime H1,H4;
double OPEN,CLOSE,H1EMAprev,HAHigh,HALow,HAOpen,HAClose,H1iHighest;
int total=9999999999,i;

   OPEN=iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,2,1);
   CLOSE=iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,3,1);
   H1EMAprev=NormalizeDouble(iMA(_Symbol,PERIOD_H1,14,0,MODE_EMA,PRICE_CLOSE,1),Digits());

   if(H1!=iTime(Symbol(),PERIOD_H1,0)) // new candle on D1
     {
      if(OPEN<CLOSE)
        {
         for(i=0;i<total;i++)
           {
            // OPEN=NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,2,i),Digits);
            // CLOSE=NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,3,i),Digits);
            HAHigh= NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",0,i),Digits);
            HALow = NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",1,i),Digits);
            HAOpen= NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",2,i),Digits);
            HAClose=NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",3,i),Digits);
            H1iHighest=NormalizeDouble(iHigh(Symbol(),PERIOD_H1,iHighest(Symbol(),PERIOD_H1,MODE_HIGH,i-1,i)),Digits);
            if(OPEN<CLOSE)
              {
               Print("its White, i: ",i,", HAHigh: ",HAHigh,", HALow: ",HALow,", HAOpen: ",HAOpen,", HAClose: ",HAClose,
                     ", OPEN: ",OPEN,", CLOSE: ",CLOSE,", Symbol: ",_Symbol,", H1iHighest: ",H1iHighest);
               break;
              }
           }
        }
      else
        {
         Print("its Red");
        }
      //Do Something...
      H1=iTime(Symbol(),PERIOD_H1,0);    // overwrite old with new value
     }
I have commented OPEN & CLOSE but still no difference.
 
Andrey Barinov:

Hi,

in your code you are reading data of HA on the bar open moment. During that time the value of HA close has not been formed yet (note that HA open value is the same). But in data window you observe the candle after it has already been formed. Thus the difference.

  1. How can I read value of bars on close?
  2. Also in logs where I have highlighted it show that bar is white but in actual that bar is red why?

 
kumaillakhani:

How can I read value of bars on close?

Read it when it is closed :), meaning do not use bar shift ==0, but only starting from 1.

 
Andrey Barinov:

Read it when it is closed :), meaning do not use bar shift ==0, but only starting from 1.

  1. Also in logs where I have highlighted it show that 19:00:00 hour bar is white but in actual that bar is red why?
 
In logs its shows me 4 bars are red and than I got white but in actual 5 bars are red and than 1 white bar appear. Where I have mistaken?
 
kumaillakhani:
  1. Also in logs where I have highlighted it show that bar is white but in actual that bar is red why?

Because you use WRONG close value in your code. When the bar just opened, and you read its data it WAS white, but than it turned red (after you read the data).

 
Andrey Barinov:

Because you use WRONG close value in your code. When the bar just opened, and you read its data it WAS white, but than it turned red (after you read the data).


   OPEN=iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,2,1);
   CLOSE=iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,3,1);
   H1EMAprev=NormalizeDouble(iMA(_Symbol,PERIOD_H1,14,0,MODE_EMA,PRICE_CLOSE,1),Digits());

   if(H1!=iTime(Symbol(),PERIOD_H1,1)) // new candle on D1
     {
      if(OPEN<CLOSE)
        {
         for(i=0;i<total;i++)
           {
//            OPEN=NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,2,i),Digits);
//            CLOSE=NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",Red,White,Red,White,3,i),Digits);
            HAHigh= NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",0,i),Digits);
            HALow = NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",1,i),Digits);
            HAOpen= NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",2,i),Digits);
            HAClose=NormalizeDouble(iCustom(_Symbol,PERIOD_H1,"Heiken Ashi",3,i),Digits);
            H1iHighest=NormalizeDouble(iHigh(Symbol(),PERIOD_H1,iHighest(Symbol(),PERIOD_H1,MODE_HIGH,i-1,i)),Digits);

            if(OPEN<CLOSE)
              {
               Print("its White, i: ",i,", HAHigh: ",HAHigh,", HALow: ",HALow,", HAOpen: ",HAOpen,", HAClose: ",HAClose,
                     ", OPEN: ",OPEN,", CLOSE: ",CLOSE,", Symbol: ",_Symbol,", H1iHighest: ",H1iHighest);
               break;
              }
           }
        }
      else
        {
         Print("its Red");
        }

      //Do Something...
      H1=iTime(Symbol(),PERIOD_H1,1);    // overwrite old with new value
     }
   

Changed the code but still not solved my problem. Review snapshot.


Reason: