Scripts are executable files in MetaTrader that only run one time. They are perfect for tasks that are routine but time consuming or unpleasant to do. The most advantageous use of scripts is that they do not rely on incoming price ticks in order to run. The script executes the moment that the trader drops it onto a chart.
An expert advisor runs continuously, but it relies on incoming ticks to know that it should update itself. The frequency that the market changes price varies with the time of day. This means that the period between updates for an expert advisor is highly unpredictable. The predictability of a script’s timing – it runs immediately – makes it more suitable for some trading tasks than an EA.
Script examples
A scalper wants to open a trade quickly. He routinely applies a 20 pip stop loss and a 3 pip take profit. His usual process would involve:
- Click inside of MetaTrader to open a trade
- Select the correct forex pair
- Wait for the trade to open
- Frantically add the take profit as quickly as possible
Alternatively, the trader could keep his chart open and the scripts window nearby. Whenever he decides to trade, he drags the script onto the chart. The steps above still occur. The critical difference is that they happen in a fraction of the time. The script runs once, then it removes itself from the chart.
Some tasks require a tiresome amount of clicking rather than speed. Scripts are also useful in that scenario. A lot of MetaTrader users like to stack multiple pending orders above and below the market in a grid pattern. An example would involve placing 10 orders above and below the market at different prices. Doing this manually would take a few minutes.
The alternative is to run a script that does it one time for you. Scripts can display input screens just like expert advisors. That way the user can control the settings.
When the trader is ready to bracket his orders, he drags and drops the script onto the chart. The orders show up on the screen at the requested prices. The total time takes a few seconds instead of several minutes.
Other script uses
The most common, though unorthodox, use of a script is to feed prices into the History Center for a custom offline chart. The script is programmed to run at a set interval such as every half second. The script samples the price, then records the information where the historical prices are kept. The offline chart then re-reads the information and updates the price.
MQL programmers elect to use a script instead of an expert advisor because they are setting an infinite loop. Although scripts technically run once, this script is never allowed to finish its first run. It keeps waiting every set interval to update the price. EAs would not work well here because new, incoming ticks would create a backlog of occasions where the EA is supposed to have run. I would expect MT4’s memory use to eventually get out of control and crash the program if someone elected to take this approach. Even though it is not an ideal solution, scripts update historical prices successfully without the tick backlog concern.