programming-languages:mql:mql4vsmql5
Table of Contents
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
//+------------------------------------------------------------------+ //| buffer_no_timeseries.mq5 | //| Torpedo | //| https://torpedo.altervista.org | //+------------------------------------------------------------------+ #property copyright "Torpedo" #property link "https://www.torpedo.altervista.org" #property version "1.00" #property indicator_chart_window #property indicator_buffers 2 // tot number of buffers #property indicator_plots 2 // buffers visibile in data window #property indicator_label1 "index" #property indicator_type1 DRAW_NONE #property indicator_label2 "close" #property indicator_type2 DRAW_LINE #property indicator_color2 Red #property indicator_width2 2 // buffers double index_buffer[]; double close_buffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ const int prev_tot_bars, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { int limit = tot_bars - prev_tot_bars > 1 ? tot_bars - 1 : tot_bars - prev_tot_bars; for (int i = limit; i >= 0 ; i--) { index_buffer[i] = i; close_buffer[i] = close[i]; if (i == 0) { close_buffer[i] = EMPTY_VALUE; } } return(tot_bars); }
//+------------------------------------------------------------------+ //| buffer_timeseries.mq5 | //| Torpedo | //| https://www.torpedo.altervista.org | //+------------------------------------------------------------------+ #property copyright "Torpedo" #property link "https://www.torpedo.altervista.org" #property version "1.00" #property indicator_chart_window #property indicator_buffers 2 // tot number of buffers #property indicator_plots 2 // buffers visibile in data window #property indicator_label1 "index" #property indicator_type1 DRAW_NONE #property indicator_label2 "close" #property indicator_type2 DRAW_LINE #property indicator_color2 Red #property indicator_width2 2 // buffers double index_buffer[]; double close_buffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ const int prev_tot_bars, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { int limit = tot_bars - prev_tot_bars > 1 ? tot_bars - 1 : tot_bars - prev_tot_bars; for (int i = limit; i >= 0 ; i--) { index_buffer[i] = i; close_buffer[i] = close[i]; if (i == 0) { close_buffer[i] = EMPTY_VALUE; } } return(tot_bars); }
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); }
Print Extra Statistics in Testing Mode
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