Author: Copyright 2010, Alf.
Profit factor:
0.00

This script is an automated trading system designed to manage market positions using a combination of trend-following indicators and a risk management strategy known as "Martingale." Instead of relying on simple price movements, it evaluates the relationship between two different moving averages to determine the direction of the market.

Core Trading Logic

The system operates by monitoring two separate MACD (Moving Average Convergence Divergence) indicators simultaneously. These indicators act as filters to identify momentum:

  • Trend Identification: The script looks for specific patterns in these indicators to decide whether to enter a "Buy" or "Sell" position. It essentially waits for the data points to form a specific "V" or "Inverted V" shape, signaling a potential shift in market direction.
  • Execution Rules:
    • If the conditions for an upward trend are met, the system opens a Buy order.
    • If the conditions for a downward trend are met, the system opens a Sell order.

Smart Entry and Management

The script includes sophisticated logic to handle how it enters trades based on historical results:

  1. Initial Entry: If there are no currently open orders, the system scans the current indicators to find the first available entry point.
  2. Historical Awareness: The script is capable of "looking back" at its most recent completed trades. It checks whether previous trades were profitable or not. This awareness allows it to adjust its behavior based on whether the previous move was successful, helping it navigate market fluctuations more effectively.
  3. Position Sizing (Martingale): A key feature of this script is its integrated Martingale logic. This means that if a trade results in a loss, the system can automatically adjust the volume of the next trade to attempt to recover the previous loss.

Safety and Parameters

To protect the account, the script operates within predefined boundaries:

  • Take Profit (Tp): Automatically sets a target price to lock in profits.
  • Stop Loss (Stop): Automatically sets a limit to exit a trade if it moves against the position, preventing excessive losses.
  • Volume Control: It manages the size of each trade dynamically based on its internal Martingale rules and user-defined inputs.

In summary, this tool automates the process of identifying momentum shifts through dual-indicator analysis while employing a recovery strategy to manage risk and maximize the potential for profit over time.

Price Data Components
Series array that contains open prices of each bar
Orders Execution
It automatically opens orders when conditions are reached
Indicators Used
MACD Histogram
13 Views
2 Downloads
0 Favorites
promart_v1
//+------------------------------------------------------------------+
//|                                                   MartGreg_1.mq5 |
//|                                             Copyright 2010, Alf. |
//|        http://forum.liteforex.org/showthread.php?p=6210#post6210 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, Alf."
#property link      "http://forum.liteforex.org/showthread.php?p=6210#post6210"
#property version   "1.00"

#include <OnTesterFunctions.mqh>
#include <Martingail.mqh>
//--- input parameters
input double   DML=1000;
input int      Ud=1;
input int      Stop=500;
input int      Tp=1500;
input int      Slipage=50;
input int      MACD1Fast=5;
input int      MACD1Slow=20;
input int      MACD2Fast=10;
input int      MACD2Slow=15;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int m1=0;
int m2=0;


Martingail lt;
//+------------------------------------------------------------------+
//| Open                                                             |
//+------------------------------------------------------------------+
void Open()
  {
   double t[3];
   double k[2];
   int poz=0;
   if(CopyBuffer(m1,0,1,3,t)<0) return;
   if(CopyBuffer(m2,0,1,2,k)<0) return;

   MqlTick last_tick;
   SymbolInfoTick(_Symbol,last_tick);
   MqlTradeResult result;
   MqlTradeRequest request;
   ZeroMemory(result);
   ZeroMemory(request);
   
   HistorySelect(0,TimeCurrent());
   if(HistoryOrdersTotal()==0)
     {
      if(t[0]>t[1] && t[1]<t[2] && k[1]>k[0]) poz=1;
      if(t[0]<t[1] && t[1]>t[2] && k[1]<k[0]) poz=-1;
     }
   else
     {
      double cl=HistoryOrderGetDouble(HistoryOrderGetTicket(HistoryOrdersTotal()-1),ORDER_PRICE_OPEN);
      double op=HistoryOrderGetDouble(HistoryOrderGetTicket(HistoryOrdersTotal()-2),ORDER_PRICE_OPEN);
      long ot=HistoryOrderGetInteger(HistoryOrderGetTicket(HistoryOrdersTotal()-2),ORDER_TYPE);
      if(ot==ORDER_TYPE_BUY)
        {
         if(cl-op>0)
           {
            if(t[0]>t[1] && t[1]<t[2] && k[1]>k[0]) poz=1;
            if(t[0]<t[1] && t[1]>t[2] && k[1]<k[0]) poz=-1;
           }
         else poz=-1;

        }
      else
        {
         if(op-cl>0)
           {
            if(t[0]>t[1] && t[1]<t[2] && k[1]>k[0]) poz=1;
            if(t[0]<t[1] && t[1]>t[2] && k[1]<k[0]) poz=-1;
           }
         else poz=1;
        }
     }
   Print("Poz=",poz);
   request.symbol=_Symbol;
   request.magic=777;
   request.deviation=50;
   request.action=TRADE_ACTION_DEAL;
   request.type_filling=ORDER_FILLING_FOK;
   if(poz==1)
     {
      request.volume=lt.Lot();
      request.price=last_tick.ask;
      request.type=ORDER_TYPE_BUY;
      request.sl=last_tick.bid-Stop*Point();
      request.tp=last_tick.ask+Tp*Point();
      OrderSend(request,result);

     }
   if(poz==-1)
     {
      request.volume=lt.Lot();
      request.price=last_tick.bid;
      request.type=ORDER_TYPE_SELL;
      request.sl=last_tick.ask+Stop*Point();
      request.tp=last_tick.bid-Tp*Point();
      OrderSend(request,result);

     }
  }
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   m1=iMACD(_Symbol,0,MACD1Fast,MACD1Slow,3,PRICE_MEDIAN);
   m2=iMACD(_Symbol,0,MACD2Fast,MACD2Slow,3,PRICE_MEDIAN);
   lt.GVarName="PMG_1";
   lt.Shape=DML;
   lt.DoublingCount=Ud;
   lt.GVarGet();
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   lt.GVarSet();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   MqlRates rt[1];
   if(CopyRates(Symbol(),_Period,0,1,rt)<0) return;
   if(rt[0].tick_volume>1) return;
   if(!PositionSelect(_Symbol))Open();

  }
//+------------------------------------------------------------------+
//| Expert OnTester function                                         |
//+------------------------------------------------------------------+
double OnTester()
  {
   double p=profitc_divide_lossc();
   double s=max_series_loss();
   if(Tp<Stop) return 0;
   s=s+1;
   return p/s;
  }
//+------------------------------------------------------------------+

Comments