How can I get Close [0]?

 
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma;
   int    res;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//--- 
   //pips
   double a = MathAbs(Close[0]-Open[0]);
   string b = DoubleToStr( NormalizeDouble( ( a/( 1*Point ) ),3 ),3 );
   double p = StrToDouble(b);
   
//average
   double rosoku = 0;
   double sum_rosoku =0;
   double count =0;
   double average =0;

   for(int j=1;j<=21;j++)
    { 
      double a1 = MathAbs(Close[j]-Open[j]);
      string b1 = DoubleToStr( NormalizeDouble( a1/ (1*Point  ),3 ),3 );
      double e1 = StrToDouble(b1);
      
      rosoku = e1;
      sum_rosoku += rosoku;
      count++;
    }
    
   if(count == 21 )
    {
     average = sum_rosoku/count;
    }
//--- sell conditions
   if(Open[1]>ma && Close[0]<ma && a*2.0>average)
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//--- buy conditions
   if(Open[1]<ma && Close[0]>ma && a*2.0>average)
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }
//---
  }

Close [0] is not working in the Expert Advisor void CheckForOpen (). The value of Close [0] is the opening price. How can I get Close [0]? Close [1] can get the correct value.

 
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
    int handle = FileOpen("file.csv", FILE_CSV|FILE_WRITE, ";");
  if(handle>0)
    {
     // table column headers recording
     FileWrite(handle, "Time;Open;High;Low;Close;Volume");
     // data recording
     for(int i=0; i<Bars; i++)
       FileWrite(handle, Time[i], Open[i], High[i], Low[i], Close[i], Volume[i]);
     FileClose(handle);
    }
   double ma;
   int    res;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//--- 
   //pips
   double a = MathAbs(Close[0]-Open[0]);
   string b = DoubleToStr( NormalizeDouble( ( a/( 1*Point ) ),3 ),3 );
   double p = StrToDouble(b);
   printf(DoubleToStr(a));   

//average
   double rosoku = 0;
   double sum_rosoku =0;
   double count =0;
   double average =0;

   for(int j=1;j<=21;j++)
    { 
      double a1 = MathAbs(Close[j]-Open[j]);
      string b1 = DoubleToStr( NormalizeDouble( a1/ (1*Point  ),3 ),3 );
      double e1 = StrToDouble(b1);
      
      rosoku = e1;
      sum_rosoku += rosoku;
      count++;
    }
    
   if(count == 21 )
    {
     average = sum_rosoku/count;
    }
//--- sell conditions
   if(Open[1]>ma && Close[0]<ma && a*2.0>average)
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//--- buy conditions
   if(Open[1]<ma && Close[0]>ma && a*2.0>average)
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }
//---
  }

double a = MathAbs(Close[0]-Open[0]);

I tried it but it's not effect. ↑ a=0

 
Kosei S #:

I tried it but it's not effect. ↑ a=0

Try diferent … 
double a=Close[0];
double b=Open[0];
Print(a);
Print(b);
Print(a-b);

//Also

if(Close[0]== Bid)
Alert( " I understand now and I promise I will stop asking same question over and over again "); 
And copy this into a script and run it
 
Kosei S #: I tried it but it's not effect. ↑ a=0

How many times must we explain to you ... https://www.mql5.com/en/forum/429285, that when a new bar is formed (i.e. first tick of a bar), that open and close are the same and that, "Close[0] - Open[0]" will be zero.

There is no way around that. It is a fact of trading. You should not be comparing Close[0] to Close[1], but instead Close[1] to Close[2].

The value of Close [0] is the value of Open [0]. Why is it different?
The value of Close [0] is the value of Open [0]. Why is it different?
  • 2022.07.23
  • www.mql5.com
General: The value of Close [0] is the value of Open [0]. Why is it different?
 
Also, if you have difficulty communicating in English, then it may be easier for you to communicate in your own language which I assume is Japanese — MQL5フォーラム
MQL5フォーラム
MQL5フォーラム
  • www.mql5.com
MQL5: 自動取引システムとストラテジーテスティングに関するフォーラム
 
Daniel Cioca #:
Try diferent … 
double a=Close[0];
Double b=Open[0];
Print(a);
Print(b);
Print(a-b);

I tried it But Close[0] = Open[0] in the

void CheckForOpen()

Every candlle same price.

What happen?

 
Fernando Carreiro #:

How many times must we explain to you ... https://www.mql5.com/en/forum/429285, that when a new bar is formed (i.e. first tick of a bar), that open and close are the same and that, "Close[0] - Open[0]" will be zero.

There is no way around that. It is a fact of trading. You should not be comparing Close[0] to Close[1], but instead Close[1] to Close[2].

I want to write a code that I want to entry possition when current bar is X times the average Pips in the past, is that impossible?

I did that when I created the arrow indicator before, But when it comes to EA, I can't do that

 
Kosei S #:

I tried it But Close[0] = Open[0] in the

Every candlle same price.

What happen?

Try Monday
 

Thank you appreciate guys.

I'll try on monday

 
Kosei S #:

Thank you appreciate guys.

I'll try on monday

Reason: