alert close cross Moving Average

 

alert close cross Moving Average
recently I've gone from metastock to Meta4 who I can write this formula:


Alert cross (c, Moving Average 20)

 
if you are entering your trades manually and wish to create a script to close your order on MA cross (i think i read your other post). It should not be that hard. however, you have only supplied one MA period? what is it crossing? MA50?

You will need to define your moving averages first:

double MA1 = iMA(NULL, PERIOD_M30, 20, 0, MODE_SMMA, PRICE_CLOSE, Current + 0);

double MA2 = iMA(NULL, PERIOD_M30, 50, 0, MODE_SMMA, PRICE_CLOSE, Current + 0);

double Prev_MA1 = iMA(NULL, PERIOD_M30, 20, 0, MODE_SMMA, PRICE_CLOSE, Current + 1); // MA 20 of previous bar

double Prev_MA2 = iMA(NULL, PERIOD_M30, 50, 0, MODE_SMMA, PRICE_CLOSE, Current + 1); // MA 50 of previous bar

// close order when MA has crossed

if ((MA1 < MA2) && (Prev_MA1 > Prev_MA2)) Order = SIGNAL_CLOSEBUY;

if ((MA1 > MA2) && (Prev_MA1 < Prev_MA2)) Order = SIGNAL_CLOSESELL;



You will of course need to define SIGNAL_CLOSEBUY; and SIGNAL_CLOSESELL; with something to retreive and close the order. Code below is from a template, so this is just an example as im not a programmer ;)

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}