水平線を自動描画するシンプルなインジケーターを作ってみました。
入力パラメータは次の通りです。
- RangePeriod
RangePeriodは対象とする範囲を整数で指定。 - TimerSec
タイマー起動間隔(秒)
TimerSecは秒間隔で起動させるパラメータで、0にするとティック毎に再計算します。
1を設定すると1秒間隔、60を設定すると60秒間隔で再計算します。
一画面に収まる範囲のRangePeriodを設定するなりして調整しながら使ってください。
インジケーターの時間軸変更やパラメータ変更などで再処理し、インジケーター削除時には水平線オブジェクトが削除されます。
コピペ&コンパイルするだけで使えます。
//+------------------------------------------------------------------+
//| AutoHorizonLine.mq4 |
//| Copyright 2020, Greeds Co., Ltd. |
//| https://greeds.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Greeds Co., Ltd."
#property link "https://greeds.net/"
#property version "1.00"
#property strict
#property indicator_chart_window
//パラメータ
input int RangePeriod = 100;
input int TimerSec = 60;
//定数
const string OBJ_UPPER = "HorizonLineUpper";
const string OBJ_LOWER = "HorizonLineLower";
//変数
double PriceMax=DBL_MIN;
double PriceMin=DBL_MAX;
//+------------------------------------------------------------------+
//| ロード処理
//+------------------------------------------------------------------+
int OnInit()
{
ObjectDelete(0,OBJ_UPPER);
ObjectDelete(0,OBJ_UPPER);
if(TimerSec>0)
EventSetTimer(TimerSec);
HorizonLineProcess();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| アンロード処理
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectDelete(0,OBJ_UPPER);
ObjectDelete(0,OBJ_UPPER);
}
//+------------------------------------------------------------------+
//| ティック処理
//+------------------------------------------------------------------+
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[])
{
if(TimerSec>0) return(rates_total);
Print("OnCalc");
HorizonLineProcess();
return(rates_total);
}
//+------------------------------------------------------------------+
//| タイマー処理
//+------------------------------------------------------------------+
void OnTimer()
{
Print("OnTimer");
HorizonLineProcess();
}
//+------------------------------------------------------------------+
//| イベント処理
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
}
//+------------------------------------------------------------------+
//| 水平線ロジック
//+------------------------------------------------------------------+
void HorizonLineProcess()
{
for(int i=0;i<RangePeriod;i++)
{
if(PriceMax<High[i])
{
PriceMax=High[i];
if(ObjectFind(OBJ_UPPER)<0)
HorizonLineCreate(OBJ_UPPER,PriceMax);
HorizonLineMove(OBJ_UPPER,i,PriceMax);
}
if(PriceMin>Low[i])
{
PriceMin=Low[i];
if(ObjectFind(OBJ_LOWER)<0)
HorizonLineCreate(OBJ_LOWER,PriceMin);
HorizonLineMove(OBJ_LOWER,i,PriceMin);
}
}
}
void HorizonLineCreate(string name, double price)
{
//Print("Create");
ObjectCreate(0,name,OBJ_HLINE,0,0,price);
}
void HorizonLineMove(string name, int index, double price)
{
//Print("Move");
ObjectMove(name,0,Time[index],price);
}