内容自体はMT4のサンプルプログラム「Moving Average.mq4」の「CheckForOpen」関数を参考にして、下記のように作成しました。
input int MovingPeriodShort =5;input int MovingPeriodLong =50;input int MovingShiftShort =6;input int MovingShiftLong =18;//+------------------------------------------------------------------+//| The function of judgment the trend of the market |//| return value: 1 = Up trend |//| 2 = Down trend |//| 0 = Neither Up trend nor down trend |//| -1 = Other than first tiks of new bar |//+------------------------------------------------------------------+int CheckMarketTrend() {double maLong;double maShort;int res;//--- go trading only for first tiks of new barif(Volume[0]>1) return(-1);//--- get Moving AveragemaLong = iMA(NULL,0,MovingPeriodLong, MovingShiftLong, MODE_SMA,PRICE_CLOSE,0);maShort = iMA(NULL,0,MovingPeriodShort,MovingShiftShort,MODE_SMA,PRICE_CLOSE,0);//--- Judge the trend of the marketif(maShort > maLong) { // アップトレンド判定res = 1;}else if(maLong > maShort) { // ダウントレンド判定res = 2;} else {res = 0;}return(res);}
この関数「CheckMarketTrend」の返値として、1ならアップトレンド、2ならダウントレンド、0ならそれ以外、-1なら実行したかったタイミングではないとの判断結果を返します。
この中で、MQL4の組み込み関数「iMA関数」は移動平均の値を算出する関数です。
詳しくは下記で確認すると良いですが、短期および長期の移動平均の値をそれぞれ算出しています。
移動平均線(Moving Average)【iMA】
設計に基づいて、この関数で算出した maLong と maShort を比較して、トレンドを判断しています。
設計:トレードロジック①
なお、後で最適化できるように移動平均の期間および表示移動をパラメータとして定義しました。
input int MovingPeriodShort =5;この辺の値は適当だったので、調整できる柔軟性を持たせているのは一つのポイントです。input int MovingPeriodLong =50;input int MovingShiftShort =6;input int MovingShiftLong =18;
これについては、実際にバックテストを行うタイミングで改めて触れたいと思います。
※本日の更新分はGitHubに更新しました。
https://github.com/sysfxtry1/ea01/blob/master/ea01.mq4