MQL5_Wizard_MACD_Parabolic_SAR

Download script
Author: Copyright © 2017, Vladimir Karputov

This automated trading script, designed for the MetaTrader 5 platform, is an Expert Advisor (EA) that combines multiple technical analysis tools to execute trades automatically. It uses a modular logic structure, meaning it pulls in various "components" to make decisions rather than relying on a single indicator.

Here is a breakdown of how the logic works in plain language:

Core Trading Mechanics

The script operates by monitoring market data and reacting to specific conditions. Its primary goal is to identify high-probability entries and exits based on three main pillars:

  • Signal Generation: It uses a "Main Signal" object that handles the basic rules for opening and closing positions. This includes defining your target profit (Take Profit), your maximum loss (Stop Loss), and the specific price levels where actions should occur.
  • Trend Filtering: To avoid trading against the market's momentum, the script applies two primary filters:
    1. MACD (Moving Average Convergence Divergence): This is used to analyze the strength and direction of a trend by comparing two moving averages. The script allows you to customize the "fast" and "slow" periods to suit different trading styles.
    2. Parabolic SAR: This is typically used to identify potential reversals in price. It acts as a secondary confirmation to ensure that the trade aligns with the current trajectory of the market.

Money Management & Safety

The script includes a dedicated money management module designed to handle your risk profile:

  • Fixed Lot Trading: It allows you to set a specific volume (lot size) for every trade.
  • Risk Percentages: You can define how much of your capital you wish to allocate to a specific trade, ensuring that the EA remains consistent with your financial goals.

Execution Flow

When you run this script, it follows a systematic "startup" sequence:

  1. Initialization: The script first checks if the trading environment is valid. If it cannot connect to the market or load its components, it will alert you and stop.
  2. Component Loading: It then builds itself by loading the MACD filter, the SAR filter, the money management rules, and the trade execution logic into a single cohesive unit.
  3. Real-Time Monitoring: Once active, it listens to every "tick" (every small price movement). It constantly compares the live price against its internal filters.
  4. Trade Action: Only when the MACD and Parabolic SAR indicators agree with the "Main Signal" requirements will the script execute a trade. It also manages "Trailing" logic, which is a way to adjust your exit points automatically as the market moves in your favor.

User Customization

The logic is highly flexible. Even though it follows a strict set of rules, it provides several "knobs" for you to turn:

  • Sensitivity: You can adjust how fast or slow the MACD and SAR react to price changes.
  • Thresholds: You can define exactly what percentage of a signal is required before the EA considers it "strong enough" to open a position.
  • Expiration: You can set rules for how long a pending order should remain active before being cancelled.
7 Views
0 Downloads
0 Favorites
MQL5_Wizard_MACD_Parabolic_SAR
ÿþ//+------------------------------------------------------------------+

//|                               MQL5 Wizard MACD Parabolic SAR.mq5 |

//|                              Copyright © 2017, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2017, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.00"

//+------------------------------------------------------------------+

//| Include                                                          |

//+------------------------------------------------------------------+

#include <Expert\Expert.mqh>

//--- available signals

#include <Expert\Signal\SignalMACD.mqh>

#include <Expert\Signal\SignalSAR.mqh>

//--- available trailing

#include <Expert\Trailing\TrailingNone.mqh>

//--- available money management

#include <Expert\Money\MoneyFixedLot.mqh>

//+------------------------------------------------------------------+

//| Inputs                                                           |

//+------------------------------------------------------------------+

//--- inputs for expert

input string             Expert_Title            ="MQL5 Wizard MACD"; // Document name

ulong                    Expert_MagicNumber      =756;                // 

bool                     Expert_EveryTick        =false;              // 

//--- inputs for main signal

input int                Signal_ThresholdOpen    =20;                 // Signal threshold value to open [0...100]

input int                Signal_ThresholdClose   =100;                // Signal threshold value to close [0...100]

input double             Signal_PriceLevel       =0.0;                // Price level to execute a deal

input double             Signal_StopLevel        =50.0;               // Stop Loss level (in points)

input double             Signal_TakeLevel        =115.0;              // Take Profit level (in points)

input int                Signal_Expiration       =5;                  // Expiration of pending orders (in bars)

input int                Signal_MACD_PeriodFast  =12;                 // MACD(12,24,9,PRICE_CLOSE) Period of fast EMA

input int                Signal_MACD_PeriodSlow  =24;                 // MACD(12,24,9,PRICE_CLOSE) Period of slow EMA

input int                Signal_MACD_PeriodSignal=9;                  // MACD(12,24,9,PRICE_CLOSE) Period of averaging of difference

input ENUM_APPLIED_PRICE Signal_MACD_Applied     =PRICE_CLOSE;        // MACD(12,24,9,PRICE_CLOSE) Prices series

input double             Signal_MACD_Weight      =0.9;                // MACD(12,24,9,PRICE_CLOSE) Weight [0...1.0]

input double             Signal_SAR_Step         =0.02;               // Parabolic SAR(0.02,0.2) Speed increment

input double             Signal_SAR_Maximum      =0.2;                // Parabolic SAR(0.02,0.2) Maximum rate

input double             Signal_SAR_Weight       =0.1;                // Parabolic SAR(0.02,0.2) Weight [0...1.0]

//--- inputs for money

input double             Money_FixLot_Percent    =10.0;               // Percent

input double             Money_FixLot_Lots       =1;                // Fixed volume

//+------------------------------------------------------------------+

//| Global expert object                                             |

//+------------------------------------------------------------------+

CExpert ExtExpert;

//+------------------------------------------------------------------+

//| Initialization function of the expert                            |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- Initializing expert

   if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))

     {

      //--- failed

      printf(__FUNCTION__+": error initializing expert");

      ExtExpert.Deinit();

      return(INIT_FAILED);

     }

//--- Creating signal

   CExpertSignal *signal=new CExpertSignal;

   if(signal==NULL)

     {

      //--- failed

      printf(__FUNCTION__+": error creating signal");

      ExtExpert.Deinit();

      return(INIT_FAILED);

     }

//---

   ExtExpert.InitSignal(signal);

   signal.ThresholdOpen(Signal_ThresholdOpen);

   signal.ThresholdClose(Signal_ThresholdClose);

   signal.PriceLevel(Signal_PriceLevel);

   signal.StopLevel(Signal_StopLevel);

   signal.TakeLevel(Signal_TakeLevel);

   signal.Expiration(Signal_Expiration);

//--- Creating filter CSignalMACD

   CSignalMACD *filter0=new CSignalMACD;

   if(filter0==NULL)

     {

      //--- failed

      printf(__FUNCTION__+": error creating filter0");

      ExtExpert.Deinit();

      return(INIT_FAILED);

     }

   signal.AddFilter(filter0);

//--- Set filter parameters

   filter0.PeriodFast(Signal_MACD_PeriodFast);

   filter0.PeriodSlow(Signal_MACD_PeriodSlow);

   filter0.PeriodSignal(Signal_MACD_PeriodSignal);

   filter0.Applied(Signal_MACD_Applied);

   filter0.Weight(Signal_MACD_Weight);

//--- Creating filter CSignalSAR

   CSignalSAR *filter1=new CSignalSAR;

   if(filter1==NULL)

     {

      //--- failed

      printf(__FUNCTION__+": error creating filter1");

      ExtExpert.Deinit();

      return(INIT_FAILED);

     }

   signal.AddFilter(filter1);

//--- Set filter parameters

   filter1.Step(Signal_SAR_Step);

   filter1.Maximum(Signal_SAR_Maximum);

   filter1.Weight(Signal_SAR_Weight);

//--- Creation of trailing object

   CTrailingNone *trailing=new CTrailingNone;

   if(trailing==NULL)

     {

      //--- failed

      printf(__FUNCTION__+": error creating trailing");

      ExtExpert.Deinit();

      return(INIT_FAILED);

     }

//--- Add trailing to expert (will be deleted automatically))

   if(!ExtExpert.InitTrailing(trailing))

     {

      //--- failed

      printf(__FUNCTION__+": error initializing trailing");

      ExtExpert.Deinit();

      return(INIT_FAILED);

     }

//--- Set trailing parameters

//--- Creation of money object

   CMoneyFixedLot *money=new CMoneyFixedLot;

   if(money==NULL)

     {

      //--- failed

      printf(__FUNCTION__+": error creating money");

      ExtExpert.Deinit();

      return(INIT_FAILED);

     }

//--- Add money to expert (will be deleted automatically))

   if(!ExtExpert.InitMoney(money))

     {

      //--- failed

      printf(__FUNCTION__+": error initializing money");

      ExtExpert.Deinit();

      return(INIT_FAILED);

     }

//--- Set money parameters

   money.Percent(Money_FixLot_Percent);

   money.Lots(Money_FixLot_Lots);

//--- Check all trading objects parameters

   if(!ExtExpert.ValidationSettings())

     {

      //--- failed

      ExtExpert.Deinit();

      return(INIT_FAILED);

     }

//--- Tuning of all necessary indicators

   if(!ExtExpert.InitIndicators())

     {

      //--- failed

      printf(__FUNCTION__+": error initializing indicators");

      ExtExpert.Deinit();

      return(INIT_FAILED);

     }

//--- ok

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Deinitialization function of the expert                          |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   ExtExpert.Deinit();

  }

//+------------------------------------------------------------------+

//| "Tick" event handler function                                    |

//+------------------------------------------------------------------+

void OnTick()

  {

   ExtExpert.OnTick();

  }

//+------------------------------------------------------------------+

//| "Trade" event handler function                                   |

//+------------------------------------------------------------------+

void OnTrade()

  {

   ExtExpert.OnTrade();

  }

//+------------------------------------------------------------------+

//| "Timer" event handler function                                   |

//+------------------------------------------------------------------+

void OnTimer()

  {

   ExtExpert.OnTimer();

  }

//+------------------------------------------------------------------+

Comments