MT4 Coding
Post by admin » Fri Apr 08, 2022 10:05 am
1. Great way to call a script in an EA or to trigger an EA from an indicator :
https://www.mql5.com/en/forum/149967
A/ Extracted here the way to call a script from an indicator (may work for EA too):
Here an example:
// indicator code
#property strict
#import "user32.dll"
int SendMessageA(int, int, int, char&[]);
int RegisterWindowMessageA(char&[]);
#import
char sz_mt4imsg[];
char sz_script[];
string scriptname = "startme"; // call the script to be started "startme"
int mt4imsg;
int OnInit() {
StringToCharArray("MetaTrader4_Internal_Message", sz_mt4imsg);
StringToCharArray(scriptname, sz_script);
mt4imsg = RegisterWindowMessageA(sz_mt4imsg);
return 0;
}
int OnCalculate(const int rates_total, const int prev_calculated, 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[]) {
// start script
SendMessageA(WindowHandle(Symbol(), Period()), mt4imsg, 16, sz_script);
return rates_total;
}
B/ Extracted here the way to have an indicator trigger the EA (that is attached to the same chart):
first the expert code:
// EA code
#property strict
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) {
if(id == CHARTEVENT_CUSTOM)
Print(sparam);
}
now the indicator:
// indicator code
#property strict
#property indicator_chart_window
string message = "Indi triggers EA!";
int OnCalculate(const int rates_total,
const int prev_calculated,
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[])
{
EventChartCustom(0, 0, 0, 0, message);
return(rates_total);
}
Post by admin » Fri Apr 08, 2022 10:05 am
1. Great way to call a script in an EA or to trigger an EA from an indicator :
https://www.mql5.com/en/forum/149967
A/ Extracted here the way to call a script from an indicator (may work for EA too):
Here an example:
// indicator code
#property strict
#import "user32.dll"
int SendMessageA(int, int, int, char&[]);
int RegisterWindowMessageA(char&[]);
#import
char sz_mt4imsg[];
char sz_script[];
string scriptname = "startme"; // call the script to be started "startme"
int mt4imsg;
int OnInit() {
StringToCharArray("MetaTrader4_Internal_Message", sz_mt4imsg);
StringToCharArray(scriptname, sz_script);
mt4imsg = RegisterWindowMessageA(sz_mt4imsg);
return 0;
}
int OnCalculate(const int rates_total, const int prev_calculated, 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[]) {
// start script
SendMessageA(WindowHandle(Symbol(), Period()), mt4imsg, 16, sz_script);
return rates_total;
}
B/ Extracted here the way to have an indicator trigger the EA (that is attached to the same chart):
first the expert code:
// EA code
#property strict
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) {
if(id == CHARTEVENT_CUSTOM)
Print(sparam);
}
now the indicator:
// indicator code
#property strict
#property indicator_chart_window
string message = "Indi triggers EA!";
int OnCalculate(const int rates_total,
const int prev_calculated,
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[])
{
EventChartCustom(0, 0, 0, 0, message);
return(rates_total);
}