two indicators in one simple EA - page 2

Hamdi Fouzai
41
Hamdi Fouzai  
Fernando Carreiro:

Just noticed now, that you are trying to read the second buffer but Trix only has one buffer.

You also seem to be using a different start position than the RSI. Its not an error but it does not make much sense. Also make sure you really have that much data available in terms of Bars available.

thank you Fernando for your help but I test with all kind of possibilities...

if(!CopyBuffer(TriX_handle,1,100,100,TriX)) return; 

if(!CopyBuffer(TriX_handle,1,0,100,TriX)) return; 

nothing change...


Is it not possible an EA work (make conditions, actions ect...) with the datas of 2 standards indicators?

for exemple "if rsi=... AND trix=... then do something..."

Is there an example?

thank you for the help

Fernando Carreiro
5858
Fernando Carreiro  
Hamdi Fouzai: thank you Fernando for your help but I test with all kind of possibilities...

All possibilities except the correct one! I said, use buffer "0" not "1":

if(!CopyBuffer(TriX_handle,0,100,100,TriX)) return; 

if(!CopyBuffer(TriX_handle,0,0,100,TriX)) return; 
Fernando Carreiro
5858
Fernando Carreiro  
Hamdi Fouzai: Is it not possible an EA work (make conditions, actions ect...) with the datas of 2 standards indicators?

AN EA can use as many Indicator as it wants (within a very big limit). That is not the case. Your code is just not properly coded. So, please read the previous post and try again. One step at a time, please!

Hamdi Fouzai
41
Hamdi Fouzai  
Fernando Carreiro:

AN EA can use as many Indicator as it wants (within a very big limit). That is not the case. Your code is just not properly coded. So, please read the previous post and try again. One step at a time, please!

Thank you Fernando for all your support

I wrote my code after the code example of the tutorial "indicator for newbie" 

on this tutorial there are two example,

  • one with RSI that works, 
  • and one with TriX that always return 0 and not for example -0.00001 

That why I used   CopyBufferAsSeries() and not  CopyBuffer()

I used buffer 0 for TriX but I have always not the current TriX (for exemple -0.00001) but 0


you said me:

You also seem to be using a different start position than the RSI. Its not an error but it does not make much sense. Also make sure you really have that much data available in terms of Bars available.



I don't understand how to check that there are enough data in terms of Bars available.

In facts, I don't need, at this moment, for this test EA, the past values of RSI and TriX but just the current values.

I just need that the EA know what is the current value of the Trix and RSI but I don't understand why the TriX remain at 0

MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors
MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors
  • www.mql5.com
In order to obtain values of a built-in or custom indicator in an Expert Advisor, first its handle should be created using the corresponding function. Examples in the article show how to use this or that technical indicator while creating your own programs. The article describes indicators that are built n the MQL5 language. It is intended for those who don't have much experience in the development of trading strategies and offers simple and clear ways of working with indicators using the offered library of functions.
Fernando Carreiro
5858
Fernando Carreiro  
Hamdi Fouzai: I wrote my code after the code example of the tutorial "indicator for newbie" on this tutorial there are two example,

  • one with RSI that works, 
  • and one with TriX that always return 0 and not for example -0.00001 

That why I used   CopyBufferAsSeries() and not  CopyBuffer(). I used buffer 0 for TriX but I have always not the current TriX (for exemple -0.00001) but 0

I don't understand how to check that there are enough data in terms of Bars available.  In facts, I don't need, at this moment, for this test EA, the past values of RSI and TriX but just the current values. I just need that the EA know what is the current value of the Trix and RSI but I don't understand why the TriX remain at 0

"CopyBuffer" copies many bars of indicator values, but if you only need the current bar or even the previous bar, you don't need to copy 100 bars as you are doing. You just need to copy 1 or 2 bars!

However, that has nothing to do with the buffer index that I asked you change. Both RSI and TriX only have one buffer, so you have to use index 0 for both!

The following code sample is copying only the current bar:

// CopyBuffer( handle, buffer, start, count, array )

if( CopyBuffer( RSI_handle,  0, 0, 1, RSI  ) > 0 )
   Print( "RSI  value at current bar: ", RSI[0]  );  

if( CopyBuffer( TriX_handle, 0, 0, 1, TriX ) > 0 )
   Print( "TriX value at current bar: ", TriX[0] );  
Hamdi Fouzai
41
Hamdi Fouzai  
Fernando Carreiro:

"CopyBuffer" copies many bars of indicator values, but if you only need the current bar or even the previous bar, you don't need to copy 100 bars as you are doing. You just need to copy 1 or 2 bars!

However, that has nothing to do with the buffer index that I asked you change. Both RSI and TriX only have one buffer, so you have to use index 0 for both!

The following code sample is copying only the current bar:

thank you Fernando

but even with your code I can't get the TriX value, only the RSI

I don't use the getindicatorbuffers.mqh with the copybuffer because it made wrong number of RSI, I desactivate it

here I want to get the RSI and the TriX, but I just get the RSI in the expert console, even with your code.

the problem is elsewhere but I do not know what is preventing the value of the TriX from being received

I didn't think it was so hard to receive the last value of the TriX

here is the code modified with your help and a screen capture of the EA in MT5 (see screen capture attached file)

I tell it I want the RSI 5 and TriX 5, and I run it in m1 eur/usd graph but I just get the rsi...

//+------------------------------------------------------------------+
//|                                                rsi-trix-test.mq5 |
//|                                                     Hamdi FOUZAI |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Hamdi FOUZAI"
#property link      "https://www.mql5.com"
#property version   "1.00"


//#include <Getindicatorbuffers.mqh>


/*    

      the purpose of this code is to get 
      the current RSI and TRIX to be able 
      to code conditions and actions
      however, I cannot get the last value of the TRIX
      
      Thank you for your help
      
*/

      
//+--------------+
//| RSI Array    |
//+--------------+

//---- arrays for indicators
double      RSI[];                // array for the indicator iRSI
//---- handles for indicators
int         RSI_handle;           // handle of the indicator iRSI


//+--------------+
//| TRIX Array   |
//+--------------+

//---- arrays for indicators
double      TriX[];                // array for the indicator iTriX
//---- handles for indicators
int         TriX_handle;           // handle of the indicator iTriX


//////////////////////////////////////////////////////////////////////
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
//////////////////////////////////////////////////////////////////////

int OnInit()
  {
//---

//+--------------+
//| RSI          |
//+--------------+

//--- creation of the indicator iRSI
   RSI_handle=iRSI(NULL,0,5,PRICE_CLOSE);
//--- report if there was an error in object creation
   if(RSI_handle<0)
     {
      Print("The creation of iRSI has failed: Runtime error =",GetLastError());
      //--- forced program termination
      return(-1);
     }
   return(0);
   

//+--------------+
//| TRIX         |
//+--------------+   
   
//--- creation of the indicator iTriX
   TriX_handle=iTriX(NULL,0,5,PRICE_CLOSE);
//--- report if there was an error in object creation
   if(TriX_handle<0)
     {
      Print("The creation of iTriX has failed: Runtime error =",GetLastError());
      //--- forced program termination
      return(-1);
     }
   return(0);  
   
//+------------------+
//| set buffer index |
//+------------------+     
   
//SetIndexBuffer(0,RSI,INDICATOR_DATA); 
//SetIndexBuffer(1,TriX,INDICATOR_DATA);   
   
//---
   return(INIT_SUCCEEDED);
  }

//////////////////////////////////////////////////////////////////////
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
//////////////////////////////////////////////////////////////////////

void OnDeinit(const int reason)
  {
//---
   
  }
  
//////////////////////////////////////////////////////////////////////  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
//////////////////////////////////////////////////////////////////////

void OnTick()
  {
//---

// here the modification you suggest me

// CopyBuffer( handle, buffer, start, count, array )

if( CopyBuffer( RSI_handle,  0, 0, 1, RSI  ) > 0 )
   Print( "RSI  value at current bar: ", RSI[0]  );  

if( CopyBuffer( TriX_handle, 0, 0, 1, TriX ) > 0 )
   Print( "TriX value at current bar: ", TriX[0] );    

  }
//+------------------------------------------------------------------+
Files:
Capture-3.jpg  285 kb
Fernando Carreiro
5858
Fernando Carreiro  
Hamdi Fouzai: thank you Fernando but even with your code I can't get the TriX value, only the RSI
In Post #4, Valdimir, told you to correct the way you were handling the Indicator buffers, but you ignored him and did not fix it in your code! Why did you ignore him?
Hamdi Fouzai
41
Hamdi Fouzai  

I am sorry for all my problems, but I can't do it

simply I try Vladimir's code but when I compile it I get some errors with that line

   if(!iGetArray(RSI_handle,0,start_pos,count,RSI))

and the problem is not for me the RSI (or the previous value of RSI), but that I can't get the last TriX

here is my code after Vladimir's post

//+------------------------------------------------------------------+
//|                                               rsi-trix-test2.mq5 |
//|                                                     Hamdi FOUZAI |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Hamdi FOUZAI"
#property link      "https://www.mql5.com"
#property version   "1.00"


//+--------------+
//| RSI Array    |
//+--------------+

//---- arrays for indicators
double      RSI[];                // array for the indicator iRSI
//---- handles for indicators
int         RSI_handle;           // handle of the indicator iRSI


//+--------------+
//| TRIX Array   |
//+--------------+

//---- arrays for indicators
double      TriX[];                // array for the indicator iTriX
//---- handles for indicators
int         TriX_handle;           // handle of the indicator iTriX



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//--- create handle of the indicator iRSI
   RSI_handle=iRSI(Symbol(),Period(),5,PRICE_CLOSE;
//--- if the handle is not created
   if(RSI_handle==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",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
   
//---
   return(INIT_SUCCEEDED);
   
   
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   ArraySetAsSeries(RSI,true);
   int start_pos=0,count=2;
   if(!iGetArray(RSI_handle,0,start_pos,count,RSI))
         return;
//---
   string text="";
   int limit=(count>2)?2:count;
   for(int i=0; i<limit; i++)
     {
      text=text+
           " bar #"+IntegerToString(i)+": "+
           " rsi "+DoubleToString(RSI[i],2)+"\n";
     }
   Comment(text);
   
  }
//+------------------------------------------------------------------+

I thank you, you and Vladimir for your help but I will certainly give up

It's now some days that I try to get this TriX value and it's impossible...

Fernando Carreiro
5858
Fernando Carreiro  
Hamdi Fouzai: I am sorry for all my problems, but I can't do it simply I try Vladimir's code but when I compile it I get some errors with that line

and the problem is not for me the RSI (or the previous value of RSI), but that I can't get the last TriX here is my code after Vladimir's post

I thank you, you and Vladimir for your help but I will certainly give up It's now some days that I try to get this TriX value and it's impossible...


Here is my version of your code! Hope it helps! Please note my style of coding is different to yours!

// Define Inputs for Expert Advisor
   input int
      intInpPeriodRSI   =  5,          // Period for RSI
      intInpPeriodTriX  =  5;          // Period for TriX
    
   input ENUM_APPLIED_PRICE
      enumInpPriceRSI   = PRICE_CLOSE, // Applied Price for RSI
      enumInpPriceTriX  = PRICE_CLOSE; // Applied Price for TriX

// Define Global Variables
   int
      intHandleRSI,     // Handle for RSI  Indicator
      intHandleTriX;    // Handle for TriX Indicator
   double
      dblArrayRSI[],    // Dynamic array for RSI  data
      dblArrayTriX[];   // Dynamic array for Trix data

// OnInit Event Handler
   int OnInit()
   {
      // Create Handle for RSI Oscillator Indicator
         intHandleRSI = iRSI( _Symbol, _Period, intInpPeriodRSI, enumInpPriceRSI );
         if( intHandleRSI == INVALID_HANDLE )
         {
            Print("Error creating iRSI handle: ", _LastError );
            return( INIT_FAILED );
         }
   
      // Create Handle for Trix Oscillator Indicator
         intHandleTriX = iTriX( _Symbol, _Period, intInpPeriodTriX, enumInpPriceTriX );
         if( intHandleTriX == INVALID_HANDLE )
         {
            Print("Error creating iTriX handle: ", _LastError );
            return( INIT_FAILED );
         }
   
      // Initialisation Succeeded
         return( INIT_SUCCEEDED );
   }
     
// OnTick Event Handler
   void OnTick()
   {
      // Define Local Variables
         int intBarStart = 0, intBarCount = 1;
      
      // Get & Print RSI value of current bar/candle
         if( CopyBuffer( intHandleRSI,  0, intBarStart, intBarCount, dblArrayRSI  ) > 0 )
            Print( "RSI  value at current bar: ", DoubleToString( dblArrayRSI[0], 2 ) );  
   
      // Get & Print TriX value of current bar/candle
         if( CopyBuffer( intHandleTriX, 0, intBarStart, intBarCount, dblArrayTriX ) > 0 )
            Print( "TriX value at current bar: ", DoubleToString( dblArrayTriX[0], _Digits ) );
   }
Fernando Carreiro
5858
Fernando Carreiro  
Please note that I re-edited the previous post as there was a error when I pasted it the first time! Please use updated version!