How to get the value of e.g. price and CCI

 

I have created a script where i can 'grab' date, time within a certain periode of time and then write all the data into a file and that part works fine. I'm also interested to get the price and the value of e.g. CCI(14) and write that to the file.

The script looks like this, but the price and CCI value is the same all the time. What am i doing wrong? Remember the file is located in \experts\files

#property copyright "Forex Script"
#property link      "http://www.xyz.com"

#include <stdlib.mqh>

// external variables
extern bool showArrows = true;
extern string fromDate="2011.1.1";
extern string toDate="2011.12.31";

// local variables
int xCountBars=0;
double BodySize = 3;

int init()
{
   ObjectsDeleteAll();      // clear the chart
   Comment("");    // clear the chart
}

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   int FileHandle;
   FileHandle = FileOpen(Symbol()+"_CCI.csv", FILE_CSV|FILE_WRITE,',');
   if(FileHandle < 1)
   {
      Print ("Creation of " + Symbol() + "_CCI.csv failed. Error #", GetLastError());
      return(0);
   }
    
   FileHandle = FileOpen(Symbol()+"_CCI.csv", FILE_CSV|FILE_WRITE,',');
   FileWrite(FileHandle, "DATE","HOUR","MIN","PRICE","CCI");  // File HEADER
   
   int ix;
   
   for (ix = 4; ix<Bars; ix++)
   {
   
      if (ix>2999) Print (ix, " : ", Bars, " , ", TimeToStr(Time[ix])); 

      if (Time[ix] < StrToTime(fromDate) || Time[ix] > StrToTime(toDate)) continue;
      
      // Get CCI value
      double xCCI = iCCI(NULL, NULL,14,PRICE_CLOSE,0);           
        
      FileWrite(FileHandle, TimeToStr(Time[ix], TIME_DATE), StringSubstr(TimeToStr(Time[ix], TIME_MINUTES),0,2),
      StringSubstr(TimeToStr(Time[ix], TIME_MINUTES),3,2),Bid,xCCI);      
                        
   } // For loop
                   
      // Close file
      FileClose(FileHandle);

   return(0);
}
 

A lot :)

Just kid ya.

#property copyright "Forex Script"
#property link      "http://www.xyz.com"

#include <stdlib.mqh>

// external variables
extern bool showArrows = true;
extern string fromDate="2011.1.1";
extern string toDate="2011.12.31";

// local variables
int xCountBars=0;
double BodySize = 3;

int init()
{
   ObjectsDeleteAll();      // clear the chart
   Comment("");    // clear the chart
}

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   int FileHandle;
   FileHandle = FileOpen(Symbol()+"_CCI.csv", FILE_CSV|FILE_WRITE,',');      // this is the first file open
   if(FileHandle < 1)                                                        // if FileHandle > 0 then do the action
   {
      Print ("Creation of " + Symbol() + "_CCI.csv failed. Error #", GetLastError());
      return(0);
   }
    
   FileHandle = FileOpen(Symbol()+"_CCI.csv", FILE_CSV|FILE_WRITE,',');      // there's no need for second file open
   FileWrite(FileHandle, "DATE","HOUR","MIN","PRICE","CCI");  // File HEADER
   
   int ix;
   
   for (ix = 4; ix<Bars; ix++)
   {
   
      if (ix>2999) Print (ix, " : ", Bars, " , ", TimeToStr(Time[ix])); 

      if (Time[ix] < StrToTime(fromDate) || Time[ix] > StrToTime(toDate)) continue;
      
      // Get CCI value
      double xCCI = iCCI(NULL, NULL,14,PRICE_CLOSE,0);  // the correct one is ..., PRICE_CLOSE,ix) 
        
      FileWrite(FileHandle, TimeToStr(Time[ix], TIME_DATE), StringSubstr(TimeToStr(Time[ix], TIME_MINUTES),0,2),
      StringSubstr(TimeToStr(Time[ix], TIME_MINUTES),3,2),Bid,xCCI);      
                        
   } // For loop
                   
      // Close file
      FileClose(FileHandle);

   return(0);
}
 


onewithzachy Thanks!


That solved the CCI value problem, but what about the Bid price, it still has the same value.


I tried to make this code below to get arround it, but it will not allow me to put (ix) after the Bid price.


// Get Bid price
double BidPrice = Bid(ix);


 

Bid is the Bid price now . . . you can't get the Bid price for a specific time in the past . . . you can get Open Close High & Low for M1 (assuming you have the M1 data) or any other timeframe data from the past . . . iOpen, iClose, etc

Maybe instead of Bid you could use Open[ix] ?

 
RaptorUK:

Bid is the Bid price now . . . you can't get the Bid price for a specific time in the past . . . you can get Open Close High & Low for M1 (assuming you have the M1 data) or any other timeframe data from the past . . . iOpen, iClose, etc

Maybe instead of Bid you could use Open[ix] ?


Yeah it sounds like a good idea RaptorUK.

Thanks!

 

So here is the correct code for the 'loop' if one need it. Thanks!

   for (ix = 4; ix<Bars; ix++)
   {
   
      if (ix>2999) Print (ix, " : ", Bars, " , ", TimeToStr(Time[ix])); 

      if (Time[ix] < StrToTime(fromDate) || Time[ix] > StrToTime(toDate)) continue;
 
      // Get Bid price
      double BidPrice = Open[ix];
      
      // Get CCI value
      double xCCI = iCCI(NULL, NULL,14,PRICE_CLOSE,ix);           
        
      FileWrite(FileHandle, TimeToStr(Time[ix], TIME_DATE), StringSubstr(TimeToStr(Time[ix], TIME_MINUTES),0,2),
      StringSubstr(TimeToStr(Time[ix], TIME_MINUTES),3,2),BidPrice,xCCI);      
                        
   } // For loop
 
EagleEye:

So here is the correct code for the 'loop' if one need it. Thanks!

Mmm ... if may suggest, coz your CCI is calculated on PRICE_CLOSE, you better use Close price as well.

 for (ix = 4; ix<Bars; ix++)
   {
   
      if (ix>2999) Print (ix, " : ", Bars, " , ", TimeToStr(Time[ix])); 

      if (Time[ix] < StrToTime(fromDate) || Time[ix] > StrToTime(toDate)) continue;
 
      // Get Bid price
      //double BidPrice = Open[ix]; let's take it's value directly
      
      // Get CCI value
      double xCCI = iCCI(NULL, NULL,14,PRICE_CLOSE,ix);           
        
      FileWrite(FileHandle, TimeToStr(Time[ix], TIME_DATE), StringSubstr(TimeToStr(Time[ix], TIME_MINUTES),0,2),
      StringSubstr(TimeToStr(Time[ix], TIME_MINUTES),3,2),Close[ix],xCCI);      
                        
   } // For loop
 
onewithzachy:

Mmm ... if may suggest, coz your CCI is calculated on PRICE_CLOSE, you better use Close price as well.

The Time used is based on the Open . . . ;-)
 
RaptorUK:
The Time used is based on the Open . . . ;-)
Well, if EagleEye wanted to compare CCI's PRICE_CLOSE then I guess EagleEye should compare it with price Close :D
 

Ok i see your comments, but what is best practise? To use the Open or Close on both?

I have another problem thoungh.

The script works perfect from TF H4 and up, but if i'm trying on H1 sometimes it works and other times not. From M30 and below the script only writes the headline in the file, but no data.

Any idea why it doesn't work on lower TF's?

 
EagleEye:

Ok i see your comments, but what is best practise? To use the Open or Close on both?

I have another problem thoungh.

The script works perfect from TF H4 and up, but if i'm trying on H1 sometimes it works and other times not. From M30 and below the script only writes the headline in the file, but no data.

Any idea why it doesn't work on lower TF's?

IMHO still, PRICE_CLOSE for CCI then Close price is the friend.

I didn't check your script, however you may have to do this first (click here, and read the whole thread), and while you open "Option window" make sure you also set "Max bars in chart" to 9999999999999. You probably don't have enough bars in chart - just probably.

:D

Reason: