Download MetaTrader 5

Is it possible to call a Script from an Indicator ?

To add comments, please log in or register
file45
157
file45  

Hello

I've googled this question without success.

Is it possible to call a Script from and Indicator ?

If Yes, can you kindly point where information may be found.

Thanks in advance.

Simon Gniadkowski
Moderator
18023
Simon Gniadkowski  
You could assign a Keypress combination to the Script and then do something like this : https://www.mql5.com/en/forum/110904
whroeder1
15554
whroeder1  
But what you should actually do, is make the script into EA which reads the indicator's value(s.)
file45
157
file45  
RaptorUK:
You could assign a Keypress combination to the Script and then do something like this : https://www.mql5.com/en/forum/110904
Yes, was thinking of that - will follow up - great !
file45
157
file45  
WHRoeder:
But what you should actually do, is make the script into EA which reads the indicator's value(s.)


The script interacts with a DLL and draws SL & TP Lines on a chart ( I am not the author of the script). If orders are close using the the script the relevant lines are deleted. If orders are closed via Terminal SL & TP values or via MT4 Terminal the lines do not delete.

Hotkeys are assigned to the script and if pressed will deleted the lines of closed orders initiated via MT4 and SL & TP values. The indicator assigns text to the underlying default SL and TP values. Irrespective of how orders are closed the text will always delete, so my assumption was to add script calls to the indicator. An EA is already running. In hindsight, I should have clarified in first post.

file45
157
file45  
RaptorUK:
You could assign a Keypress combination to the Script and then do something like this : https://www.mql5.com/en/forum/110904

I have manages to put the following code together as and indicator that effectively sends Keybd-events to the script (see above) when orders are closed via SL & TP but not when orders are closed via the Terminal. Here is the here and there nicked code, so far. For others interested in solving for similar here is some long winded code that may be of interest - still trawling through it. Experimented with some of the code as EA and indicator and got it to send Keybd-events to the script when orders closed via the terminal but experience instability (chart jumping back in time). Something short and simple similar to - if (Order_Close==OrderStopLoss() ) SendKey(VK_HOME,true); - should do the trick !

#property indicator_chart_window

extern int Hot_Key1 = 162;
extern int Hot_Key2 = 90;

//#include <WinUser32.mqh>
#import "user32.dll"

void keybd_event(int bVk, int bScan,int dwFlags,int dwExtrainfo); 
//bool GetAsyncKeyState(int nVirtKey);

#define KEYEVENTF_EXTENDEDKEY          0x0001
#define KEYEVENTF_KEYUP                0x0002

#define VK_0   48
#define VK_1   49
#define VK_2   50
#define VK_3   51
#define VK_4   52
#define VK_5   53
#define VK_LMENU           164   //Left MENU key
#define VK_RMENU           165   //Right MENU key

int Orders;
//+------------------------------------------------------------------+
int start()
  {
   if (Orders>OrdersTotal()) Orders_Close(); 
   Orders=OrdersTotal(); 
    
return(0);
}
  
//+------------------------------------------------------------------+
void Orders_Close()
{
   
   double Order_Close;
   int i=OrdersHistoryTotal()-1;
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
   {                                     
      Order_Close=OrderClosePrice();

      if (Order_Close==OrderStopLoss()  )  SendKey(VK_HOME,true);                  
  
      if (Order_Close==OrderTakeProfit())  SendKey(VK_HOME,true);                           
        
   }  
}
//+------------------------------------------------------------------+
void SendKey(int key, bool release = false)
{
    keybd_event(key, 0, 0, 0);
    keybd_event(Hot_Key1, 0, 0, 0); // Left-Ctrl
    keybd_event(Hot_Key2, 0, 0, 0); // Z
    keybd_event(Hot_Key2, 0, KEYEVENTF_KEYUP, 0); // 2 
    keybd_event(Hot_Key1, 0, KEYEVENTF_KEYUP, 0);    
  
    if(release) keybd_event(key, 0, KEYEVENTF_KEYUP, 0); 
}
file45
157
file45  

if(OrderCloseTime()!=0) SendKey(VK_HOME,true); has done the trick ! Got it from here

In fact, it looks like that is all that is required

if (Order_Close==OrderStopLoss()  )  SendKey(VK_HOME,true);                  
  
if (Order_Close==OrderTakeProfit())  SendKey(VK_HOME,true);       

the above does not appear to be needed.

qjol
3243
qjol  
file45
157
file45  
Revised from
void Orders_Close()
{
   
   double Order_Close;
   int i=OrdersHistoryTotal()-1;

to

int i, histTotal=OrdersHistoryTotal();
for(i=0;i<hisTotal;i++)
{
.........

and this added

........
.......
if (release) keybd_event(key,0,KEYEVENTF_KEYUP,0)
 
ReleaseKey(0);

}
void ReleaseKey(int Key)
{
   keybd_event(key,0,2,0);
}
Simon Gniadkowski
Moderator
18023
Simon Gniadkowski  
If you are looping through Orders and closing any of them you will need to count down . . . not up.
file45
157
file45  
RaptorUK:
If you are looping through Orders and closing any of them you will need to count down . . . not up.

There is no automated closing of orders - orders are closed either manually via the Easy Order script or manually via MT4 Terminal or when current price equals SL or TP. The script only deletes the lines and is called after an order has closed. I tested this - for(int i = OrdersHistoryTotal()-1;i >=0; i--), which I believe to be counting down, and it appears to work equally well when multiple or a single order is in the history ie. it does not miss the last closed order in the order history. Anyhow I'll stay with this.

Irrespective of counting up or down the code calls scripts without problems but EAs stutter (flash on/off) and the Common-Input window locks.
When a position is closed the EA loads before any other indicator has had time to respond and the Common-Input window flashes on/off until other indicator (like a text on a SL line) drops off the chart (which it should do as the position is closed).
After that it stops flashing but the OK, Cancel and Reset buttons on the Common and Input tabs don't work. Focus cannot be taken off this Common-Input Window and it takes numerous clicks to close the Input window before focus can be moved.

If no other indicators are on the chart the EA behaves the same in that the Comon-Input window locks with OK, Cancel and Reset buttons not responding to mouse clicks.

I have tested on two machines and the same on both.

Regardless if counting up or down when scripts are called all goes successfully but the following alert always pops up.

more investigation required - thanks

12
To add comments, please log in or register