//+------------------------------------------------------------------+
//| Demo_iAlligator.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 description "The indicator demonstrates how to obtain data"
#property description "of indicator buffers for the iAlligator technical indicator."
#property description "A symbol and timeframe used for calculation of the indicator,"
#property description "are set by the symbol and period parameters."
#property description "The method of creation of the handle is set through the 'type' parameter (function type)."
#property description "All the other parameters are similar to the standard Alligator."
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
//--- 顎をプロットする
#property indicator_label1 "Jaws"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 歯をプロットする
#property indicator_label2 "Teeth"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- 口をプロットする
#property indicator_label3 "Lips"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrLime
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//+------------------------------------------------------------------+
//| 作成を処理する方法の列挙 |
//+------------------------------------------------------------------+
enum Creation
{
Call_iAlligator, // iAlligator を使用する
Call_IndicatorCreate // IndicatorCreateを使用する
};
//--- 入力パラメータ
input Creation type=Call_iAlligator; // 関数の種類
input string symbol=" "; // シンボル
input ENUM_TIMEFRAMES period=PERIOD_CURRENT; // 時間軸
input int jaw_period=13; // 顎線の期間
input int jaw_shift=8; // 顎線のシフト
input int teeth_period=8; // 歯線の期間
input int teeth_shift=5; // 歯線のシフト
input int lips_period=5; // 口線の期間
input int lips_shift=3; // 口線のシフト
input ENUM_MA_METHOD MA_method=MODE_SMMA; // アリゲーター線の平均化の方法
input ENUM_APPLIED_PRICE applied_price=PRICE_MEDIAN;// アリゲーター計算に使用される価格の種類
//--- 指標バッファ
double JawsBuffer[];
double TeethBuffer[];
double LipsBuffer[];
//--- iAlligator 指標ハンドルを格納する変数
int handle;
//--- 格納に使用される変数
string name=symbol;
//--- チャートでの指標名
string short_name;
//--- アリゲーター指標に値の数を保存
int bars_calculated=0;
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 配列の指標バッファへの割り当て
SetIndexBuffer(0,JawsBuffer,INDICATOR_DATA);
SetIndexBuffer(1,TeethBuffer,INDICATOR_DATA);
SetIndexBuffer(2,LipsBuffer,INDICATOR_DATA);
//--- それぞれの線のシフトを設定
PlotIndexSetInteger(0,PLOT_SHIFT,jaw_shift);
PlotIndexSetInteger(1,PLOT_SHIFT,teeth_shift);
PlotIndexSetInteger(2,PLOT_SHIFT,lips_shift);
//--- 指標が描画するシンボルを決める
name=symbol;
//--- 左右のスペースを削する
StringTrimRight(name);
StringTrimLeft(name);
//---「name」文字列の長さがゼロになった場合
if(StringLen(name)==0)
{
//--- 指標が接続されているチャートのシンボルを取る
name=_Symbol;
}
//--- 指標ハンドルを作成する
if(type==Call_iAlligator)
handle=iAlligator(name,period,jaw_period,jaw_shift,teeth_period,
teeth_shift,lips_period,lips_shift,MA_method,applied_price);
else
{
//--- 構造体を指標のパラメータで記入
MqlParam pars[8];
//--- アリゲーター線の期間とシフト
pars[0].type=TYPE_INT;
pars[0].integer_value=jaw_period;
pars[1].type=TYPE_INT;
pars[1].integer_value=jaw_shift;
pars[2].type=TYPE_INT;
pars[2].integer_value=teeth_period;
pars[3].type=TYPE_INT;
pars[3].integer_value=teeth_shift;
pars[4].type=TYPE_INT;
pars[4].integer_value=lips_period;
pars[5].type=TYPE_INT;
pars[5].integer_value=lips_shift;
//--- 平滑化の種類
pars[6].type=TYPE_INT;
pars[6].integer_value=MA_method;
//--- 価格の種類
pars[7].type=TYPE_INT;
pars[7].integer_value=applied_price;
//--- ハンドルを作成
handle=IndicatorCreate(name,period,IND_ALLIGATOR,8,pars);
}
//--- ハンドルが作成されなかった場合
if(handle==INVALID_HANDLE)
{
//--- 失敗した事実とエラーコードを出力する
PrintFormat("Failed to create handle of the iAlligator indicator for the symbol %s/%s, error code %d",
name,
EnumToString(period),
GetLastError());
//--- 指標が早期に中止された
return(INIT_FAILED);
}
//--- アリゲーターが計算された銘柄/時間軸を表示
short_name=StringFormat("iAlligator(%s/%s, %d,%d,%d,%d,%d,%d)",name,EnumToString(period),
jaw_period,jaw_shift,teeth_period,teeth_shift,lips_period,lips_shift);
IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- 通常の指標の初期化
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| カスタム指標の反復関数 |
//+------------------------------------------------------------------+
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[])
{
//--- iAlligator 指標から複製された値の数
int values_to_copy;
//--- 指標で計算された値の数を決める
int calculated=BarsCalculated(handle);
if(calculated<=0)
{
PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());
return(0);
}
//--- これが指標計算の初めであるか iAlligator 指標の値の数が変更した
//---または、2 つ以上のバーの指標の計算が必要である場合(価格履歴で何かが変更された)
if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
{
//--- もし JawsBuffer 配列が銘柄/期間で iAlligator 指標の値の数より大きい場合、全体のコピーはしない
//--- 他の場合、指標バッファサイズより少ない量をコピーをする
if(calculated>rates_total) values_to_copy=rates_total;
else values_to_copy=calculated;
}
else
{
//--- これは初回の計算ではなく、
//--- 前回の OnCalculate() から、一以上のバーが加えられてない。
values_to_copy=(rates_total-prev_calculated)+1;
}
//--- 配列をアリゲーター指標の値で記入
//--- FillArraysFromBuffer が false を返した場合、情報の準備が終わっていないので操作を終了する
if(!FillArraysFromBuffers(JawsBuffer,jaw_shift,TeethBuffer,teeth_shift,LipsBuffer,lips_shift,handle,values_to_copy)) return(0);
//--- メッセージを形成する
string comm=StringFormat("%s ==> Updated value in the indicator %s: %d",
TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS),
short_name,
values_to_copy);
//--- チャートにサービスメッセージを表示する
Comment(comm);
//--- アリゲーター指標の値の数を覚える
bars_calculated=calculated;
//--- prev_calculated 値を次の関数呼び出しのために返す
return(rates_total);
}
//+------------------------------------------------------------------+
//| iAlligator 指標から指標バッファを記入する |
//+------------------------------------------------------------------+
bool FillArraysFromBuffers(double &jaws_buffer[], // 顎線の指標バッファ
int j_shift, // 顎線のシフト
double &teeth_buffer[], // 歯線の指標バッファ
int t_shift, // 歯線のシフト
double &lips_buffer[], // 口線の指標バッファe
int l_shift, // 口線のシフト
int ind_handle, // iAlligator 指標ハンドル
int amount // 複製された値の数
)
{
//--- エラーコードをリセットする
ResetLastError();
//--- インデックス0 を持つ指標バッファの値で JawsBuffer 配列の一部を記入する
if(CopyBuffer(ind_handle,0,-j_shift,amount,jaws_buffer)<0)
{
//--- 複製が失敗したら、エラーコードを出す
PrintFormat("Failed to copy data from the iAlligator indicator, error code %d",GetLastError());
//--- ゼロ結果で終了。 指標は計算されていないと見なされる
return(false);
}
//--- インデックス0 を持つ指標バッファの値で TeethBuffer 配列の一部を記入する
if(CopyBuffer(ind_handle,1,-t_shift,amount,teeth_buffer)<0)
{
//--- 複製が失敗したら、エラーコードを出す
PrintFormat("Failed to copy data from the iAlligator indicator, error code %d",GetLastError());
//--- ゼロ結果で終了。 指標は計算されていないと見なされる
return(false);
}
//--- インデックス 2 を持つ指標バッファの値で LipsBuffer 配列の一部を記入する
if(CopyBuffer(ind_handle,2,-l_shift,amount,lips_buffer)<0)
{
//--- 複製が失敗したら、エラーコードを出す
PrintFormat("Failed to copy data from the iAlligator indicator, error code %d",GetLastError());
//--- ゼロ結果で終了。 指標は計算されていないと見なされる
return(false);
}
//--- 全てが成功
return(true);
}
//+------------------------------------------------------------------+
//| 指標初期化解除関数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(handle!=INVALID_HANDLE)
IndicatorRelease(handle);
//--- 指標の削除後チャートをクリアする
Comment("");
}
|