// Include the necessary library for trading functions #include // Define input parameters input double InitialRiskPercentage = 1.0; // Initial risk percentage input double MaxRiskPercentage = 1.0; // Maximum risk percentage input double MinRiskPercentage = 0.125; // Minimum risk percentage input double LotSize = 0.01; // Lot size (adjust as needed) // Define time parameters const int AsianStartHourNY = 20; // 8 PM NY time const int AsianEndHourNY = 0; // 12 AM NY time const int TradeStartHourNY = 3; // 3 AM NY time const int TradeEndHourNY = 4; // 4 AM NY time // Time offsets const int timeOffsetStandard = 8; // UTC+3 (Exness server time) const int timeOffsetDST = 7; // UTC+3 during DST // Global variables double AsianHigh = 0; double AsianLow = 0; double CurrentRiskPercentage = InitialRiskPercentage; bool TradeTaken = false; double MSSHigh = 0; // Market Structure Shift High double MSSLow = 0; // Market Structure Shift Low CTrade trade; // Trade management object // Function to check if we are within a specific time range (adjusted for NY time) bool IsInTimeRange(int startHourNY, int endHourNY, bool isDST) { datetime now = TimeCurrent(); int hour = TimeHour(now + (isDST ? timeOffsetDST : timeOffsetStandard) * 3600); // Adjust for NYT return (hour >= startHourNY && (endHourNY == 0 ? hour < 24 : hour < endHourNY)); } // Function to calculate the Asian range (8 PM to 12 AM NY time) void CalculateAsianRange() { datetime asianStart = TimeCurrent() - (TimeHour(TimeCurrent()) - AsianStartHourNY) * 3600; datetime asianEnd = asianStart + 4 * 3600; // 4 hours duration AsianHigh = iHigh(NULL, PERIOD_M1, iBarShift(NULL, PERIOD_M1, asianEnd, true)); AsianLow = iLow(NULL, PERIOD_M1, iBarShift(NULL, PERIOD_M1, asianEnd, true)); for (datetime time = asianStart; time < asianEnd; time += 60) { double high = iHigh(NULL, PERIOD_M1, iBarShift(NULL, PERIOD_M1, time, true)); double low = iLow(NULL, PERIOD_M1, iBarShift(NULL, PERIOD_M1, time, true)); if (high > AsianHigh) AsianHigh = high; if (low < AsianLow) AsianLow = low; } } // Function to check for market structure shift (MSS) bool CheckMarketStructureShift(double &lastHigh, double &lastLow, double currentPrice) { if (currentPrice > lastHigh) { MSSHigh = currentPrice; // Shift to the upside return true; } else if (currentPrice < lastLow) { MSSLow = currentPrice; // Shift to the downside return true; } return false; } // Function to check for retracement after market structure shift bool CheckRetracement(double currentPrice, int tradeType) { if (tradeType == ORDER_SELL && currentPrice >= MSSHigh) { return true; // Retracement to MSSHigh for sell } else if (tradeType == ORDER_BUY && currentPrice <= MSSLow) { return true; // Retracement to MSSLow for buy } return false; // No retracement } // Function to open a trade void OpenTrade(int type) { double stopLoss, takeProfit, price; double volume = LotSize * CurrentRiskPercentage / 100; // Calculate volume based on risk if (type == ORDER_SELL) { price = SymbolInfoDouble(Symbol(), SYMBOL_BID); stopLoss = MSSHigh; // High of market structure shift takeProfit = AsianLow; // Target is the low of the Asian range trade.Sell(volume, Symbol(), price, stopLoss, takeProfit, "FVG Trade"); } else { price = SymbolInfoDouble(Symbol(), SYMBOL_ASK); stopLoss = MSSLow; // Low of market structure shift takeProfit = AsianHigh; // Target is the high of the Asian range trade.Buy(volume, Symbol(), price, stopLoss, takeProfit, "FVG Trade"); } TradeTaken = true; // Mark that trade was executed } // Main function void OnTick() { // Check if we need to calculate the Asian range if (IsInTimeRange(AsianStartHourNY, AsianEndHourNY, false) && !TradeTaken) { CalculateAsianRange(); } // Trading time is between 3 AM and 4 AM NY time (adjusted for the MT5 server) bool isDST = (TimeDaylightSaving(TimeCurrent()) != 0); if (IsInTimeRange(TradeStartHourNY, TradeEndHourNY, isDST)) { double currentPrice = iClose(NULL, PERIOD_M1, 0); // Check for market structure shift and retracement logic if (!TradeTaken) { // If price has taken the high of the Asian range (sell condition) if (currentPrice > AsianHigh) { if (CheckMarketStructureShift(MSSHigh, MSSLow, currentPrice)) { // Wait for retracement to MSSHigh } } // If price has taken the low of the Asian range (buy condition) else if (currentPrice < AsianLow) { if (CheckMarketStructureShift(MSSLow, MSSHigh, currentPrice)) { // Wait for retracement to MSSLow } } } // Wait for retracement and then enter trade if (MSSHigh > 0 && currentPrice >= MSSHigh && CheckRetracement(currentPrice, ORDER_SELL)) { OpenTrade(ORDER_SELL); } else if (MSSLow > 0 && currentPrice <= MSSLow && CheckRetracement(currentPrice, ORDER_BUY)) { OpenTrade(ORDER_BUY); } } // Risk management: reduce risk after a loss if (OrderSelect(0, SELECT_BY_POS) && OrderGetDouble(OrderTicket(), ORDER_PROFIT) < 0) { // If the last trade was a loss, halve the risk CurrentRiskPercentage = MathMax(CurrentRiskPercentage / 2, MinRiskPercentage); } else if (OrderSelect(0, SELECT_BY_POS) && OrderGetDouble(OrderTicket(), ORDER_PROFIT) > 0) { // If the last trade was a win, reset risk to maximum CurrentRiskPercentage = MaxRiskPercentage; } // Make sure risk percentage doesn't exceed the maximum CurrentRiskPercentage = MathMin(CurrentRiskPercentage, MaxRiskPercentage); }