I am looking for any enhancements to this function

 
/// GET ENTRY PRICE FUNCTION

double GetCorrectEntryPrice(string sym, int direction)
{
    RefreshRates();

    double ask = symAsk(sym);
    double bid = symBid(sym);
    double pip = PipSize(sym);

    // -----------------------------------------------------
    // HIGHER TIMEFRAME for structure + liquidity
    // -----------------------------------------------------
    int HTF = EntryPriceTF;   // DEFAULT: H1

    double oH = iOpen(sym, HTF, 1);
    double cH = iClose(sym, HTF, 1);
    double hH = iHigh(sym, HTF, 1);
    double lH = iLow(sym, HTF, 1);

    double prevHigh = iHigh(sym, HTF, 2);
    double prevLow  = iLow(sym, HTF, 2);

    bool sweptHigh = (hH > prevHigh);
    bool sweptLow  = (lH < prevLow);

    double rangeH = hH - lH;
    double fairPrice = (oH + cH + hH + lH) / 4.0;    // institutional "mean"

    // -----------------------------------------------------
    // LOWER TIMEFRAME for FVG / imbalance precision
    // -----------------------------------------------------
    int LTF = PERIOD_M15;

    double h2 = iHigh(sym, LTF, 2);
    double l2 = iLow(sym, LTF, 2);
    double h3 = iHigh(sym, LTF, 3);
    double l3 = iLow(sym, LTF, 3);

    bool fvgBuy  = (l2 > h3);     // bullish FVG
    bool fvgSell = (h2 < l3);     // bearish FVG

    double fvgBuyLevel  = h3;     // top of the gap
    double fvgSellLevel = l3;     // bottom of gap

    // -----------------------------------------------------
    // ATR-based institutional pullback sizing
    // -----------------------------------------------------
    double atr = iATR(sym, HTF, 14, 0);
    double maxOffset = MathMin(atr * 0.30, 10 * pip);

    // -----------------------------------------------------
    // INSTITUTIONAL BUY LOGIC
    // -----------------------------------------------------
    if(direction == OP_BUY)
    {
        double entry = ask;

        // Sweep of lows + bullish structure → premium entry
        if(sweptLow)
            entry = ask - (rangeH * 0.25);

        // Bullish FVG present → entry at imbalance
        if(fvgBuy)
            entry = MathMin(entry, fvgBuyLevel + (maxOffset / 2));

        // Pullback to institutional fair price
        entry = MathMin(entry, fairPrice + (maxOffset / 2));

        // Limit entry shift
        entry = MathMax(entry, ask - maxOffset);

        return NormalizeDouble(entry, Digits);
    }

    // -----------------------------------------------------
    // INSTITUTIONAL SELL LOGIC
    // -----------------------------------------------------
    if(direction == OP_SELL)
    {
        double entry = bid;

        // Sweep of highs → aggressive sell entry
        if(sweptHigh)
            entry = bid + (rangeH * 0.25);

        // Bearish FVG → entry at imbalance
        if(fvgSell)
            entry = MathMax(entry, fvgSellLevel - (maxOffset / 2));

        // Pullback to fair price
        entry = MathMax(entry, fairPrice - (maxOffset / 2));

        // Limit shift
        entry = MathMin(entry, bid + maxOffset);

        return NormalizeDouble(entry, Digits);
    }

    return ask;
}

////Main Entry Function

void TryEnter()
{
    if(!TradingEnabled) return;

    double spreadPips = GetSpreadPips(SYM);
    if(spreadPips > MaxAllowedSpreadPips(SYM))
        return;

    TradeSignal signal = GetEnhancedSignal(SYM, PrimaryTF);
    if(signal == SIGNAL_NEUTRAL) return;

    int direction = (signal == SIGNAL_BUY ? OP_BUY : OP_SELL);

    double atr = iATR(SYM, PrimaryTF, 14, 0);
    if(atr <= 0) return;

    double atrPips = atr / PipSize(SYM);
    double stopP = MathMax(20.0, atrPips * 1.4);
    double tpP   = stopP * 2.5;
    

    // ? CORRECT entry price based on candle structure
    double entry = GetCorrectEntryPrice(SYM, direction);

    double lots = (UseDynamicLot ? CalcLots(stopP) : FixedLot);
    lots = MathMin(MaxLot, MathMax(FixedLot*0.1, lots));
    lots = RoundLot(lots);

    double sl = (direction==OP_BUY ?
                 entry - SafePipsToPrice(SYM, stopP) :
                 entry + SafePipsToPrice(SYM, stopP));

    double tp = (direction==OP_BUY ?
                 entry + SafePipsToPrice(SYM, tpP) :
                 entry - SafePipsToPrice(SYM, tpP));

    sl = NormalizePriceToDigits(sl);
    tp = NormalizePriceToDigits(tp);

    if(gLastTradeTime > 0)
{
    int secondsWait = MinMinutesBetweenTrades * 60;
    if(TimeCurrent() - gLastTradeTime < secondsWait)
        return;
}
    Debug(StringFormat("ENTRY=%f | SL=%f | TP=%f", entry, sl, tp));
    // ? NOW uses the correct entry price (not ask/bid)
    int ticket = SafeOrderSendML(SYM, direction, lots, entry, 30, sl, tp, "AdaptiveEntry");

    if(ticket > 0)
    {
        gLastTradeTime = TimeCurrent();
        gTotalTradesToday++;
    }
}
 

Please don't post message in all UPPERCASES. I edited your title.

If you want help about code you should :

1. Post code that compiles.

2. Be more specific in your request. "Any enhancements" is too vague. 

 
    double h2 = iHigh(sym, LTF, 2);
    double l2 = iLow(sym, LTF, 2);
    double h3 = iHigh(sym, LTF, 3);
    double l3 = iLow(sym, LTF, 3);

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
          Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
          Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
          Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
          Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
          SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

 
Alain Verleyen #:

Please don't post message in all UPPERCASES. I edited your title.

If you want help about code you should :

1. Post code that compiles.

2. Be more specific in your request. "Any enhancements" is too vague. 

I am attaching the file that compiles


Basically i want to enhance the entry level and direction. the direction is working fine most of the time but levels can be improved

Files:
test.txt  91 kb
 
Tiju Stephen #:

I am attaching the file that compiles


Basically i want to enhance the entry level and direction. the direction is working fine most of the time but levels can be improved


It will be tough to optimize this code. - MQL4, BTW.

There are a whole lot of assumptions, and quite some to make it be incompatible with most symbols, and symbol specifications.

But, I can see the strategy that has been implemented here. An SMC approach.

For start, try to get rid of pip calculations. Use points only. Also, for searching low values, use DBL_MAX as your initial value. Currently you will fail on BTC. (99999.0 is insufficient)

Some of your helper-wrappers should be actually removed, use the API instead, or, better, have these values as global variables, init them once, and use them from there on.

Also, add some inputs instead of using fixed limits in your loops. Or use #defines or global variables for these.

As an idea, and I guess you know what I am referring to: you can use the ratio of a wick size to the body size to get rid of some outliars. Get the upper wick and lower wick size, add them, compare them to the body size. Incorporate the volume, if necessary. - Should give you a hint at news candles.

Additionally, try to make the code work on both, MT4 and MT5. MT4 is dying out slowly, and you might want to use other brokers/prop firms.