オブジェクトを使うので面倒だからかあまり見かけない
折角なので,備忘を兼ねてここに・・・


一応サンプル
MAを軸に上下にATR値を引く仕様テストモードがデフォルト
移動平均線と期間等をセットすると、上のラインには
( A ) 始値がMAより下で一度はMAを超えた
( B ) Aの条件を満たして、終値がMAより上
( C ) B/Aを%で
⇒ B : A = C% のように表示される
下のラインには逆条件からの結果を表示だよん

テストモードを、false にすると、それぞれの Price が
表示される。見に行く足の数は、10000本で変更できる
意味があるのかどうかは分からないけどね

一度作っておくと後でラクできるっしょ

ほかの細かいところは、mq4 の中身を見て自分用に手直
ししたり応用したり勝手にやっちくりくり



当然、少し書き換えるだけで好きな期間のトレンドも・・・
//+------------------------------------------------------------------+
//| MyMA.mq4
//| ( MA Test by TAWASHI )
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 DodgerBlue
#property indicator_color3 DodgerBlue
#property indicator_style1 STYLE_SOLID
#property indicator_style2 STYLE_DOT
#property indicator_style3 STYLE_DOT
#property indicator_width1 2
//---- indicator parameters
extern int MAPeriod=20;
extern int MAMode=0; // (0) SMA (1) EMA (2) SMMA (3) LWMA
extern int MAPrice=6; // (0) Close (1) Open (5) Typical (6) Weighted
extern int ATRPeriod=8;
extern int honsuu = 10000;
extern color LineColor = Yellow;
extern int LineStyle = STYLE_SOLID;
extern int LineWidth = 2;
extern color LineColor2 = DodgerBlue;
extern int LineStyle2 = STYLE_SOLID;
extern int LineWidth2 = 0;
extern bool TestMode=true;
//---- buffers
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
IndicatorBuffers(3);
//---- indicators
SetIndexBuffer(0,MovingBuffer); SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(1,UpperBuffer); SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(2,LowerBuffer); SetIndexStyle(2,DRAW_LINE);
//----
SetIndexDrawBegin(0,MAPeriod);
SetIndexDrawBegin(1,MAPeriod);
SetIndexDrawBegin(2,MAPeriod);
//----
ObjectCreate("MA_center" +MAPeriod, OBJ_TREND, 0, 0,0, 0,0);
ObjectSet("MA_center"+MAPeriod, OBJPROP_COLOR, LineColor);
ObjectSet("MA_center"+MAPeriod, OBJPROP_STYLE, LineStyle);
ObjectSet("MA_center"+MAPeriod, OBJPROP_WIDTH, LineWidth);
//----
ObjectCreate("MA_upper" +MAPeriod, OBJ_TREND, 0, 0,0, 0,0);
ObjectSet("MA_upper"+MAPeriod, OBJPROP_COLOR, LineColor2);
ObjectSet("MA_upper"+MAPeriod, OBJPROP_STYLE, LineStyle2);
ObjectSet("MA_upper"+MAPeriod, OBJPROP_WIDTH, LineWidth2);
ObjectCreate("MA_lower" +MAPeriod, OBJ_TREND, 0, 0,0, 0,0);
ObjectSet("MA_lower"+MAPeriod, OBJPROP_COLOR, LineColor2);
ObjectSet("MA_lower"+MAPeriod, OBJPROP_STYLE, LineStyle2);
ObjectSet("MA_lower"+MAPeriod, OBJPROP_WIDTH, LineWidth2);
//----
// put a label on the line
ObjectCreate("MT_upper"+MAPeriod, OBJ_TEXT, 0, 0, 0);
ObjectCreate("MT_lower"+MAPeriod, OBJ_TEXT, 0, 0, 0);
ObjectCreate("MT_center"+MAPeriod, OBJ_TEXT, 0, 0, 0);
//----
short_name="MyMA("+MAPeriod+")";
IndicatorShortName(short_name);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
void deinit() {
ObjectDelete("MA_upper" +MAPeriod);
ObjectDelete("MA_lower" +MAPeriod);
ObjectDelete("MA_center" +MAPeriod);
ObjectDelete("MT_upper" +MAPeriod);
ObjectDelete("MT_lower" +MAPeriod);
ObjectDelete("MT_center" +MAPeriod);
Comment("");
}
//+------------------------------------------------------------------+
//| My MA |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
double atr, ma;
//----
if(Bars<=MAPeriod) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=MAPeriod;i++)
{
MovingBuffer[Bars-i]=EMPTY_VALUE;
UpperBuffer[Bars-i]=EMPTY_VALUE;
LowerBuffer[Bars-i]=EMPTY_VALUE;
}
//----
int limit=Bars-counted_bars;
if(counted_bars>0) limit++;
for(i=0; i<limit; i++) {
MovingBuffer[i]=iMA(NULL,0,MAPeriod,0,MAMode,MAPrice,i);
atr = iATR(NULL, 0, ATRPeriod, i );
ma=MovingBuffer[i];
UpperBuffer[i]=ma + atr;
LowerBuffer[i]=ma - atr;
}
//----
string stup = " " ;
string stdn = " " ;
string stvl = " ";
int nup=0, tup=0, ndn=0, tdn=0;
double ub, lb, cb, dup, ddn;
string perup, perdn;
//---- TEST ----
for(i=0; i<honsuu; i++) {
if( High[i] > MovingBuffer[i] && Open[i] < MovingBuffer[i] ) {
tup++;
if( Close[i] > MovingBuffer[i] ) nup++;
}
if( Low[i] < MovingBuffer[i] && Open[i] > MovingBuffer[i] ) {
tdn++;
if( Close[i] < MovingBuffer[i] ) ndn++;
}
}
if( tup > 0 ) dup = 1000 * nup / tup;
if( tdn > 0 ) ddn = 1000 * ndn / tdn;
perup = DoubleToStr( dup/10, 1);
perdn = DoubleToStr( ddn/10, 1);
//-----------
ub=UpperBuffer[0];
lb=LowerBuffer[0];
cb=MovingBuffer[0];
//
ObjectSet("MA_upper"+MAPeriod, OBJPROP_TIME1, Time[1]);
ObjectSet("MA_upper"+MAPeriod, OBJPROP_TIME2, Time[0]);
ObjectSet("MA_upper"+MAPeriod, OBJPROP_PRICE1, ub);
ObjectSet("MA_upper"+MAPeriod, OBJPROP_PRICE2, ub);
//
ObjectSet("MA_lower"+MAPeriod, OBJPROP_TIME1, Time[1]);
ObjectSet("MA_lower"+MAPeriod, OBJPROP_TIME2, Time[0]);
ObjectSet("MA_lower"+MAPeriod, OBJPROP_PRICE1, lb);
ObjectSet("MA_lower"+MAPeriod, OBJPROP_PRICE2, lb);
//
ObjectSet("MA_center"+MAPeriod, OBJPROP_TIME1, Time[1]);
ObjectSet("MA_center"+MAPeriod, OBJPROP_TIME2, Time[0]);
ObjectSet("MA_center"+MAPeriod, OBJPROP_PRICE1, cb);
ObjectSet("MA_center"+MAPeriod, OBJPROP_PRICE2, cb);
//----
ObjectSet("MT_upper"+MAPeriod, OBJPROP_TIME1, Time[0]);
ObjectSet("MT_upper"+MAPeriod, OBJPROP_PRICE1, ub);
ObjectSet("MT_lower"+MAPeriod, OBJPROP_TIME1, Time[0]);
ObjectSet("MT_lower"+MAPeriod, OBJPROP_PRICE1, lb);
ObjectSet("MT_center"+MAPeriod, OBJPROP_TIME1, Time[0]);
ObjectSet("MT_center"+MAPeriod, OBJPROP_PRICE1, cb);
if( TestMode ) {
stup = stup + DoubleToStr(nup, 0) + " : " + DoubleToStr(tup, 0) + " = " + perup + "%";
stdn = stdn + DoubleToStr(ndn, 0) + " : " + DoubleToStr(tdn, 0) + " = " + perdn + "%";
} else {
stup = stup + DoubleToStr(ub, Digits);
stdn = stdn + DoubleToStr(lb, Digits);
stvl = stvl + DoubleToStr(cb, Digits);
}
ObjectSetText("MT_upper"+MAPeriod, stup, 10, "Arial", White);
ObjectSetText("MT_lower"+MAPeriod, stdn, 10, "Arial", White);
ObjectSetText("MT_center"+MAPeriod, stvl, 10, "Arial", Yellow);
//----
return(0);
}
//+------------------------------------------------------------------+

