• Home
  • Forums
  • News
  • Calendar
  • Coins
  • Market
  • Login
  • Join
  • User/Email: Password:
  • 8:30pm
Menu
  • Forums
  • News
  • Calendar
  • Coins
  • Market
  • Login
  • Join
  • 8:30pm
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Forex Factory

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

MQL4 code help 12 replies

Help - any tools that can easily convert MQL4 code to Easy Language code? 7 replies

MQL4 Language Most Recent Version is it updated beyond the tutorial on the mql4 websi 6 replies

Need some help with MQL4 code... 1 reply

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 7
Attachments: MQL4/5 Code Corner
Exit Attachments
Tags: MQL4/5 Code Corner
Cancel

MQL4/5 Code Corner

  • Post #1
  • Quote
  • First Post: Edited 10:14am Mar 16, 2023 7:02am | Edited 10:14am
  •  Georgebaker
  • | Joined Nov 2009 | Status: Member | 518 Posts
This space is for code examples!

This is NOT a space for "can you code this for me"! We already have that in this group https://www.forexfactory.com/thread/...indicators-for

I'm using a ton of time looking up code snippet/examples and when i finally, after a whole day search, find something that seems to be what i'm looking for, it doesn't work. MQL5 have a lot of documentation and that is fine, but they do not have so much code examples and i know we are many that need code examples to learn from.

Ways of improve our code is also very welcome in here so we can learn from that.
It could be like "Easiest way of making a rectangle on the chart"
Or maybe simplest way to show a obj-label on the chart
Stuff like that

IMPORTANT: Upload only code examples that you have tested so when you upload it you know it works so others is not wasting their time on things that do not work. If i see code that do not work it will be remove!

More to come, Let's begin
Blindly following others will make you blind!
  • Post #2
  • Quote
  • Edited 12:03pm Mar 16, 2023 7:35am | Edited 12:03pm
  •  Georgebaker
  • | Joined Nov 2009 | Status: Member | 518 Posts
How to run a script from an indicator

This example is using a DLL file, so if you don't like that, this example is not for you
(I have looked for other ways to do it but could not find something that worked, will still search for it though)

From this link below (Go to post 7) unzip the zip file and find the DLL file. Copy that DLL file into your library in the mq4 folder.
https://www.forexfactory.com/thread/...from-indicator

Inserted Code
//+------------------------------------------------------------------+
//|                                               StartScriptBox.mq4 |
//|               For educational purpose only, use on your own risk |
//|                             Coded by Georgebaker aka FXSniperGuy |
//|                                                 Date: 16.03.2023 |
//+------------------------------------------------------------------+
#import "rsfExpander.dll"
   bool LoadMqlProgramW(int hChart, int programType, string programName);
#import
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
 
// MQL program types
#define PROGRAMTYPE_INDICATOR   1
#define PROGRAMTYPE_EXPERT      2
#define PROGRAMTYPE_SCRIPT      4
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   int  hChart = WindowHandle(Symbol(), NULL);
   bool result = LoadMqlProgramW(hChart, PROGRAMTYPE_SCRIPT, "Box1");
   Print("result=", result);  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Source code file:
Attached File(s)
File Type: mq4 StartScriptBox.mq4   2 KB | 13 downloads


This is the script i trigger form the indicator: (The code in this script is so messy and it could probably have been a lot easier, but here it is)
Attached File(s)
File Type: mq4 Box1.mq4   10 KB | 13 downloads

Attached File(s)
File Type: ex4 Box1.ex4   14 KB | 16 downloads


See the short video on how it works
Attached File(s)
File Type: mp4 StartScriptBox.mp4   178 KB | 20 downloads



Tags: #mq4 #run #script #indicator
Blindly following others will make you blind!
 
1
  • Post #3
  • Quote
  • Mar 25, 2:39pm (5 hr ago) Mar 25, 2:39pm (5 hr ago)
  •  pips4life
  • Joined Apr 2007 | Status: Member | 891 Posts | Online Now
Quoting Georgebaker
Disliked
How to run a script from an indicator This example is using a DLL file, so if you don't like that, this example is not for you (I have looked for other ways to do it but could not find something that worked, will still search for it though) From this link below (Go to post 7) unzip the zip file and find the DLL file. Copy that DLL file into your library in the mq4 folder. https://www.forexfactory.com/thread/...from-indicator //+------------------------------------------------------------------+...
Ignored
I haven't yet had the need for a script to run an Indicator/EA or another script, so I don't have actual code examples I wrote or used. In your post, you referenced another thread on the topic that looks like it has a lot of great discussion to learn from, but like others I'm not thrilled to need a custom dll file (though perhaps this source is on GibHub or in the thread? I didn't dive deeper).

We all have Google, and maybe you already found these threads, but here are two I noticed that use the user32.dll, instead of the custom dll.

See : How To Call/Execute an mql4 file from a Script
See : Issues with PostMessageW in MQL4

For completeness, a link to the thread you already mentioned: calling scripts from indicator
 
 
  • Post #4
  • Quote
  • Mar 25, 4:28pm (4 hr ago) Mar 25, 4:28pm (4 hr ago)
  •  Georgebaker
  • | Joined Nov 2009 | Status: Member | 518 Posts
Quoting pips4life
Disliked
{quote} I haven't yet had the need for a script to run an Indicator/EA or another script, so I don't have actual code examples I wrote or used. In your post, you referenced another thread on the topic that looks like it has a lot of great discussion to learn from, but like others I'm not thrilled to need a custom dll file (though perhaps this source is on GibHub or in the thread? I didn't dive deeper). We all have Google, and maybe you already found these threads, but here are two I noticed that use the user32.dll, instead of the custom dll. See...
Ignored
Jeah some of the links you show here i already went through, but they do not all works.

But there is one interesting link i will take a look at.

Thanks!
Blindly following others will make you blind!
 
 
  • Post #5
  • Quote
  • Last Post: Edited 6:58pm Mar 25, 6:33pm (1 hr 56 min ago) | Edited 6:58pm
  •  Georgebaker
  • | Joined Nov 2009 | Status: Member | 518 Posts
From pips4life post #3

This is an EA that will insert a stochastic indicator in the sub windows when you attach the EA to the chart.

Attached Image (click to enlarge)
Click to Enlarge

Name: ExpertOpenIndicatorWhenAttached.gif
Size: 276 KB


For the lazy people
Attached File(s)
File Type: ex4 TestEA.ex4   9 KB | 0 downloads

Attached File(s)
File Type: mq4 TestEA.mq4   4 KB | 0 downloads


Inserted Code
#import "user32.dll"
   int  RegisterWindowMessageW(string lpString);
   int  FindWindowW(string lpClassName, string lpWindowName);
   int  GetDlgItem(int hDlg, int nIdDlgItem);
   int  SetActiveWindow(int hWnd);
   bool PostMessageA(int hWnd, int msg, int wParam, uchar &lParam[]);
   bool PostMessageW(int hWnd, int msg, int wParam, int lParam);
#import
#define MAX_PATH                                      260              // e.g. the max. path on drive D is "D:\some-256-chars-path-string<NUL>"
#define MT4_LOAD_CUSTOM_INDICATOR           15
#define IDC_CUSTOM_INDICATOR_OK            1                // control id of "Ok" button in "Custom Indicator" dialog
 
#define BM_CLICK                                     0x00F5
uchar buffer[MAX_PATH];
void StartCustomIndicator(int hWnd, string indicatorName, bool autoCloseDlg = true) {
   StringToCharArray(indicatorName, buffer);
 
   int WM_MT4 = RegisterWindowMessageW("MetaTrader4_Internal_Message");
   PostMessageA(hWnd, WM_MT4, MT4_LOAD_CUSTOM_INDICATOR, buffer);
 
   if (autoCloseDlg) {
      string className, title = "Custom Indicator - "+ indicatorName;
      int i = 0;
      while (i < 5) {
         Sleep(10);
         int hWndDlg = FindWindowW(className, title);
         if (hWndDlg != 0) {
            int hWndOk = GetDlgItem(hWndDlg, IDC_CUSTOM_INDICATOR_OK);
            if (hWndOk != 0) {
               SetActiveWindow(hWndDlg);
               PostMessageW(hWndOk, BM_CLICK, 0, 0);
            }
 
            else Print("Error: \"OK\" button not found");
            break;
         }
         i++;
         if (i >= 5) Print("Error: Dialog \""+ title +"\" not found");
      }
   }
}
 
int OnInit()
  {
//---
   int hWnd=WindowHandle(Symbol(),0);
   StartCustomIndicator(hWnd,"Stochastic");   // For example STO
//---
   return(INIT_SUCCEEDED);
  }
Blindly following others will make you blind!
 
 
  • Platform Tech
  • /
  • MQL4/5 Code Corner
  • Reply to Thread
2 traders viewing now, 1 is a member:
Invisible
Top of Page
  • Facebook
  • Twitter
About CC
  • Mission
  • Products
  • User Guide
  • Blog
  • Contact
CC Products
  • Forums
  • Calendar
  • News
  • Coins
  • Market
CC Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow CC
  • Facebook
  • Twitter

CC Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Forex Factory

Crypto Craft® is a brand of Fair Economy, Inc.

Terms of Service / ©2023