Summary: This article provides a line-by-line interpretation of a practical trailing stop script in MQL4. Learn how to read EA source code, understand variables, OrderSend vs OrderModify, and how to safely modify parameters before recompiling.




Many traders want to modify parameters in free EA source code but don‘t understand MQL4 syntax. This source code interpretation walks through a complete trailing stop script – a simpler starting point than a full EA. After reading, you’ll be able to compile and modify scripts confidently.

The Complete Source Code (Trailing Stop Script)


```cpp
//+------------------------------------------------------------------+
//| Trailing.mq4 |
//| Generated by AutoCompile AI |
//| |
//+------------------------------------------------------------------+
#property copyright "AutoCompile AI"
#property link ""
#property version "1.00"
#property strict
#property show_inputs

input int TrailingStart = 15; // Trailing activates after 15 pips profit
input int TrailingStep = 10; // Trail distance in pips
input int Slippage = 30; // Slippage for modify orders

//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() != Symbol()) continue;

if(OrderType() == OP_BUY)
{
double profitPips = (Bid - OrderOpenPrice()) / Point;
if(profitPips > TrailingStart)
{
double newStopLoss = Bid - TrailingStep * Point;
if(newStopLoss > OrderStopLoss())
OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrBlue);
}
}
else if(OrderType() == OP_SELL)
{
double profitPips = (OrderOpenPrice() - Ask) / Point;
if(profitPips > TrailingStart)
{
double newStopLoss = Ask + TrailingStep * Point;
if(newStopLoss < OrderStopLoss() || OrderStopLoss() == 0)
OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrBlue);
}
}
}
}
}
//+------------------------------------------------------------------+
```

Line-by-Line Interpretation



#### 1. Property Declarations (Lines 4-8)
```cpp
#property copyright "AutoCompile AI"
#property strict
#property show_inputs
```
  • `#property strict`: Enables strict type checking. Always include this in modern MQL4.

  • `#property show_inputs`: Makes input parameters appear as a dialog box when running the script.


  • #### 2. Input Parameters (Lines 10-12)
    ```cpp
    input int TrailingStart = 15;
    input int TrailingStep = 10;
    input int Slippage = 30;
    ```
  • `input` keyword: Creates user-modifiable parameters visible in the script properties window.

  • These are the modify parameters you can change without recompiling (just reopen the script).


  • #### 3. OnStart() Function (Line 16)
    ```cpp
    void OnStart()
    ```
  • Every script executes code inside `OnStart()`. EAs use `OnTick()` instead.


  • #### 4. Order Loop (Lines 17-18)
    ```cpp
    for(int i = OrdersTotal() - 1; i >= 0; i--)
    ```
  • `OrdersTotal()` returns number of open orders.

  • Loop runs backwards (from highest index to 0) – this is safer when you might modify or close orders.


  • #### 5. OrderSelect() (Line 19)
    ```cpp
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    ```
  • Selects an order by its position in the pool (0, 1, 2...).

  • Must call before accessing order properties like `OrderOpenPrice()`.


  • #### 6. Symbol Filter (Line 21)
    ```cpp
    if(OrderSymbol() != Symbol()) continue;
    ```
  • Only processes orders matching the current chart’s symbol.


  • #### 7. Buy Order Logic (Lines 23-32)
    ```cpp
    double profitPips = (Bid - OrderOpenPrice()) / Point;
    ```
  • `Bid`: Current sell price for the pair.

  • `Point`: Smallest price increment (0.0001 for most pairs).

  • Profit in pips = difference divided by Point.


  • ```cpp
    if(profitPips > TrailingStart)
    ```
  • Only activate trailing if profit exceeds the start threshold.


  • ```cpp
    double newStopLoss = Bid - TrailingStep * Point;
    ```
  • For buy orders: new stop loss is placed `TrailingStep` pips below current Bid.


  • ```cpp
    if(newStopLoss > OrderStopLoss())
    ```
  • Only modify if new stop loss is higher than the existing one.


  • ```cpp
    OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrBlue);
    ```
  • `OrderTicket()`: Unique order ID.

  • Parameters: ticket, open price, new stop loss, take profit, expiration (0=none), arrow color.


  • #### 8. Sell Order Logic (Lines 33-42)
    ```cpp
    double profitPips = (OrderOpenPrice() - Ask) / Point;
    ```
  • For sell orders: profit = open price minus current Ask.


  • ```cpp
    double newStopLoss = Ask + TrailingStep * Point;
    ```
  • New stop loss goes `TrailingStep` pips above current Ask.


  • How to Compile and Modify


    1. Save as `Trailing.mq4` in the `Scripts` folder.
    2. Open in MetaEditor (F4), press F7 to compile.
    3. If successful, drag script to chart – input dialog appears.
    4. Modify parameters like TrailingStart from 15 to 20 pips without editing code.
    5. To permanently change defaults: edit the `input` line values, then recompile (F7).

    Common Modification Examples


    | Change Desired | Line to Modify |
    |----------------|----------------|
    | Change default activation pips | `input int TrailingStart = 25;` |
    | Change trail distance | `input int TrailingStep = 15;` |
    | Add minimum lot check (advanced) | Add after line 23: `if(OrderLots() < 0.01) continue;` |
    | Add alert on modification | Add after OrderModify: `Alert(“Trailing adjusted”);` |

    This MQL4 tutorial shows that reading source code interpretation is the fastest way to learn EA programming entry. After mastering scripts, move to EAs with `OnTick()` and timer functions. Subscribe for weekly compile and modify guides on advanced EA tools.

    Reference: AutoCompile AI - Original code interpretation tutorial, 2025.