RSI EA problem

 

HI everyone today I coded this ea and it works but I've a problem with the buy positions, when opening a buy positions, the ea close it at the next bar! please help me!


the error


#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade/Trade.mqh>

input int Magicnumber = 2345;
input double lot      = 0.1;
input int RSIlevel    = 70;
input int MAperiod    = 20;
int RSIhandle;
double RSIvalue[];
CTrade trade;
MqlTick LastTick;

int OnInit(){
   
   RSIhandle = iRSI(_Symbol,PERIOD_CURRENT,MAperiod,PRICE_CLOSE);
   
   ArraySetAsSeries(RSIvalue,true);
   
   trade.SetExpertMagicNumber(Magicnumber);
   
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason){   
  }

void OnTick(){
   
   CopyBuffer(RSIhandle,0,1,2,RSIvalue);
   
   if(RSIvalue[1] < RSIlevel && RSIvalue[0] > RSIlevel && CountOpenPositions() == 0){
     trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,lot,LastTick.bid,0,0,"RSI SELL");
   }
   
   if(RSIvalue[1] > (100 -RSIlevel) && RSIvalue[0] < (100 - RSIlevel) && CountOpenPositions() == 0){
      trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,lot,LastTick.ask,0,0,"RSI BUY");
   }
   
   if(RSIvalue[1] <= (100 -RSIlevel)){
        ClosePositionSell();
   }   
    if(RSIvalue[1] >= RSIlevel){
         ClosePositionBuy();
   } 
  }
//+------------------------------------------------------------------+
int CountOpenPositions(){
int counter = 0;
int total = PositionsTotal();
   for(int i = total-1; i >= 0; i--){
      ulong ticket = PositionGetTicket(i);
      if(ticket <= 0 ){Print("Failed to get posiyion ticket"); return -1;}
      if(!PositionSelectByTicket(ticket)){Print("Failed to select position by ticket"); return -1;}
      ulong magicnumber;
      if(!PositionGetInteger(POSITION_MAGIC,magicnumber)){Print("Failed to get position magicnumber"); return -1;}
      if(Magicnumber == magicnumber){counter++;}
   }   
   return counter;
}

bool ClosePositionBuy(){
   int total = PositionsTotal();
   for(int i = total-1; i>=0; i--){
      if(total != PositionsTotal()){total = PositionsTotal(); i = total; continue;}
      ulong ticket = PositionGetTicket(i); // select position
      if(ticket <= 0){Print("Failed to get position ticket"); return false;} 
      if(!PositionSelectByTicket(ticket)){Print("Failed to select position by ticket"); return false;}
      long type;
      if(PositionGetInteger(POSITION_TYPE,type)==POSITION_TYPE_BUY){
      long magicnumber ;
      if(!PositionGetInteger(POSITION_MAGIC,magicnumber)){Print("Failde to select position magicnumber");}
      if(Magicnumber == magicnumber){
         trade.PositionClose(ticket);
         if(trade.ResultRetcode() != TRADE_RETCODE_DONE){
            Print("Failed to close position. Result : " + (string)trade.ResultRetcode() + " : " + trade.ResultRetcodeDescription());
            return false;
         }
      }
   } 
}     
    return true;
} 

bool ClosePositionSell(){
   int total = PositionsTotal();
   for(int i = total-1; i>=0; i--){
      if(total != PositionsTotal()){total = PositionsTotal(); i = total; continue;}
      ulong ticket = PositionGetTicket(i); // select position
      if(ticket <= 0){Print("Failed to get position ticket"); return false;} 
      if(!PositionSelectByTicket(ticket)){Print("Failed to select position by ticket"); return false;}
      long type;
      if(PositionGetInteger(POSITION_TYPE,type)==POSITION_TYPE_SELL){
      long magicnumber ;
      if(!PositionGetInteger(POSITION_MAGIC,magicnumber)){Print("Failde to select position magicnumber");}
      if(magicnumber == Magicnumber){
         trade.PositionClose(ticket);
         if(trade.ResultRetcode() != TRADE_RETCODE_DONE){
            Print("Failed to close position. Result : " + (string)trade.ResultRetcode() + " : " + trade.ResultRetcodeDescription());
            return false;
         }
      }
   } 
}     
    return true;
}
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.10.12
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Gabriele Zuccala: when opening a buy positions, the ea close it at the next bar! please help me!

Of course, it does, that is what you wrote.

You open with the RSI is below 80 (assuming RSIlevel is 20)
   if(RSIvalue[1] > (100 -RSIlevel) && RSIvalue[0] < (100 - RSIlevel) && CountOpenPositions() == 0){
On a new bar, the RSI is still below 80 and hasn't reached 20. You close.
    if(RSIvalue[1] >= RSIlevel){

         ClosePositionBuy();

   } 

Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
William Roeder #:

Of course, it does, that is what you wrote.

You open with the RSI is below 80 (assuming RSIlevel is 20)
On a new bar, the RSI is still below 80 and hasn't reached 20. You close.

Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

if I delete the if constructor to close buy position, the ea works the same way