Ea works perfectly in back testing but not in live

Hello ,

My name is Sabrina Im a manual trader im trying to learn mql to improve my strategies and i have a built a simple EA that shown good results in back testing of mt4 nut when tried it in live account it doesnt work properly.

it does open positions but can’t sell it under close buy position.

Please if someone can modify it and makes it work it’s gonna be really helpful

Thank you

runnerx.txt (2.52 KB)

Following! Waiting for the gurus!

You need to clean your code. Why do you have pieces of code for a short position in your long position code?

// EXTERNAL VARIABLES //
extern double LotSize = 1;
extern double StopLoss = 0;
extern double TakeProfit = 0;
extern double TrailingStopLimit = 0;
extern double TrailingStopStop = 0;
extern int MagicNumber = 23310;

// GLOBAL VARIABLES //
int LongTicket;
int ShortTicket;
int BarsCount = 0;
double RealPoint;

// INIT FUNCTION //
int init() {
    RealPoint = RealPipPoint(Symbol());
}

// START FUNCTION // 
int start() {

    if (Bars > BarsCount) {

        if (OrdersTotal() == 0) {

            // LONG // 
            bool buy_condition_1 = Close[0] > Open[1];

            if (buy_condition_1) {

                OrderSelect(LongTicket, SELECT_BY_TICKET);

                LongTicket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 0, 0, 0, "Buy Order", MagicNumber, 0, Green);

                BarsCount = Bars;

            }
        }

        // CLOSE LONG //
        bool close_buy_condition_1 = Open[0] > Low[1];

        if (close_buy_condition_1) {

            OrderSelect(LongTicket, SELECT_BY_TICKET);

            bool Closed = OrderClose(LongTicket, OrderLots(), Bid, 0, Green);

            BarsCount = Bars;

        }

        // LONG STOP LOSS //
        OrderSelect(LongTicket, SELECT_BY_TICKET);
        double OpenPrice = OrderOpenPrice();

        if (StopLoss > 0) double LongStopLoss = OpenPrice - (StopLoss * RealPoint);
        if (TakeProfit > 0) double LongTakeProfit = OpenPrice + (TakeProfit * RealPoint);

        if (LongStopLoss > 0 || LongTakeProfit > 0) {
            bool LongMod = OrderModify(LongTicket, OpenPrice, LongStopLoss, LongTakeProfit, 0);

        }
    }

    return (0);

}

// PIP POINT FUNCTION //
double RealPipPoint(string Currency) {
    int CalcDigits = MarketInfo(Currency, MODE_DIGITS);
    if (CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
    else if (CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
    return (CalcPoint);
}