Run script through Expert Advisor - page 2

 
Shaun:

True story, i am new, to the site, to coding as well. Never coded before.
start here
 
qjol:
start here

Thanks for that information.

To be honest i came to the traders/coding forum to get the answer from someone who has done it already and could possibly help me out, not learn how to code everything myself from scratch.

 
Shaun:

Thanks for that information.

To be honest i came to the traders/coding forum to get the answer from someone who has done it already and could possibly help me out, not learn how to code everything myself from scratch.

Show your Script, show your EA maybe someone will take pity on you and combine them . . .
 
Shaun:

Thanks for that information.

To be honest i came to the traders/coding forum to get the answer from someone who has done it already and could possibly help me out, not learn how to code everything myself from scratch.

right, show your code & we will try to help u but of course i'm sure u don't expect me to do the entire code for u
 

Thanks Guys!

Following your advice, I wrote smth like this inside my EA:

if(MyCondition)
{
  PlaySound("my_alert.wav");
    if(MessageBox("Do you really want to run the script?", 0, MB_YESNO|MB_ICONQUESTION)!=IDNO)
     { 
      MyScript();
      DisableAllEA();
      Sleep(3000);    
     }
  }

In a certain situation, defined by MyCondition, this plays my alert, so I can hear that I have to check what is going on.

A message box pops out, asking if I want to run the script - which is a close all positions script.

When I click the [YES] button, the script executes - closes all positions - and disables the [Experts] button in MT4.

Of course, you do not need to disable Experts, unless you have a reason to do that.

If on the next loop your EA does not find the condition, it will not trigger the script.

Obviously, this is only a workaround and not a perfect solution, because the EA keeps looping on every tick.

And you cannot disable Experts until the script has been executed, because the script is inside the EA, not stand alone.

P.S.

In order for this to work, you need to include the following in the begining of your EA:

#include <WinUser32.mqh>
 

I have just thought of another solution!!!

You can assign a shortcut key (HOTKEY) to your script.

Thus, you can emulate the keyboard event.

So, better:

if(MyCondition)
{
      EnableScript();
      DisableAllEA();
      Sleep(3000);
}

You can include your alert.wav in the stand alone script, and the message box, as well.

And, you can decide wheather to enable Experts or not, after your action on the situation.

This way, you do not risk overstacking.

Of course, this needs more study on key events.

The function for Ctrl+E keyboard event is:

void DisableAllEA() {
   keybd_event(17, 0, 0, 0); // CTRL down
   keybd_event(69, 0, 0, 0); // E down
   keybd_event(69, 0, 2, 0); // E up
   keybd_event(17, 0, 2, 0); // CTRL up
}

With a bit of googling, you can find number codes for every key.

Or, you can use this link: https://www.mql5.com/en/code

Of course, keybd_event is not part of mql4, that is why you need:

#include <WinUser32.mqh>
 

And, by the way!

Has anybody ever tired to implement an EA on the chart as an indicator?

It should work just as well!

And you can have any number of indicators running on a chart!

I have tried implementing scripts as indicators, and that works just fine!

 
Dadas:

And, by the way!

Has anybody ever tired to implement an EA on the chart as an indicator?

It should work just as well!


You can't place trades from an Indicator, only from Scripts and EAs.
 
RaptorUK:

You can't place trades from an Indicator, only from Scripts and EAs.


So, an EA could run as a script? In a loop?

Only, when in an infinite loop, the computer will eventually crash.

Is there a way to trigger a script in a loop controlled by some ticker, like every 50ms or smth like that?

Not by MT4 ticks?

 
RaptorUK:

You can't place trades from an Indicator, only from Scripts and EAs.


And another thought:

So you can use the keyboard event function to place orders by Script.

1. Write a Script to automatically place orders.

2. Asssign a HOTKEY to it.

3. In the EA running as Indicator define the keyboard event.

Then, you can do the same for closing orders.

There you have it! An EA running as an Indicator and doing its job!

The only downside to this way is that you can not disable an Indicator with a keyboard event - only manually remove it from chart.

Reason: