Resetting variable value after Stoploss or Profit Taken using EA

 
Hi

I am very new to MQL4 programming and I am try to write Expert Advisor.

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





 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.

Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.

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.

 
ishf fady:
Hi

I am very new to MQL4 programming and I am try to write Expert Advisor.

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
Reason: