Ichimoku of RSI EA not opening positions. May you help

 

I have coded an EA which trades the Ichimoku of RSI.

It is compiling no errors, no warnings but its not running any trade even on the strategy tester.

In case i missed a certain line of algorithm or did something wrong, may you assist.

#property link      "https://www.mql5.com"
#property version   "1.00"

//Ichimoku on RSI indicator input parametres
   input int              tenkan_sen      =  9;                // period of Tenkan-sen 
   input int              kijun_sen       =  9;                // period of Kijun-sen 
   input int              senkou_span_b   =  52;               // period of Senkou Span B 
   input ENUM_TIMEFRAMES  period          =  PERIOD_CURRENT;   // timeframe   

   input int              InpTakeProfitPts=  100;    //Take profit points
   input int              InpStopLossPts  =  50;     //Stop loss points
   input double           InpOrderSize     =  0.20;     //Order size
   input string           InpTradeComment =__FILE__;   //Trade comment
   input int              InpMagicNumber  =  20001;    //Magic number
        
   double   TakeProfit;    //InpTakeProfitPts converted to hold take profit as a double variable
   double   StopLoss;      //InpStopLossPts converted to hold stop loss as a double variable
   
   //identify the buffer numbers
   const string   IndicatorName           =  "Indicators\\Ichimoku on RSI indicator";
   const int      IndexTenkan_sen         =  0;         //these are indexes shown on the Data window mt5
   const int      IndexKijun_sen          =  1;         //--Arranged in their order
   const int      IndexRealRSI            =  2;
   const int      IndexRSIValue           =  3; 
   int            Handle;                               //will be Handle to the iCustom function             
   double         BufferTenkan_sen[3];                  //will capture 3 values each time i run the loop of trading/signal    
   double         BufferKijun_sen[3];
   double         BufferRealRSI[3]; 
   double         BufferRSIValue[3]; 
   
   #include  <Trade\Trade.mqh>
   //Create an instance of CTrade
   CTrade *Trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   Trade =  new CTrade();
   Trade.SetExpertMagicNumber(InpMagicNumber);
   
   double   point = SymbolInfoDouble(Symbol(),SYMBOL_POINT);
   TakeProfit     = InpTakeProfitPts * point;
   StopLoss       = InpStopLossPts   * point;

//create handle for the iCustom
   Handle   =  iCustom(Symbol(),Period(),IndicatorName,
                       tenkan_sen,kijun_sen,senkou_span_b,period);
   
   if (Handle==INVALID_HANDLE)
   {
        PrintFormat("Error &i",GetLastError());
        return(INIT_FAILED);
   }
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(Handle);
   delete   Trade;
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   //where this is to only run once per bar
   if (!NewBar()) return;
   
   //Perform any calculations and analysis here
   //Create Arrays from the handle
   int cnt  = CopyBuffer(Handle,IndexTenkan_sen,0,3,BufferTenkan_sen);
   if (cnt<3)  return;
   cnt  = CopyBuffer(Handle,IndexKijun_sen,0,3,BufferKijun_sen);
   cnt  = CopyBuffer(Handle,IndexRealRSI,0,3,BufferRealRSI);
   cnt  = CopyBuffer(Handle,IndexRSIValue,0,3,BufferRSIValue);
   
   //creating real values for signals
   double currentTenkansen = BufferTenkan_sen[1];     //tenkansen for the current bar or candle
   double priorTenkansen   = BufferTenkan_sen[0];     //tenkansen for the previous bar or candle
   //how do i create RSIValues for signals????
   
   //We calculate the Ask price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   //We calculate the Bid price
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
 //Since i want to merge "Ichimoku on RSI indicator" with RSI indicator 
 // i have to also Create an Array for RSI 
   double myRSIArray[];
   
   //Sort the price array from the current candle downwards
   ArraySetAsSeries(myRSIArray,true);
   
   //we define RSI
   int RSIDefinition = iRSI(_Symbol,_Period,6,PRICE_CLOSE);
   
   //we fill the array with price data
   //Defined EA, one line, current candle for 3 candles, store in array
   CopyBuffer(RSIDefinition,0,0,3,myRSIArray);
   
   //Calculate the current RSI value 
   double myRSIValue=NormalizeDouble(myRSIArray[0],2);
   
   //Executing strategy here
   bool buycondition = (currentTenkansen<15 && priorTenkansen<=15) //Tenkansen line has touched level 15
                        && (myRSIValue<15);                        //while RSI line is below level 15
   bool sellcondition = (currentTenkansen>85 && priorTenkansen>=85)//Tenkansen line has touched level 85
                        && (myRSIValue<85);                        //while RSI line is above level 85
   
   if(buycondition) {
   CloseAll(POSITION_TYPE_SELL);     //if i am buying, close all sell positions
   OrderOpen(ORDER_TYPE_BUY);
   }  else
   if(sellcondition) {
   CloseAll(POSITION_TYPE_BUY);     //if i am selling, close all buy positions
   OrderOpen(ORDER_TYPE_SELL);
   }
   
   //save any information for next time
   return;
   
 }
 
 bool NewBar() {
 
   static datetime prevTime      = 0;
   datetime        currentTime   = iTime(Symbol(),Period(),0);
   if (currentTime!=prevTime) {
   
   prevTime   =   currentTime;
   return(true);
   }
 return(false);
 
 }
//creating a closeall function (close all buying positions first when opening a sell position)
void  CloseAll(ENUM_POSITION_TYPE positionType) {

      int cnt  = PositionsTotal();                                 //count the number of totl positions
      for (int i = cnt-1; i>0; i--) {                             //looping from top to bottom
         ulong ticket   = PositionGetTicket(i);
         if (ticket>0) {
            if (PositionGetString(POSITION_SYMBOL)==Symbol() 
               && PositionGetInteger(POSITION_MAGIC)==InpMagicNumber
               && PositionGetInteger(POSITION_TYPE)==positionType) {
              Trade.PositionClose(ticket);
          }
       }
    }

}

bool OrderOpen (ENUM_ORDER_TYPE orderType) {

     double price = (orderType==ORDER_TYPE_BUY) ?
                     SymbolInfoDouble(Symbol(),SYMBOL_ASK) :
                     SymbolInfoDouble(Symbol(),SYMBOL_BID);
                     
     Trade.PositionOpen(Symbol(),orderType,InpOrderSize,price,0,0,InpTradeComment);
     
     return(true);
     
  }
//+------------------------------------------------------------------+
 
Vladimir Karputov:

So the reason why its not opening trades is because of position misinterpretation right? 

The challenge i am having is of adding Ichimoku on the same window with RSI. Could you help on that as well?

 
JayB :

So the reason why its not opening trades is because of position misinterpretation right? 

The challenge i am having is of adding Ichimoku on the same window with RSI. Could you help on that as well?

I told you the first mistake. Until you fix the first mistake, there is no point in discussing anything.

 
Vladimir Karputov:

I have made quite a number of changes on the algorithm.

I have restructured it and corrected this as well

[Remember the rule: the indicator handle MUST be received ONCE! This is done in OnInit () !!!]


EA compiles with no errors but still does not run in the strategy tester.

Files:
 
JayB :

I have made quite a number of changes on the algorithm.

I have restructured it and corrected this as well

[Remember the rule: the indicator handle MUST be received ONCE! This is done in OnInit () !!!]


EA compiles with no errors but still does not run in the strategy tester .

Never spare ink. Always handle possible errors - if an error is received, you will see it immediately. Use step-by-step debugging -


this is the foundation of testing.

Example of correct processing (for iRSI)

//--- create handle of the indicator iRSI
   handle_iRSI=iRSI(m_symbol.Name(),Inp_RSI_period,Inp_RSI_ma_period,Inp_RSI_applied_price);
//--- if the handle is not created
   if(handle_iRSI==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iRSI indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Inp_RSI_period),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
 
Vladimir Karputov:

Never spare ink. Always handle possible errors - if an error is received, you will see it immediately. Use step-by-step debugging -


this is the foundation of testing.

Example of correct processing (for iRSI)

I had created the handle for RSI (stand-alone), but then i had to create the handle for ichimoku on RSI, thats where i had to use ichimoku handle as applied price for RSI. 

I am not sure which variables and function i have to use now for my EA to use Tenkansen as signal line on RSI. Thats my big problem. 
 
JayB :
I had created the handle for RSI (stand-alone), but then i had to create the handle for ichimoku on RSI, thats where i had to use ichimoku handle as applied price for RSI. 

I am not sure which variables and function i have to use now for my EA to use Tenkansen as signal line on RSI. Thats my big problem. 

So that you can see your mistake - first do as I said: do the defense and check. When you catch an error, you will see a description of your error in the terminal log.

 
JayB:

I have coded an EA which trades the Ichimoku of RSI.

It is compiling no errors, no warnings but its not running any trade even on the strategy tester.

In case i missed a certain line of algorithm or did something wrong, may you assist.

1st of all the images that you have displayed makes no sense. As stochastic/rsi and ichimoku cloud does not match based on their values.

Also you have applied them in a single indicator window that will not do any kind of changes in your code neither you can make sense of two indicators that have entirely different values to each other.

E.g:

RSI/Stochastic has value range between 0-100 and can be marked in indicator window, based on that you can trade or make any trading system.

Ichimoku Cloud has value based on the chart you apply that indicator, the indicator will have values relevant to the instrument prices not between the range of 0-100. Doesn't matters if you apply them in indicator window with any combination of other indicator. They are not going to make sense.


So you might consider changing your approach regarding the system you are trying to build or use some other indicators and use the indicators in correct way.

 
JayB:

I have coded an EA which trades the Ichimoku of RSI.

It is compiling no errors, no warnings but its not running any trade even on the strategy tester.

In case i missed a certain line of algorithm or did something wrong, may you assist.

//Executing strategy here
   bool buycondition = (currentTenkansen<15 && priorTenkansen<=15) //Tenkansen line has touched level 15
                        && (myRSIValue<15);                        //while RSI line is below level 15
   bool sellcondition = (currentTenkansen>85 && priorTenkansen>=85)//Tenkansen line has touched level 85
                        && (myRSIValue<85);                        //while RSI line is above level 85

These two cases are always true, because the value of sen band is always greater than these values.

Reason: