Summary: This EA trades Gold and Forex pairs with low risk using ATR stop loss and dual moving average trend filter. It avoids over-trading and includes hard stop-loss and take-profit levels.




This EA is designed for Gold (XAUUSD) and major Forex pairs. It uses a dual moving average crossover with ATR-based dynamic stop loss and take profit. The logic requires trend confirmation (fast MA above slow MA for long, below for short) and a minimum ATR filter to avoid low volatility periods. Only one position at a time is allowed. Hard SL/TP levels protect against market gaps. Compile with MQL4 build 600 or higher. Reference: Based on classic trend-following and volatility management techniques.

```mql4
//+------------------------------------------------------------------+
//| LowRiskGoldEA.mq4 |
//| Generated by Professional Developer |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, LowRisk Trader"
#property link ""
#property version "1.00"
#property strict

//+------------------------------------------------------------------+
//| Input parameters with comments |
//+------------------------------------------------------------------+
input double LotSize = 0.01; // Fixed lot size (min 0.01 for micro accounts)
input int FastMAPeriod = 5; // Fast moving average period
input int SlowMAPeriod = 20; // Slow moving average period
input int ATRPeriod = 14; // ATR period for volatility calculation
input double ATRMultiplierSL = 2.0; // ATR multiplier for Stop Loss (e.g., 2.0 means SL = 2 * ATR)
input double ATRMultiplierTP = 3.0; // ATR multiplier for Take Profit (must be > ATRMultiplierSL)
input int MinATRValue = 150; // Minimum ATR value in points (filter low volatility, e.g., 150 for Gold)
input int MagicNumber = 202501; // Unique EA identifier for orders
input int Slippage = 30; // Allowed slippage in points
input bool TradeLong = true; // Allow long positions
input bool TradeShort = true; // Allow short positions

// Global variables
double atrValue;
int ticket;
bool isOrderOpened;

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
if(FastMAPeriod >= SlowMAPeriod)
{
Print("Error: Fast MAPeriod must be less than Slow MAPeriod. EA stopped.");
return(INIT_PARAMETERS_INCORRECT);
}
if(ATRMultiplierTP <= ATRMultiplierSL)
{
Print("Error: TP multiplier must be greater than SL multiplier. EA stopped.");
return(INIT_PARAMETERS_INCORRECT);
}
isOrderOpened = false;
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment(""); // Clear chart comment
}

//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check if we already have an open position for this EA
isOrderOpened = IsExistingOrder();
if(isOrderOpened)
return;

// Calculate ATR and filter low volatility
atrValue = iATR(Symbol(), PERIOD_CURRENT, ATRPeriod, 1);
if(atrValue < MinATRValue)
return; // Market too quiet, do not trade

// Get moving average values
double fastMA = iMA(Symbol(), PERIOD_CURRENT, FastMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);
double slowMA = iMA(Symbol(), PERIOD_CURRENT, SlowMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);

// Trading signals
bool buySignal = (fastMA > slowMA) && TradeLong;
bool sellSignal = (fastMA < slowMA) && TradeShort;

if(buySignal)
{
double sl = NormalizeDouble(Ask - (atrValue * ATRMultiplierSL), Digits);
double tp = NormalizeDouble(Ask + (atrValue * ATRMultiplierTP), Digits);
sl = NormalizeDouble(MathMax(sl, MarketInfo(Symbol(), MODE_STOPLEVEL)), Digits);
tp = NormalizeDouble(MathMax(tp, MarketInfo(Symbol(), MODE_STOPLEVEL)), Digits);
ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, sl, tp, "LowRiskGold EA", MagicNumber, 0, clrGreen);
if(ticket < 0) Print("Buy order failed: ", GetLastError());
}
else if(sellSignal)
{
double sl = NormalizeDouble(Bid + (atrValue * ATRMultiplierSL), Digits);
double tp = NormalizeDouble(Bid - (atrValue * ATRMultiplierTP), Digits);
sl = NormalizeDouble(MathMin(sl, Bid - MarketInfo(Symbol(), MODE_STOPLEVEL)), Digits);
tp = NormalizeDouble(MathMin(tp, Bid - MarketInfo(Symbol(), MODE_STOPLEVEL)), Digits);
ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, Slippage, sl, tp, "LowRiskGold EA", MagicNumber, 0, clrRed);
if(ticket < 0) Print("Sell order failed: ", GetLastError());
}
}

//+------------------------------------------------------------------+
//| Check if an order with MagicNumber already exists |
//+------------------------------------------------------------------+
bool IsExistingOrder()
{
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
return true;
}
}
return false;
}
//+------------------------------------------------------------------+
```

Loading Period Recommendation: Apply the EA to H1 (1-hour) chart for Gold (XAUUSD) or M30 for major Forex pairs. Higher timeframe reduces market noise.

Trading Logic Summary: The EA uses dual SMA crossover (fast 5-period over slow 20-period) to determine trend direction. ATR (14-period) calculates market volatility to set dynamic stop loss and take profit, ensuring risk is proportional to volatility. A minimum ATR filter prevents trading during extremely quiet markets. Only one trade at a time, with hard SL/TP levels.

*Reference: Adapted from standard moving average crossover and ATR stop methods used in institutional algorithmic trading.*

Disclaimer: This EA is for educational purposes only. Past performance does not guarantee future results. Test thoroughly on demo account before live trading. The author assumes no responsibility for financial losses.