Help with coding alerts

 
Hi,

I'm new with mql4 and coding as a whole so can any of the experts please help me with the following two dilemmas.

I need to add an arrow and alert when price breaches an MA from above, but it should repaint as when the price does not close below the MA to disappear again.
It draws the arrow and alert for candle 0 when it breaches, but the alert goes of for the whole time the bar is open.
How can I make it to only alert just when the arrow is drawn, and not with every tick.

I've tried the following.


if(i==1 && SoundAlert)Alert(_Symbol, _Period," Sell Alert"); 

or 

if(i==0 && SoundAlert)Alert(_Symbol, _Period," Sell Alert"); 

.... 

i = 0 in the for loop

But keeps alerting with every tick.


Then my second query: I'd also like to know as with example above, if price closes above an MA and stays there, but after x bars closes again below to give me an alert. What I'd like help with is how do I specify the x bars back when price closed again below the MA. I need to specify how many bars back that price was above the MA and then when the current bar closed below the MA should give me an alert.

Where do I start, what would I need to look at?

I'm thinking the logic is something like follows: 

 if((Close [i+1] && Close[i+2]&&Close[i+3]) > MA && Close[i] <MA)

but instead like this line with the RED, I need to be able to select in the input how many bars back to look and it should adjust this logic accordingly. ie.: if(Close[range selected]>MA && Close[i]<MA)

Any help and direction is appreciated.