[Backtesting] Why using the "Open prices only ...) doesn't show any result?

To add comments, please log in or register
thomas2004
372
thomas2004  

Hi all,

As I use the "Open prices only ...) to backtest my EA, it ends just in a second and there is no any result. But if I use the other two 'Every Tick' and 'Control points ...', it takes much longer and I can see the results.


Why?

thomas2004
372
thomas2004  
//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

// Globale Externe Variablen
extern int MA_kurz = 50;
extern double SL_prozent = 0.5, HandlesLots = 0.1, TP_prozent = 1.5;
extern double stoploss = 50.0, takeprofit = 100.0;
extern int MagicNummer, Slippage = 5;
extern int LongOrder, ShortOrder;

// Globale Variablen
datetime PeriodeStartZeit;
bool NeuePeriodeBegonnen, LongSignal, ShortSignal;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   PeriodeStartZeit = Time[0];
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   // Überprüfen ob neuer Periodebeginn vorliegt
   if (PeriodeStartZeit != Time[0])   // Time[0] -> aktuelle Zeit
   {
      NeuePeriodeBegonnen = true;
      PeriodeStartZeit = Time[0];
   }
   else
     {
      NeuePeriodeBegonnen = false;
     }
     
   // Marktdaten ermitteln
   double MAkurz = iMA(NULL,0,MA_kurz,0,MODE_SMA,PRICE_CLOSE,1);     
   
   // Handelssignale ermitteln
   if(NeuePeriodeBegonnen == true)
     {
      // Buy Signal
      if(Close[1]>MAkurz)
        {
         LongSignal = true;
        }
      else
       {
         LongSignal = false;
       }        
      // Short Signal      
      if(Close[1] < MAkurz)
        {
         ShortSignal = true;
        }      
      else
        {
         ShortSignal = false;
        }
     }
   else
     {
      LongSignal = false;
      ShortSignal = false;
     }   
     
   // Long Signale umsetzen
   if(LongSignal == true)
     {   
      // Evtl. Short Order schliessen
      if(ShortOrder>0)
        {
         if(OrderSelect(ShortOrder,SELECT_BY_TICKET) == true)
         {
            bool ShortOrderGeclosed = OrderClose(ShortOrder,OrderLots(),Ask,Slippage,Blue);
            if(ShortOrderGeclosed == true)
            {
               ShortOrder = 0;
            }
         }
        }
      // Long Order eröffen
         while(LongOrder<=0)
         {
            LongOrder = OrderSend(Symbol(),OP_BUY,HandlesLots,Ask,Slippage,0,0,"MAXing LONG",MagicNummer,0,Green);
         }
     }            
   // Short Signale umsetzen
   if(ShortSignal == true)
     {
      // Evtl. Long Order schliessen
      if(LongOrder>0)
        {
         if(OrderSelect(LongOrder,SELECT_BY_TICKET)== true)
         {
            bool LongOrderGeclosed = OrderClose(LongOrder,OrderLots(),Bid,Slippage,Blue);
            if(LongOrderGeclosed == true)
              {
                LongOrder = 0;
              }
         }
        }
       // Short Order eröffen
         while(ShortOrder<=0)
         {
            ShortOrder = OrderSend(Symbol(),OP_SELL,HandlesLots,Bid,Slippage,0,0,"MAXing SHORT",MagicNummer,0,Red);
         }
     }       

   //////////////////////////////////////////////////////////////////////
   //   Calculate StopLoss and Takeprofit                              //
   //////////////////////////////////////////////////////////////////////

   double StoppLoss, TakeProfit;
   bool OrderAngepasst;   
        
   // SL Long Deals Setzen
   if(OrderSelect(LongOrder,SELECT_BY_TICKET) == true)
     {
      if(OrderCloseTime() == 0 && OrderStopLoss() == 0)
        {
         StoppLoss = NormalizeDouble(OrderOpenPrice() / ( 1 + (SL_prozent/100)), Digits);
         OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),StoppLoss,OrderTakeProfit(),0,Yellow);
        }
     }   
   // SL Short Deals Setzen
   if(OrderSelect(ShortOrder,SELECT_BY_TICKET) == true)
     {
      if(OrderCloseTime() == 0 && OrderStopLoss() == 0)
        {
         StoppLoss = NormalizeDouble(OrderOpenPrice() * ( 1 + (SL_prozent/100)), Digits);
         OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),StoppLoss,OrderTakeProfit(),0,Yellow);
        }
     }   

   // TP Long Deals Setzen
   if(OrderSelect(LongOrder,SELECT_BY_TICKET) == true)
     {
      if(OrderCloseTime() == 0 && OrderTakeProfit() == 0)
        {
         TakeProfit = NormalizeDouble(OrderOpenPrice() * ( 1 + (TP_prozent /100)), Digits);
         OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,0,Orange);
        }
     }   
   // TP Short Deals Setzen
   if(OrderSelect(ShortOrder,SELECT_BY_TICKET) == true)
     {
      if(OrderCloseTime() == 0 && OrderTakeProfit() == 0)
        {
         TakeProfit = NormalizeDouble(OrderOpenPrice() / ( 1 + (TP_prozent /100)), Digits);
         OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,0,Orange);
        }
     }   

   // Ticketnummer nach Closinf auf 0 zurücksetzen
   if(OrderSelect(LongOrder,SELECT_BY_TICKET) == true)
     {
      if(OrderTicket() > 0 && OrderCloseTime() > 0)
        {
         LongOrder = 0;
        }
     }                           
   if(OrderSelect(ShortOrder,SELECT_BY_TICKET) == true)
     {
      if(OrderTicket() > 0 && OrderCloseTime() > 0)
        {
         ShortOrder = 0;
        }
     }                           
     
  }
   
//+------------------------------------------------------------------+
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. Display immediatly from OnCalculate...
Sergey Golubev
Moderator
114214
Sergey Golubev  

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

Thank you.

William Roeder
20207
William Roeder  
  1.       if(Close[1]>MAkurz)
            {
             LongSignal = true;
            }
          else
           {
             LongSignal = false;
           }        
    Simplify your code.
              Increase Order after stoploss - MQL4 and MetaTrader 4 - MQL4 programming forum № 3

  2. if(LongSignal == true)
    You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  3. OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,0,Orange);
    Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  4. extern int LongOrder, ShortOrder;
    
          if(ShortOrder>0)
            {
             if(OrderSelect(ShortOrder,SELECT_BY_TICKET) == true)
                 // close
    If you ever set either of those two inputs to non-zero (invalid ticket numbers,) then the OrderSelect will always fail, and nothing will ever happen, and you won't know why.
    1. № 3
    2. 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 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 loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.
thomas2004
372
thomas2004  
whroeder1:
  1. Simplify your code.
              Increase Order after stoploss - MQL4 and MetaTrader 4 - MQL4 programming forum № 3

  2. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  3. Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  4. If you ever set either of those two inputs to non-zero (invalid ticket numbers,) then the OrderSelect will always fail, and nothing will ever happen, and you won't know why.
    1. № 3
    2. 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 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 loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

Hi whroeder,

Many thanks! It works now.

To add comments, please log in or register