Where did I do wrong ? Help for newbie. (Function Call or Loop Error , I don't know)

 

Hi, I'm trying to write a simple rsi EA code for mt4 but my code is working just part time . ClosePosition function is not working.( probably my code is a bit weirdo lol ) . It worked without a problem few times but mostly didn't work on the same computer and also didn't produce any error code... I'm trying to solve the problem since 3 hours. Anyone can explain me the overall problem also where I exactly put the ClosePosition function in the code ? 

 ////////////////////////////////////

extern double rsi_high =         80;

extern double rsi_low =          20;

extern double rsi5_high =        68;

extern double rsi5_low =         32;

extern int RSILength =           14;

extern double Lots =             0.1;

extern int TakeProfit =          25;

extern double StopLoss =         0;

extern int Slippage =            7;

extern int OrderMax =            1;

extern bool BuyRisk =            TRUE;

extern bool SellRisk =           TRUE;

extern int riskClockBuy =        600;

extern int riskClockSell =       900;

extern int startTime =           9;

extern int finishTime =          19;   

/////////////////////////////////////



int ThisBarTrade        =      0;

int Position            =      0;



double RSI;

double RSI5;



////////////////////////





int start() {



ClosePosition();

    

if( OrdersTotal() < OrderMax ) { 

    

    int iShift = iBarShift(Symbol(), PERIOD_M1, Time[0], false );

    int iShift5 = iBarShift(Symbol(), PERIOD_M5, Time[0], false );

    

    RSI=iRSI(NULL, PERIOD_M1, RSILength, PRICE_CLOSE, iShift );

    RSI5=iRSI(NULL, PERIOD_M5, RSILength, PRICE_CLOSE, iShift5 );

    

    double BuyStopLossLevel = Ask - StopLoss*Point;

    double BuyTakeProfitLevel = Ask +TakeProfit*Point;

    double SellStopLossLevel = Bid + StopLoss*Point;         

    double SellTakeProfitLevel = Bid - TakeProfit*Point;

    

    if ((TimeHour(TimeCurrent())>= startTime && TimeHour(TimeCurrent()) < finishTime)){ 

       if ( ThisBarTrade != Bars) {

          if(StopLoss==0) {

          SellStopLossLevel = 0; BuyStopLossLevel = 0;

          }

               // Buy Condition

              

          if (RSI <= rsi_low && RSI5 <= rsi5_low  ) {

         

               ThisBarTrade = Bars;

               Print("Buy ",Symbol()," ","RSI=", RSI," ","RSI 5=",RSI5," " , "Price = ",Ask);

               OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, BuyStopLossLevel ,BuyTakeProfitLevel);

               

          // Buy code goes here    

          }

          // Sell Condition

          if (RSI >= rsi_high && RSI5 >= rsi5_high  ){

         

               ThisBarTrade = Bars;

               Print("Sell ",Symbol()," ","RSI=", RSI," ","RSI 5=",RSI5," " , "Price = ",Bid);

               OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage,SellStopLossLevel, SellTakeProfitLevel);

               

          // Sell code goes here

    

          }

      }

   }

}

return 0;                 

}

 
void ClosePosition() {
    bool closed;
for (int i = 0; i < OrdersTotal(); i++) {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    while(!IsTradeAllowed()) Sleep(100);
    RefreshRates();
    if (Symbol()== OrderSymbol()){               
               if ( OrderType() == OP_BUY ) {
               if (BuyRisk == TRUE) {
                 if( (int)TimeCurrent()-(int)OrderOpenTime() >= riskClockBuy && RSI >= rsi_low && RSI5 >= rsi5_low ) {
                       closed=OrderClose( OrderTicket(), OrderLots(), Ask,0,  White);
                       Print ("Attempting to CloseSingleBuy order # " + i + " " + Ask);
                        if(closed==false)
                        return(false);
                        else 
                        return(true);
                    
                    }
                 }
              }
              
                          
              if ( OrderType() == OP_SELL) {
                  if (SellRisk == TRUE){
                
                  if ( (int)TimeCurrent()-(int)OrderOpenTime() >= riskClockSell && RSI <= rsi_high && RSI5 <= rsi5_high ){ 
                        closed=OrderClose( OrderTicket(), OrderLots(),Bid,0, White);
                        Print ("Attempting to CloseSingleBuy order # " + i + " " + Bid);
                        if(closed==false)
                        return(false);
                        else 
                        return(true);
                        
                      
                   }
              }
         }                 
            
               
     }
   }   
 }
 
Cnsnmmsdidn't produce any error code...
OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage,SellStopLossLevel, SellTakeProfitLevel);

closed=OrderClose( OrderTicket(), OrderLots(), Ask,0,  White);
  1. No error code because you don't check for them.
  2. Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
 
Thanks for comment. I solved it and you are right about the error code problem :) sorry
Reason: