Update .hst file for offline chart

 

I have a script which does write 1000 candelsticks into a .hst file, my problem is when i change in my code the for loop to make only 1 run then i have also just 1 candelstick in my .hst file saved, so it looks like he overwwrite everything in the .hst file.

And i have just a easy question, how ust i write it to make it just update the .hst file instead of deleting all other candelsticks which are saved in the file?

Here is my code:

//+------------------------------------------------------------------+
 //| Save HST File                                                    |
 //+------------------------------------------------------------------+

   if(StringLen(str)>10)
   {
    ExtHandle=FileOpenHistory(NewFileName,FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_ANSI);
    if(ExtHandle<0) return;


    //--- write history file header
    c_copyright="(C)opyright 2003, MetaQuotes Software Corp.";
    ArrayInitialize(i_unused,0);
    FileWriteInteger(ExtHandle,file_version,LONG_VALUE);
    FileWriteString(ExtHandle,c_copyright,64);
    FileWriteString(ExtHandle,c_symbol,12);
    FileWriteInteger(ExtHandle,i_period,LONG_VALUE);
    FileWriteInteger(ExtHandle,i_digits,LONG_VALUE);
    FileWriteInteger(ExtHandle,0,LONG_VALUE);
    FileWriteInteger(ExtHandle,0,LONG_VALUE);
    FileWriteArray(ExtHandle,i_unused,0,13);

            int BarsBack=1000;

            for(int i=BarsBack; i>=1; i--)
            {
               //--- write history file
               if(date>0)
               {
                start_pos=BarsBack;
                rate.open=open;
                rate.low=low;
                rate.high=high;
                rate.close=close;
                rate.tick_volume=(long)volume;
                rate.spread=0;
                rate.real_volume=0;
                rate.time=date;
      
                if(IsStopped()) break;
      
                last_fpos=FileTell(ExtHandle);
                last_volume=(long)volume;
                FileWriteStruct(ExtHandle,rate);
               }
           }//for

  //---
  FileFlush(ExtHandle);
  FileClose(ExtHandle);
  }//StringLen(str)>10


//+------------------------------------------------------------------+
//| Update Chart                                                     |
//+------------------------------------------------------------------+

   if(chart_id==0)
     {
        long id=ChartFirst();
        while(id>=0)
        {
            //--- find appropriate offline chart
            if(ChartSymbol(id)==c_symbol && ChartPeriod(id)==i_period && ChartGetInteger(id,CHART_IS_OFFLINE))
              {
                chart_id=id;
                ChartSetInteger(chart_id,CHART_AUTOSCROLL,true);
                ChartSetInteger(chart_id,CHART_SHIFT,true);
                ChartNavigate(chart_id,CHART_END);
                ChartRedraw(chart_id);
                PrintFormat("Chart window [%s,%d] found",c_symbol,i_period);
                break;
              }
            //--- enumerate opened charts
            id=ChartNext(id);
         }
       }
    //--- refresh window 
    if(chart_id!=0)
      { 
        ChartSetSymbolPeriod(chart_id,Symbol(),i_period);

        Print("Chart refresh call");
      }
Reason: