Hi!
I have 1 problem with my EA. I am entering trade when EMA 5 > EMA 10 (buy signall). I have defined sl & tp. Trade is allways closed when sl or tp are reached. I would like to change that.
For example: I have bought pair EURUSD, and I would like to close this trade when EMA 5 crosses EMA 10. I set conditions (ticket != 0, stop=1 => buy and EMA 5 < EMA 10), but my operator if isn't working ()?!?
Seems my condition for closing that trade isn't good ??
Here is my code:
//+------------------------------------------------------------------+
//| testing.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//|
Forex Trading Software: Forex Trading Platform MetaTrader 4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
extern int Period_EMA_1=5;
extern int Period_EMA_2=10;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start() {
//----
int ticket;
double volume = MarketInfo(Symbol(),MODE_MINLOT);
double sl = Bid - 300*Point;
double tp = Bid + 300*Point;
double EMA_1_now, EMA_1_prev, EMA_1_pp;
double EMA_2_now, EMA_2_prev, EMA_2_pp;
double stop;
EMA_1_now=iMA(NULL,0,Period_EMA_1,0,MODE_EMA,PRICE _TYPICAL,0);
EMA_1_prev=iMA(NULL,0,Period_EMA_1,0,MODE_EMA,PRIC E_TYPICAL,+1);
EMA_1_pp=iMA(NULL,0,Period_EMA_1,0,MODE_EMA,PRICE_ TYPICAL,+2);
EMA_2_now=iMA(NULL,0,Period_EMA_2,0,MODE_EMA,PRICE _TYPICAL,0);
EMA_2_prev=iMA(NULL,0,Period_EMA_2,0,MODE_EMA,PRIC E_TYPICAL,+1);
EMA_2_pp=iMA(NULL,0,Period_EMA_2,0,MODE_EMA,PRICE_ TYPICAL,+2);
if ((EMA_1_pp < EMA_2_pp) && (OrdersTotal() == 0)) { // buy when EMA 5 >= EMA 10
if ((EMA_1_prev >= EMA_2_prev) && (EMA_1_prev-EMA_2_prev <= 5)) {
ticket= OrderSend (Symbol(), OP_BUY, volume, Ask, 2, sl, tp);
stop=1;
}
} else if ((EMA_1_pp > EMA_2_pp) && (OrdersTotal() == 0)) { // sell when EMA 5 <= EMA 10
if ((EMA_1_prev <= EMA_2_prev) && (EMA_2_prev-EMA_1_prev <= 5)) {
ticket=OrderSend (Symbol(), OP_SELL, volume, Bid, 2, tp, sl);
stop=2;
}
} else if ((EMA_1_pp == EMA_2_pp)&& (EMA_1_pp != 0) && (OrdersTotal() == 0)){ // buy when EMA 5 >= EMA 10
if ((EMA_1_prev > EMA_2_prev) && (EMA_1_prev-EMA_2_prev <= 5)) {
ticket= OrderSend (Symbol(), OP_BUY, volume, Ask, 2, sl, tp);
stop=1;
} if ((EMA_1_prev < EMA_2_prev) && (EMA_2_prev-EMA_1_prev <= 5)) { // sell when EMA 5 <= EMA 10
ticket=OrderSend (Symbol(), OP_SELL, volume, Bid, 2, tp, sl);
stop=2;
}
}
if ((ticket != 0) && (EMA_1_now < EMA_2_now) && (stop == 1)) { // <--------- PROBLEM ---------->
New_Stop_Sell(ticket, volume);
ticket=0;
}
return;
}
//+------------------------------------------------------------------+
int New_Stop_Sell(int tick, double vol){
OrderClose(tick,1,Bid,2);
}