//+------------------------------------------------------------------+
//| CustomRatesReplace.mq5 |
//| Copyright 2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#define CUSTOM_SYMBOL_NAME Symbol()+".C" // カスタム銘柄名
#define CUSTOM_SYMBOL_PATH "Forex" // 銘柄が作成されるグループの名前
#define CUSTOM_SYMBOL_ORIGIN Symbol() // カスタム銘柄の基となる銘柄の名前
#define DATARATES_COUNT 4 // 操作ログに送信されるバーの数
//+------------------------------------------------------------------+
//| スクリプトプログラム開始関数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- カスタム銘柄を作成するときにエラーコードを取得する
int create=CreateCustomSymbol(CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_PATH, CUSTOM_SYMBOL_ORIGIN);
//--- エラーコードが0(銘柄の作成に成功)でも5304(銘柄がすでに作成されている)でもない場合は、終了する
if(create!=0 && create!=5304)
return;
//--- 標準銘柄バーの数を取得する
int bars=Bars(CUSTOM_SYMBOL_ORIGIN, PERIOD_M1);
//--- 標準銘柄の分単位の時間枠のすべてのバーのデータをMqlRates配列に取得する
MqlRates rates[]={};
ResetLastError();
if(CopyRates(CUSTOM_SYMBOL_ORIGIN, PERIOD_M1, 0, bars, rates)!=bars)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_ORIGIN, bars, GetLastError());
return;
}
//--- コピーしたデータをカスタム銘柄の分単位の履歴に設定する
ResetLastError();
if(CustomRatesUpdate(CUSTOM_SYMBOL_NAME, rates)<0)
{
PrintFormat("CustomRatesUpdate(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError());
return;
}
//--- 履歴データを更新した後、カスタム銘柄バーの数を取得する
bars=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1);
//--- カスタム銘柄の分単位の時間枠のすべてのバーのデータをMqlRates配列に取得する
ResetLastError();
if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars, rates)!=bars)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars, GetLastError());
return;
}
//--- カスタム銘柄の分単位履歴の最後のDATARATES_COUNTバーを操作ログに出力する
int digits=(int)SymbolInfoInteger(CUSTOM_SYMBOL_NAME, SYMBOL_DIGITS);
PrintFormat("Last %d bars of the custom symbol's minute history:", DATARATES_COUNT);
ArrayPrint(rates, digits, NULL, bars-DATARATES_COUNT, DATARATES_COUNT);
//--- カスタム銘柄の分単位の履歴の最後から2つのデータバーを変更する
datetime time_from= rates[bars-3].time;
datetime time_to = rates[bars-2].time;
//--- 最後から2番目のバーのすべての価格をrates配列内のこれらのバーの始値と等しくする
rates[bars-3].high=rates[bars-3].open;
rates[bars-3].low=rates[bars-3].open;
rates[bars-3].close=rates[bars-3].open;
rates[bars-2].high=rates[bars-2].open;
rates[bars-2].low=rates[bars-2].open;
rates[bars-2].close=rates[bars-2].open;
//--- 既存のバーを変更されたrates配列のデータに置き換える
ResetLastError();
int replaced=CustomRatesUpdate(CUSTOM_SYMBOL_NAME, rates);
if(replaced<0)
{
PrintFormat("CustomRatesUpdate(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError());
return;
}
//--- 履歴データの2つのバーを変更した後、カスタム銘柄バーの数を再度取得する
bars=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1);
//--- カスタム銘柄の分単位のすべてのバーのデータを再度取得する
ResetLastError();
if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars, rates)!=bars)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars, GetLastError());
return;
}
//--- 更新されたカスタム銘柄の分単位履歴の最後のDATARATES_COUNTバーを操作ログに出力する
PrintFormat("\nLast %d bars after applying CustomRatesUpdate() with %d replaced bars:", DATARATES_COUNT, replaced);
ArrayPrint(rates, digits, NULL, bars-DATARATES_COUNT, DATARATES_COUNT);
//--- チャートのコメントにスクリプト終了キーに関するヒントを表示する
Comment(StringFormat("Press 'Esc' to exit or 'Del' to delete the '%s' symbol and exit", CUSTOM_SYMBOL_NAME));
//--- EscキーまたはDelキーが押されるまで待機して無限ループを終了する
while(!IsStopped() && TerminalInfoInteger(TERMINAL_KEYSTATE_ESCAPE)==0)
{
Sleep(16);
//--- Delキーを押すと、作成したカスタム銘柄とそのデータが削除される
if(TerminalInfoInteger(TERMINAL_KEYSTATE_DELETE)<0)
{
//--- バーデータを削除する
int deleted=CustomRatesDelete(CUSTOM_SYMBOL_NAME, 0, LONG_MAX);
if(deleted>0)
PrintFormat("%d history bars of the custom symbol '%s' were successfully deleted", deleted, CUSTOM_SYMBOL_NAME);
//--- ティックデータを削除する
deleted=CustomTicksDelete(CUSTOM_SYMBOL_NAME, 0, LONG_MAX);
if(deleted>0)
PrintFormat("%d history ticks of the custom symbol '%s' were successfully deleted", deleted, CUSTOM_SYMBOL_NAME);
//--- 銘柄を削除する
if(DeleteCustomSymbol(CUSTOM_SYMBOL_NAME))
PrintFormat("Custom symbol '%s' deleted successfully", CUSTOM_SYMBOL_NAME);
break;
}
}
//--- 終了する前にチャートをクリアする
Comment("");
/*
結果:
Last 4 bars of the custom symbol's minute history:
[time] [open] [high] [low] [close] [tick_volume] [spread] [real_volume]
[0] 2024.07.29 13:37:00 1.08394 1.08396 1.08388 1.08390 16 1 0
[1] 2024.07.29 13:38:00 1.08389 1.08400 1.08389 1.08398 35 1 0
[2] 2024.07.29 13:39:00 1.08398 1.08410 1.08394 1.08410 29 1 0
[3] 2024.07.29 13:40:00 1.08409 1.08414 1.08408 1.08414 14 1 0
Last 4 bars after applying CustomRatesUpdate() with 250820 replaced bars:
[time] [open] [high] [low] [close] [tick_volume] [spread] [real_volume]
[0] 2024.07.29 13:37:00 1.08394 1.08396 1.08388 1.08390 16 1 0
[1] 2024.07.29 13:38:00 1.08389 1.08389 1.08389 1.08389 35 1 0
[2] 2024.07.29 13:39:00 1.08398 1.08398 1.08398 1.08398 29 1 0
[3] 2024.07.29 13:40:00 1.08409 1.08414 1.08408 1.08414 14 1 0
*/
}
//+------------------------------------------------------------------+
//| カスタム銘柄ルを作成し、エラーコードを返す |
//+------------------------------------------------------------------+
int CreateCustomSymbol(const string symbol_name, const string symbol_path, const string symbol_origin=NULL)
{
//--- カスタム銘柄の基となる銘柄の名前をする
string origin=(symbol_origin==NULL ? Symbol() : symbol_origin);
//--- カスタム銘柄の作成に失敗し、エラー5304ではない場合は、操作ログに報告する
ResetLastError();
int error=0;
if(!CustomSymbolCreate(symbol_name, symbol_path, origin))
{
error=GetLastError();
if(error!=5304)
PrintFormat("CustomSymbolCreate(%s, %s, %s) failed. Error %d", symbol_name, symbol_path, origin, error);
}
//--- 成功
return(error);
}
//+------------------------------------------------------------------+
//| カスタム銘柄を削除する |
//+------------------------------------------------------------------+
bool DeleteCustomSymbol(const string symbol_name)
{
//--- 気配値表示ウィンドウから銘柄を非表示にする
ResetLastError();
if(!SymbolSelect(symbol_name, false))
{
PrintFormat("SymbolSelect(%s, false) failed. Error %d", GetLastError());
return(false);
}
//--- カスタム銘柄の削除に失敗した場合は、操作ログにこれを報告してfalseを返す
ResetLastError();
if(!CustomSymbolDelete(symbol_name))
{
PrintFormat("CustomSymbolDelete(%s) failed. Error %d", symbol_name, GetLastError());
return(false);
}
//--- 成功
return(true);
}
|