Execute an existing EA from a script/ea ? - page 2

To add comments, please log in or register
Tom Sasson
187
Tom Sasson  
pansa:

Hallo,paulepanke!

i tested You code for attach a expert to chart -sorry it work not!

give please a samle for attach expert name "profit" to active chart

with a script.

pansa


Replace all PosMessageA with PostMessageW and RegisterWindowMessageA with RegisterWindowMessageW.

This has been so since build 600+, when W (wide) or Unicode form of the functions was introduced.

shoxie
29
shoxie  
Tom Sasson:

Replace all PosMessageA with PostMessageW and RegisterWindowMessageA with RegisterWindowMessageW.

This has been so since build 600+, when W (wide) or Unicode form of the functions was introduced.

PostMessageW crashes the MT4 terminal when I use it from within a script to place an indicator. I'm using the latest build of MT4: v4.00 Build 1090 (19 May 2017). SendMessageW works in placing the indicator, but it won't allow for a simulated keyboard event which will get rid of the dialog box.

Is there an update to this whole method of placing an indicator/script/EA programmatically and then getting rid of the dialog box programmatically? Something which is compatible with the latest build of MT4?

Tom Sasson
187
Tom Sasson  
shoxie:

PostMessageW crashes the MT4 terminal when I use it from within a script to place an indicator. I'm using the latest build of MT4: v4.00 Build 1090 (19 May 2017). SendMessageW works in placing the indicator, but it won't allow for a simulated keyboard event which will get rid of the dialog box.

Is there an update to this whole method of placing an indicator/script/EA programmatically and then getting rid of the dialog box programmatically? Something which is compatible with the latest build of MT4?

Are you using keybd_event from WinUser32.mqh ?

shoxie
29
shoxie  
Tom Sasson:

Are you using keybd_event from WinUser32.mqh ?

Thanks for responding. Yes I'm using the same function from WinUser32, but I included it at the top of my script:

#import "user32.dll"
   //.......
   void keybd_event(int VirtualKey, int ScanCode, int Flags, int ExtraInfo);
#import
   
Mehmet Bastem
713
Mehmet Bastem  
Manu:

Hello,

Is it possible run existing EA from a script/ea ?

If so, is it possible to define the ea settings like Pair, Timeframe and parameters ?

Thanks in advance,

Manu

//+------------------------------------------------------------------+
//|                                              hariciEACagirma.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//|                                                    aswincmds.mqh |
//|                               Copyright © 2010, A. Robert Strube |
//|                                                  arstrube@gmx.de |
//+------------------------------------------------------------------+
 //int PostMessageA(int hWnd,int Msg,int wParam,int IParam)
/* I don't recommend using this file as an actual #include in your
   code (although you can do so if desired( because it isn't anywhere 
   near complete or finished. Rather, use it as a source of 
   information for #define's in your own code.
   
   Still missing:
     Remove Script
 
   Feel free to add to this as you discover more commands. 
   Please share the result.                                          */
   
#property copyright "Copyright © 2010, A. Robert Strube"
#property link      "arstrube@gmx.de"
 
//+------------------------------------------------------------------+
//| defines                                                          |
//+------------------------------------------------------------------+
 
                                        /* 00000-32999 not inspected */
                                        /* 33000-33016 unknown */
#define MT4_WMCMD_AUTOSCROLL      33017
#define MT4_WMCMD_BARS            33018
#define MT4_WMCMD_CANDLES         33019
                                        /* 33020       unknown */
#define MT4_WMCMD_GRID            33021
#define MT4_WMCMD_LINE            33022
#define MT4_WMCMD_SHIFT           33023
#define MT4_WMCMD_VOLUMES         33024
#define MT4_WMCMD_ZOOM_IN         33025
#define MT4_WMCMD_ZOOM_OUT        33026
                                        /* 33027-33047 unknown */
#define MT4_WMCMD_EXPERT_INPUTS   33048 /* Open expert Inputs dialog */
                                        /* 33049       unknown */
#define MT4_WMCMD_REMOVE_EXPERT   33050 /* Remove expert advisor from chart */
                                        /* 33051-33053 unknown */
#define MT4_WMCMD_SAVE_AS_PICTURE 33054 
                                        /* 33055-33133 unknown */
#define MT4_WMCMD_PERIOD_D1       33134
                                        /* 33135       unknown */
#define MT4_WMCMD_PERIOD_H4       33136
#define MT4_WMCMD_PERIOD_M1       33137
#define MT4_WMCMD_PERIOD_M5       33138
#define MT4_WMCMD_PERIOD_M15      33139
#define MT4_WMCMD_PERIOD_M30      33140
#define MT4_WMCMD_PERIOD_W1       33141
                                        /* 33142-33156 unknown */
#define MT4_WMCMD_PROPERTIES      33157 /* Chart Properties (F8) */
                                        /* 33158-33176 apparently unused */
#define MT4_WMCMD_PERIOD_SEPARATE 33177 /* Show/hide period separators */
                                        /* 33178-33196 unknown */
#define MT4_WMCMD_MOVE_RIGHT      33197 /* Move right (right/down arrow) */
#define MT4_WMCMD_MOVE_LEFT       33198 /* Move left (left/up arrow) */
                                        /* 33199-33219 unknown */
#define MT4_WMCMD_SAVE_TEMPLATE   33220 /* Save template... */
                                        /* 33221-33229 unknown */
#define MT4_WMCMD_33230           33230 /* Arrow Down */
#define MT4_WMCMD_33231           33231 /* Arrow Up */
#define MT4_WMCMD_33232           33232 /* icons-ticks */
#define MT4_WMCMD_MOUSE_CROSS     33233 /* crosshair */
#define MT4_WMCMD_MOUSE_CURSOR    33234 /* the cursor */
#define MT4_WMCMD_33235           33235 /* Box-cyclical Line */
#define MT4_WMCMD_33236           33236 /* equidistant channel */
#define MT4_WMCMD_33237           33237 /* Fibonacci lines */
#define MT4_WMCMD_33238           33238 /* Box-Fibonacci-arc */
#define MT4_WMCMD_33239           33239 /* Box-Fibonacci-fan */
#define MT4_WMCMD_33240           33240 /* Box Channels, Fibonacci */
#define MT4_WMCMD_33241           33241 /* Box-Gann-grid Hanna */
#define MT4_WMCMD_33242           33242 /* Box-Gann-line Hanna */
#define MT4_WMCMD_33243           33243 /* Box-Gann-fan Hanna */
#define MT4_WMCMD_HLINE           33244 /* horizontal line */
                                        /* 33245       unknown */
#define MT4_WMCMD_33246           33246 /* Box Andrews pitchfork */
#define MT4_WMCMD_33247           33247 /* Box-shape-rectangle */
#define MT4_WMCMD_33248           33248 /* Box-channels-linear regression */
                                        /* 33249-33250 apparently unused */
#define MT4_WMCMD_33251           33251 /* Box-icons-stop */
                                        /* 33252       unknown */
#define MT4_WMCMD_33253           33253 /* text */
#define MT4_WMCMD_33254           33254 /* Box-icons-bad */
#define MT4_WMCMD_33255           33255 /* Box-icons-well */
#define MT4_WMCMD_33256           33256 /* Box-Fibonacci-time-zone */
#define MT4_WMCMD_TRENDLINE       33257 /* trend line */
#define MT4_WMCMD_33258           33258 /* Box-Line-trend line for the corner */
#define MT4_WMCMD_33259           33259 /* Box-figure-triangle */
#define MT4_WMCMD_VLINE           33260 /* Vertical Line */
#define MT4_WMCMD_LABEL           33261 /* text label */
                                        /* 33262-33323 unknown */
#define MT4_WMCMD_UPDATE_DATA     33324 /* This doesn't cause experts start() to run */
                                        /* 33325-33333 unknown */
#define MT4_WMCMD_PERIOD_MN1      33334 /* timeframe MN */
                                        /* 33335-34299 unknown */
#define MT4_WMCMD_34300           34300 /* 34300-34699 custom indicators in alphabetical order */
#define MT4_WMCMD_34700           34700 /* Indicators-moving average of oscillator */
#define MT4_WMCMD_34701           34701 /* Indicators-accelerator oscillator */
#define MT4_WMCMD_34702           34702 /* Indicators-accumulation/distribution */
#define MT4_WMCMD_34703           34703 /* Indicators-alligator */
#define MT4_WMCMD_34704           34704 /* indicators-average directional movement index */
#define MT4_WMCMD_34705           34705 /* indicators-average true range */
#define MT4_WMCMD_34706           34706 /* Indicators-awesome oscillator */
#define MT4_WMCMD_34707           34707 /* Indicators-bears power */
#define MT4_WMCMD_34708           34708 /* indicator-bollinger bands */
#define MT4_WMCMD_34709           34709 /* Indicators-bulls power */
#define MT4_WMCMD_34710           34710 /* Indicators-commodity channel index */
#define MT4_WMCMD_34711           34711 /* Indicators-demarker */
#define MT4_WMCMD_34712           34712 /* Indicators-envelopes */
#define MT4_WMCMD_34713           34713 /* Indicators-force index */
#define MT4_WMCMD_34714           34714 /* Indicators-fractals */
#define MT4_WMCMD_34715           34715 /* Indicators-gator oscillator */
#define MT4_WMCMD_34716           34716 /* Indicators-ichimoku kinko hyo */
#define MT4_WMCMD_34717           34717 /* Indicators-macd */
#define MT4_WMCMD_34718           34718 /* indicators-market facilitation index */
#define MT4_WMCMD_34719           34719 /* Indicators-momentum */
#define MT4_WMCMD_34720           34720 /* indicators-money flow index */
#define MT4_WMCMD_34721           34721 /* indicators-moving averages */
#define MT4_WMCMD_34722           34722 /* Indicators-on balance volume */
#define MT4_WMCMD_34723           34723 /* Indicators-parabolic sar */
#define MT4_WMCMD_34724           34724 /* indicators-relative strength index */
#define MT4_WMCMD_34725           34725 /* indicators-relative vigor index */
#define MT4_WMCMD_34726           34726 /* Indicators-standard deviation */
#define MT4_WMCMD_34727           34727 /* Indicators-stochastic oscillator */
#define MT4_WMCMD_34728           34728 /* Indicators-volumes */
#define MT4_WMCMD_34729           34729 /* Indicators-william's percent range */
                                        /* 34730-34799 unknown */
#define MT4_WMCMD_LOAD_TEMPLATE   34800 /* 34800-34899 load template in alphabetical order */
#define MT4_WMCMD_REMOVE_TEMPLATE 34900 /* 34900-34998 Remove Template in alph. order (conf. dialog) */
#define MT4_WMCMD_KILL_TEMPLATES  34999 /* remove *.tpl (confirmation dialog) */ 
                                        /* 35000-35399 unknown */
#define MT4_WMCMD_PERIOD_H1       35400 /* timeframe H1 */
#define MT4_WMCMD_OBJECTS_LIST    35402 /* open objects list window */
#define MT4_WMCMD_ORDER_BUYLIMIT  35454 /* order buy limit */
#define MT4_WMCMD_ORDER_BUYSTOP   35455 /* order buy stop */
#define MT4_WMCMD_ORDER_SELLLIMIT 35456 /* order sell limit */
#define MT4_WMCMD_ORDER_SELLSTOP  35457 /* order sell stop */
#define MT4_WMCMD_ORDER_MARKET    35458 /* new order (F9) */
#define MT4_WMCMD_35464           35464 /* right price tag */
#define MT4_WMCMD_35511           35511 /* Graphic template-download template */
                                        /* 35512-37011 unknown */
                                        /* 37012-40000 not inspected */
#define MT4_WMCMD_57602           57602 /* close the current chart */
#define MT4_WMCMD_57603           57603 /* Save as (ascii/csv) */
#define MT4_WMCMD_57604           57604 /* Save as (ascii/csv) - apparently same */
#define MT4_WMCMD_PRINT_DIALOG    57607 /* Open Print... Dialog */
#define MT4_WMCMD_PRINT_DIRECT    57608 /* Print immediately */
#define MT4_WMCMD_PRINT_PREVIEW   57609 /* Open Print Preview dialog */
                                        /* 57610-59675 unknown (re-checked up to 58011) */
                                        /* 59675-...   not inspected */
//+------------------------------------------------------------------+

#import "user32.dll"

   int RegisterWindowMessageA(string MessageName);

   int PostMessageA(int hwnd, int msg, int wparam, string Name);

   void keybd_event(int VirtualKey, int ScanCode, int Flags, int ExtraInfo);

#import

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
 string    ScriptName="aa1";
    StartScript(WindowHandle( Symbol(), Period()), ScriptName, false);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+


// Import of functions from User32. If their purpose isn't pretty much immediately

// clear, then documenting it here frankly isn't going to help much. Requires 

// "Allow DLL imports" to be turned on.

#import "user32.dll"

   int RegisterWindowMessageA(string MessageName);

   int PostMessageA(int hwnd, int msg, int wparam, string Name);

   void keybd_event(int VirtualKey, int ScanCode, int Flags, int ExtraInfo);

#import





// The hWnd parameter for all these functions is obtained using WindowHandle().

// For example, an EA/script can get the handle of its own chart using

// WindowHandle(Symbol(), 0). If the EA/script knows that another chart 

// is open, then its handle can be obtained using the known symbol and

// timeframe. Unpredictable behaviour if there are multiple charts for 

// the same symbol and timeframe. AutomaticallyAcceptDefaults clears

// the configuration window for the new indicator/EA/script by simulating

// a press of the Enter key after a small wait. A longer wait might 

// prove to be more robust. None of this is for the faint-hearted...



void StartStandardIndicator(int hWnd, string IndicatorName, bool AutomaticallyAcceptDefaults = false)

{

   int MessageNumber = RegisterWindowMessageA("MetaTrader4_Internal_Message");

   PostMessageA(hWnd, MessageNumber, 13, IndicatorName);

   if (AutomaticallyAcceptDefaults) ClearConfigDialog();

}



void StartCustomIndicator(int hWnd, string IndicatorName, bool AutomaticallyAcceptDefaults = false)

{

   int MessageNumber = RegisterWindowMessageA("MetaTrader4_Internal_Message");

   PostMessageA(hWnd, MessageNumber, 15, IndicatorName);

   if (AutomaticallyAcceptDefaults) ClearConfigDialog();

}



void StartEA(int hWnd, string EAName, bool AutomaticallyAcceptDefaults = false)

{

   int MessageNumber = RegisterWindowMessageA("MetaTrader4_Internal_Message");

   PostMessageA(hWnd, MessageNumber, 14, EAName);

   if (AutomaticallyAcceptDefaults) ClearConfigDialog();

}



void StartScript(int hWnd, string ScriptName, bool AutomaticallyAcceptDefaults = false)

{

   int MessageNumber = RegisterWindowMessageA("MetaTrader4_Internal_Message");
   Alert(MessageNumber);
   PostMessageA(hWnd, 33050, 16, ScriptName);
  
  //  PostMessageA(hWnd, MessageNumber, 16, ScriptName);

   if (AutomaticallyAcceptDefaults) ClearConfigDialog();

}



void ClearConfigDialog()

{

   Sleep(100);

   keybd_event(13, 0, 0, 0);

}
/// önemeli

//int PostMessageA(int hWnd,int Msg,int wParam,int IParam)
shoxie
29
shoxie  
Mehmet Bastem, have you compiled your code and run it on the latest build of MT4? PostMessageA no longer works with the most recent builds of MT4 because a change from ANSI to UNICODE came in with Build 600 onwards. Only PostMessageW should work. Except it doesn't in the most recent build (Build 1090). It will crash the MT4 terminal when you run it. RegisterWindowMessageA should also be replaced by RegisterWindowMessageW. And we can't use strings as arguments. We have to use char arrays and pass them as reference to the windows functions. Not that all this matters because PostMessageW doesn't work in any case.
Tom Sasson
187
Tom Sasson  
shoxie:

Thanks for responding. Yes I'm using the same function from WinUser32, but I included it at the top of my script:

Why would you want to do that within a script? 

Loading a script to a chart is the same operation as loading an EA/script/indicator to the chart.

Consider using your code in an EA and this way the PostMessageW command will not crash your MT4.

shoxie
29
shoxie  
Tom Sasson:

Why would you want to do that within a script? 

Loading a script to a chart is the same operation as loading an EA/script/indicator to the chart.

Consider using your code in an EA and this way the PostMessageW command will not crash your MT4.

My situation is this:

I would like to place an indicator on the chart just as I'm about to place a trade. I don't want the indicator running constantly. I would like to press a hotkey and then have the indicator be instantly placed on the chart without it showing the config dialog box. I would like the script to be linked to the hotkey, to run just once and perform just this very task - placing the indicator on the chart and getting rid of the dialog box without any manual input from me. The reason I go through the trouble of trying to get a script to do all this is to get rid of the config dialog box. I've got multiple charts open and I don't want to be clicking OK to get rid of the dialog box all the time. One hotkey press and the indicator should be up and running. That's what I want.

So you're saying that PostMessageW only crashes the terminal when it's called from a script? Why does it work in an EA and not in a script? What is the deeper programmatic reason for this?

Tom Sasson
187
Tom Sasson  
shoxie:

My situation is this:

I would like to place an indicator on the chart just as I'm about to place a trade. I don't want the indicator running constantly. I would like to press a hotkey and then have the indicator be instantly placed on the chart without it showing the config dialog box. I would like the script to be linked to the hotkey, to run just once and perform just this very task - placing the indicator on the chart and getting rid of the dialog box without any manual input from me. The reason I go through the trouble of trying to get a script to do all this is to get rid of the config dialog box. I've got multiple charts open and I don't want to be clicking OK to get rid of the dialog box all the time. One hotkey press and the indicator should be up and running. That's what I want.

So you're saying that PostMessageW only crashes the terminal when it's called from a script? Why does it work in an EA and not in a script? What is the deeper programmatic reason for this?

I am not sure, but I remember reading somewhere about it - an EA is working under a different thread, while scripts and indicators work under the MT4 GUI.

That is why when we're using WinAPI in indicators and scripts, we may harm the way MT4 works.

When doing the same in an Expert Advisor, it can implant internal messages to MT4 terminal without interrupting the total flow of the MT4 GUI.

Hotkeys, WinAPI - I'd rather use them in an EA and not under an infinite-loop running script.

shoxie
29
shoxie  
Tom Sasson:

I am not sure, but I remember reading somewhere about it - an EA is working under a different thread, while scripts and indicators work under the MT4 GUI.

That is why when we're using WinAPI in indicators and scripts, we may harm the way MT4 works.

When doing the same in an Expert Advisor, it can implant internal messages to MT4 terminal without interrupting the total flow of the MT4 GUI.

Hotkeys, WinAPI - I'd rather use them in an EA and not under an infinite-loop running script.

The page where you read about threads is here:

https://docs.mql4.com/runtime/running

Each script and EA runs its own thread. Only indicators share a common thread with the MT4 GUI. I'm well aware of this, which I why I avoid using while loops in all my indicators, so that the indicator doesn't go into an infinite loop and freeze MT4's GUI.

I use several WinAPI functions in my scripts and indicators. Only PostMessageW has caused a problem so far.

....sigh....I wonder what the problem is with PostMessageW in a script. I haven't tested it in an EA. Does it work without crashing the terminal?

Program Running - MQL4 programs - MQL4 Reference
Program Running - MQL4 programs - MQL4 Reference
  • docs.mql4.com
Each script and each Expert Advisor runs in its own separate thread. All indicators work in the graphic interface thread. processing of ticks and history synchronization are also performed in graphic interface thread. Custom indicators work in the main interface thread. If a custom indicator has been called with the iCustom() function, this...
123
To add comments, please log in or register