This MQL5 template translates your discretionary logic into an automated algorithmic framework. Because MQL5 syntax is highly comparable to C#, the architecture is modular and decoupled.
The core mechanical rules—specifically the strict Time Stop and the M5/M1 timeframe hierarchy—are fully implemented. The subjective price action components (identifying the exact structural sweep or displacement) are compartmentalized into distinct methods, allowing you to inject your specific mathematical definitions of a sweep or BoS (Break of Structure).
The MQL5 Scalp Template
Code: Select all
//+------------------------------------------------------------------+
//| GoldScalp_Template.mq5|
//| Strict Time-Stop Architecture |
//+------------------------------------------------------------------+
#property copyright "Asymmetric Journaling Template"
#property version "1.00"
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\SymbolInfo.mqh>
//--- Inputs
input group "Risk Management"
input double InpLotSize = 0.1; // Lot Size
input int InpStopLossPts = 200; // Hard Stop Loss (in points)
input int InpTakeProfitPts = 400; // Take Profit (in points)
input group "Time Stop Mechanics"
input int InpTimeStopMins = 15; // Time Stop Window (minutes)
input group "System Parameters"
input ulong InpMagicNumber = 100100; // EA Magic Number
input string InpTradeComment = "M5_M1_Scalp"; // Trade Comment
//--- Globals
CTrade m_trade;
CPositionInfo m_position;
CSymbolInfo m_symbol;
enum ENUM_MARKET_BIAS {
BIAS_NONE = 0,
BIAS_LONG = 1,
BIAS_SHORT = -1
};
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
if(!m_symbol.Name(_Symbol)) return(INIT_FAILED);
m_trade.SetExpertMagicNumber(InpMagicNumber);
m_trade.SetMarginMode();
m_trade.SetTypeFillingBySymbol(_Symbol);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// 1. Manage existing positions (Time Stop Enforcement)
if(PositionsTotal() > 0)
{
ManageOpenPositions();
}
// 2. Scan for new entries if flat
else
{
EvaluateEntry();
}
}
//+------------------------------------------------------------------+
//| Enforces the strict time-stop exit rule |
//+------------------------------------------------------------------+
void ManageOpenPositions()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
if(m_position.SelectByIndex(i))
{
if(m_position.Symbol() == _Symbol && m_position.Magic() == InpMagicNumber)
{
datetime openTime = m_position.Time();
datetime currentTime = TimeCurrent();
// Calculate elapsed time in seconds
int elapsedSeconds = (int)(currentTime - openTime);
int timeLimitSeconds = InpTimeStopMins * 60;
if(elapsedSeconds >= timeLimitSeconds)
{
double pnl = m_position.Profit();
ulong ticket = m_position.Ticket();
// Asymmetric Journaling: Log the specific time-stop exit BEFORE closing
PrintFormat("TIME STOP TRIGGERED: Ticket #%I64u | Elapsed: %d mins | PnL: %.2f. Reason: Lack of momentum.",
ticket, elapsedSeconds/60, pnl);
m_trade.PositionClose(ticket);
}
}
}
}
}
//+------------------------------------------------------------------+
//| Coordinates the M5/M1 hierarchy for execution |
//+------------------------------------------------------------------+
void EvaluateEntry()
{
// Step 1: Establish M5 Bias (Structure & Impulse)
ENUM_MARKET_BIAS m5Bias = GetM5Bias();
if(m5Bias == BIAS_NONE)
return; // M5 is chaotic or unclear. Pass.
// Step 2: Validate M1 Entry (Retest/Reclaim)
if(m5Bias == BIAS_LONG && ValidateM1Entry(BIAS_LONG))
{
ExecuteTrade(ORDER_TYPE_BUY);
}
else if(m5Bias == BIAS_SHORT && ValidateM1Entry(BIAS_SHORT))
{
ExecuteTrade(ORDER_TYPE_SELL);
}
}
//+------------------------------------------------------------------+
//| PLACEHOLDER: Define your M5 structural sweep or displacement |
//+------------------------------------------------------------------+
ENUM_MARKET_BIAS GetM5Bias()
{
// Arrays to hold M5 price data
double close[], open[], high[], low[];
ArraySetAsSeries(close, true); ArraySetAsSeries(open, true);
ArraySetAsSeries(high, true); ArraySetAsSeries(low, true);
CopyClose(_Symbol, PERIOD_M5, 1, 3, close);
CopyOpen(_Symbol, PERIOD_M5, 1, 3, open);
CopyHigh(_Symbol, PERIOD_M5, 1, 3, high);
CopyLow(_Symbol, PERIOD_M5, 1, 3, low);
/*
INJECT PRICE ACTION LOGIC HERE
Example: Detect a strong bullish displacement (BoS) or a sweep of a key low.
If the candle body is large and closes near the high -> BIAS_LONG
*/
// Naive example condition (Replace with your structural logic)
double bodySize = MathAbs(open[0] - close[0]);
double totalSize = high[0] - low[0];
if(totalSize > 0 && bodySize/totalSize > 0.8) // Strong full-bodied candle
{
if(close[0] > open[0]) return BIAS_LONG;
if(close[0] < open[0]) return BIAS_SHORT;
}
return BIAS_NONE;
}
//+------------------------------------------------------------------+
//| PLACEHOLDER: Define your M1 execution trigger |
//+------------------------------------------------------------------+
bool ValidateM1Entry(ENUM_MARKET_BIAS bias)
{
double close[], open[];
ArraySetAsSeries(close, true); ArraySetAsSeries(open, true);
CopyClose(_Symbol, PERIOD_M1, 1, 2, close);
CopyOpen(_Symbol, PERIOD_M1, 1, 2, open);
/*
INJECT ENTRY LOGIC HERE
Example: Wait for a specific M1 pullback candle to close,
or an engulfing candle confirming the retest holds.
*/
// Naive example: Require a small pullback/pause candle on M1 to hold
if(bias == BIAS_LONG && close[0] > open[0]) return true;
if(bias == BIAS_SHORT && close[0] < open[0]) return true;
return false;
}
//+------------------------------------------------------------------+
//| Executes the trade with physical Hard Stop and TP |
//+------------------------------------------------------------------+
void ExecuteTrade(ENUM_ORDER_TYPE orderType)
{
m_symbol.RefreshRates();
double price = (orderType == ORDER_TYPE_BUY) ? m_symbol.Ask() : m_symbol.Bid();
double sl = 0.0, tp = 0.0;
if(orderType == ORDER_TYPE_BUY)
{
sl = price - (InpStopLossPts * m_symbol.Point());
tp = price + (InpTakeProfitPts * m_symbol.Point());
}
else
{
sl = price + (InpStopLossPts * m_symbol.Point());
tp = price - (InpTakeProfitPts * m_symbol.Point());
}
if(m_trade.PositionOpen(_Symbol, orderType, InpLotSize, price, sl, tp, InpTradeComment))
{
PrintFormat("ENTRY EXECUTED: %s at %.3f. Time Stop initialized for %d mins.",
EnumToString(orderType), price, InpTimeStopMins);
}
}
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.