# 求两根均线，金叉死叉之间的K线根数、

```void OnStart()
{
Print(GetMaCrossKCount(30,60,MODE_SMA));
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetMaCrossKCount(int maPeriodShort,int maPeriodLong,ENUM_MA_METHOD maMethod)
{
int kStart=-1,kEnd=-1;
double maShortCur,maShortPrev,maLongCur,maLongPrev;
for(int ii=0; ii<Bars(Symbol(),Period())-1; ii++)
{
maShortCur=iMA(Symbol(),Period(),maPeriodShort,0,maMethod,PRICE_CLOSE,ii);
maShortPrev=iMA(Symbol(),Period(),maPeriodShort,0,maMethod,PRICE_CLOSE,ii+1);
maLongCur=iMA(Symbol(),Period(),maPeriodLong,0,maMethod,PRICE_CLOSE,ii);
maLongPrev=iMA(Symbol(),Period(),maPeriodLong,0,maMethod,PRICE_CLOSE,ii+1);
if(kStart==-1 &&
((maShortCur>maLongCur && maShortPrev<maLongPrev) ||
(maShortCur<maLongCur && maShortPrev>maLongPrev)))
{
kStart=ii;
continue;
}
if(kStart!=-1 && kEnd==-1 &&
((maShortCur>maLongCur && maShortPrev<maLongPrev) ||
(maShortCur<maLongCur && maShortPrev>maLongPrev)))
{
kEnd=ii;
break;
}
if(kStart!=-1 && kEnd!=-1)
break;
}
if(kStart!=-1 && kEnd!=-1)
return kEnd-kStart+1;
return -1;
}```

mt4 版

tanjimn:

mt5呢？

ambercrystal:

ambercrystal:

mt4 版

stone1968: