
【MQL4/MT4】トレンド状態を表示するインジケーター[Trend01] ソースコード無料公開
どうも、ヒロマックスです。
FXで資金を増やしていけるサインツールやEA(自動売買ツール)を作るために情報収集をしたり、コードを買いたりしています。
FX初心者なので、とにかく手を動かして、圧倒的作業量を投下して様々な手法をテストすることで、自分のトレードルールを作っていこうと思っています。
正直、FXだけに固執する必要もないんだろうなと思いつつ、まずは自動プログラムが書きやすそうなFXに絞ってノウハウを確立していきます。
とりあえず直近ではトレンド状態を可視化してくれるツールを複数作り、実践に使えそうなものについて、手応えを掴んでいこうと思っています。
■インジケーター「Trend01」
ということで今回は、エントリーにも使えることを目的にした、トレンド状態を指し示すインジケーターを作成しました。ソースコードと共に公開します。
▶インジケーター画像

表示は、ただ色分けするだけではなく、上下の配置を工夫することで、上昇なのか下降なのかをわかりやすくしました。
▶インジケーター設定画面と説明

内部的にはiMA()関数を使っていて、
時間軸(分)指定
SMA/EMA/SMMA/LMMAの選択
という2つの設定変更ができるように作っています。
▶プログラムファイル
実行ファイル(ex4)はこちら
ソースコード(mq4)はこちら
▶ソースコード
ソースコードはこちらです。
#property copyright "2024-2025, Hiromax."
#property link "https://note.com/hiro_max"
#property description "Hiromx Indicator Trend 01"
#property strict
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1 Black
#property indicator_color2 Lime
#property indicator_color3 Lime
#property indicator_color4 Red
#property indicator_color5 Red
#property indicator_color6 clrGold
#property indicator_color7 clrGold
#property indicator_width1 4
#property indicator_width2 4
#property indicator_width3 4
#property indicator_width4 4
#property indicator_width5 4
#property indicator_width6 4
#property indicator_width7 4
double rate=0.0001; // 許容幅
input int MA_PERIOD=5; // 時間軸(分)
input ENUM_MA_METHOD InpMAMethod=MODE_SMA; // 移動平均メソッド SMA/EMA/SMMA/LWMA
double ExtAOBuffer[];
double ExtUpBuffer1[];
double ExtUpBuffer2[];
double ExtDnBuffer1[];
double ExtDnBuffer2[];
double ExtFlBuffer1[];
double ExtFlBuffer2[];
#define PERIOD_FAST 5
#define PERIOD_SLOW 34
#define DATA_LIMIT 34
void OnInit(void)
{
SetIndexStyle(0,DRAW_NONE);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexStyle(2,DRAW_HISTOGRAM);
SetIndexStyle(3,DRAW_HISTOGRAM);
SetIndexStyle(4,DRAW_HISTOGRAM);
SetIndexStyle(5,DRAW_HISTOGRAM);
SetIndexStyle(6,DRAW_HISTOGRAM);
IndicatorDigits(Digits+1);
SetIndexDrawBegin(0,DATA_LIMIT);
SetIndexDrawBegin(1,DATA_LIMIT);
SetIndexDrawBegin(2,DATA_LIMIT);
SetIndexDrawBegin(3,DATA_LIMIT);
SetIndexDrawBegin(4,DATA_LIMIT);
SetIndexDrawBegin(5,DATA_LIMIT);
SetIndexDrawBegin(6,DATA_LIMIT);
SetIndexBuffer(0,ExtAOBuffer);
SetIndexBuffer(1,ExtUpBuffer1);
SetIndexBuffer(2,ExtUpBuffer2);
SetIndexBuffer(3,ExtDnBuffer1);
SetIndexBuffer(4,ExtDnBuffer2);
SetIndexBuffer(5,ExtFlBuffer1);
SetIndexBuffer(6,ExtFlBuffer2);
IndicatorShortName("Trend_01");
SetIndexLabel(1,NULL);
SetIndexLabel(2,NULL);
SetIndexLabel(3,NULL);
SetIndexLabel(4,NULL);
SetIndexLabel(5,NULL);
SetIndexLabel(6,NULL);
}
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 i,limit=rates_total-prev_calculated,trend=0;
double prev=0.0,current,ratio;
if(rates_total<=DATA_LIMIT)
return(0);
if(prev_calculated>0)
{
limit++;
prev=ExtAOBuffer[limit];
}
for(i=0; i<limit; i++)
ExtAOBuffer[i]=iMA(NULL,0,MA_PERIOD,0,InpMAMethod,PRICE_MEDIAN,i);
for(i=limit-1; i>=0; i--)
{
current=ExtAOBuffer[i];
if (prev>0) ratio=current/prev;
if (ratio>1+rate) trend=1;
if (ratio<1-rate) trend=0;
if (ratio>=1-rate && ratio<=1+rate) trend=2;
if (trend==0)
{
ExtDnBuffer1[i]=0;
ExtDnBuffer2[i]=-100;
}
if (trend==1)
{
ExtUpBuffer1[i]=100;
ExtUpBuffer2[i]=0;
}
if (trend==2)
{
ExtFlBuffer1[i]=50;
ExtFlBuffer2[i]=-50;
}
prev=current;
}
return(rates_total);
}
ご興味あれば、使ってみたり、コードを改変したりしてみてください。
今後もこんな感じで、勉強がてらインジケーターやEAを意欲的に作って、情報共有していきます。
■閑話休題
ところで最近、衝撃的な事実を知りました。
「トレードは後天的に学べるのか?」で有名な「タートルズ」の実験の被験者であり、稼ぎ頭であったカーティス・フェイスについてです。
彼は本も出しており、僕自身のトレードルールを作る上でかなり参考になっています。
そんな形でこれまで割と注目していたのですが、以下のサイトを見ると、今では生活に困窮していて、何度もしょうもない事件を起こして逮捕されており、現在はホームレスをやっているようです。
タートルを卒業した当初は相当資産を持っていて、素晴らしいトレードスキルも獲得していたはずなのに。
とはいえ、彼以外のタートルの何人かは大成功し続けることにも成功しています。例えば数億ドルを超える運用資金を今も運用しつづけているファンドを経営している人もいます。
トレードは後天的に学ぶことはできたとしても、人生単位で自分の幸福をしっかり作っていけるかは、また別の話ということみたいですね。
何か一つのことに成功しても、奢ることなく、人生単位で賢く立ち回っていきたいものです。
ということで、最後までお読み頂きましてありがとうございました!