SaveBars_TradeAviserReal

Download script
SaveBars_TradeAviserReal

The SaveBars_TradeAviserReal script is designed as a background data management tool for the MetaTrader platform. Its primary purpose is to automatically capture and export historical price information from your trading charts into an organized external file format.

Core Functionality

The script acts as an automated "logger." Instead of requiring you to manually copy and paste price data, it scans the historical candles (bars) on your current chart and writes their specific details into a text-based CSV file. This is particularly useful for traders who need to archive market history for external analysis or record-keeping.

How the Logic Works

The script operates based on the following logical steps:

  • Automated Triggering: The tool is designed to be efficient. It does not constantly write to your hard drive every second; instead, it checks the time and only initiates its data-saving process when a new candle (bar) appears on your chart.
  • File Organization: Every time it runs, it creates a unique filename based on the specific currency pair you are viewing and the timeframe (e.g., "EURUSD1440"). This ensures that data for different assets or timeframes remains separated and organized in your folders.
  • Data Extraction: Once triggered, the script looks back through a user-defined number of previous candles. For each of these bars, it extracts:
    • The specific Date (Year, Month, Day).
    • The precise Time (Hour and Minute).
    • The Price Action (Open, High, Low, and Close prices).
    • The Trading Volume.
  • Data Formatting: To ensure the exported file is easy to read by other programs (like Excel), the script standardizes the time format, adding leading zeros where necessary (e.g., ensuring "05" hours is recorded correctly rather than just "5").
  • Safe Execution: The script includes a timing mechanism to prevent it from restarting its process too quickly, ensuring that it doesn't interfere with the smooth operation of your trading platform while it performs its recording tasks.

Key Parameters for Users

  • Bar: Determines how many historical candles back the script should record at one time.
  • LastBar: Allows for an additional offset to fine-tune which specific bars are captured.
  • TimeSave: Controls the cooldown period, ensuring the script behaves correctly during the transition between new market bars.
Miscellaneous
Implements a curve of type %1Uses files from the file systemIt writes information to file
9 Views
0 Downloads
0 Favorites
SaveBars_TradeAviserReal
// SaveBars_TradeAviserReal
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- input parameters
extern int       Bar=300;
extern int       LastBar=0;
extern int       TimeSave=300;
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   string sName="",ss1=" ",ss2=" ";
   int file=0, NumBar=0, shift=0; 
   int Ye=0,Mo=0,Da=0,Ho=0,Mi=0,Se=0,Ti=0; 
   int    counted_bars=IndicatorCounted();
//---- 
   //---- check for possible errors
   if (counted_bars<0) return(-1);
//---- 
//    if (NumBar || counted_bars) //ðàáîòàåì òîëüêî ïðè ñìåíå áàðà! íó è ïðè ïåðâîì çàïóñêå åùå. 
   if (NumBar<CurTime()) //ðàáîòàåì òîëüêî ïðè ñìåíå áàðà! íó è ïðè ïåðâîì çàïóñêå åùå.
	{
     sName=Symbol()+Period()+".txt"; //ôîðìèðóåì èìÿ ôàéëà, òèïà "EURUSD1440.csv". ÌÒ ëþáèò òîëüêî *.csv, ïàäëà
     FileDelete(sName);
     file =FileOpen(sName,FILE_CSV|FILE_WRITE," ");

     shift=Bar+LastBar-1;
     while(shift>=0)
     {	
				Ti=Time[shift]; //âðåìÿ áàðà â ñåê. âûòàñêèâàåì èç íåãî âñå, ÷òî íàäî
				Ye=TimeYear(Ti);
				Mo=TimeMonth(Ti);
				Da=TimeDay(Ti);
				Ho=TimeHour(Ti);
				Mi=TimeMinute(Ti);
//				Se=TimeSeconds(Ti);
//				ss1=Ye-3;// âû÷èòàåì ãîäû - ýòî ÷òîá Ðåôèíåä êîòèðîâêó ãëîòàë.
				if (Mo<10) ss1="0"+Mo; else ss1=Mo;
				if (Da<10) ss1=ss1+"0"+Da; else ss1=ss1+Da;
				ss1=ss1+Ye; //ss1 - ÌÌÄÄÃÃÃà íå âû÷èòàåì ãîäû - ýòî ÷òîáû íå ïóòàòüñÿ.
				if (Ho<10) ss2="0"+Ho; else ss2=Ho; 
				if (Mi<10) ss2=ss2+":"+"0"+Mi; else ss2=ss2+":"+Mi; //ss2 - ××:ÌÌ
//				if (Se<10) ss2=ss2+"0"+Se; else ss2=ss2+Se; //ss2 - ××ÌÌÑÑ
     FileWrite(file,ss1,ss2,Open[shift],High[shift],Low[shift],Close[shift],Volume[shift]," "); //ïèøåì î÷åðåäíîé áàðFileWrite//(file,ss1,ss2,O[shift],H[shift],L[shift],C[shift],V[shift]);
     shift--;
     }   
//     Comment("Save of "+ss1+" "+ss2+" is ok");
     //NumBar=counted_bars;   //ìåòèì áàð - òèïà íà íåì óæå îòðàáîòàëè. Äî ñëåäóþùåãî áàðà èíäþê ðàññëàáëÿåòñÿ.
     NumBar=(CurTime()+ TimeSave); //ìåòèì áàð (+TimeSave ñåêóíä) - òèïà íà íåì óæå îòðàáîòàëè. Äî ñëåäóþùåãî áàðà èíäþê ðàññëàáëÿåòñÿ.
     FileClose(file);  //çàêðûëè ôàéëî. òèïà ïðèíÿòî òàê.
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments