Expert Advisor Does Not Make Trades

 

I tried creating a 2 line cross EA using a custom indicator (Triple EMA or DEMA) crossing an EMA, but it does not make trades; however, the EA compiles just fine. If someone could review the coding below and help me out, I would really appreciate it. Thanks.

EA:

//+------------------------------------------------------------------+
//|                                                    EMA_CROSS.mq4 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| TODO: Add Money Management routine                               |
//+------------------------------------------------------------------+

//---- input parameters
extern double TakeProfit = 0;
extern double StopLoss = 15;
extern double Lots = 1;
extern double TrailingStop = 0;
extern int TEMA = 21;
extern int EMA = 64;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Crossed (double line1 , double line2)
  {
    static int last_direction = 0;
    static int current_direction = 0;
    //Don't work in the first load, wait for the first cross!
    static bool first_time = true;
    if(first_time == true)
      {
        first_time = false;
        return (0);
      }
//----
    if(line1 > line2)
        current_direction = 1;  //up
    if(line1 < line2)
        current_direction = 2;  //down
//----
    if(current_direction != last_direction)  //changed 
      {
        last_direction = current_direction;
        return(last_direction);
      }
    else
      {
        return (0);  //not changed
      }
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
    int cnt, ticket, total;
    double SEma, LEma;
    Print(MarketInfo(Symbol(), MODE_STOPLEVEL));
//----
    if(Bars < 100)
      {
        Print("bars less than 100");
        return(0);  
      }
//----
    if(TakeProfit < 10)
      {
        Print("TakeProfit less than 10");
        return(0);  // check TakeProfit
      }
//----
    SEma = iCustom(NULL, 0, "TEMA", TEMA, 1, 0);
    LEma = iMA(NULL, 0, EMA, 0, MODE_EMA, PRICE_CLOSE, 0);
//----
    static int isCrossed  = 0;
    isCrossed = Crossed (LEma, SEma);
//----
    total  = OrdersTotal(); 
    if(total < 1) 
      {
        if(isCrossed == 1)
          {
            ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - StopLoss*Point, 
                               Ask + TakeProfit*Point, "TEMA_EMA_CROSS", 12345, 0, Green);
            if(ticket > 0)
              {
                if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)) 
                    Print("BUY order opened : ",OrderOpenPrice());
              }
            else 
                Print("Error opening BUY order : ", GetLastError()); 
            return(0);
          }
        if(isCrossed == 2)
          {
            ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Bid + StopLoss*Point, 
                               Bid - TakeProfit*Point, "TEMA_EMA_CROSS", 12345, 0, Red);
            if(ticket > 0)
              {
                if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)) 
                    Print("SELL order opened : ", OrderOpenPrice());
              }
            else 
                Print("Error opening SELL order : ",GetLastError()); 
            return(0);
          }
        return(0);
      } 
//----
    for(cnt = 0; cnt < total; cnt++)
      {
        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
        if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())
          {
            if(OrderType() == OP_BUY)   // long position is opened
              {
                // check for trailing stop
                if(TrailingStop > 0)  
                  {                 
                    if(Bid - OrderOpenPrice() > Point*TrailingStop)
                      {
                        if(OrderStopLoss() < Bid - Point*TrailingStop)
                          {
                            OrderModify(OrderTicket(), OrderOpenPrice(), 
                                        Bid - Point*TrailingStop, 
                                        OrderTakeProfit(), 0, Green);
                            return(0);
                          }
                      }
                  }
              }
            else // go to short position
              {
                // check for trailing stop
                if(TrailingStop > 0)  
                  {                 
                    if((OrderOpenPrice() - Ask) > (Point*TrailingStop))
                      {
                        if((OrderStopLoss() > (Ask + Point*TrailingStop)) || 
                           (OrderStopLoss() == 0))
                          {
                            OrderModify(OrderTicket(), OrderOpenPrice(), 
                                        Ask + Point*TrailingStop,
                                        OrderTakeProfit(), 0, Red);
                            return(0);
                          }
                      }
                  }
              }
          }
      }
//----
    return(0);
  }
//+------------------------------------------------------------------+


TEMA:

//+------------------------------------------------------------------+
//|                                                         TEMA.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.ru/"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 DarkBlue
#property  indicator_width1  2
//---- input parameters
extern int       EMA_period=14;
//---- buffers
double TemaBuffer[];
double Ema[];
double EmaOfEma[];
double EmaOfEmaOfEma[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,TemaBuffer);
   SetIndexBuffer(1,Ema);
   SetIndexBuffer(2,EmaOfEma);
   SetIndexBuffer(3,EmaOfEmaOfEma);

   IndicatorShortName("TEMA("+EMA_period+")");
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i,limit,limit2,limit3,counted_bars=IndicatorCounted();
//----
   if (counted_bars==0)
      {
      limit=Bars-1;
      limit2=limit-EMA_period;
      limit3=limit2-EMA_period;
      }
   if (counted_bars>0)
      {
      limit=Bars-counted_bars-1;
      limit2=limit;
      limit3=limit2;
      }
   for (i=limit;i>=0;i--) Ema[i]=iMA(NULL,0,EMA_period,0,MODE_EMA,PRICE_CLOSE,i);
   for (i=limit2;i>=0;i--) EmaOfEma[i]=iMAOnArray(Ema,0,EMA_period,0,MODE_EMA,i);
   for (i=limit3;i>=0;i--) EmaOfEmaOfEma[i]=iMAOnArray(EmaOfEma,0,EMA_period,0,MODE_EMA,i);
   for (i=limit3;i>=0;i--) TemaBuffer[i]=3*Ema[i]-3*EmaOfEma[i]+EmaOfEmaOfEma[i];
//----
   return(0);
  }
//+------------------------------------------------------------------+
Thanks again in advance.
 

Do you have a smilely face on the chart?

  1. it is not adjust for 4/5 digit brokers.
  2. Do you have an ECN broker account ?
  3. Have you learned yourself some basic coding mq4 from book and code exampels ?
 
deVries:

Do you have a smilely face on the chart?

  1. it is not adjust for 4/5 digit brokers.
  2. Do you have an ECN broker account ?
  3. Have you learned yourself some basic coding mq4 from book and code exampels ?

Yes, live trading is enabled, and there is a smiley face on the chart. However, even when backtesting/optimizing, there are 0 total trades.

1. Is there any coding adjustment needed, or just the parameters need to be changed to reflect the change. Example - a Stop Loss of 5/50 for a 4/5 digit broker, respectively.

2. I use FX Solutions as my broker which is a 4 digit, fixed spread broker with no ECN accounts. http://www.broker-review.com/fx-solutions/

3. I am making the move from FXCM Strategy Trader which used C#, so I have been trying to learn MQL4 as I go.

Any suggestions?

 

backtest again and look inside journal the messages you get

like to know if there are and what kind of messages you read there

extern double TakeProfit = 0;
    if(TakeProfit < 10)
      {
        Print("TakeProfit less than 10");
        return(0);  // check TakeProfit
      }

What takeprofit setting are you testing ???

 

After attempting to optimize, I get this in the Journal which looks pretty normal to me:

2012.09.06 15:04:41 There were 10 passes done during optimization
2012.09.06 15:04:41 TEMA_EMA_Cross: optimization stopped
2012.09.06 14:51:02 TEMA_EMA_Cross: optimization started

In this test, I just let the Take Profit range from 2-20 with a step of 2.

I'm assuming the issue lies somewhere with using the custom indicator. It may not be coded correctly. When using this same strategy as a 2 line EMA cross, I get results just fine.

 
rocko3:

After attempting to optimize, I get this in the Journal which looks pretty normal to me:

2012.09.06 15:04:41 There were 10 passes done during optimization
2012.09.06 15:04:41 TEMA_EMA_Cross: optimization stopped
2012.09.06 14:51:02 TEMA_EMA_Cross: optimization started

In this test, I just let the Take Profit range from 2-20 with a step of 2.

I'm assuming the issue lies somewhere with using the custom indicator. It may not be coded correctly. When using this same strategy as a 2 line EMA cross, I get results just fine.


So basically you don't have a clue what you are doing

If you read my last message again and you understand the code lines then you would have know that with a choice of takeprofit < 10 the EA is not working

Uncheck optimization and do normal backtest..... ( no optimization )

So backtest again and look inside journal the messages you get

like to know if there are and what kind of messages you read there

 

Although I am no expert by any means, I understand that when the takeprofit is less than 10 the return(0) is triggered, and the EA goes back to start() causing no trades to be taken. I'm merely testing the EA as well as my understanding of the coding which is why I let it also test values above 10.

When I run a normal backtest, I get the following Journal entry repeated (with an updated timestamp): 2012.09.06 15:59:24 2012.08.30 23:59 TEMA_EMA_Cross EURUSD,M15: 5

I appreciate your help. Thank you.

 
Strategy Tester Report
EMA_Cross
FinFX-Demo (Build 432)

Symbol EURUSD (Euro vs US Dollar)
Period 1 Hour (H1) 2012.02.01 00:00 - 2012.08.30 23:00 (2012.02.01 - 2012.08.31)
Model Every tick (the most precise method based on all available least timeframes)
Parameters TakeProfit=1000; StopLoss=150; Lots=1; TrailingStop=250; TEMA=21; EMA=64;
Bars in test 4647 Ticks modelled 13293930 Modelling quality n/a
Mismatched charts errors 310
Initial deposit 10000.00
Total net profit 641.00 Gross profit 7552.20 Gross loss -6911.20
Profit factor 1.09 Expected payoff 9.57
Absolute drawdown 649.00 Maximal drawdown 2304.60 (18.96%) Relative drawdown 18.96% (2304.60)
Total trades 67 Short positions (won %) 33 (30.30%) Long positions (won %) 34 (38.24%)
Profit trades (% of total) 23 (34.33%) Loss trades (% of total) 44 (65.67%)
Largest profit trade 993.10 loss trade -158.20
Average profit trade 328.36 loss trade -157.07
Maximum consecutive wins (profit in money) 2 (1625.90) consecutive losses (loss in money) 9 (-1414.20)
Maximal consecutive profit (count of wins) 1625.90 (2) consecutive loss (count of losses) -1414.20 (9)
Average consecutive wins 1 consecutive losses 3

# Time Type Order Size Price S / L T / P Profit Balance
1 2012.02.01 00:02 buy 1 1.00 1.30840 1.30690 1.31840
2 2012.02.01 03:03 s/l 1 1.00 1.30690 1.30690 1.31840 -157.00 9843.00
3 2012.02.01 17:05 sell 2 1.00 1.31777 1.31927 1.30777
4 2012.02.01 17:09 s/l 2 1.00 1.31927 1.31927 1.30777 -157.00 9686.00
5 2012.02.01 17:19 buy 3 1.00 1.31778 1.31628 1.32778
6 2012.02.01 18:02 modify 3 1.00 1.31778 1.31780 1.32778
7 2012.02.01 18:02 modify 3 1.00 1.31778 1.31783 1.32778
8 2012.02.01 18:02 modify 3 1.00 1.31778 1.31786 1.32778
9 2012.02.01 18:02 modify 3 1.00 1.31778 1.31788 1.32778
10 2012.02.01 18:02 modify 3 1.00 1.31778 1.31791 1.32778
11 2012.02.01 18:02 modify 3 1.00 1.31778 1.31794 1.32778
12 2012.02.01 18:02 modify 3 1.00 1.31778 1.31797 1.32778
13 2012.02.01 18:02 modify 3 1.00 1.31778 1.31800 1.32778
14 2012.02.01 18:02 modify 3 1.00 1.31778 1.31803 1.32778
15 2012.02.01 18:02 modify 3 1.00 1.31778 1.31805 1.32778
16 2012.02.01 18:02 modify 3 1.00 1.31778 1.31808 1.32778
17 2012.02.01 18:02 modify 3 1.00 1.31778 1.31811 1.32778

It is trading in backtester ......

 

What settings did you try ???

As you can see I did this on 5 digit account with

Parameters TakeProfit=1000; StopLoss=150; Lots=1; TrailingStop=250; TEMA=21; EMA=64;

 

I tried simulating your backtest above on my 4 digit account and only came out with 1 trade. I have imported data through the History Center, but for some reason it seems as if my backtesting models much less data than yours does. Am I missing something else?

Strategy Tester Report

TEMA_EMA_Cross
FXSolutions-Demo Server (Build 432)

Symbol EURUSD (Euro vs US Dollar)
Period 1 Hour (H1) 2012.08.29 21:00 - 2012.08.30 23:00 (2012.02.01 - 2012.08.31)
Model Every tick (the most precise method based on all available least timeframes)
Parameters TakeProfit=100; StopLoss=15; Lots=1; TrailingStop=25; TEMA=21; EMA=64;

Bars in test 1029 Ticks modelled 4986 Modelling quality 90.00%
Mismatched charts errors 0

Initial deposit 10000.00
Total net profit -150.00 Gross profit 0.00 Gross loss -150.00
Profit factor 0.00 Expected payoff -150.00
Absolute drawdown 150.00 Maximal drawdown 380.00 (3.71%) Relative drawdown 3.71% (380.00)

Total trades 1 Short positions (won %) 0 (0.00%) Long positions (won %) 1 (0.00%)
Profit trades (% of total) 0 (0.00%) Loss trades (% of total) 1 (100.00%)
Largest profit trade 0.00 loss trade -150.00
Average profit trade 0.00 loss trade -150.00
Maximum consecutive wins (profit in money) 0 (0.00) consecutive losses (loss in money) 1 (-150.00)
Maximal consecutive profit (count of wins) 0.00 (0) consecutive loss (count of losses) -150.00 (1)
Average consecutive wins 0 consecutive losses 1

# Time Type Order Size Price S / L T / P Profit Balance
1 2012.08.29 21:24 buy 1 1.00 1.2539 1.2524 1.2639
2 2012.08.30 10:37 s/l 1 1.00 1.2524 1.2524 1.2639 -150.00 9850.00
 

1 trade

That is not enough for doing backtesting or optimizations

Difference in test

.....................

Bars in test 4647 Ticks modelled 13293930

Your test Bars in test 1029 Ticks modelled 4986

Your testing was not more ticks ??? Model: Every Tick ??

=================================

    total  = OrdersTotal(); 
    if(total < 1)  

A big problem you have with this EA is that it only open trades if OrdersTotal() is zero

If you have this EA running on your account it can't trade if there is already an open trade manually or from other EA

====================================

For trailing this EA is also checking your trades but it is not checking MagicNumber() EA

it will also trail open trades with same OrdersSymbol()

...........................................................

and it is counting up the loop in this case it will work but if you also close trades inside the loop of checking

then you need to count down for checking trades Make it common to count down checking Trades if you make your own EA

====================================

And also you get problems if you wanna use it as well for 4 as 5 digit brokers and ECN accounts

Reason: