rates_total

 

Hello everyone,

I would like to make a function that works the same way as OnCalculate in my EA. I'm not sure where to start, but I think this might be the same as rates_total used in indicators.

int calculated = BarsCalculated(Cal_Value);
    RatesTotal = Bars(NULL,TIMEFRAME);
 
Don't try do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

Just get the value of the indicator into the EA and do what you want with it. You should encapsulate your iCustom calls to make your code self-documenting.
          Detailed explanation of iCustom - MQL4 and MetaTrader 4 - MQL4 programming forum

If you ignore that, then simply code it.
#define rates_total Bars(_Symbol, ENUM_TIMEFRAMES(_Period) )
int prev_calculated;
int OnInit(){ prev_calculated = 0; ... }
void OnTick(){
   for(int iBar = rates_total - MathMax(LOOKBACK, prev_calculated); iBar >= LAST; --iBar){
      :
   }
   prev_calculated = rates_total-LAST-REDRAW_BAR_LAST;
   :
 
whroeder1:
Don't try do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

Just get the value of the indicator into the EA and do what you want with it. You should encapsulate your iCustom calls to make your code self-documenting.
          Detailed explanation of iCustom - MQL4 and MetaTrader 4 - MQL4 programming forum

If you ignore that, then simply code it.

Hello again,

whroeder1, As usual your replies are to the point. I am still reading over the threads dealing with the use of iCustom.

I refer to the reference manual more now than I use to when I began coding MQL.

Great job sir. 

 

Hello again,

I keep getting "Array out of range"

void Calculate()
  { 
  double rsi;
  int InpPeriodRSI=14; // Period
  double    ExtRSIBuffer[];
  double    ExtPosBuffer[];
  double    ExtNegBuffer[];
  int       ExtPeriodRSI;
  int       i=0;

rsi = iCustom(NULL,0,RSI,InpPeriodRSI,ExtRSIBuffer[i],ExtPosBuffer[i],ExtNegBuffer[i],ExtPeriodRSI);
}
 
GrumpyDuckMan: Hello again, I keep getting "Array out of range"

Well, you have created empty (undefined size) arrays, so not even the index [0] exists:

void Calculate()
{ 
  double rsi;
  int    InpPeriodRSI=14; // Period
  double ExtRSIBuffer[];  // empty array
  double ExtPosBuffer[];  // empty array
  double ExtNegBuffer[];  // empty array
  int    ExtPeriodRSI;  
  int    i=0;             // using [i] on any of the above arrays will cause error because they have no size 

  rsi = iCustom(NULL,0,RSI,InpPeriodRSI,ExtRSIBuffer[i],ExtPosBuffer[i],ExtNegBuffer[i],ExtPeriodRSI);
}

Also, that is not the way iCustom() works. You pass it parameters, and receive a single value from one of its buffer at a particular bar-shift; not entire arrays of buffer data. Please read the documentation again.

On a side, may I suggest a book to help improve your knowledge? I highly recommend it.

Forum on trading, automated trading systems and testing trading strategies

Something Interesting to Read

Sergey Golubev, 2017.09.16 05:40

Expert Advisor Programming for MetaTrader 4


This book will teach you the following concepts:

  • The basic of the MLQ4 language, including variables and data types, operations, conditional and loop operators, functions, classes and objects, event handlers and more.
  • Place, modify and close market and pending orders.
  • Add a stop loss and/or take profit price to an individual order, or to multiple orders.
  • Close orders individually or by order type.
  • Get a total of all currently opened orders.
  • Work with OHLC bar data and locate basic candlestick patterns.
  • Find the highest high and lowest low of recent bars.
  • Work with MetaTrader’s built-in indicators, as well as custom indicators.
  • Add a trailing stop or break even stop feature to an expert advisor.
  • Use money management and lot size verification techniques.
  • Add a flexible trading timer to an expert advisor.
  • Construct several types of trading systems, including trend, counter-trend and breakout systems.
  • Add alert, emails, sounds and other notifications.
  • Add and manipulate chart objects.
  • Read and write to CSV files.
  • Construct basic indicators, scripts and libraries.
  • Learn how to effective debug your programs, and use the Strategy Tester to test your strategies.

All of the source code in this book is available for download, including an expert advisor framework that allows you to build robust and fully-featured expert advisors with minimal effort.



iCustom - Technical Indicators - MQL4 Reference
iCustom - Technical Indicators - MQL4 Reference
  • docs.mql4.com
[in]  Custom indicator compiled program name, relative to the root indicators directory (MQL4/Indicators/). If the indicator is located in subdirectory, for example, in MQL4/Indicators/ The passed parameters and their order must correspond with the declaration order and the type of extern variables of the custom indicator...
 

Hello again,

I just got hold of the book.

 
GrumpyDuckMan: Hello again, I just got hold of the book.

Did you buy the current edition (which has been updated for the latest version of MQL4+), or did you just get one of those old edition "pirate" copies floating around on the Internet?

If you just got one of those "old" editions which uses the old style code, I do not recommended it due to "incompatibilities" with current builds of MetaTrader.

 
Fernando Carreiro:

Did you buy the current edition (which has been updated for the latest version of MQL4+), or did you just get one of those old edition "pirate" copies floating around on the Internet?

If you just got one of those "old" editions which uses the old style code, I do not recommended it due to "incompatibilities" with current builds of MetaTrader.

About This Book

This book updates the approach used in the original Expert Advisor Programming book, released in 2010.

While the code in that book will still work in MetaTrader 4, this book uses the new MQL5 features where

appropriate.

 

Hello again,

I have started using MQL4 again. I believe it will be easier to understand the book better than using MQL5.

 
Expert Advisor Programming for MetaTrader 4 | Expert Advisor Programming for MetaTrader
Expert Advisor Programming for MetaTrader 4 | Expert Advisor Programming for MetaTrader
  • expertadvisorbook.com
Expert Advisor Programming for MetaTrader 4 Brand new and fully updated for the latest versions of MetaTrader 4, is a practical guide to programming expert advisors in the MQL4 language. Leverage the latest features imported from the MQL5 language, including object-oriented programming, enumerations, structure and more. This book will teach...
 
GrumpyDuckMan:I followed your link Expert Advisor Programming for MetaTrader 4
Yes, if you bought the book then you have the latest edition!
Reason: