My EA not taking trades

 

Hi,

I have tried to create an EA but it doesn't take trades. When I put it in the strategy tester I get the following error: 2023.06.16 10:47:50.092    EURUSD,M1: 466454 tick events (13844 bars, 466554 bar states) processed in 0:00:01.266 (total time 0:00:01.313)
2023.06.16 10:47:50.092    2023.06.16 12:46:59  CandleBlast EURUSD,M1: OrderSend error 131
2023.06.16 10:47:50.092    2023.06.16 12:46:59  CandleBlast EURUSD,M1: OrderSend error 131
2023.06.16 10:47:50.092    2023.06.16 12:46:59  CandleBlast EURUSD,M1: OrderSend error 131

My code is attached.

Can someone please help me with editing this code.

Daniel

Files:
EA_code.txt  12 kb
 
  1. Do not rename your extension. Keep it as MQ4/MQ5

  2.       ticket = OrderSend(Symbol(), OP_BUY, LotSize, entryPrice, slippage, stopLossPrice, takeProfitPrice, "Buy order", 0, 0, Green);

    Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  3.         stopLossPrice = entryPrice - StopLossPips * Point;
            takeProfitPrice = entryPrice + TargetProfitPips * Point;

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  4. void CloseTrade(){
        ⋮
                    ticket = OrderClose(ticket, LotSize, Bid, slippage, Violet);

    OrderClose does not return a ticket number

  5. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static / global ticket variables will have been lost. You will have an open order / position but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.

  6.         // Apply martingale recovery strategy if enabled
            if (UseMartingale)

    Martingale is not a strategy. It's a betting system.

    Hedging, grid trading, same as Martingale.
              Martingale, Hedging and Grid : MHG - General - MQL5 programming forum (2016)

    Martingale, guaranteed to blow your account eventually. If your strategy is not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum (2015)

    Why it won't work:
              Calculate Loss from Lot Pips - MQL5 programming forum (2017)
              THIS Trading Strategy is a LIE... I took 100,000 TRADES with the Martingale Strategy - YouTube (2020)

  7.    // Check if there is a new crossover
        if (FastMAValue > SlowMAValue)
        {

    That is not a crossover. Look for a cross.

    double aPrev = …(i+1), aCurr = …(i),
           bPrev = …(i+1), bCurr = …(i);
    bool   wasUp = aPrev > bPrev,
            isUp = aCurr > bCurr,
         isCross = isUp != wasUp;

  8. stopLossPrice = entryPrice - StopLossPips * Point;

    A PIP is not a point. PIP, Point, or Tick are all different in general.
              Ticks, PIPs or points in the GUI. Make up your mind. - MQL4 programming forum #1 (2014)

  9. Daniel Matthew: CandleBlast EURUSD,M1: OrderSend error 131
    A SL 5 PIPs below the entry price minus the spread doesn't exceed the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).  See № 3.
              Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

    On some ECN type brokers, the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.

    The checks a trading robot must pass before publication in the Market - MQL5 Articles (2016)

 
As you can tell.... i'm not an expert... and i'm really struggling. I know you went through a lot of effort to share that information, but I still don't know what to do and can can't fix what is wrong
 
Your topic has been moved to the section: MQL4 e MetaTrader 4 — In the future, please consider which section is most appropriate for your query.
 
Daniel Matthew #: As you can tell.... i'm not an expert... and i'm really struggling. I know you went through a lot of effort to share that information, but I still don't know what to do and can can't fix what is wrong

Then it might be best to pass it onto someone else. Maybe consider hiring someone to correct it for you.

 
But I know it's just a couple changes to a couple of the lines. Someone must know and be able to tell me?
 
Daniel Matthew #: But I know it's just a couple changes to a couple of the lines. Someone must know and be able to tell me?

And William has already pointed out the problematic issues. It is up to you to put in the effort in researching it and applying it to your code.

If you are unable to do that, then please read the following ... Is it easy (or difficult) to …?

 

Hi, Feel I have made all the changes but I am still getting the error. What is wrong now? Please see attached code.

File attachment removed by moderator. Please attach MQL source file with valid extension ".mq?".

 
Daniel Matthew #: Feel I have made all the changes but I am still getting the error. What is wrong now? Please see attached code.
  1. You ignored #1.1

  2. You ignored #1.2

  3.            if (UseTargetProfit && Bid >= takeProfitPrice)
                    result = OrderClose(ticket, LotSize, takeProfitPrice, slippage, Violet);

    You can only close at the market. Check and report your return codes and errors.

  4. Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  5. You don't have time to code orders in deinit. Just to be precise, the documentation says: The OnDeinit() function run is restricted to 2.5 seconds.

  6. You ignored #1.7

 
Daniel Matthew #: Hi, Feel I have made all the changes but I am still getting the error. What is wrong now? Please see attached code.

File attachment removed by moderator. Please attach MQL source file with valid extension ".mq?".

You have already been informed about the correct file type to attach. Please do so correctly!
 

Hi,

I appreciate the responses because of course you don't have to.


But you say i'm ignoring steps. I've not ignored any step. If a change is not made to your standard then it's because I didn't understand what you were refering to. And regarding 1.1 1.2 1.7 I still don't know what that is.

I feel I have to guess what you're referring to rather than gaining knowledge for me to move forward and fix this code. It seems like a riddle instead of being told what part of my code is wrong and what changes should be made.

Can the instructions be clearer please.

Thank you for your time,

Daniel

Reason: