//+------------------------------------------------------------------+
//| Demo_ChartIndicatorDelete.mq5 |
//| Copyright 2011, MetaQuotes Software Corp. |
//| https://www.MQL5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
//--- ヒストグラムをプロットする
#property indicator_label1 "Histogram"
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 入力パラメータ
input int first_param=1;
input int second_param=2;
input int third_param=3;
input bool wrong_init=true;
//--- 指標バッファ
double HistogramBuffer[];
string shortname;
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数 |
//+------------------------------------------------------------------+
int OnInit()
{
int res=INIT_SUCCEEDED;
//--- HistogramBuffer 配列を指標バッファに関係づける
SetIndexBuffer(0,HistogramBuffer,INDICATOR_DATA);
//--- 入力パラメータに基づいて、指標短縮名を構築する
shortname=StringFormat("Demo_ChartIndicatorDelete(%d,%d,%d)",
first_param,second_param,third_param);
IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- 指標の強制終了が設定されている場合、ゼロ以外の値を返す
if(wrong_init) res=INIT_FAILED;
return(res);
}
//+------------------------------------------------------------------+
//| カスタム指標の反復関数 |
//+------------------------------------------------------------------+
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[])
{
//--- ループを操作するための開始位置
int start=prev_calculated-1;
if(start<0) start=0;
//--- 指標バッファに値を記入する
for(int i=start;i<rates_total;i++)
{
HistogramBuffer[i]=close[i];
}
//--- 次の呼び出しのために prev_calculated の値を返す
return(rates_total);
}
//+------------------------------------------------------------------+
//| Deinit イベントハンドラ |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
PrintFormat("%s: Deinitialization reason code=%d",__FUNCTION__,reason);
if(reason==REASON_INITFAILED)
{
PrintFormat("An indicator with a short name %s (file %s) deletes itself from the chart",shortname,__FILE__);
int window=ChartWindowFind();
bool res=ChartIndicatorDelete(0,window,shortname);
//--- ChartIndicatorDelete() 呼び出しの結果を分析する
if(!res)
{
PrintFormat("Failed to delete indicator %s from window #%d. Error code %d",
shortname,window,GetLastError());
}
}
}
|