#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#define OBJ_NAME "TestObjectGetTimeByValue" // グラフィックオブジェクト名
#define STEP 100 // 価格ステップ
//+------------------------------------------------------------------+
//| スクリプトプログラム開始関数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- ID、シンボル
long chart_id=ChartID();
string chart_symbol=ChartSymbol(chart_id);
//--- チャートシンボルのPoint値を取得する
double point=SymbolInfoDouble(chart_symbol, SYMBOL_POINT);
if(point==0)
{
PrintFormat("Failed to get the Point value of the \"%s\" symbol. Error %d", chart_symbol, GetLastError());
return;
}
//--- 左端の可視バーの高値から右端の低値まで等間隔チャネルを構築する
if(!CreateChannel(chart_id))
return;
//--- チャートの最大値・最小値、チャートシンボルの小数桁数
double chart_max=ChartGetDouble(chart_id, CHART_PRICE_MAX);
double chart_min=ChartGetDouble(chart_id, CHART_PRICE_MIN);
int digits=(int)SymbolInfoInteger(chart_symbol, SYMBOL_DIGITS);
//--- 計算された価格がチャートの最小価格より大きい間
//--- STEP ごとにチャート価格をループして時間値を取得する
//--- 等間隔チャネルの各ラインの計算価格値に対して
//--- 各ラインで取得した時間を操作ログに出力する
int index=0;
double price=chart_max;
do
{
price=chart_max-STEP*index*point;
datetime time0=ObjectGetTimeByValue(chart_id, OBJ_NAME, price, 0);
datetime time1=ObjectGetTimeByValue(chart_id, OBJ_NAME, price, 1);
string time0_str=(time0>0 ?TimeToString(time0) : "No value at this price");
string time1_str=(time1>0 ?TimeToString(time1) : "No value at this price");
string idx=StringFormat("%02d", index);
PrintFormat("[%s] For price %.*f the time value at line 0: %s, at line 1: %s", idx, digits, price, time0_str, time1_str);
index++;
}
while(!IsStopped() && price>=chart_min);
//--- 5秒待ってクリーンアップを行う
Sleep(5000);
ObjectDelete(chart_id, OBJ_NAME);
ChartRedraw(chart_id);
/*
結果:
[00] For price 1.26110 the time value at line 0: No value at this price, at line 1: No value at this price
[01] For price 1.26010 the time value at line 0: 2024.12.30 17:00, at line 1: No value at this price
[02] For price 1.25910 the time value at line 0: 2024.12.30 22:30, at line 1: No value at this price
[03] For price 1.25810 the time value at line 0: 2024.12.31 04:00, at line 1: 2024.12.30 16:30
[04] For price 1.25710 the time value at line 0: 2024.12.31 10:00, at line 1: 2024.12.30 22:00
[05] For price 1.25610 the time value at line 0: 2024.12.31 15:30, at line 1: 2024.12.31 03:30
[06] For price 1.25510 the time value at line 0: 2024.12.31 21:00, at line 1: 2024.12.31 09:00
[07] For price 1.25410 the time value at line 0: 2025.01.02 03:30, at line 1: 2024.12.31 14:30
[08] For price 1.25310 the time value at line 0: No value at this price, at line 1: 2024.12.31 20:30
[09] For price 1.25210 the time value at line 0: No value at this price, at line 1: 2025.01.02 03:00
[10] For price 1.25110 the time value at line 0: No value at this price, at line 1: No value at this price
[11] For price 1.25010 the time value at line 0: No value at this price, at line 1: No value at this price
[12] For price 1.24910 the time value at line 0: No value at this price, at line 1: No value at this price
[13] For price 1.24810 the time value at line 0: No value at this price, at line 1: No value at this price
*/
}
//+--------------------------------------------------------------------------------------------+
//| 左端バーの高値から右端バーの低値まで等間隔チャネルを構築する |
//+--------------------------------------------------------------------------------------------+
bool CreateChannel(const long chart_id=0)
{
long bar1 =0, bar2 =0, visible=0;
datetime time1 =0, time2 =0;
double price1=0, price2=0;
//--- 左端に表示されているチャートの最初のバーを取得する
ResetLastError();
if(!ChartGetInteger(chart_id, CHART_FIRST_VISIBLE_BAR, 0, bar1))
{
PrintFormat("%s: ChartGetInteger() failed. Error %d",__FUNCTION__, GetLastError());
return(false);
}
//--- チャートで表示されているバーの数
if(!ChartGetInteger(chart_id, CHART_VISIBLE_BARS, 0, visible))
{
PrintFormat("%s: ChartGetInteger() failed. Error %d",__FUNCTION__, GetLastError());
return(false);
}
//--- 取得した値を調整し、右端に表示される最初のバーのインデックスを計算する
bar1-=1;
visible-=2;
bar2=bar1-visible;
//--- チャートシンボル
string symbol=ChartSymbol(chart_id);
//--- チャート左端に表示されている最初のバーの時間を取得する
ResetLastError();
datetime time_array[1];
if(CopyTime(symbol, PERIOD_CURRENT, (int)bar1, 1, time_array)!=1)
{
PrintFormat("%s: CopyTime() failed. Error %d",__FUNCTION__, GetLastError());
return(false);
}
time1=time_array[0];
//--- 右端に表示されているチャートの最初のバーの時刻を取得する
if(CopyTime(symbol, PERIOD_CURRENT, (int)bar2, 1, time_array)!=1)
{
PrintFormat("%s: CopyTime() failed. Error %d",__FUNCTION__, GetLastError());
return(false);
}
time2=time_array[0];
//--- チャート左端に表示されている最初のバーの高値を取得する
double price_array[];
if(CopyHigh(symbol, PERIOD_CURRENT, (int)bar1, 1, price_array)!=1)
{
PrintFormat("%s: CopyHigh() failed. Error %d",__FUNCTION__, GetLastError());
return(false);
}
price1=price_array[0];
//--- チャート右端に表示されている最初のバーの安値を取得する
if(CopyLow(symbol, PERIOD_CURRENT, (int)bar2, 1, price_array)!=1)
{
PrintFormat("%s: CopyLow() failed. Error %d",__FUNCTION__, GetLastError());
return(false);
}
price2=price_array[0];
//--- チャートの価格幅を計算する(ポイント単位)
//--- 等間隔チャネルの場合、2本目のラインの距離は価格幅の1/3になる
double range=price1-price2;
double distance=range*0.3;
//--- 計算された座標に、グラフィックオブジェクト(等間隔チャネル)を作成する
if(!ObjectCreate(chart_id, OBJ_NAME, OBJ_CHANNEL, 0, time1, price1, time2, price2, time1, price1-distance))
{
PrintFormat("%s: ObjectCreate() failed. Error %d",__FUNCTION__, GetLastError());
return(false);
}
//--- チャートを更新して「true」を戻す
ChartRedraw(chart_id);
return(true);
}
|