# 求教！！！一个从不同周期取指标值的 新问题

71

1、有的交叉点时间还显示不对，比如，在M1有一个点是9H26，在M5显示是9H30，按说正常在M5显示在9H25的柱才对啊，？？？
2、有的交叉点丢失，这不是因为时间重叠的丢失，看图在10点多的大坑之前，M1有4次显示，而M5只有3次，时间上没有重叠可能，？？？

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Silver
#property indicator_width1 1
double LLL[];
double M1[];
double M2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(3);
SetIndexStyle(0, DRAW_ARROW, 0, 1);
SetIndexBuffer(0, LLL);
SetIndexBuffer(1, M1);
SetIndexBuffer(2, M2);
//----
return(0);
}
//----
int start()
{
int i, counted_bars=IndicatorCounted();
datetime time;
int iM1;
//----
i = Bars - counted_bars - 1;
while(i>=0)
{
time=iTime(NULL,0,i);
iM1=iBarShift(NULL,1,time,true);
M1[i]=iMA(NULL,1,55,0,MODE_SMA,PRICE_CLOSE,iM1);
M2[i]=iMA(NULL,1,21,0,MODE_SMA,PRICE_CLOSE,iM1);

if(M1[i+1]>M2[i+1] && M2[i]>M1[i])
LLL[i]=High[i]+10*Point; //1MN出现金叉时,在交叉点上方标记
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
195

71

hexinchen 写道 >>

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Silver
#property indicator_width1 1
#property indicator_color2 Red
#property indicator_width2 1
#property indicator_color3 Yellow
#property indicator_width3 1
double LLL[];
double M1[];
double M2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(3);
SetIndexStyle(0, DRAW_ARROW, 0, 1);
SetIndexBuffer(0, LLL);
SetIndexStyle(1, DRAW_LINE, 0, 1);
SetIndexBuffer(1, M1);
SetIndexStyle(2, DRAW_LINE, 0, 1);
SetIndexBuffer(2, M2);
//----
return(0);
}
//----
int start()
{
int i, counted_bars=IndicatorCounted();
datetime time;
int iM1;
//----
i = Bars - counted_bars - 1;
while(i>=0)
{
time=iTime(NULL,0,i);
iM1=iBarShift(NULL,1,time,false);

M1[i]=iMA(NULL,1,55,0,MODE_SMA,PRICE_CLOSE,iM1);
M2[i]=iMA(NULL,1,21,0,MODE_SMA,PRICE_CLOSE,iM1);

if(M1[i+1]>M2[i+1] && M2[i]>M1[i])
LLL[i]=High[i]+10*Point; //1MN出现金叉时,在交叉点上方标记
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+

178

```/*
test.mq4
last change: 2009.09.29.01:15.
draw_flag为画线标志。0:全画，1:画金叉，-1:画死叉。
以timeframe周期为计算基准，计算ma(period_1, period_2)的金叉、死叉。并将结果以竖线，画在k线图上。
*/

#property indicator_chart_window

#define SellColor	Red

extern int	draw_flag=1, timeframe=PERIOD_M1, period_1=21, period_2=55;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
return(0);
}

int deinit() {
ObjectsDeleteAll(0, OBJ_VLINE);

return(0);
}

int start() {
static int old_bars=0, count=0, old_time=0;
int i, j, bars, flag;
datetime time;
double M1[2], M2[2];
string str;

bars = iBars(NULL, timeframe);
i = bars - old_bars;
M2[0] = iMA(NULL, timeframe, period_1, 0, MODE_SMA, PRICE_CLOSE, i);
M2[1] = iMA(NULL, timeframe, period_2, 0, MODE_SMA, PRICE_CLOSE, i);
for (i=i-1; i>0; i--, M2[0]=M1[0], M2[1]=M1[1]) {
M1[0] = iMA(NULL, timeframe, period_1, 0, MODE_SMA, PRICE_CLOSE, i);
M1[1] = iMA(NULL, timeframe, period_2, 0, MODE_SMA, PRICE_CLOSE, i);
if (M2[0] < M2[1] && M1[0] > M1[1]) {
// ma(21)上穿ma(55)
time = iTime(NULL, timeframe, i);
j = TimeDayOfYear(time);
if (j != old_time) {
count = 0;
old_time = j;
} else
count++;
flag = 1;
} else if (M2[0] > M2[1] && M1[0] < M1[1]) {
// ma(21)下穿ma(55)
time = iTime(NULL, timeframe, i);
str = "_Sell point";
j = TimeDayOfYear(time);
if (j != old_time) {
count = 0;
old_time = j;
} else
count++;
flag = -1;
} else
flag = 0;
// 画金叉、死叉
if (flag != 0) {
if (draw_flag == flag)
draw_vline(flag, time, count, str);
else if (draw_flag == 0)
draw_vline(flag, time, count, str);
}
}

old_bars = bars;
return(0);
}
//+------------------------------------------------------------------+

void draw_vline(int flag, datetime time, int count, string str) {
// 画竖线
str = TimeToStr(time, TIME_DATE) + str + count + "#";
// 竖线已经存在
if (ObjectFind(str) != -1)
return;
ObjectCreate(str, OBJ_VLINE, 0, time, 0);
if (flag < 0)
ObjectSet(str,  OBJPROP_COLOR, SellColor);
else
ObjectSet(str, OBJPROP_STYLE, STYLE_DOT);
ObjectSet(str, OBJPROP_WIDTH, 1);
ObjectSet(str, OBJPROP_BACK, true);
ObjectSet(str, OBJPROP_TIMEFRAMES, OBJ_PERIOD_H4 | OBJ_PERIOD_H1 | OBJ_PERIOD_M30 | OBJ_PERIOD_M15 | OBJ_PERIOD_M5 | OBJ_PERIOD_M1);
}
```
36

y2k_connect 写道 >>