Author: Copyright � 2006-2008, Stab-Invest[Dreamer]
MTrendLine

This script is designed to automate the adjustment of pending trade orders based on specific price levels defined by trend lines on your trading chart. It functions as a management tool that ensures your entry points, stop-loss limits, and take-profit targets remain synchronized with your visual analysis.

Core Logic Overview

The script monitors three independent sections (slots), allowing you to link different pending orders to different trend lines simultaneously. For each section, the logic follows a specific sequence:

1. Validation of Elements Before performing any actions, the script checks if the necessary components are present. It verifies that:

  • A valid trend line has been drawn on the chart.
  • The trade order linked to that section is currently active (i.e., it has not been filled or closed yet).
  • The order type is a "pending" order (Limit or Stop orders), as these are the only types that can be repositioned before they are executed.

2. Price Detection and Alignment The script identifies the current value of the trend line at its starting point. It then calculates a specific "target price" by applying a user-defined distance from that trend line.

3. Automated Order Modification If the script detects that your pending order's entry price does not match this calculated target price, it will attempt to move the order automatically. During this process, the script performs two critical tasks:

  • Entry Adjustment: It moves the order's entry price to align perfectly with the trend line plus your chosen distance.
  • Relative Protection Scaling: Instead of moving your Stop-Loss (SL) or Take-Profit (TP) to fixed prices, the script preserves the distance between your entry and your protection levels. For example, if your original plan was to have a stop-loss 50 pips away from your entry, the script will ensure that even after moving the entry price to the trend line, the stop-loss remains exactly 50 pips away.

4. Safety Checks The script includes built-in safeguards to ensure trade execution is possible. Before moving an order, it checks the broker's "Stop Level" requirements—the minimum distance required between a trade price and its protection levels. If a proposed move would violate these rules, the script will skip the modification to prevent execution errors.

Summary of Benefits

  • Consistency: Ensures that your technical analysis (the trend line) is perfectly reflected in your actual trading orders.
  • Dynamic Management: Automatically updates your prices if you decide to shift your trend lines on the chart.
  • Preserved Risk Profile: Keeps your risk-to-reward ratio intact by moving your protection levels proportionally as the entry price changes.
Orders Execution
It can change open orders parameters, due to possible stepping strategy
10 Views
0 Downloads
0 Favorites
MTrendLine
//+------------------------------------------------------------------+
//|                                                   MTrendLine.mq4 |
//|                      Copyright © 2006-2008, Stab-Invest[Dreamer] |
//|                                               www.stab-invest.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006-2008, Stab-Invest[Dreamer]"
#property link      "www.stab-invest.ru"

#define major   1
#define minor   0

extern string _tmp1_ = " --- Section 1 ---";
extern int Ticker1 = 0;
extern string TrendLine1 = "";
extern int Dist1 = 10;

extern string _tmp2_ = " --- Section 2 ---";
extern int Ticker2 = 0;
extern string TrendLine2 = "";
extern int Dist2 = 10;

extern string _tmp3_ = " --- Section 3 ---";
extern int Ticker3 = 0;
extern string TrendLine3 = "";
extern int Dist3 = 10;

extern string _tmp4_ = " --- Colors ---";
extern color clModify = Gold;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <stdlib.mqh>
#include <stderror.mqh>

void init () 
{
}

void deinit() 
{
}

void start()
{
  modify(Ticker1, TrendLine1, Dist1);
  modify(Ticker2, TrendLine2, Dist2);
  modify(Ticker3, TrendLine3, Dist3);
}

void modify(int Ticker, string TrendLine, int Dist) 
{
  if (StringLen(TrendLine) == 0) return;
  if (ObjectFind(TrendLine) == -1) return;
  if (ObjectType(TrendLine) != OBJ_TREND) return;

  if (!OrderSelect(Ticker, SELECT_BY_TICKET)) return;
  if (OrderCloseTime() > 0) return;

  //-----
  
  double TL0 = ObjectGetValueByShift(TrendLine, 0);
  TL0 = NormalizeDouble(TL0, Digits);
  //Comment(TL0);
  
  if (TL0 == 0) return;

  
  int type = OrderType();
  if (type == OP_BUYLIMIT || type == OP_SELLLIMIT || type == OP_BUYSTOP || type == OP_SELLSTOP)
  {
    double open_price = OrderOpenPrice();
    open_price = NormalizeDouble(open_price, Digits);

    if (open_price == TL0 + Dist*Point) return;
    
    //-----

    double price, sl, tp, sl_pt, tp_pt;
          
    price = TL0 + Dist*Point;
    int StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL) +1;

    if (type == OP_BUYLIMIT)
    {
      if (Ask-price < StopLevel*Point) return;  
    }
    
    if (type == OP_BUYSTOP)
    {
      if (price-Ask < StopLevel*Point) return;
    }
    
    if (type == OP_SELLLIMIT)
    {
      if (price-Bid < StopLevel*Point) return;
    }
    
    if (type == OP_SELLSTOP)
    {
      if (Bid-price < StopLevel*Point) return;
    }


    if (OrderStopLoss() == 0)
      sl_pt = 0;
    else
      sl_pt = MathAbs(OrderOpenPrice() - OrderStopLoss());

    if (OrderTakeProfit() == 0)
      tp_pt = 0;
    else
      tp_pt = MathAbs(OrderOpenPrice() - OrderTakeProfit());

    
    if (type == OP_BUYLIMIT || type == OP_BUYSTOP)
    {
      sl = If(sl_pt > 0, price - sl_pt, 0);
      tp = If(tp_pt > 0, price + tp_pt, 0);
    }

    if (type == OP_SELLLIMIT || type == OP_SELLSTOP)
    {
      sl = If(sl_pt > 0, price + sl_pt, 0);
      tp = If(tp_pt > 0, price - tp_pt, 0);
    }
    
    Print("OrderModify => price: ", DoubleToStr(price, Digits), 
      " sl: ", DoubleToStr(sl, Digits), 
      " tp: ", DoubleToStr(tp, Digits));
    
    bool ret = OrderModify(OrderTicket(), price, sl, tp, OrderExpiration(), clModify);
    
    if (!ret)
    {
      int code = GetLastError();
      if (code != ERR_NO_ERROR) Print("OrderModify failed => ", ErrorDescription(code));
    }
  }
}

double If(bool cond, double if_true, double if_false)
{
  if (cond) return (if_true);
  return (if_false);
}

Comments