How to update an existing file ?

 
Comments that do not relate to the "Indicator modification help needed.", have been moved into this topic.
 

Hi,

I have a question. I am trying to output the values of some variable into a csv file, at the same time I am printing the values into a logfile.

While all the values are correctly printing every minute (M1 timeframe) into a log file the CSV file only gets one line entry and stops.

How do I make all the entries go into the csv file. The output csv file names are slopeReport1.csv and slopeReport2.csv.

here is the attached code:

#property indicator_separate_window


#property indicator_buffers 3

#property indicator_color1 Blue

#property indicator_width1 2

#property indicator_color2 Red

#property indicator_width2 2

#property indicator_color3 DarkGreen

#property indicator_width3 2


//---- indicator parameters

extern int RegLin_Period=14;


//---- indicator buffers

double SLOPE[];

double Negativo[];

double Positivo[];

//----

int ExtCountedBars=0;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int init()

  {

   IndicatorBuffers(3); 

   SetIndexBuffer(0,SLOPE);

   SetIndexStyle(0,DRAW_NONE);

    

   SetIndexBuffer(1,Negativo);

   SetIndexStyle(1,DRAW_HISTOGRAM);

    

   SetIndexBuffer(2,Positivo);

   SetIndexStyle(2,DRAW_HISTOGRAM);      

   IndicatorShortName("Linear Regression Slope("+RegLin_Period+")");  

   return(0);

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

double slope(int iBar, int nBars)

{

   double sumy=0,

          sumx=0,

          sumxy=0,

          sumx2=0;

   double beta = 0.0;

   int iLimit = iBar + nBars;

   for(; iBar < iLimit; ++iBar)

      {

      sumy+=Close[iBar];

      sumxy+=Close[iBar]*iBar;

      sumx+=iBar;

      sumx2+=iBar*iBar;

      }

   int handle=FileOpen("slopeReport1.csv",FILE_WRITE|FILE_CSV,",");

   FileWrite(handle,"timeset",TimeCurrent(),",",Symbol(),",",Period(),","," IBAR ",iBar," NBAR ",nBars," ILIMIT ",iLimit," value ",((nBars*sumxy - sumx*sumy) / (nBars*sumx2 - sumx*sumx)));

   return( (nBars*sumxy - sumx*sumy) / (nBars*sumx2 - sumx*sumx) );

}

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

int start()

{           

   int count = IndicatorCounted();

   

   if(count < RegLin_Period) count = RegLin_Period; // Lookback

   

   for(int iBar = Bars - 1 - count; iBar >= 0; iBar--)

     {

      SLOPE[iBar] = -slope(iBar, RegLin_Period);    

      if(SLOPE[iBar]>0)

        {

        Positivo[iBar]=SLOPE[iBar];

        Negativo[iBar]=EMPTY_VALUE;

        }

     if(SLOPE[iBar]<0)

        {

        Negativo[iBar]=SLOPE[iBar];

        Positivo[iBar]=EMPTY_VALUE;

        }       

     }

int handle=FileOpen("slopeReport2.csv",FILE_WRITE|FILE_CSV,",");

FileWrite(handle,"timeset",",",TimeCurrent(),",",Symbol(),",",Period(),",","IBAR",SLOPE[iBar]);


 return(0);     

}
 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
delbroooks:

Hi,

I have a question. I am trying to output the values of some variable into a csv file, at the same time I am printing the values into a logfile.

While all the values are correctly printing every minute (M1 timeframe) into a log file the CSV file only gets one line entry and stops.

How do I make all the entries go into the csv file. The output csv file names are slopeReport1.csv and slopeReport2.csv.

here is the attached code:

You have to use FILE_READ|FILE_WRITE to update a file, and also FileSeek() to place the file pointer at the end of the file. Please read more carefully the documentation.