Resetting variable value after Stoploss or Profit Taken using EA
I have moved your topic to the MQL4 and Metatrader 4 section.
Check the OrderCloseTime() and if it does not equal 0, you know the trade has been closed.
Here is my simple EA using RSI. It is working fine and also put order. But have a ticket id, which I want to reset to 0 (Zero) after stopLoss or profitTaken.
I am having a problem to reset ticket to 0.
Here is the code. Please can some one guide be in the right direction.
Thank you.
Ish
int ticket = 0;
double lots = 0.01;
int takeprofit = 40;
int stoploss = 40;
int OnInit() {
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
}
//+------------------------------------------------------------------+
void OnTick(){
string comment;
double rsi = iRSI(_Symbol, PERIOD_CURRENT, 14, PRICE_CLOSE, 0);
comment += "\nRSI: " + DoubleToStr(rsi, _Digits);
if(rsi > 70){
comment += "\nShort";
if(OrderSelect(ticket,SELECT_BY_TICKET) && OrderType() == OP_BUY){
if(OrderClose(OrderTicket(), OrderLots(), Bid, 1000, clrAliceBlue)){
ticket = 0;
}
}
if(ticket <= 0){
ticket = OrderSend(_Symbol, OP_SELL, 0.01, Bid, 100, Bid+stoploss*Point, Bid-takeprofit*Point, "Sell Order", 1,0, clrRed);
}
}else if(rsi < 30){
comment += "\nLong";
if(OrderSelect(ticket,SELECT_BY_TICKET) && OrderType() == OP_SELL){
if(OrderClose(OrderTicket(), OrderLots(), Ask, 1000, clrAliceBlue)){
ticket = 0;
}
}
if(ticket <= 0){
ticket = OrderSend(_Symbol, OP_BUY, 0.01, Ask, 100, Bid+stoploss*Point, Bid-takeprofit*Point, "Buy Order", 1,0, clrGreen);
}
} // end if
Comment(comment);
} // end main
int ticket = 0; double lots = 0.01; int takeprofit = 40; int stoploss = 40; int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ void OnTick() { string comment; double rsi = iRSI(_Symbol, PERIOD_CURRENT, 14, PRICE_CLOSE, 0); comment += "\nRSI: " + DoubleToStr(rsi, _Digits); if(rsi > 70) // Buy Close and SELL Open { comment += "\nShort"; if(OrderSelect(ticket,SELECT_BY_TICKET) && OrderType() == OP_BUY) { if(OrderClose(OrderTicket(), OrderLots(), Bid, 1000, clrAliceBlue)) { ticket = OrderSend(_Symbol, OP_SELL, 0.01, Bid, 100, Bid+stoploss*Point, Bid-takeprofit*Point, "Sell Order", 1,0, clrRed); } } else if(rsi < 30) // SELL Close and BUY Open { comment += "\nLong"; if(OrderSelect(ticket,SELECT_BY_TICKET) && OrderType() == OP_SELL) { if(OrderClose(OrderTicket(), OrderLots(), Ask, 1000, clrAliceBlue)) { ticket = OrderSend(_Symbol, OP_BUY, 0.01, Ask, 100, Bid+stoploss*Point, Bid-takeprofit*Point, "Buy Order", 1,0, clrGreen); } } // end if Comment(comment); } } } // end main
Here is my simple EA using RSI. It is working fine and also put order. But have a ticket id, which I want to reset to 0 (Zero) after stopLoss or profitTaken.
I am having a problem to reset ticket to 0.
Here is the code. Please can some one guide be in the right direction.
Thank you.
Ish
int ticket = 0;
double lots = 0.01;
int takeprofit = 40;
int stoploss = 40;
int OnInit() {
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
}
//+------------------------------------------------------------------+
void OnTick(){
string comment;
double rsi = iRSI(_Symbol, PERIOD_CURRENT, 14, PRICE_CLOSE, 0);
comment += "\nRSI: " + DoubleToStr(rsi, _Digits);
if(rsi > 70){
comment += "\nShort";
if(OrderSelect(ticket,SELECT_BY_TICKET) && OrderType() == OP_BUY){
if(OrderClose(OrderTicket(), OrderLots(), Bid, 1000, clrAliceBlue)){
ticket = 0;
}
}
if(ticket <= 0){
ticket = OrderSend(_Symbol, OP_SELL, 0.01, Bid, 100, Bid+stoploss*Point, Bid-takeprofit*Point, "Sell Order", 1,0, clrRed);
}
}else if(rsi < 30){
comment += "\nLong";
if(OrderSelect(ticket,SELECT_BY_TICKET) && OrderType() == OP_SELL){
if(OrderClose(OrderTicket(), OrderLots(), Ask, 1000, clrAliceBlue)){
ticket = 0;
}
}
if(ticket <= 0){
ticket = OrderSend(_Symbol, OP_BUY, 0.01, Ask, 100, Bid+stoploss*Point, Bid-takeprofit*Point, "Buy Order", 1,0, clrGreen);
}
} // end if
Comment(comment);
} // end main