//+------------------------------------------------------------------+
//| Demo_FileWriteDouble.mq5 |
//| Copyright 2013, MetaQuotes Software Corp. |
//| https://www.MQL5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//--- スクリプトの起動時に入力パラメータのウィンドウを表示する
#property script_show_inputs
//--- 端末からデータを受け取るパラメータ
。input string InpSymbolName="EURJPY"; // 通貨ペア
input ENUM_TIMEFRAMES InpSymbolPeriod=PERIOD_M15; // 時間軸
input int InpMAPeriod=10; // 平滑化期間
input int InpMAShift=0; // 指標のシフト shift
input ENUM_MA_METHOD InpMAMethod=MODE_SMA; // 平滑化の種類
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // 価格の種類
input datetime InpDateStart=D'2013.01.01 00:00'; // データコピー開始日
//--- データをファイルに書くパラメータ
input string InpFileName="MA.csv"; // ファイル名
input string InpDirectoryName="Data"; // ディレクトリ名
//+------------------------------------------------------------------+
//| スクリプトプログラムを開始する関数 |
//+------------------------------------------------------------------+
void OnStart()
{
datetime date_finish=TimeCurrent();
double ma_buff[];
datetime time_buff[];
int size;
//--- MA 指標ハンドルを受け取る
ResetLastError();
int ma_handle=iMA(InpSymbolName,InpSymbolPeriod,InpMAPeriod,InpMAShift,InpMAMethod,InpAppliedPrice);
if(ma_handle==INVALID_HANDLE)
{
//--- 指標ハンドルの受け取りに失敗
PrintFormat("Error when receiving indicator handle. Error code = %d",GetLastError());
return;
}
//--- 全ての指標値が計算されるまでループに留まる
while(BarsCalculated(ma_handle)==-1)
Sleep(20); // 指標が全ての値を計算出来るように一時停止
PrintFormat("Indicator values starting from %s will be written to the file",TimeToString(InpDateStart));
//--- 指標値を複製する
ResetLastError();
if(CopyBuffer(ma_handle,0,InpDateStart,date_finish,ma_buff)==-1)
{
PrintFormat("Failed to copy the indicator values. Error code = %d",GetLastError());
return;
}
//--- 適切なバーの到着時間を複製する
ResetLastError();
if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,time_buff)==-1)
{
PrintFormat("Failed to copy time values. Error code = %d",GetLastError());
return;
}
//--- バッファサイズを受け取る
size=ArraySize(ma_buff);
//--- 指標で使用されたメモリを解放する
IndicatorRelease(ma_handle);
//--- 指標値を書き込むためにファイルを開く(ファイルが存在しない場合は自動的に作成される)
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_BIN);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is available for writing",InpFileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
//--- 初めにデータサンプルのサイズを書く
FileWriteDouble(file_handle,(double)size);
//--- 指標の時間と値をファイルに書く
for(int i=0;i<size;i++)
{
FileWriteDouble(file_handle,(double)time_buff[i]);
FileWriteDouble(file_handle,ma_buff[i]);
}
//--- ファイルを閉じる
FileClose(file_handle);
PrintFormat("Data is written, %s file is closed",InpFileName);
}
else
PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
}
|