Re: Fractals and Swing Points, Defining Highs and Lows Properly
Posted: Tue Oct 20, 2026 7:51 am
MQL5 Source Code
Copy and paste this code into the MetaEditor in MetaTrader 5 (press F4 in MT5 to open it), and compile it.
Copy and paste this code into the MetaEditor in MetaTrader 5 (press F4 in MT5 to open it), and compile it.
Code: Select all
//+------------------------------------------------------------------+
//| EMA_Crossover_EA.mq5|
//| |
//+------------------------------------------------------------------+
#property copyright "AI Assistant"
#property version "1.00"
// Include the standard trade library for order execution
#include <Trade\Trade.mqh>
//--- Input Parameters (Adjustable from the MT5 interface)
input int FastEMA_Period = 10; // Fast EMA Period
input int SlowEMA_Period = 50; // Slow EMA Period
input double LotSize = 0.1; // Trade Volume
input int StopLossPoints = 500; // Stop Loss in Points (500 pts = 50 pips on 5-digit broker)
input int TakeProfitPts = 1000; // Take Profit in Points (1000 pts = 100 pips)
input ulong MagicNumber = 123456; // EA Identification Number
//--- Global Variables
CTrade trade; // Trade object for execution
int fastEmaHandle; // Handle for the fast EMA indicator
int slowEmaHandle; // Handle for the slow EMA indicator
double fastEmaBuffer[]; // Array to store Fast EMA values
double slowEmaBuffer[]; // Array to store Slow EMA values
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Set the Magic Number to identify trades opened by this EA
trade.SetExpertMagicNumber(MagicNumber);
// Initialize the indicator handles
fastEmaHandle = iMA(_Symbol, _Period, FastEMA_Period, 0, MODE_EMA, PRICE_CLOSE);
slowEmaHandle = iMA(_Symbol, _Period, SlowEMA_Period, 0, MODE_EMA, PRICE_CLOSE);
// Check if indicators loaded successfully
if(fastEmaHandle == INVALID_HANDLE || slowEmaHandle == INVALID_HANDLE)
{
Print("Error creating EMA indicators");
return(INIT_FAILED);
}
// Set arrays as series (Index 0 is the current candle, 1 is previous, etc.)
ArraySetAsSeries(fastEmaBuffer, true);
ArraySetAsSeries(slowEmaBuffer, true);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Release indicator handles from memory when EA is removed
IndicatorRelease(fastEmaHandle);
IndicatorRelease(slowEmaHandle);
}
//+------------------------------------------------------------------+
//| Expert tick function (Executed on every price movement) |
//+------------------------------------------------------------------+
void OnTick()
{
// 1. Check if we already have an open position for this EA
if(CountOpenPositions() > 0)
return;
// 2. Copy the latest indicator values into our arrays
// We copy 3 candles (Index 0, 1, and 2)
if(CopyBuffer(fastEmaHandle, 0, 0, 3, fastEmaBuffer) <= 0) return;
if(CopyBuffer(slowEmaHandle, 0, 0, 3, slowEmaBuffer) <= 0) return;
// 3. Define pricing variables
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
// 4. BUY LOGIC: Fast EMA crossed ABOVE Slow EMA
// We check candle [1] (closed) and candle [2] (previous) to confirm a completed crossover
if(fastEmaBuffer[1] > slowEmaBuffer[1] && fastEmaBuffer[2] <= slowEmaBuffer[2])
{
double sl = ask - (StopLossPoints * point);
double tp = ask + (TakeProfitPts * point);
trade.Buy(LotSize, _Symbol, ask, sl, tp, "EMA Crossover BUY");
Print("Buy Order Sent. SL: ", sl, " TP: ", tp);
}
// 5. SELL LOGIC: Fast EMA crossed BELOW Slow EMA
else if(fastEmaBuffer[1] < slowEmaBuffer[1] && fastEmaBuffer[2] >= slowEmaBuffer[2])
{
double sl = bid + (StopLossPoints * point);
double tp = bid - (TakeProfitPts * point);
trade.Sell(LotSize, _Symbol, bid, sl, tp, "EMA Crossover SELL");
Print("Sell Order Sent. SL: ", sl, " TP: ", tp);
}
}
//+------------------------------------------------------------------+
//| Helper Function: Count Open Positions for this EA |
//+------------------------------------------------------------------+
int CountOpenPositions()
{
int count = 0;
// Loop through all open positions on the account
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
// Only count positions that match this chart's symbol AND this EA's magic number
if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == MagicNumber)
{
count++;
}
}
return count;
}
//+------------------------------------------------------------------+