#Correction code // Define input parameters input double LotSize = 0.1; // Trading lot size input int TakeProfit = 100; // Take profit in pips input int TrailingStop = 25; // Distance in pips to start trailing stop input int TrailingStep = 25; // Step in pips to move trailing stop input int StopLossBuffer = 5; // Buffer in pips for stop loss input int MaxSpread = 3; // Maximum allowed spread for entry input double MinATR = 0.002; // Minimum ATR range for entry // Global variables int lastTradeTicket = 0; double lastTradePrice = 0; double trailingStopPrice = 0; double atrValue[]; int bufferSize = 14; // ATR buffer size //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnInit() { ArrayResize(atrValue, bufferSize); // Initialize the ATR buffer with the correct size ArrayInitialize(atrValue, 0.0); // Initialize the ATR buffer elements to zero } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { // Check the current spread double spread = SymbolInfoDouble(Symbol(), SYMBOL_SPREAD); // Check if the spread is within the allowed range if (spread <= MaxSpread) { // Calculate ATR value atrValue[0] = CalculateATR(); // Check if ATR range is above the minimum requirement if (atrValue[0] > MinATR) { // Check for long trade conditions (place a buy order) if (IsBullishCandlestickPattern() && IsAbovePreviousCandle()) { double currentPrice = SymbolInfoDouble(Symbol(), SYMBOL_BID); double stopLoss = currentPrice - StopLossBuffer * SymbolInfoDouble(Symbol(), SYMBOL_POINT); double takeProfit = currentPrice + TakeProfit * SymbolInfoDouble(Symbol(), SYMBOL_POINT); lastTradeTicket = OrderSend(Symbol(), OP_BUY, LotSize, currentPrice, 0, stopLoss, takeProfit, "Buy Order", 0, clrGreen); lastTradePrice = currentPrice; // Store the entry price for stop loss adjustment trailingStopPrice = currentPrice - TrailingStop * SymbolInfoDouble(Symbol(), SYMBOL_POINT); } // Check for short trade conditions (place a sell order) else if (IsBearishCandlestickPattern() && IsBelowPreviousCandle()) { double currentPrice = SymbolInfoDouble(Symbol(), SYMBOL_BID); double stopLoss = currentPrice + StopLossBuffer * SymbolInfoDouble(Symbol(), SYMBOL_POINT); double takeProfit = currentPrice - TakeProfit * SymbolInfoDouble(Symbol(), SYMBOL_POINT); lastTradeTicket = OrderSend(Symbol(), OP_SELL, LotSize, currentPrice, 0, stopLoss, takeProfit, "Sell Order", 0, clrRed); lastTradePrice = currentPrice; // Store the entry price for stop loss adjustment trailingStopPrice = currentPrice + TrailingStop * SymbolInfoDouble(Symbol(), SYMBOL_POINT); } } else { Print("ATR range is too low for entry: ", atrValue[0]); } } else { Print("Spread is too wide for entry: ", spread); } // Check and update trailing stop double currentPrice = SymbolInfoDouble(Symbol(), SYMBOL_BID); if (lastTradeTicket > 0 && currentPrice - trailingStopPrice >= TrailingStep * SymbolInfoDouble(Symbol(), SYMBOL_POINT)) { trailingStopPrice = currentPrice; double newStopLoss = currentPrice - StopLossBuffer * SymbolInfoDouble(Symbol(), SYMBOL_POINT); OrderModify(lastTradeTicket, currentPrice, newStopLoss, lastTradePrice + TakeProfit * SymbolInfoDouble(Symbol(), SYMBOL_POINT), 0, clrNONE); } } // Function to calculate the ATR value in pips double CalculateATR() { double atrBuffer[]; ArrayResize(atrBuffer, bufferSize); int atrHandle = iATR(Symbol(), 0, bufferSize); if (atrHandle != INVALID_HANDLE) { CopyBuffer(atrHandle, 0, 0, bufferSize, atrBuffer); ArrayFree(atrHandle); return (atrBuffer[0] * SymbolInfoDouble(Symbol(), SYMBOL_POINT)); // Convert ATR value to account for the decimal places of the symbol } return 0.0; } // Function to check for bullish Heiken Ashi candlestick pattern on the previous two candles bool IsBullishCandlestickPattern() { double open1 = iOpen(Symbol(), 0, 1); double close1 = iClose(Symbol(), 0, 1); double low1 = iLow(Symbol(), 0, 1); double high1 = iHigh(Symbol(), 0, 1); double open2 = iOpen(Symbol(), 0, 2); double close2 = iClose(Symbol(), 0, 2); double low2 = iLow(Symbol(), 0, 2); double high2 = iHigh(Symbol(), 0, 2); bool isBullishCandle1 = close1 > open1 && close1 == low1 && close1 == high1; bool isBullishCandle2 = close2 > open2 && close2 == low2 && close2 == high2; return isBullishCandle1 && isBullishCandle2; } // Function to check for bearish Heiken Ashi candlestick pattern on the previous two candles bool IsBearishCandlestickPattern() { double open1 = iOpen(Symbol(), 0, 1); double close1 = iClose(Symbol(), 0, 1); double low1 = iLow(Symbol(), 0, 1); double high1 = iHigh(Symbol(), 0, 1); double open2 = iOpen(Symbol(), 0, 2); double close2 = iClose(Symbol(), 0, 2); double low2 = iLow(Symbol(), 0, 2); double high2 = iHigh(Symbol(), 0, 2); bool isBearishCandle1 = close1 < open1 && close1 == low1 && close1 == high1; bool isBearishCandle2 = close2 < open2 && close2 == low2 && close2 == high2; return isBearishCandle1 && isBearishC