MT5用ラウンドナンバー表示インジケーターを作ってみた
ラウンドナンバー表示インジケーターは実は良いものがない問題
MT5向けにはラウンドナンバー表示系インジケーターは探せば結構あります。過去色々使ってみましたが、機能が今一歩足りなかったり、逆に機能が豊富過ぎて重かったりして、丁度良いものがありませんでした。
というわけで、いつものようにChatGPTを使って、なるべく軽く、それでいて使いやすいラウンドナンバー表示インジケーターを作ってみました。
IntervalLines.ex5
ダウンロード 10.37 KB
コード解説
出来上がったコードはこんな感じ。ChatGPTとラリーして、多少手を加えて完成させました。
//+------------------------------------------------------------------+
//| IntervalLines.mq5 |
//| Copyright 2024, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
input double Interval = 0.100; // 価格間隔
input int MaxLines = 10; // 現在価格を基準とした上下各本数
input color LineColor = clrRed; // 水平線の色
input ENUM_LINE_STYLE LineStyle = STYLE_DASH; // 水平線のスタイル
input int LineWidth = 1; // 水平線の幅
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// インジケーターの初期化時に水平線を描画
DrawIntervalLines();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// 水平線を削除
DeleteIntervalLines();
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
return(rates_total);
}
//+------------------------------------------------------------------+
//| Draw round number lines |
//+------------------------------------------------------------------+
void DrawIntervalLines()
{
double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
for(int i = -MaxLines; i <= MaxLines; i++)
{
double price = RoundToNearest(currentPrice + i * Interval, Interval);
string lineName = "IntervalLine_" + DoubleToString(price, _Digits);
if(ObjectFind(0, lineName) == -1)
{
ObjectCreate(0, lineName, OBJ_HLINE, 0, 0, price);
ObjectSetInteger(0, lineName, OBJPROP_COLOR, LineColor);
ObjectSetInteger(0, lineName, OBJPROP_STYLE, LineStyle);
ObjectSetInteger(0, lineName, OBJPROP_WIDTH, LineWidth);
}
}
}
//+------------------------------------------------------------------+
//| Delete round number lines |
//+------------------------------------------------------------------+
void DeleteIntervalLines()
{
int total = ObjectsTotal(0, -1, -1);
for(int i = total - 1; i >= 0; i--)
{
string name = ObjectName(0, i);
if(StringFind(name, "IntervalLine_") != -1)
{
ObjectDelete(0, name);
}
}
}
//+------------------------------------------------------------------+
//| Round to the nearest interval |
//+------------------------------------------------------------------+
double RoundToNearest(double value, double interval)
{
return(MathRound(value / interval) * interval);
}
//+------------------------------------------------------------------+
// This code was generated with the assistance of ChatGPT by OpenAI
// No copyright claims are made by OpenAI for the generated code
ポイントは2点。
1つ目は、水平線の間隔を価格ベースの小数値で指定する形にしたこと。ポイントやpipsの形式で整数指定するインジケーターが多いのですが、これだとポイントの小数位置が感覚的に把握し辛いという問題があります。個人的にもっと分かりやすい、桁の位置が分かる形に変更しました。
結果的に"0.123″の倍数など、あえてキリ番とならない間隔で引くこともできるようになりました。(需要は全くなさそうですが…。)
2つ目は、ライン本数の制限。現在価格を中心に上下に任意の本数を指定します。線があり過ぎてチャートが見づらくなることを防止するための措置です。"10″を指定すると、現在価格を基準に上に10本、下に10本の合計20本の水平線を引きます。
新たな現在価格を基準に線を引き直したい時は、外部入力の項目のどれかを変えるのが手っ取り早いです。





ディスカッション
コメント一覧
まだ、コメントがありません