ExpM_Laguerre
Download scriptThis automated trading script, titled ExpM_Laguerre, is designed to function as an autonomous trading robot for the MetaTrader 5 platform. It utilizes a sophisticated mathematical filtering system to identify market trends and execute trades based on specific price actions and volatility levels.
Core Trading Logic
The primary intelligence of this script relies on a specialized signal filter called the Laguerre Signal. Rather than looking at simple price movements in isolation, it analyzes data through several custom parameters:
- Trend Sensitivity (Gamma): The robot uses a smoothing ratio to determine how aggressively it should react to market fluctuations.
- Level Thresholds: The logic categorizes market momentum into three distinct zones—High, Middle, and Low. By defining these specific boundaries, the script can distinguish between a strong trending move and a period of stagnation.
- Timeframe Analysis: The system is configured to evaluate signals on a specific timeframe (defaulting to the H4 chart), allowing it to ignore minor "noise" and focus on more significant price movements.
Execution Strategy
The script follows a structured protocol for entering and exiting the market:
- Entry Conditions: For a trade to be opened, the Laguerre filter must meet specific "Open Thresholds." You can customize these values to dictate how strong a signal must be before the robot commits capital. The script also includes dedicated permissions to independently decide when it is appropriate to buy, sell, or close existing positions.
- Risk Management: Every trade is governed by predefined safety nets:
- Stop Loss: Automatically limits potential losses to a specific number of points.
- Take Profit: Sets the target for closing a winning trade at a predetermined profit level.
- Expiration: Includes an optional "expiration" feature for pending orders, ensuring that if a price doesn't move as expected within a certain timeframe, the order is cleared.
- Money Management: The script features a dual-layer approach to position sizing. It can manage risk based on a fixed percentage of your account balance or by utilizing a strictly fixed lot size for every transaction.
Operational Behavior
The robot is built to be "set and forget." Once initialized, it constantly monitors the live market feed (the "Tick" event). It automatically validates all settings upon startup to ensure they are compatible with your trading environment. If the market conditions meet the mathematical criteria established by the Laguerre filter and your personal risk parameters, the robot will execute the trade, manage the profit/loss levels, and move on to the next opportunity.
//+------------------------------------------------------------------+
//| ExpM_Laguerre.mq5 |
//| Copyright 2011, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
#include <Expert\Expert.mqh>
//--- available signals
#include <Expert\Signal\MySignals\LaguerreSignal.mqh>
//--- available trailing
#include <Expert\Trailing\TrailingNone.mqh>
//--- available money management
#include <Expert\Money\MoneyFixedLot.mqh>
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
//--- inputs for expert
input string Expert_Title ="ExpM_Laguerre"; // Document name
ulong Expert_MagicNumber =1619; //
bool Expert_EveryTick =false; //
//--- inputs for main signal
input int Signal_ThresholdOpen =40; // Signal threshold value to open [0...100]
input int Signal_ThresholdClose=20; // 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 =50.0; // Take Profit level (in points)
input int Signal_Expiration =1; // Expiration of pending orders (in bars)
input bool Signal__BuyPosOpen =true; // ColorLaguerre() Permission to buy
input bool Signal__SellPosOpen =true; // ColorLaguerre() Permission to sell
input bool Signal__BuyPosClose =true; // ColorLaguerre() Permission to exit a long position
input bool Signal__SellPosClose =true; // ColorLaguerre() Permission to exit a short position
input ENUM_TIMEFRAMES Signal__Ind_Timeframe=PERIOD_H4; // ColorLaguerre() Timeframe
input double Signal__gamma =0.7; // ColorLaguerre() Smoothing ratio
input uint Signal__HighLevel =85; // ColorLaguerre() maximum level
input uint Signal__MiddleLevel =50; // ColorLaguerre() middle level
input uint Signal__LowLevel =15; // ColorLaguerre() minimum level
input uint Signal__SignalBar =1; // ColorLaguerre() Bar index for entry signal
input double Signal__Weight =1.0; // ColorLaguerre() Weight [0...1.0]
//--- inputs for money
input double Money_FixLot_Percent =10.0; // Percent
input double Money_FixLot_Lots =0.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(-1);
}
//--- Creating signal
CExpertSignal *signal=new CExpertSignal;
if(signal==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating signal");
ExtExpert.Deinit();
return(-2);
}
//---
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 CLaguerreSignal
CLaguerreSignal *filter0=new CLaguerreSignal;
if(filter0==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating filter0");
ExtExpert.Deinit();
return(-3);
}
signal.AddFilter(filter0);
//--- Set filter parameters
filter0.BuyPosOpen(Signal__BuyPosOpen);
filter0.SellPosOpen(Signal__SellPosOpen);
filter0.BuyPosClose(Signal__BuyPosClose);
filter0.SellPosClose(Signal__SellPosClose);
filter0.Ind_Timeframe(Signal__Ind_Timeframe);
filter0.gamma(Signal__gamma);
filter0.HighLevel(Signal__HighLevel);
filter0.MiddleLevel(Signal__MiddleLevel);
filter0.LowLevel(Signal__LowLevel);
filter0.SignalBar(Signal__SignalBar);
filter0.Weight(Signal__Weight);
//--- Creation of trailing object
CTrailingNone *trailing=new CTrailingNone;
if(trailing==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating trailing");
ExtExpert.Deinit();
return(-4);
}
//--- Add trailing to expert (will be deleted automatically))
if(!ExtExpert.InitTrailing(trailing))
{
//--- failed
printf(__FUNCTION__+": error initializing trailing");
ExtExpert.Deinit();
return(-5);
}
//--- Set trailing parameters
//--- Creation of money object
CMoneyFixedLot *money=new CMoneyFixedLot;
if(money==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating money");
ExtExpert.Deinit();
return(-6);
}
//--- Add money to expert (will be deleted automatically))
if(!ExtExpert.InitMoney(money))
{
//--- failed
printf(__FUNCTION__+": error initializing money");
ExtExpert.Deinit();
return(-7);
}
//--- 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(-8);
}
//--- Tuning of all necessary indicators
if(!ExtExpert.InitIndicators())
{
//--- failed
printf(__FUNCTION__+": error initializing indicators");
ExtExpert.Deinit();
return(-9);
}
//--- ok
return(0);
}
//+------------------------------------------------------------------+
//| 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