Download MetaTrader 5

How to start a script from a button in an indicator.

To add comments, please log in or register
Do you want to write a trading robot? Find solution in MQL5 Reference!
Lenpa
22
Lenpa 2013.01.17 14:23 

How to start a script from a button in an indicator.

I have a button on the indicator, and the button works fine.
I have a script with a hotkey connected, and it works well.

What is the code to start the script with a click of the button.

Grateful for help ....

Lenpa

phi nuts
2184
phi nuts 2013.01.17 15:49  
Lenpa:

How to start a script from a button in an indicator.

I have a button on the indicator, and the button works fine.
I have a script with a hotkey connected, and it works well.

What is the code to start the script with a click of the button.

Grateful for help ....

Lenpa

You need Win API keyboard event and virtual key codes

Tyt this first, below is my code on script to open new order or press F9 :

#import "user32.dll"
    void keybd_event(int bVk, int bScan, int dwFlags,int dwExtraInfo);
#import

#define  VK_F9 0x78               //--- this is key F9
#define  KEYEVENTF_KEYUP 0x0002   //--- key is released (not pressed)

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
  PlaySound("wait.wav");
  
  keybd_event(VK_F9, 0, 0, 0);
  keybd_event(VK_F9, 0, KEYEVENTF_KEYUP , 0);

  return;
  }
//+------------------------------------------------------------------

 <Edit : Code added : KEYEVENT_KEYUP>

keybd_event function (Windows)
  • msdn.microsoft.com
Synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function. Note This function has been superseded. Use SendInput instead. Syntax Parameters bVk [in] A virtual-key code. The code must be a value in the range 1...
Lenpa
22
Lenpa 2013.01.17 18:43  

Thank you very much ..
I still have problems.
I get the following message in the ToolBox-> Experts

DLL import is allowed


The indicator shows the

Code right now

and this...

 

 

To no misunderstandings to occur.
The button is coded into an indicator, and shall send hotkey
to the terminal to start a script.

regards
Lenpa

phi nuts
2184
phi nuts 2013.01.18 03:27  
Lenpa:

Thank you very much ..
I still have problems.
I get the following message in the ToolBox-> Experts

DLL import is allowed


The indicator shows the

Code right now

and this...

To no misunderstandings to occur.
The button is coded into an indicator, and shall send hotkey
to the terminal to start a script.

regards
Lenpa

1. Let's fix the code first :) I edit my previous code, by adding KEYEVENT_KEYUP, sorry for not writing it correctly - I'm a little bit rusty for Win API :( 

This is how to open Market Watch (Ctrl + M) and here's another info on how to use keybd_event : codeproject - keyboard simulation

#define  VK_M 0x4D               //--- letter M
#define  VK_LCONTROL 0xA2        //--- left key of Ctrl 
#define  VK_RCONTROL 0xA3        //--- right key of Ctrl
#define  KEYEVENTF_KEYUP 0x0002  //--- key is released (not pressed)

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
  PlaySound("wait.wav");
  
  keybd_event(VK_LCONTROL, 0, 0, 0);               //--- press left Ctrl key
  keybd_event(VK_M, 0, 0, 0);                      //--- press M key

  keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0); //--- release left Ctrl key
  keybd_event(VK_M, 0, KEYEVENTF_KEYUP, 0);        //--- release M key

  return;
  }
//+------------------------------------------------------------------+

2. About your *.dll problem, if you're using Windows Vista/7/8, try to install MetaTrader outside C:\Program Files\..., like for example C:\my other prog\... or C:\whatever\... 

Keyboard Events Simulation using keybd_event() function
Keyboard Events Simulation using keybd_event() function
  • votes: 57
  • 2004.06.04
  • Naren Neelamegam
  • www.codeproject.com
Simulation of a keyboard input is a well known concept for those who are all familiar with Visual Basic. in Visual Basic does all the things, if you want to do anything without keyboard. But what is in function? What does it do? Can we do such a thing with Visual C++? This article is the answer. I think this will be useful for beginners who are...
Lenpa
22
Lenpa 2013.01.21 16:56  
phi.nuts:

1. Let's fix the code first :) I edit my previous code, by adding KEYEVENT_KEYUP, sorry for not writing it correctly - I'm a little bit rusty for Win API :( 

This is how to open Market Watch (Ctrl + M) and here's another info on how to use keybd_event : codeproject - keyboard simulation

2. About your *.dll problem, if you're using Windows Vista/7/8, try to install MetaTrader outside C:\Program Files\..., like for example C:\my other prog\... or C:\whatever\... 

 

Hello, thank you for your commitment.

I use MT5 on a Windows 7 computer.

It still does not work.

I have NOT moved MT5 installation outside of the application folder.
Are you convinced that it is the right solution?

However, I have copied a copy of the User32.dll to .... MQL5 \ Indicators \
I get the Tool Box message "is not 64-bit version"

Read from the bottom up


I am open to new solutions.

Thanks and regards

Lenpa

phi nuts
2184
phi nuts 2013.01.21 23:29  
Lenpa:

Hello, thank you for your commitment.

I use MT5 on a Windows 7 computer.

It still does not work.

I have NOT moved MT5 installation outside of the application folder.
Are you convinced that it is the right solution?

However, I have copied a copy of the User32.dll to .... MQL5 \ Indicators \
I get the Tool Box message "is not 64-bit version"

Read from the bottom up


I am open to new solutions.

Thanks and regards

Lenpa

1. Don't copy or even move user32.dll to anywhere. User32.dll is a system file used heavily by your OS, so would you please leave your system files alone.

2. In 7 - 64 bit, there's 2 version of user32.dll. The first one is for 32 bit which located at C:\Windows\SysWOW64\user32.dll, and the other is for 64 bit which located at C:\Windows\System32\user32.dll. That probably confusing a little, why the 64 bit is located at System32 folder and still named user32.dll and not user64.dll ?. Well, Microsoft can't change the name of location and the file name, because that surely will ruin any application that using hard coded address of user32.dll.

3, If you want to save any *.dll, save it in MQL5\libraries . Read here "Call of imported function" .

4. Yes, installing outside C:\Program Files\... will help avoiding many UAC issue, that prevent many program from running correctly. If you love long headache reading, feel free to join in Microsoft's Windows 7 Application Compatibility forum. If you worry that installing outside C:\Program Files\... then you can un-install it later and use System Restore to get back to your computer previous state, but do this first.

4.1. Run full scan System File checker and then restart no matter what. Click here for System File Checker.

4.2. Create System Restore > Start > All Programs > Accessories > System Tool > System Restore 

4.3. Install your MT5 outside C:\Program Files\... if it is still does not works, install the 32 bit version (click here, how to install 32 bit version on 54 bit Windows)

4.4. When you're done, you can un-install your MT and run System Restore to restore your PC to previous state.

There, you have all I can think of.

Seriously no kidding, that is a funny one if you are worry installing outside C:\Program Files and yet making copy of user32.dll some where else. 

Technet forums - Windows 7 Application Compatibility
  • social.technet.microsoft.com
Microsoft is conducting an online survey to understand your opinion of the Technet Web site. If you choose to participate, the online survey will be presented to you when you leave the Technet Web site. Would you like to participate...
Lenpa
22
Lenpa 2013.01.23 12:06  
phi.nuts:

1. Don't copy or even move user32.dll to anywhere. User32.dll is a system file used heavily by your OS, so would you please leave your system files alone.

2. In 7 - 64 bit, there's 2 version of user32.dll. The first one is for 32 bit which located at C:\Windows\SysWOW64\user32.dll, and the other is for 64 bit which located at C:\Windows\System32\user32.dll. That probably confusing a little, why the 64 bit is located at System32 folder and still named user32.dll and not user64.dll ?. Well, Microsoft can't change the name of location and the file name, because that surely will ruin any application that using hard coded address of user32.dll.

3, If you want to save any *.dll, save it in MQL5\libraries . Read here "Call of imported function" .

4. Yes, installing outside C:\Program Files\... will help avoiding many UAC issue, that prevent many program from running correctly. If you love long headache reading, feel free to join in Microsoft's Windows 7 Application Compatibility forum. If you worry that installing outside C:\Program Files\... then you can un-install it later and use System Restore to get back to your computer previous state, but do this first.

4.1. Run full scan System File checker and then restart no matter what. Click here for System File Checker.

4.2. Create System Restore > Start > All Programs > Accessories > System Tool > System Restore 

4.3. Install your MT5 outside C:\Program Files\... if it is still does not works, install the 32 bit version (click here, how to install 32 bit version on 54 bit Windows)

4.4. When you're done, you can un-install your MT and run System Restore to restore your PC to previous state.

There, you have all I can think of.

Seriously no kidding, that is a funny one if you are worry installing outside C:\Program Files and yet making copy of user32.dll some where else. 

 

Hello,
Thanks for any help.
I'm not worried to install outside C: \, I'm lazy.
Yes, now I gjoer all the steps except the last (4.4).
It does not help. Not even with 32-bit installation.
Probably there is something wrong with the rights in Windows 7.
Have you or any other, ideas on how this can be fixed.

Grateful for help.

Lenpa
Alexander Piechotta
6949
Alexander Piechotta 2013.01.23 16:08  

It would also be possible to insert your script in the indicator.

With #resource Any EX5 file...


phi nuts
2184
phi nuts 2013.01.23 16:58  
Lenpa:
Hello,
Thanks for any help.
I'm not worried to install outside C: \, I'm lazy.
Yes, now I gjoer all the steps except the last (4.4).
It does not help. Not even with 32-bit installation.
Probably there is something wrong with the rights in Windows 7.
Have you or any other, ideas on how this can be fixed.

Grateful for help.

Lenpa
Not run ? Not even my code above there https://www.mql5.com/en/forum/10101#comment_408987 ?
How to start a script from a button in an indicator.
How to start a script from a button in an indicator.
  • www.mql5.com
How to start a script from a button in an indicator.
Lenpa
22
Lenpa 2013.01.25 14:20  

phi.nuts:
Not run ? Not even my code above there https://www.mql5.com/en/forum/10101#comment_408987 ?

 

Hello,
Here are the tests with your code.

 

 

 

 

the result is

 

If I use your code

 

 

Blir reultatet

 

 I assume that the error is in my operating system Windows 7.

Any setting in UAC?

Any idea on this?


Regards

Lenpa

 

 

 

 

phi nuts
2184
phi nuts 2013.01.25 17:34  

Lenpa:

Hello,
Here are the tests with your code.

the result is

If I use your code

Blir reultatet

 I assume that the error is in my operating system Windows 7.

Any setting in UAC?

Any idea on this?

Regards

Lenpa 


You are installing the 32 bit. Can you install the 64 bit and run on it.

Add : what the script do anyway ? 

12
To add comments, please log in or register