User Tools

Site Tools


programming-languages:mql:mql4vsmql5

MQL4 vs MQL5

Buffers Timeseries and not-Timeseries

In MT4 buffers are set as timeseries for default (index 0 is on the right).

In MT5 buffers are not set as timeseries for default (index 0 is on the left).

Info: to check if a buffer is a timeseries or not use ArrayGetAsSeries and not ArrayIsSeries.

Draw indicator till second last bar

  1. //+------------------------------------------------------------------+
  2. //| buffer_no_timeseries.mq5 |
  3. //| Torpedo |
  4. //| https://torpedo.altervista.org |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Torpedo"
  7. #property link "https://www.torpedo.altervista.org"
  8. #property version "1.00"
  9. #property indicator_chart_window
  10.  
  11. #property indicator_buffers 2 // tot number of buffers
  12. #property indicator_plots 2 // buffers visibile in data window
  13.  
  14. #property indicator_label1 "index"
  15. #property indicator_type1 DRAW_NONE
  16.  
  17. #property indicator_label2 "close"
  18. #property indicator_type2 DRAW_LINE
  19. #property indicator_color2 Red
  20. #property indicator_width2 2
  21.  
  22. // buffers
  23. double index_buffer[];
  24. double close_buffer[];
  25.  
  26.  
  27. //+------------------------------------------------------------------+
  28. //| Custom indicator initialization function |
  29. //+------------------------------------------------------------------+
  30. int OnInit() {
  31.  
  32. SetIndexBuffer(0, index_buffer);
  33. SetIndexBuffer(1, close_buffer);
  34.  
  35. ArraySetAsSeries(index_buffer, true);
  36. ArraySetAsSeries(close_buffer, true);
  37.  
  38. return(INIT_SUCCEEDED);
  39. }
  40.  
  41.  
  42. //+------------------------------------------------------------------+
  43. //| Custom indicator iteration function |
  44. //+------------------------------------------------------------------+
  45. int OnCalculate(const int tot_bars,
  46. const int prev_tot_bars,
  47. const datetime &time[],
  48. const double &open[],
  49. const double &high[],
  50. const double &low[],
  51. const double &close[],
  52. const long &tick_volume[],
  53. const long &volume[],
  54. const int &spread[]) {
  55. ArraySetAsSeries(close, true);
  56. int limit = tot_bars - prev_tot_bars > 1 ? tot_bars - 1 : tot_bars - prev_tot_bars;
  57.  
  58. for (int i = limit; i >= 0 ; i--) {
  59. index_buffer[i] = i;
  60. close_buffer[i] = close[i];
  61.  
  62. if (i == 0) {
  63. close_buffer[i] = EMPTY_VALUE;
  64. }
  65. }
  66.  
  67. return(tot_bars);
  68. }
  1. //+------------------------------------------------------------------+
  2. //| buffer_timeseries.mq5 |
  3. //| Torpedo |
  4. //| https://www.torpedo.altervista.org |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Torpedo"
  7. #property link "https://www.torpedo.altervista.org"
  8. #property version "1.00"
  9. #property indicator_chart_window
  10.  
  11. #property indicator_buffers 2 // tot number of buffers
  12. #property indicator_plots 2 // buffers visibile in data window
  13.  
  14. #property indicator_label1 "index"
  15. #property indicator_type1 DRAW_NONE
  16.  
  17. #property indicator_label2 "close"
  18. #property indicator_type2 DRAW_LINE
  19. #property indicator_color2 Red
  20. #property indicator_width2 2
  21.  
  22. // buffers
  23. double index_buffer[];
  24. double close_buffer[];
  25.  
  26.  
  27. //+------------------------------------------------------------------+
  28. //| Custom indicator initialization function |
  29. //+------------------------------------------------------------------+
  30. int OnInit() {
  31.  
  32. SetIndexBuffer(0, index_buffer);
  33. SetIndexBuffer(1, close_buffer);
  34.  
  35. ArraySetAsSeries(index_buffer, true);
  36. ArraySetAsSeries(close_buffer, true);
  37.  
  38. return(INIT_SUCCEEDED);
  39. }
  40.  
  41.  
  42. //+------------------------------------------------------------------+
  43. //| Custom indicator iteration function |
  44. //+------------------------------------------------------------------+
  45. int OnCalculate(const int tot_bars,
  46. const int prev_tot_bars,
  47. const datetime &time[],
  48. const double &open[],
  49. const double &high[],
  50. const double &low[],
  51. const double &close[],
  52. const long &tick_volume[],
  53. const long &volume[],
  54. const int &spread[]) {
  55. ArraySetAsSeries(close, true);
  56. int limit = tot_bars - prev_tot_bars > 1 ? tot_bars - 1 : tot_bars - prev_tot_bars;
  57.  
  58. for (int i = limit; i >= 0 ; i--) {
  59. index_buffer[i] = i;
  60. close_buffer[i] = close[i];
  61.  
  62. if (i == 0) {
  63. close_buffer[i] = EMPTY_VALUE;
  64. }
  65. }
  66.  
  67. return(tot_bars);
  68. }

Multi-dimensional Array

  double arr[][1] = {};
  ArrayResize(arr, 2);
  arr[0][0] = 1;
  arr[1][0] = 2;
  ArrayPrint(arr);
  Print(" >>> ", arr[1][0]);
  ArrayResize(arr, ArraySize(arr) + 1);
  arr[2][0] = 3;
  ArrayPrint(arr);

Pass dynamic array as reference

void OnStart() {
  double arr[][2] = {};
 
  function(arr);
}  
 
void function(double &arr[][2]) {
 
}

Write to file

The following code appends text at the end of the file after FileSeek has moved the pointer to the end of it. That is why the file must be open also in FILE_READ mode.

int handle = FileOpen("file.txt", FILE_READ | FILE_WRITE | FILE_TXT, ";");
if (handle != INVALID_HANDLE) {
  FileSeek(handle, 0, SEEK_END);
  FileWrite(handle, arr[ArraySize(arr) - 1], time[i]);
  FileClose(handle);
}
double arr_stat[2] = {};
double OnTester() {  
  arr_stat[0] = TesterStatistics(STAT_PROFIT);
  arr_stat[1] = TesterStatistics(STAT_LOSS_TRADES);
 
  FrameAdd("statistics", 1, 0, arr_stat);
 
  return(0);
}
 
void OnTesterPass() {
  string name = "";
  ulong pass = 0;
  long id = 0;
  double val = 0;
 
  FrameNext(pass, name, id, val, arr_stat);
 
  Print(">>> pass ", pass, " STAT_PROFIT ", arr_stat[0], " STAT_LOSS_TRADES ", arr_stat[1]);
}
programming-languages/mql/mql4vsmql5.txt · Last modified: 2023/08/30 19:16 by tormec