Coding RSI protect

 
Hi!! I've been trying code a RSI protect. Something like this logic: if RSI>90 and orderType=buy and orderprofit>=x pips, move SL to +x pips. This has to be easy to code but I'm newbie and I can't get the code I want. any help?
 
Great!! Finally I'll get without help!! This code seems work fine on backtest. Can you check for some bad code? Thanks!!
void checkOverProtect(){
  if(OrdersTotal()>0){
      for(int j=0;j<=OrdersTotal()-1;j++){   
         OrderSelect(j, SELECT_BY_POS);
        if (OrderMagicNumber() == MagicNumber){
         if(OrderType()==0 || OrderType()==1){
          if (OrderProfit()>=OverProtect && iRSI(NULL,0,RSI_period,PRICE_CLOSE,0)>90 && OrderType()==0) {
             OrderClose(OrderTicket(),OrderLots(),Bid,2,Violet);
             }
             if (OrderProfit()>=OverProtect && iRSI(NULL,0,RSI_period,PRICE_CLOSE,0)<10 && OrderType()==1){
             OrderClose(OrderTicket(),OrderLots(),Ask,2,Violet);
}
             }
             }
             }            
             }
Also if you can help me with this closeorder code..It works but close order with other Magic Number, so I think the MAgic Number code is wrong.
void CloseAllOrder()
{
      int total  = OrdersTotal();
      for (int cnt = 0 ; cnt < total ; cnt++){
     
         OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
         if (OrderMagicNumber() == MagicNumber) {
            if(OrderType()==OP_BUY){
             OrderClose(OrderTicket(),OrderLots(),Bid,2,Violet);
           }
            if(OrderType()==OP_SELL)  {
             OrderClose(OrderTicket(),OrderLots(),Ask,2,Violet);
            }
             if(OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)  {  
           OrderDelete(OrderTicket());
   }     
}
}
}
 

Run the loops in the other direction, from the top of the "list" to the bottom

Old:

for (int cnt = 0 ; cnt < total ; cnt++){

Change to:

for( int cnt = OrdersTotal()-1; cnt >= 0; cnt--)

Or you can have failures when there is more than one order to be processed.

 

Thanks for your help.

Now I have other problem, the expternal vatiable for Rsi protect is in pips, but OrderProfit returns net money progit. I've been testing with 0.1 lot and works fine because 1pips=1$.

How can I code the RSI protect to returns Order profit in pips?

I've tried this but dosen't work. I have to learn a lot of!!!

if(OrdersTotal()>0){
      for(int j=0;j<=OrdersTotal()-1;j++){   
         OrderSelect(j, SELECT_BY_POS);
        if (OrderMagicNumber() == MagicNumber){
         if(OrderType()==0 || OrderType()==1){
          if ((Bid-OrderOpenPrice())>=OverProtect && iRSI(NULL,0,3,PRICE_CLOSE,0)>90 && OrderType()==0) {
             OrderClose(OrderTicket(),OrderLots(),Bid,2,Violet);
             }
             if ((OrderOpenPrice()-Ask)>=OverProtect && iRSI(NULL,0,3,PRICE_CLOSE,0)<10 && OrderType()==1){
             OrderClose(OrderTicket(),OrderLots(),Ask,2,Violet);}
             }
             }
             }
              }
 
I've write this and seem work.
if(OrdersTotal()>0){
      for(int j=0;j<=OrdersTotal()-1;j++){   
         OrderSelect(j, SELECT_BY_POS);
        if (OrderMagicNumber() == MagicNumber){
         if(OrderType()==0 || OrderType()==1){
          if (((MarketInfo(Symbol(),MODE_BID) - OrderOpenPrice()) / MarketInfo(Symbol(),MODE_POINT) )>=OverProtect && iRSI(NULL,0,3,PRICE_CLOSE,0)>90 && OrderType()==0) {
             OrderClose(OrderTicket(),OrderLots(),Bid,2,Violet);//Buy case
             }
             if (((OrderOpenPrice() - MarketInfo(Symbol(),MODE_ASK)) / MarketInfo(Symbol(),MODE_POINT))>=OverProtect && iRSI(NULL,0,3,PRICE_CLOSE,0)<10 && OrderType()==1){
             OrderClose(OrderTicket(),OrderLots(),Ask,2,Violet);}//Sell case
             }
             }
             }
              }
Reason: