MENU
  • 必要証拠金計算ツール
  • FX複利計算シミュレーター
  • 余力に応じたロット計算ツール
  • お問い合わせ
FXあれこれ
  • 必要証拠金計算ツール
  • FX複利計算シミュレーター
  • 余力に応じたロット計算ツール
  • お問い合わせ
  1. ホーム
  2. MQL
  3. 自作インジケータ
  4. 【MQL4】フラクタルにトレンドラインを自動で引く

【MQL4】フラクタルにトレンドラインを自動で引く

2025 7/09
自作インジケータ
2025年7月9日
当ページのリンクには広告が含まれています。

直近2点のフラクタルの状態が下記条件に合致する場合にのみ、フラクタルを繋いでトレンドラインを引くインジケータを作成しました。

  • 直近のUpperフラクタルが、前回のUpperフラクタルより低い場合、下落トレンドラインの生成
  • 直近のLowerフラクタルが、前回のLowerフラクタルより高い場合、上昇トレンドラインの生成
//+------------------------------------------------------------------+
//|                                            FractalTrendLines.mq4 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  Gold
#property indicator_color2  Gold

//---- indicator parameters

//---- indicator buffers
double ExtUpperBuffer[];
double ExtLowerBuffer[];
const string prefix = "FT";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
    SetIndexStyle(0,DRAW_ARROW,0,2);
    SetIndexBuffer(0,ExtUpperBuffer);
    SetIndexArrow(0, 217);
    ArraySetAsSeries(ExtUpperBuffer, true);
    SetIndexLabel(0,"Fractals Upper");

    SetIndexStyle(1,DRAW_ARROW,0,2);
    SetIndexBuffer(1,ExtLowerBuffer);
    SetIndexArrow(1, 218);
    ArraySetAsSeries(ExtLowerBuffer, true);
    SetIndexLabel(1,"Fractals Lower");

    return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
    ObjectsDeleteAll(0,prefix,0);
}
//+------------------------------------------------------------------+
//| 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[])
{
    int limit = rates_total-prev_calculated-1;
    for (int i=limit; i>=0; i--)
    {
        double upper = iFractals(NULL,0,MODE_UPPER,i);
        double lower = iFractals(NULL,0,MODE_LOWER,i);
        ExtUpperBuffer[i]=upper;
        ExtLowerBuffer[i]=lower;
    }
    
    CreateUpperLine(rates_total);
    CreateLowerLine(rates_total);

    return(rates_total);
}

void CreateUpperLine(int rates_total)
{
    for (int i = 0; i < rates_total; i++) {
        int start_pos = -1;
        int end_pos   = -1;
        double start_val = 0;
        double end_val   = 0;
    
        for (int j = i; j < rates_total; j++) {
            if (ExtUpperBuffer[j] != 0) {
                if (start_pos == -1) {
                    start_pos = j;
                    start_val = ExtUpperBuffer[j];
                } else {
                    end_pos = j;
                    end_val = ExtUpperBuffer[j];
                    break; // 2つ見つけたら終わり
                }
            }
        }
    
        if (start_pos != -1 && end_pos != -1
        && start_val<end_val) {
            string name = prefix + "_Upper_" + IntegerToString(start_pos);
            ObjectDelete(0, name);
            ObjectCreate(0, name, OBJ_TREND, 0,
                         Time[end_pos], end_val,
                         Time[start_pos], start_val);
            ObjectSetInteger(0, name,OBJPROP_WIDTH,3);
            ObjectSetInteger(0, name, OBJPROP_COLOR, clrRed);
            ObjectSetInteger(0, name, OBJPROP_RAY_RIGHT, true);
        }
    
        break;  // 最初の1本だけ描画して終了
    }
}

void CreateLowerLine(int rates_total)
{
    for (int i = 0; i < rates_total; i++) {
        int start_pos = -1;
        int end_pos   = -1;
        double start_val = 0;
        double end_val   = 0;
    
        for (int j = i; j < rates_total; j++) {
            if (ExtLowerBuffer[j] != 0) {
                if (start_pos == -1) {
                    start_pos = j;
                    start_val = ExtLowerBuffer[j];
                } else {
                    end_pos = j;
                    end_val = ExtLowerBuffer[j];
                    break; // 2つ見つけたら終わり
                }
            }
        }
    
        if (start_pos != -1 && end_pos != -1
        && start_val>end_val) {
            string name = prefix + "_Lower_" + IntegerToString(start_pos);
            ObjectDelete(0, name);
            ObjectCreate(0, name, OBJ_TREND, 0,
                         Time[end_pos], end_val,
                         Time[start_pos], start_val);
            ObjectSetInteger(0, name,OBJPROP_WIDTH,3);
            ObjectSetInteger(0, name, OBJPROP_COLOR, clrRed);
            ObjectSetInteger(0, name, OBJPROP_RAY_RIGHT, true);
        }
    
        break;  // 最初の1本だけ描画して終了
    }
}

\ すぐ始められる!/

XMでトレードする
自作インジケータ
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
  • 【MQL】MTF_Fractals(マルチタイムフレームフラクタルス)

関連記事

  • 【MQL4】とりあえずロジックのサインを確認するテストインジケータ
    2025年7月2日
  • 【MQL4】ZigZagでフィボナッチリトレースメントを自動で引くインジケーター
    2025年6月26日
  • 【MQL4】で開いていないチャートのデータを取得できない問題を解決するスクリプト
    2025年6月24日

コメント

コメントする コメントをキャンセル

新着記事
  • 【MQL4】フラクタルにトレンドラインを自動で引く
    2025年7月9日
    自作インジケータ
人気記事
  • 【MQL4】iStdDevOnArrayの使い方【配列に対する標準偏差】
    2025年6月18日
    テクニカル関数 9
  • ボリンジャーバンドの「長所」と「短所」、そして実践的なトレード手法2選!
    2025年6月2日
    FXの基礎 7
  • 【MQL】MTF_ADX(マルチタイムフレームADX)
    2025年6月2日
    マルチタイムフレーム 7
カテゴリー
  • FXの基礎 (8)
  • XM (7)
  • お知らせ (1)
  • テクニカル関数 (37)
  • トレード戦略 (2)
  • ハイレバの基本的な考え方 (1)
  • マルチタイムフレーム (6)
  • 自作インジケータ (4)
  • 証拠金計算ツール (1)
スポンサーリンク

\ すぐ始められる!/

XMでトレードする
目次

© FXあれこれ.

目次