| //+------------------------------------------------------------------+
|
| //| Supertrend_200EMA_Strategy.mq5 |
|
| //| Copyright 2026, AutoTrader EA |
|
| //| https://www.mql5.com |
|
| //+------------------------------------------------------------------>
|
| #property copyright "Copyright 2026"
|
| #property link "https://www.mql5.com"
|
| #property version "1.00"
|
|
|
| //--- Include Trade library
|
| #include <Trade\Trade.mqh>
|
| CTrade trade;
|
|
|
| //--- Input Parameters
|
| input group "=== Strategy Parameters ==="
|
| input int InpSupertrendATR = 10; // Supertrend ATR Length
|
| input double InpSupertrendFactor = 5.0; // Supertrend Factor
|
| input int InpEMAPeriod = 200; // EMA Period (1-min)
|
| input double InpRiskRewardRatio = 2.0; // Risk to Reward Ratio (1:2)
|
| input double InpLotSize = 0.1; // Lot Size
|
| input ulong InpMagicNumber = 123456; // Magic Number
|
|
|
| //--- Global Variables
|
| int hEMA1Min;
|
| int hSupertrend5Min;
|
| datetime lastBarTime1Min;
|
| datetime lastBarTime5Min;
|
|
|
| //+------------------------------------------------------------------+
|
| //| Expert initialization function |
|
| //+------------------------------------------------------------------+
|
| int OnInit()
|
| {
|
| trade.SetExpertMagicNumber(InpMagicNumber);
|
|
|
| // Create 200 EMA handle for 1-Minute timeframe
|
| hEMA1Min = iMA(_Symbol, PERIOD_M1, InpEMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
|
| if(hEMA1Min == INVALID_HANDLE)
|
| {
|
| Print("Error creating 200 EMA handle.");
|
| return(INIT_FAILED);
|
| }
|
|
|
| // Note: Standard Supertrend indicator handle setup.
|
| // Ensure your MT5 has a custom Supertrend indicator or use built-in equivalent.
|
| hSupertrend5Min = iCustom(_Symbol, PERIOD_M5, "Examples\\Supertrend", InpSupertrendATR, InpSupertrendFactor);
|
| // If you use a custom Supertrend indicator name, replace "Examples\\Supertrend" with your exact indicator file name.
|
|
|
| lastBarTime1Min = 0;
|
| lastBarTime5Min = 0;
|
|
|
| Print("EA Initialized Successfully.");
|
| return(INIT_SUCCEEDED);
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| //| Expert deinitialization function |
|
| //+------------------------------------------------------------------+
|
| void OnDeinit(const int reason)
|
| {
|
| IndicatorRelease(hEMA1Min);
|
| IndicatorRelease(hSupertrend5Min);
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| //| Expert tick function |
|
| //+------------------------------------------------------------------+
|
| void OnTick()
|
| {
|
| // Check if an open position already exists (Only one trade at a time rule)
|
| if(PositionsTotal() > 0) return;
|
|
|
| // Check for a new 1-minute bar to avoid multiple triggers on the same candle
|
| datetime currentBarTime1Min = iTime(_Symbol, PERIOD_M1, 0);
|
| if(currentBarTime1Min == lastBarTime1Min) return;
|
|
|
| // --- Get Indicator Values ---
|
| double emaVal[], closeVal1[], closeVal2[];
|
| ArraySetAsSeries(emaVal, true);
|
| ArraySetAsSeries(closeVal1, true);
|
| ArraySetAsSeries(closeVal2, true);
|
|
|
| if(CopyBuffer(hEMA1Min, 0, 0, 3, emaVal) <= 0) return;
|
| if(CopyClose(_Symbol, PERIOD_M1, 0, 3, closeVal1) <= 0) return;
|
|
|
| // Get Supertrend direction on 5-Minute timeframe
|
| // (Assuming buffer convention: Buffer returns trend direction or price line. Usually buffer 0 or line check)
|
| double supertrendVal[];
|
| ArraySetAsSeries(supertrendVal, true);
|
| if(CopyBuffer(hSupertrend5Min, 0, 0, 2, supertrendVal) <= 0) return;
|
|
|
| // Let's assume Supertrend value compared with close price or specific buffer defines Bullish/Bearish.
|
| // Simplified trend condition: If Close > Supertrend value -> Bullish, else Bearish.
|
| double close5Min = iClose(_Symbol, PERIOD_M5, 0);
|
| bool isSupertrendBullish = (close5Min > supertrendVal[0]);
|
| bool isSupertrendBearish = (close5Min < supertrendVal[0]);
|
|
|
| // --- Crossover Logic on 1-Min Chart ---
|
| // Bullish Crossover: Previous close below EMA, Current close above EMA
|
| bool crossedAbove = (closeVal1[1] <= emaVal[1] && closeVal1[0] > emaVal[0]);
|
|
|
| // Bearish Crossover: Previous close above EMA, Current close below EMA
|
| bool crossedBelow = (closeVal1[1] >= emaVal[1] && closeVal1[0] < emaVal[0]);
|
|
|
| // --- Execution Logic ---
|
| double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
|
| double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
|
| double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
|
|
|
| // 1. Bearish Condition (Short Trade)
|
| if(isSupertrendBearish && crossedBelow)
|
| {
|
| // Find recent swing high for Stop Loss calculation
|
| double stopLoss = iHigh(_Symbol, PERIOD_M1, iHighest(_Symbol, PERIOD_M1, MODE_HIGH, 5, 1));
|
| double risk = stopLoss - bid;
|
| double takeProfit = bid - (risk * InpRiskRewardRatio);
|
|
|
| trade.Sell(InpLotSize, _Symbol, bid, stopLoss, takeProfit, "Supertrend Bearish Short");
|
| lastBarTime1Min = currentBarTime1Min;
|
| }
|
|
|
| // 2. Bullish Condition (Long Trade)
|
| else if(isSupertrendBullish && crossedAbove)
|
| {
|
| // Find recent swing low for Stop Loss calculation
|
| double stopLoss = iLow(_Symbol, PERIOD_M1, iLowest(_Symbol, PERIOD_M1, MODE_LOW, 5, 1));
|
| double risk = ask - stopLoss;
|
| double takeProfit = ask + (risk * InpRiskRewardRatio);
|
|
|
| trade.Buy(InpLotSize, _Symbol, ask, stopLoss, takeProfit, "Supertrend Bullish Long");
|
| lastBarTime1Min = currentBarTime1Min;
|
| }
|
| }
|
| //+------------------------------------------------------------------+ |