function run()
{
vars Price = series(price());
vars maFast = series(SMA(Price, 50));
vars maSlow = series(SMA(Price, 200));
vars entries = series(0);
if(crossOver(maFast, maSlow))
{
entries[0] = 1;
if(Sum(entries+1, 6) == 0)
enterLong();
}
if(crossUnder(maFast, maSlow))
{
entries[0] = 1;
if(Sum(entries+1, 6) == 0)
enterShort();
}
}
This script stores each crossing of the moving averages in a series. The current value of the series is 1 if there was a cross, and 0 if there was no cross. We then shift the series backwards by one bar and sum the number of crosses in the previous 6 bars. We shift the series backwards by one bar in order to exclude the current cross from the summation. If we didn't do this, the script would never enter a trade since the if(Sum(...)) statement would always be true.