iOsMA

この関数は Moving Average of Oscillator(移動平均オシレーター)指標のハンドルを返します。OsMA オシレーターは MACD とシグナル線の差を表示します。バッファは 1 つです。

int  iOsMA(
  string              symbol,              // 銘柄名
  ENUM_TIMEFRAMES    period,              // 期間
  int                fast_ema_period,    // 高速移動平均期間
  int                slow_ema_period,    // 低速移動平均期間
  int                signal_period,      // 差の平均期間
  ENUM_APPLIED_PRICE  applied_price        // 価格の種類かハンドル
  );

パラメータ

symbol

[in] データが指標計算に使用される有価証券の銘柄名(NULL は現在のシンボル)

period

[in] 期間の値は ENUM_TIMEFRAMES 列挙の値の 1 つで、0 は現在の時間軸の意味です。

fast_ema_period

[in]  高速移動平均計算の期間

slow_ema_period

[in]  低速移動平均計算の期間

signal_period

[in]  シグナル線計算の平均期間

applied_price

[in]  使用される価格。ENUM_APPLIED_PRICE 価格定数のいずれか、または別の指標ハンドル。

戻り値

指定されたテクニカル指標ハンドル。失敗の場合 INVALID_HANDLEIndicatorRelease() 関数に指標ハンドルを渡すことによって 、コンピュータメモリを利用されていない指標から解放することが出来ます。

注意事項

システムによってはこのオシレーターは MACD ヒストグラムとして知られています。

例:

//+------------------------------------------------------------------+
//|                                                   Demo_iOsMA.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 iOsMA 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 Moving Average of Oscillator."
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- iOsMA プロット
#property indicator_label1 "iOsMA"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrSilver
#property indicator_style1 STYLE_SOLID
#property indicator_width1  1
//+------------------------------------------------------------------+
//| 作成を処理する方法の列挙                                              |
//+------------------------------------------------------------------+
enum Creation
 {
  Call_iOsMA,             // iOsMA を使用する
  Call_IndicatorCreate   // IndicatorCreateを使用する
 };
//--- 入力パラメータ
input Creation             type=Call_iOsMA;           // 関数の種類
input int                  fast_ema_period=12;       // 高速 MA 期間
input int                  slow_ema_period=26;       // 低速 MA 期間
input int                  signal_period=9;           // 差の平均期間
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // 価格の種類
input string               symbol=" ";               // シンボル
input ENUM_TIMEFRAMES     period=PERIOD_CURRENT;     // 時間軸
//--- 指標バッファ
double         iOsMABuffer[];
//--- iAMA 指標ハンドルを格納する変数
int    handle;
//--- 格納に使用される変数
string name=symbol;
//--- チャートでの指標名
string short_name;
//--- 移動平均線指標に値の数を保存
int    bars_calculated=0;
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数                                            |
//+------------------------------------------------------------------+
int OnInit()
 {
//--- 配列の指標バッファへの割り当て
  SetIndexBuffer(0,iOsMABuffer,INDICATOR_DATA);
//--- 指標が描画するシンボルを決める
  name=symbol;
//--- 左右のスペースを削する
  StringTrimRight(name);
  StringTrimLeft(name);
//---「name」文字列の長さがゼロになった場合
  if(StringLen(name)==0)
    {
    //--- 指標が接続されているチャートのシンボルを取る
     name=_Symbol;
    }
//--- 指標ハンドルを作成する
  if(type==Call_iOsMA)
     handle=iOsMA(name,period,fast_ema_period,slow_ema_period,signal_period,applied_price);
  else
    {
    //--- 構造体を指標のパラメータで記入    
     MqlParam pars[4];
    //--- 高速 MA 期間
     pars[0].type=TYPE_INT;
     pars[0].integer_value=fast_ema_period;
    //--- 低速 MA 期間
     pars[1].type=TYPE_INT;
     pars[1].integer_value=slow_ema_period;
    //--- 高速移動平均と低速移動平均の差の平均期間
     pars[2].type=TYPE_INT;
     pars[2].integer_value=signal_period;
    //--- 価格の種類
     pars[3].type=TYPE_INT;
     pars[3].integer_value=applied_price;
     handle=IndicatorCreate(name,period,IND_OSMA,4,pars);
    }
//--- ハンドルが作成されなかった場合
  if(handle==INVALID_HANDLE)
    {
    //--- 失敗した事実とエラーコードを出力する
    PrintFormat("Failed to create a handle of iOsMA for the pair %s/%s, error code is %d",
                 name,
                EnumToString(period),
                GetLastError());
    //--- 指標が早期に中止された
    return(INIT_FAILED);
    }
//--- 移動平均オシレーター指標が計算された銘柄/時間軸を表示
  short_name=StringFormat("iOsMA(%s/%s,%d,%d,%d,%s)",name,EnumToString(period),
                          fast_ema_period,slow_ema_period,signal_period,EnumToString(applied_price));
  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[])
 {
//--- iOsMA 指標から複製された値の数
  int values_to_copy;
//--- 指標で計算された値の数を決める
  int calculated=BarsCalculated(handle);
  if(calculated<=0)
    {
    PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());
    return(0);
    }
//--- これが指標計算の初めであるか iOsMA 指標の値の数が変更した changed
//---または、2 つ以上のバーの指標の計算が必要である場合(価格履歴で何かが変更された)
  if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
    {
    //--- iOsMABuffer 配列が銘柄/期間で iOsMA 指標の値の数より大きい場合、全体のコピーはしない
    //--- 他の場合、指標バッファサイズより少ない量をコピーをする
    if(calculated>rates_total) values_to_copy=rates_total;
    else                       values_to_copy=calculated;
    }
  else
    {
    //--- これは初回の計算ではなく、
    //--- 前回の OnCalculate() から、一以上のバーが加えられてない。
     values_to_copy=(rates_total-prev_calculated)+1;
    }
//--- 配列をiOsMA 指標の値で記入
//--- FillArrayFromBuffer が false を返した場合、情報の準備が終わっていないので操作を終了する
  if(!FillArrayFromBuffer(iOsMABuffer,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);
 }
//+------------------------------------------------------------------+
//| iOsMA 指標から指標バッファを記入する                                      |
//+------------------------------------------------------------------+
bool FillArrayFromBuffer(double &ama_buffer[], // OsMA 値の指標バッファ
                        int ind_handle,       // iOsMA 指標ハンドル
                        int amount             // 複製された値の数
                        )
 {
//--- エラーコードをリセットする
  ResetLastError();
//--- インデックス0 を持つ指標バッファの値で iOsMABuffer 配列の一部を記入する
  if(CopyBuffer(ind_handle,0,0,amount,ama_buffer)<0)
    {
    //--- 複製が失敗したら、エラーコードを出す
    PrintFormat("Failed to copy data from the iOsMA indicator, error code %d",GetLastError());
    //--- ゼロ結果で終了。 指標は計算されていないと見なされる
    return(false);
    }
//--- 全てが成功
  return(true);
 }
//+------------------------------------------------------------------+
//| 指標初期化解除関数                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
 {
  if(handle!=INVALID_HANDLE)
    IndicatorRelease(handle);
//--- 指標の削除後チャートをクリアする
  Comment("");
 }