New EA won't place trades

 

Hi all,


I'm completely new to developing EAs so hopefully theres a simple solution to this problem that I just can't see due to my inexperience. The EA should open a position if there is a new candle on the 5 mins timeframe, if the trend on both the 15 mins and 1 hour timeframes (based on a comparison of the 50 and 200 period EMAs) is the same, if there is a good price to enter the trade based on the 50 period EMA on the 5 mins timeframe (for a buy position, the current price should be below the 50 period EMA on the 5 mins timeframe and visa-versa) and should place a trade if the last candle was an engulfing candle or a pin bar.

Currently, it just isnt placing any trades when I backtest it using the Strategy Tester. Any advice would really be appreciated.


Thanks!

Tom


   

Files:
 
  1. double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
    double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
    That is not an assignment; it's initialization of a static with a constant. Global and static variables work exactly the same way in MT4/MT5/C/C++.
    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use.)

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) 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. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum

  2.    double ema_1H_50=iMA(_Symbol,PERIOD_H1,50,1,1,PRICE_CLOSE);
       double ema_1H_200=iMA(_Symbol,PERIOD_H1,200,1,1,PRICE_CLOSE);
    

    Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

    Also see my example for encapsulating calls
              Detailed explanation of iCustom - MQL4 programming forum 2017.05.23

  3. trade.Buy(0.03,_Symbol,Ask,Ask-200*_Point,Ask+300*_Point,NULL);
    You buy at the Ask and sell at the Bid.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the 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 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 spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.

 

Thanks for the advice, I will definitely incorporate would you said about the average spread into my SL and TP. 


Regarding point 1, I have moved them into the OnTick function, but the EA still won't place any trades when I backtest it. Have I misunderstood what you mean, or is there something else I must do?

double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
Files:
 
tebbsy96:

Thanks for the advice, I will definitely incorporate would you said about the average spread into my SL and TP. 


Regarding point 1, I have moved them into the OnTick function, but the EA still won't place any trades when I backtest it. Have I misunderstood what you mean, or is there something else I must do?

Your code still creates handles such as ema_1H_50 in the OnTick() function. This is not correct. You have also defined these handles to be of type double, which is not correct: they are of type int. All these handle definitions should be placed in the OnInit() function. In the OnTick() function you then use these handles to get the updated data.

The documentation gives a good description and example on how to define the iMA() function and how to use it.

Reason: