// Expert Advisor (EA) with combined strategies, Martingale, name in a separate sub-window, manual button, and scalp mode toggle button input int smallerTimeframe = 5; // Smaller timeframe (in minutes) input int largerTimeframe = 60; // Larger timeframe (in minutes) input int trendlinePeriods = 10; // Number of periods to consider for trendline input double threshold = 0.001; // Threshold for determining the trend direction input double trailingStop = 20; // Trailing stop distance in pips input double takeProfit = 50; // Take-profit distance in pips input double pullbackThreshold = 0.002; // Pullback threshold as a percentage input int atrPeriod = 14; // ATR period for volatility calculation input double volatilityThreshold = 1.0; // Volatility threshold for avoiding trades input int maPeriod = 20; // Moving average period input double overboughtLevel = 70; // Overbought level input double oversoldLevel = 30; // Oversold level input int MagicNumber = 12345; // Unique identifier for orders input bool useMartingale = false; // Enable Martingale input double martingaleFactor = 2.0; // Martingale lot size multiplier input bool volatilityBased = true; // Enable volatility-based strategy input int maxPositions = 5; // Maximum number of open positions in high volatility double lotSize = LotsOptimized(); // Initial lot size bool manualMode = true; // Manual mode switch bool scalpMode = false; // Scalp mode switch // Expert Advisor start function void OnTick() { // Check if manual mode is enabled if (manualMode) { // Check for a pullback on the smaller timeframe if (CheckPullback(smallerTimeframe, trendline, pullbackThreshold) || scalpMode) { // Check for overbought and oversold conditions using Moving Average double maValue = iMA(_Symbol, smallerTimeframe, maPeriod, 0, MODE_SMA, PRICE_CLOSE, 0); // Check volatility double atrValue = iATR(_Symbol, smallerTimeframe, atrPeriod, 0); int totalPositions = PositionsHistoryTotal() + OrdersHistoryTotal(); // Determine the lot size based on Martingale strategy double currentLotSize = useMartingale ? lotSize * MathPow(martingaleFactor, OrderHistoryTotal()) : lotSize; // Enable volatility-based strategy if (volatilityBased && atrValue > volatilityThreshold && totalPositions < maxPositions) { Print("High Volatility Detected. Opening additional positions."); // Open additional positions for (int i = 0; i < maxPositions - totalPositions; i++) { if (iClose(_Symbol, smallerTimeframe, i) > maValue) { Print("Buy Trade Opened (Volatility)"); OpenTrade(_Symbol, OP_BUY, currentLotSize, Ask, 3, 0, 0, "Trade", 0, Blue); } else if (iClose(_Symbol, smallerTimeframe, i) < maValue) { Print("Sell Trade Opened (Volatility)"); OpenTrade(_Symbol, OP_SELL, currentLotSize, Bid, 3, 0, 0, "Trade", 0, Red); } } } // Original logic for pullback trades else { // If price is below MA and below oversold level, consider buying if (iClose(_Symbol, smallerTimeframe, 0) < maValue && iClose(_Symbol, smallerTimeframe, 0) < oversoldLevel) { Print("Buy Trade Opened (Pullback)"); OpenTrade(_Symbol, OP_BUY, currentLotSize, Ask, 3, 0, 0, "Trade", 0, Blue); } // If price is above MA and above overbought level, consider selling else if (iClose(_Symbol, smallerTimeframe, 0) > maValue && iClose(_Symbol, smallerTimeframe, 0) > overboughtLevel) { Print("Sell Trade Opened (Pullback)"); OpenTrade(_Symbol, OP_SELL, currentLotSize, Bid, 3, 0, 0, "Trade", 0, Red); } } } } // Update the profit/loss information window UpdateInfoWindow(); } // Function to check for a pullback bool CheckPullback(int timeframe, double trendline, double threshold) { double smallerClose = iClose(_Symbol, timeframe, 0); // Calculate the distance from the trendline double distanceFromTrend = MathAbs(smallerClose - trendline); // Calculate the pullback threshold in pips double pullbackThresholdPips = threshold * SymbolInfoDouble(_Symbol, SYMBOL_POINT); // Check for a pullback if (distanceFromTrend < pullbackThresholdPips) { Print("Pullback Detected"); return true; } return false; } // Function to open a trade void OpenTrade(string symbol, int cmd, double volume, double price, int slippage, double stopLoss, double takeProfit, string comment, int deviation, color arrowColor) { int ticket = OrderSend(symbol, cmd, volume, price, slippage, stopLoss, takeProfit, comment, deviation, arrowColor); if (ticket > 0) { Print("Trade opened successfully. Ticket: ", ticket); if (trailingStop > 0) { double trailingStopValue = NormalizeDouble(trailingStop * SymbolInfoDouble(symbol, SYMBOL_POINT), Digits); OrderModify(ticket, price, 0, trailingStopValue, 0, 0); Print("Trailing stop set to ", trailingStop, " pips."); } } else { Print("Error opening trade. Error code: ", GetLastError()); } }