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

 
shoxie:

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?I

As I said before: "Consider using your code in an EA and this way the PostMessageW command will not crash your MT4."

 
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?

Probably you can prepare a template with this indicator (and required parameters) and then just switch to/from this template when needed. Hence you don't need any scripts, Windows messaging, or other automation.

 
Stanislav Korotky:

Probably you can prepare a template with this indicator (and required parameters) and then just switch to/from this template when needed. Hence you don't need any scripts, Windows messaging, or other automation.

I don't want to go through the trouble of changing the chart entirely by using a new template. This means elaborating my code for deinitialization and reinitialization for template change. There's also other stuff on the chart that I don't want to lose. I'm very intent on using a script, since it can be activated by a hotkey, runs only once and removes itself from the chart. It's effect is minimal.

Never mind, just leave it. It doesn't look like there is a decent alternative to PostMessageW which will close the config dialog from within a script.
 
Stanislav Korotky:

Probably you can prepare a template with this indicator (and required parameters) and then just switch to/from this template when needed. Hence you don't need any scripts, Windows messaging, or other automation.

FYI, it seems that checks have been added into MT4 (build 1170, possibly earlier) to see if  MQL_TRADE_ALLOWED is false (e.g. for indicators) and if so calls to ChartOpen/ChartApplyTemplate that apply an EA will have the EA's Common parameter variables changed such that trading is not allowed without manually adjusting those parameters.

I found that using Windows messaging (PostMessageA/MT4_WMCMD_LOAD_TEMPLATE from indicator) was easiest way to resolve my specific code but an MQL4 only solution could be done as well if the calling code were all moved to an EA (that has MQL_TRADE_ALLOWED).


See https://www.mql5.com/en/forum/191697#comment_10577960 for more discussion and solutions to handle this specific change in behavior...

How to Set (by Script), the Long & Short, Only Long or Only Short options, when i run an EA?
How to Set (by Script), the Long & Short, Only Long or Only Short options, when i run an EA?
  • 2017.05.04
  • www.mql5.com
Hi, I would like to know if is there a way to Set (by Script) the Long & Short, Only Long or Only Short options, when the EA is loaded...
 

Just open the EA on any chart and save it as a template.

Then for future use, when you want to reopen the EA, then open any chart with ChartOpen or ChartSetSymbolPeriod and apply the template with ChartApplyTemplate.

The EA will be loaded as it is defined as a part of the template.

 
Mehmet Bastem:
I want to call python Script from an indicator ,is that posible with this code?
 
can you help me with coding a very simple EA?

Variable is

RUN SCRIPT TO CLOSE ALL OPENED TRADES WHEN:

Equity Profit: +$X.XX

or

Equity loss: +$x.xx

execute task.... 

Press: "CTRL + F"
Wait: 150ms
Press: "Enter"

--------------

I want to close all trades executing my Script.
Reason: