My very simple EA don´t work in Back-Test

 

This is my 1º EA.

Does nothing useful, it's just to practice programming.

When there is no open trade, open an operation with TP and SL low for closing fast.


I test this EA which demo account in real time and it works as Expected. But when I use in Back-test don´t work. Only open 1 trade. In real time in 1 real hour open like 25 operations.

#property link      ""
#property version   "1.00"

#include <Trade\Trade.mqh>


/**
 * Expert initialization function 
**/
int OnInit()
  { 
  
   MessageBox("OnInit()");
   Print("OnInit()");  
 
   return(INIT_SUCCEEDED);
  }
 
 
 
/**
 * Expert tick function.
 * Event is generated for Expert Advisors only when a new tick for a symbol is received
**/
void OnTick()
  {

   PositionSelect(_Symbol);
   if(PositionGetDouble(POSITION_VOLUME)==0)
   {
  
      Print("Positions open NOT detected in " + _Symbol);
      openTrade(); 
  
   }
   else
   {
      Print("Positions open detected in " + _Symbol);
   }
  
  
 
  }


/**
* Open trade test (Demo)
**/
void openTrade()
{

   Print("openTradeCalled()");

   CTrade trade;
   trade.SetTypeFilling(ORDER_FILLING_IOC);  

   int digits=(int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
   double point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   double price=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   double SL=NormalizeDouble(price-100*point,digits);
   double TP=NormalizeDouble(price+100*point,digits);
   string comment="Buy "+_Symbol+" 0.1 open at "+DoubleToString(price,digits);

   if(!trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.01,price,SL,TP,comment))
     {

      Print("PositionOpen() fail. Return code=",trade.ResultRetcode(),
            "info: ",trade.ResultRetcodeDescription());
     }
   else
     {
      Print("PositionOpen() OK. Return code=",trade.ResultRetcode(),
            " (",trade.ResultRetcodeDescription(),")");
     }

}


Only one position is open. But I don´t see "Print("openTradeCalled()");" in log. Also I don´t see

Print("OnInit()");  


In log I only see repeated like 200 times:

Print("Positions open detected in " + _Symbol);

Files:
test.png  39 kb
test2.png  13 kb
test3.png  113 kb
 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 

I find the problem but I don´t know why happens.


PositionGetDouble(POSITION_VOLUME) return 0.0 in real time demo test when all positions are closed.


In back-test return 0.1 even if position is closed, example:


2016.02.01 01:02:00   Positions open detected in EURUSD VOLUME: 0.01
2016.02.01 01:02:20   Positions open detected in EURUSD VOLUME: 0.01
Trade    2016.02.01 01:02:40   stop loss triggered buy 0.01 EURUSD 1.08335 sl: 1.08235 tp: 1.08435 [#3 sell 0.01 EURUSD at 1.08235]
Trades    2016.02.01 01:02:40   deal #3 sell 0.01 EURUSD at 1.08235 done (based on order #3)
Trade    2016.02.01 01:02:40   deal performed [#3 sell 0.01 EURUSD at 1.08235]
Trade    2016.02.01 01:02:40   order performed sell 0.01 at 1.08235 [#3 sell 0.01 EURUSD at 1.08235]
2016.02.01 01:02:40   Positions open detected in EURUSD VOLUME: 0.01
2016.02.01 01:02:59   Positions open detected in EURUSD VOLUME: 0.01

 
dgdeivid:

I find the problem but I don´t know why happens.


PositionGetDouble(POSITION_VOLUME) return 0.0 in real time demo test when all positions are closed.


In back-test return 0.1 even if position is closed, example:

...
You should check the documentation more carefully, you have to check the return value of PositionSelect() if it's false, PositionGetDouble() results are unreliable.
 
Alain Verleyen:
You should check the documentation more carefully, you have to check the return value of PositionSelect() if it's false, PositionGetDouble() results are unreliable.
void OnTick()
  {

   if(PositionSelect(_Symbol))
   {
   
      if(PositionGetDouble(POSITION_PROFIT)==0)
      {
   
      Print("Positions open NOT detected in " + _Symbol);
      openTrade();  
   
      }
      else
      {
         Print("Positions open detected in " + _Symbol + " PROFIT: " + PositionGetDouble(POSITION_PROFIT));
      }
   
   }
   else
   {   
       Print("OnTick() called, but PositionSelect() return false. Error: " + GetLastError());   
   }
   
  
  }


I make this change and result are the same. When all operations are closed "PositionGetDouble()" return data of last closed operation. And documentation say "The function returns the requested property of an open position". Instead of "POSITION_PROFIT" I check which other parameters, and all return data of last closed operation. In real test work OK I only have this problem in back-test.


Example:


MH    0    12:45:07.976    forum (EURUSD,M1)    2016.02.01 00:59:00   Positions open detected in EURUSD PROFIT: -1.14
HH    0    12:45:07.976    forum (EURUSD,M1)    2016.02.01 00:59:20   Positions open detected in EURUSD PROFIT: -1
IR    0    12:45:07.976    forum (EURUSD,M1)    2016.02.01 00:59:40   Positions open detected in EURUSD PROFIT: -1.16
MH    0    12:45:07.976    Trade    2016.02.01 01:00:40   stop loss triggered buy 0.02 EURUSD 1.08343 sl: 1.08251 tp: 1.08451 [#4 sell 0.02 EURUSD at 1.08251]
GK    0    12:45:07.976    Trades    2016.02.01 01:00:40   deal #4 sell 0.02 EURUSD at 1.08251 done (based on order #4)
RE    0    12:45:07.976    Trade    2016.02.01 01:00:40   deal performed [#4 sell 0.02 EURUSD at 1.08251]
QE    0    12:45:07.976    Trade    2016.02.01 01:00:40   order performed sell 0.02 at 1.08251 [#4 sell 0.02 EURUSD at 1.08251]

ML    0    12:45:07.977    forum (EURUSD,M1)    2016.02.01 01:00:40   Positions open detected in EURUSD PROFIT: -1.16 (this line is repeated in all next data 100 000 times in all month back-test)

Reason: