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

 
首先在此感谢hexinchen,他提供了从不同周期取数的解决链接,也感谢xszjg 的参与
不过目前还是有2个新问题要解决,请各位能人帮忙,在此先谢了!!!
同在09年9月11下,为统一数据,M5是用M1转换的数据
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);
}
//+------------------------------------------------------------------+
 

原因可能是在M1中,同一个bar中首先出现了金叉后进行了标记,然后不久价格降低,金叉不成立,而你的指标没有重画的功能,之前的标记没有消失。

 
hexinchen 写道 >>

原因可能是在M1中,同一个bar中首先出现了金叉后进行了标记,然后不久价格降低,金叉不成立,而你的指标没有重画的功能,之前的标记没有消失。

我想应该没有你说的情况,你想我能在M1标出,应该就能在M5标出才对啊,不管它是不是真的金叉或假金叉,M5只是取M1的数据,不是取M5自身的数据

也许原因出现在我的M1数据是不全的,即不是很连续的,有时会丢失1MN,或2MN的

不知你的数据是否完整,能帮我试试吗,看是数据的问题,还是程序问题,你可以试试这个新程序,直接把均线画在M5上,另外,原来iBarShift(NULL,1,time,true)的 true 现改为false,这样就跳过不连续数据,你可以试 true 的情形又是怎样,谢谢!!!


#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);
}
//+------------------------------------------------------------------+

 

始终用timeframe周期k线计算,就不会有交叉点丢失的现象。屏幕显示的k线,不用做为计算依据,仅做为画线的背景图。

用M1[2]、M2[2]替换了M1[]、M2[],减少了内存使用量。


/*
	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 BuyColor	Lime
#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);
			str = "_Buy point_";
			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_COLOR, BuyColor);
	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);
}
 
y2k_connect 写道 >>

始终用timeframe周期k线计算,就不会有交叉点丢失的现象。屏幕显示的k线,不用做为计算依据,仅做为画线的背景图。

用M1[2]、M2[2]替换了M1[]、M2[],减少了内存使用量。

挺清晰的,楼上达人啊,我的QQ241070252,愿意交个朋友共同研究

原因: