threeema_v1
Download scriptThis automated trading system, titled "ThreeEMA," is designed to manage and execute trades on the MetaTrader 5 platform using a strategy based on moving averages. By analyzing price trends through three distinct timeframes, the script identifies specific market conditions to enter, exit, and manage positions automatically.
Core Trading Logic
The primary logic of this system revolves around the "Three EMA" (Exponential Moving Average) method. The software tracks three different types of moving averages simultaneously:
- Fast Period: Reacts quickly to recent price changes.
- Medium Period: Provides a balanced view of the current trend.
- Slow Period: Tracks long-term price momentum.
By comparing these three indicators, the system can determine the direction and strength of the market. When the relationship between these averages meets specific criteria, the script triggers a buy or sell signal.
Risk Management and Trade Execution
To ensure disciplined trading, the script incorporates several automated safety features:
- Fixed Profit and Loss Targets: Every trade is programmed with a predetermined "Stop Loss" (to limit potential losses) and a "Take Profit" (to secure gains automatically). This removes the emotional aspect of deciding when to close a trade.
- Money Management: The system uses a fixed lot size approach. Users can define the specific volume of each trade, ensuring that the amount of capital risked per transaction remains consistent and predictable.
- No Trailing Stop: The logic is configured to hold positions until the designated profit or loss targets are met, rather than moving the stop-loss point dynamically as the price fluctuates.
Technical Workflow
When the script is activated, it performs a series of initialization checks to ensure all components—the trading signals, the money management rules, and the mathematical indicators—are correctly loaded and validated. Once active, the system monitors every price "tick" (the constant stream of market data), processing this information in real-time to execute trades based on the predefined three-average logic.
//+------------------------------------------------------------------+
//| ThreeEMA.mq5 |
//| Copyright 2010, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
#include <Expert\Expert.mqh>
#include <Expert\Signal\Signal3EMA.mqh>
#include <Expert\Trailing\TrailingNone.mqh>
#include <Expert\Money\MoneyFixedLot.mqh>
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
//--- inputs for expert
input string Inp_Expert_Title ="ThreeEMA";
int Expert_MagicNumber =31286;
bool Expert_EveryTick =false;
//--- inputs for signal
input int Inp_Signal_ThreeEMA_FastPeriod =5;
input int Inp_Signal_ThreeEMA_MediumPeriod=12;
input int Inp_Signal_ThreeEMA_SlowPeriod =24;
input int Inp_Signal_ThreeEMA_StopLoss =400;
input int Inp_Signal_ThreeEMA_TakeProfit =900;
//--- inputs for money
input double Inp_Money_FixLot_Percent =10.0;
input double Inp_Money_FixLot_Lots =0.1;
//+------------------------------------------------------------------+
//| 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);
}
//--- Creation of signal object
CSignal3EMA *signal=new CSignal3EMA;
if(signal==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating signal");
ExtExpert.Deinit();
return(-2);
}
//--- Add signal to expert (will be deleted automatically))
if(!ExtExpert.InitSignal(signal))
{
//--- failed
printf(__FUNCTION__+": error initializing signal");
ExtExpert.Deinit();
return(-3);
}
//--- Set signal parameters
signal.FastPeriod(Inp_Signal_ThreeEMA_FastPeriod);
signal.MediumPeriod(Inp_Signal_ThreeEMA_MediumPeriod);
signal.SlowPeriod(Inp_Signal_ThreeEMA_SlowPeriod);
signal.StopLoss(Inp_Signal_ThreeEMA_StopLoss);
signal.TakeProfit(Inp_Signal_ThreeEMA_TakeProfit);
//--- Check signal parameters
if(!signal.ValidationSettings())
{
//--- failed
printf(__FUNCTION__+": error signal parameters");
ExtExpert.Deinit();
return(-4);
}
//--- Creation of trailing object
CTrailingNone *trailing=new CTrailingNone;
if(trailing==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating trailing");
ExtExpert.Deinit();
return(-5);
}
//--- Add trailing to expert (will be deleted automatically))
if(!ExtExpert.InitTrailing(trailing))
{
//--- failed
printf(__FUNCTION__+": error initializing trailing");
ExtExpert.Deinit();
return(-6);
}
//--- Set trailing parameters
//--- Check trailing parameters
if(!trailing.ValidationSettings())
{
//--- failed
printf(__FUNCTION__+": error trailing parameters");
ExtExpert.Deinit();
return(-7);
}
//--- Creation of money object
CMoneyFixedLot *money=new CMoneyFixedLot;
if(money==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating money");
ExtExpert.Deinit();
return(-8);
}
//--- Add money to expert (will be deleted automatically))
if(!ExtExpert.InitMoney(money))
{
//--- failed
printf(__FUNCTION__+": error initializing money");
ExtExpert.Deinit();
return(-9);
}
//--- Set money parameters
money.Percent(Inp_Money_FixLot_Percent);
money.Lots(Inp_Money_FixLot_Lots);
//--- Check money parameters
if(!money.ValidationSettings())
{
//--- failed
printf(__FUNCTION__+": error money parameters");
ExtExpert.Deinit();
return(-10);
}
//--- Tuning of all necessary indicators
if(!ExtExpert.InitIndicators())
{
//--- failed
printf(__FUNCTION__+": error initializing indicators");
ExtExpert.Deinit();
return(-11);
}
//--- ok
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Deinitialization function of the expert |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ExtExpert.Deinit();
}
//+------------------------------------------------------------------+
//| Function-event handler "tick" |
//+------------------------------------------------------------------+
void OnTick()
{
ExtExpert.OnTick();
}
//+------------------------------------------------------------------+
//| Function-event handler "trade" |
//+------------------------------------------------------------------+
void OnTrade()
{
ExtExpert.OnTrade();
}
//+------------------------------------------------------------------+
//| Function-event handler "timer" |
//+------------------------------------------------------------------+
void OnTimer()
{
ExtExpert.OnTimer();
}
//+------------------------------------------------------------------+
Comments