Summary: BTC K-Range Stabilizer EA is an MQL4 expert advisor for Bitcoin that analyzes candlestick ranges, detects volatility contraction, and enters on range breakouts with volume confirmation. Suitable for H4.




BTC K-Range Stabilizer EA is built specifically for Bitcoin's unique price behavior. Instead of traditional indicators, it analyzes pure candlestick data: range size, body-to-wick ratio, and volatility contraction patterns. The EA identifies periods of low volatility (compression) and enters when price breaks out of the established range with momentum confirmation. A dynamic take profit based on the compression range size and a trailing stop that activates after 1.5x range capture profits while limiting drawdown.

Recommended Timeframe: H4
Trading Logic:
  • Range Detection: Calculate the average true range of last 20 candles. Identify consolidation when current range < 0.55 <em> average range.

  • Compression Pattern: Require at least 3 consecutive candles with decreasing ranges (volatility contraction).

  • Breakout Entry: When price closes outside the highest high or lowest low of the compression period by at least 1.5x current ATR.

  • Risk Management: Stop loss at opposite side of compression range. Take profit at 2.5x compression range size. Trailing stop activates at 1x profit.


  • ``mql4
    //+------------------------------------------------------------------+
    //| BTCKRangeStabilizer.mq4 |
    //+------------------------------------------------------------------+
    #property copyright ""
    #property link ""
    #property version "1.00"
    #property strict

    //--- input parameters with comments
    input double LotSize = 0.01; // Fixed lot size (0.01 BTC)
    input int RangeLookback = 20; // Lookback period for average range calculation
    input double CompressionRatio = 0.55; // Compression threshold (current range / avg range)
    input int MinCompressionCandles = 3; // Minimum consecutive compression candles required
    input double BreakoutMultiplier = 1.5; // Breakout confirmation multiplier (ATR)
    input double StopLossMultiplier = 1.2; // Stop loss as multiplier of compression range
    input double TakeProfitMultiplier = 2.5; // Take profit as multiplier of compression range
    input double TrailingActivate = 1.0; // Trailing activates after profit (x compression range)
    input double TrailingStep = 0.5; // Trailing step (x compression range)
    input int MagicNumber = 202417; // Unique EA identifier
    input int MaxSpread = 200; // Maximum allowed spread in points (BTC)
    input double DailyLossLimit = 6.0; // Daily loss limit as percentage of balance
    input bool UseSundayClose = true; // Close trades before Sunday 22:00 GMT

    //--- global variables
    double dailyStartBalance = 0;
    datetime lastBarTime = 0;
    bool sundayCloseExecuted = false;
    double compressionHigh = 0;
    double compressionLow = 0;
    int compressionCandleCount = 0;
    double compressionRange = 0;

    //+------------------------------------------------------------------+
    //| Expert initialization function |
    //+------------------------------------------------------------------+
    int OnInit()
    {
    dailyStartBalance = AccountBalance();
    lastBarTime = 0;
    sundayCloseExecuted = false;
    compressionHigh = 0;
    compressionLow = 0;
    compressionCandleCount = 0;
    return(INIT_SUCCEEDED);
    }

    //+------------------------------------------------------------------+
    //| Expert deinitialization function |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
    {
    Comment("");
    }

    //+------------------------------------------------------------------+
    //| Calculate range of a single candle (high - low) |
    //+------------------------------------------------------------------+
    double CandleRange(int shift)
    {
    return iHigh(Symbol(), PERIOD_H4, shift) - iLow(Symbol(), PERIOD_H4, shift);
    }

    //+------------------------------------------------------------------+
    //| Detect volatility compression pattern |
    //+------------------------------------------------------------------+
    bool DetectCompression()
    {
    double ranges[];
    ArrayResize(ranges, RangeLookback);

    for(int i = 1; i <= RangeLookback; i++)
    ranges[i-1] = CandleRange(i);

    // Calculate average range
    double avgRange = 0;
    for(int i = 0; i < RangeLookback; i++)
    avgRange += ranges[i];
    avgRange /= RangeLookback;

    if(avgRange <= 0) return false;

    // Check current range compression
    double currentRange = CandleRange(1);
    bool isCompressed = (currentRange < avgRange CompressionRatio);

    // Track consecutive compression candles
    if(isCompressed)
    {
    compressionCandleCount++;
    // Update compression range boundaries
    double candleHigh = iHigh(Symbol(), PERIOD_H4, 1);
    double candleLow = iLow(Symbol(), PERIOD_H4, 1);
    if(compressionCandleCount == 1 || candleHigh > compressionHigh)
    compressionHigh = candleHigh;
    if(compressionCandleCount == 1 || candleLow < compressionLow)
    compressionLow = candleLow;
    }
    else
    {
    compressionCandleCount = 0;
    compressionHigh = 0;
    compressionLow = 0;
    }

    compressionRange = compressionHigh - compressionLow;

    return (compressionCandleCount >= MinCompressionCandles);
    }

    //+------------------------------------------------------------------+
    //| Check breakout from compression zone |
    //+------------------------------------------------------------------+
    bool CheckBreakout(int &cmd, double &entryPrice, double &compRange)
    {
    if(compressionCandleCount < MinCompressionCandles)
    return false;

    double close1 = iClose(Symbol(), PERIOD_H4, 1);
    double high1 = iHigh(Symbol(), PERIOD_H4, 1);
    double low1 = iLow(Symbol(), PERIOD_H4, 1);
    double atr = iATR(Symbol(), PERIOD_H4, 14, 1);

    if(atr <= 0) atr = compressionRange 0.5;

    double breakoutThreshold = atr
    BreakoutMultiplier;

    // Bullish breakout: close above compression high + threshold
    if(close1 > compressionHigh + breakoutThreshold)
    {
    cmd = OP_BUY;
    entryPrice = Ask;
    compRange = compressionRange;
    return true;
    }
    // Bearish breakout: close below compression low - threshold
    else if(close1 < compressionLow - breakoutThreshold)
    {
    cmd = OP_SELL;
    entryPrice = Bid;
    compRange = compressionRange;
    return true;
    }

    return false;
    }

    //+------------------------------------------------------------------+
    //| Manage trailing stop for open position |
    //+------------------------------------------------------------------+
    void ManageTrailing(double compRange)
    {
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    double activateLevel = compRange TrailingActivate;
    double step = compRange
    TrailingStep;

    double newSL = 0;
    if(OrderType() == OP_BUY)
    {
    double profit = Bid - OrderOpenPrice();
    if(profit >= activateLevel)
    {
    newSL = Bid - step;
    if(newSL > OrderStopLoss())
    {
    OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
    }
    }
    }
    else if(OrderType() == OP_SELL)
    {
    double profit = OrderOpenPrice() - Ask;
    if(profit >= activateLevel)
    {
    newSL = Ask + step;
    if(newSL < OrderStopLoss() || OrderStopLoss() == 0)
    {
    OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
    }
    }
    }
    break;
    }
    }
    }
    }

    //+------------------------------------------------------------------+
    //| Expert tick function |
    //+------------------------------------------------------------------+
    void OnTick()
    {
    // Daily equity protection
    double currentEquity = AccountEquity();
    double lossPercent = (dailyStartBalance - currentEquity) / dailyStartBalance 100;
    if(lossPercent >= DailyLossLimit)
    {
    Comment("Daily loss limit reached. No new trades.");
    return;
    }

    // Sunday close before weekend gap
    if(UseSundayClose && !sundayCloseExecuted)
    {
    datetime currentTime = TimeCurrent();
    if(TimeDayOfWeek(currentTime) == 0 && TimeHour(currentTime) >= 22)
    {
    CloseAllOrders();
    sundayCloseExecuted = true;
    return;
    }
    if(TimeDayOfWeek(currentTime) != 0)
    sundayCloseExecuted = false;
    }

    // Spread filter for Bitcoin
    if(MarketInfo(Symbol(), MODE_SPREAD) > MaxSpread)
    {
    Comment("Spread too high: ", MarketInfo(Symbol(), MODE_SPREAD));
    return;
    }

    // New bar logic (H4)
    if(Time[0] == lastBarTime)
    return;
    lastBarTime = Time[0];

    // Manage existing position
    int posCount = CountPositions();
    if(posCount > 0)
    {
    if(compressionRange > 0)
    ManageTrailing(compressionRange);
    return;
    }

    // Detect volatility compression pattern
    bool isCompressed = DetectCompression();
    if(!isCompressed)
    {
    Comment("No valid compression pattern detected");
    return;
    }

    // Check for breakout
    int cmd = -1;
    double entryPrice = 0;
    double compRange = 0;

    if(CheckBreakout(cmd, entryPrice, compRange))
    {
    double sl = 0, tp = 0;
    double stopDistance = compRange
    StopLossMultiplier;
    double takeDistance = compRange * TakeProfitMultiplier;

    if(cmd == OP_BUY)
    {
    sl = entryPrice - stopDistance;
    tp = entryPrice + takeDistance;
    }
    else if(cmd == OP_SELL)
    {
    sl = entryPrice + stopDistance;
    tp = entryPrice - takeDistance;
    }

    int ticket = OrderSend(Symbol(), cmd, LotSize, entryPrice, 5, sl, tp, "BTC K-Range", MagicNumber, 0, clrNONE);
    if(ticket < 0)
    {
    Print("OrderSend failed: ", GetLastError());
    }
    else
    {
    Print("Breakout trade opened. Compression range: ", compRange);
    }
    }
    }

    //+------------------------------------------------------------------+
    //| Count open positions with this MagicNumber |
    //+------------------------------------------------------------------+
    int CountPositions()
    {
    int count = 0;
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    count++;
    }
    }
    return count;
    }

    //+------------------------------------------------------------------+
    //| Close all orders for this symbol and magic |
    //+------------------------------------------------------------------+
    void CloseAllOrders()
    {
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    if(OrderType() == OP_BUY)
    OrderClose(OrderTicket(), OrderLots(), Bid, 5, clrNONE);
    else if(OrderType() == OP_SELL)
    OrderClose(OrderTicket(), OrderLots(), Ask, 5, clrNONE);
    }
    }
    }
    }
    //+------------------------------------------------------------------+
    ``
    Reference: Original MQL4 code for educational purposes.
    Disclaimer: Bitcoin trading carries extreme risk due to high volatility and potential market gaps. This EA is provided as-is without any guarantee of profit. Test thoroughly on a demo account for at least 2 months before live deployment. Past performance does not guarantee future results.