Download MetaTrader 5

stop loss limit is not executed in strategy tester

To add comments, please log in or register
MQFOURL
7
MQFOURL  

hello,

i hav ebuild a simple strategy tester, but I can't figure out why the stop loss limits are simply not executed in the strategy tester. the take profit limit in contrary is being executed.

 

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "XXX"
#property link      "XXX"
#property version   "1.04"
#property strict
//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
double initial_stop_loss_limit=0.0010;

double initial_take_profit_limit=0.0010;

datetime current_m5_candle_time;

bool new_m5_candle_has_started;

int magic_number=1234567890;

bool short_order_entry_signal;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   current_m5_candle_time=iTime(NULL,PERIOD_M5,0);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//----CANDLE TIME FUNCTION - TICK MAIN LEVEL----\\
//----CANDLE TIME FUNCTION - TICK MAIN LEVEL----\\
//----CANDLE TIME FUNCTION - TICK MAIN LEVEL----\\
   if(current_m5_candle_time!=iTime(NULL,PERIOD_M5,0))
     {
      new_m5_candle_has_started=true;
      current_m5_candle_time=iTime(NULL,PERIOD_M5,0);
     }
   else
     {
      new_m5_candle_has_started=false;
     }
//----CANDLE TIME FUNCTION - TICK MAIN LEVEL----\\
//----CANDLE TIME FUNCTION - TICK MAIN LEVEL----\\
//----CANDLE TIME FUNCTION - TICK MAIN LEVEL----\\

//---------------------------------------------------------------------------------------------------\\
//---------------------------------------------------------------------------------------------------\\
//---------------------------------------------------------------------------------------------------\\

//----ENTRY OBSERVATION PATTERN AND ENTRY SIGNAL GENERATION----\\
//----ENTRY OBSERVATION PATTERN AND ENTRY SIGNAL GENERATION----\\
//----ENTRY OBSERVATION PATTERN AND ENTRY SIGNAL GENERATION----\\

   if(new_m5_candle_has_started==true)
     {

      int m5_candle_range_05=5;
      double m5_candle_range_05_rsi_sum=0;
      double m5_candle_range_05_rsi_avg=0;

      double m5_candle_rsi_01=iRSI(NULL,PERIOD_M5,14,PRICE_CLOSE,1);

      for(int i=1; i<=m5_candle_range_05;++i)
        {
         m5_candle_range_05_rsi_sum+=iRSI(NULL,PERIOD_M5,14,PRICE_CLOSE,i);
        }

      m5_candle_range_05_rsi_avg=m5_candle_range_05_rsi_sum/m5_candle_range_05;

      if(m5_candle_range_05_rsi_avg>75)
        {
         short_order_entry_signal=true;
         Print("SHORT ORDER ENTRY SIGNAL HAS BEEN GENERATED !!!");
        }
     }

//----SHORT ORDER GENERATION AND INITIAL STOP LOSS LIMIT GENERATION AND INITIAL TAKE PROFIT LIMIT GENERATION----\\
//----SHORT ORDER GENERATION AND INITIAL STOP LOSS LIMIT GENERATION AND INITIAL TAKE PROFIT LIMIT GENERATION----\\
//----SHORT ORDER GENERATION AND INITIAL STOP LOSS LIMIT GENERATION AND INITIAL TAKE PROFIT LIMIT GENERATION----\\
   if(short_order_entry_signal==true)
     {
      int short_order_ticket_number=0;
      while(short_order_ticket_number<=0)
        {
         short_order_ticket_number=OrderSend(Symbol(),OP_SELL,0.1,Bid,0,0,0,NULL,0,0,clrMagenta);
         if(short_order_ticket_number<0)
           {
            Print("WARNING !!! SHORT ORDER HAS NOT BEEN GENERATED !!! ERROR (",GetLastError(),")");
           }
         else if(OrderSelect(short_order_ticket_number,SELECT_BY_TICKET)==true)
           {
            short_order_entry_signal=false;
            Print("SHORT ORDER HAS BEEN GENERATED !!! TICKET #",short_order_ticket_number);
           }

         if(OrderSelect(short_order_ticket_number,SELECT_BY_TICKET)==true)
           {
            //----INITIAL STOP LOSS LIMIT GENERATION----\\
            //----INITIAL STOP LOSS LIMIT GENERATION----\\
            //----INITIAL STOP LOSS LIMIT GENERATION----\\
            if(OrderCloseTime()==0 && OrderStopLoss()==0)
              {
                 {
                  double stop_loss_limit=NormalizeDouble(OrderOpenPrice()+initial_stop_loss_limit,Digits);
                  bool order_modified=OrderModify(OrderTicket(),OrderOpenPrice(),stop_loss_limit,OrderTakeProfit(),0,clrMagenta);
                  if(!order_modified)
                    {
                     Print("WARNING !!! INITIAL STOP LOSS LIMIT HAS NOT BEEN GENERATED !!! ERROR (",GetLastError(),")");
                    }
                  else
                    {
                     Print("INITIAL STOP LOSS LIMIT HAS BEEN GENERATED !!! TICKET #",short_order_ticket_number);
                    }
                 }
              }
            //----INITIAL TAKE PROFIT LIMIT GENERATION----\\
            //----INITIAL TAKE PROFIT LIMIT GENERATION----\\
            //----INITIAL TAKE PROFIT LIMIT GENERATION----\\
            if(OrderCloseTime()==0 && OrderTakeProfit()==0)
              {
               double take_profit_limit=NormalizeDouble(OrderOpenPrice()-initial_take_profit_limit,Digits);
               bool order_modified=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),take_profit_limit,0,clrMagenta);
               if(!order_modified)
                 {
                  Print("WARNING !!! INITIAL TAKE PROFIT LIMIT HAS NOT BEEN GENERATED !!! ERROR (",GetLastError(),")");
                 }
               else
                 {
                  Print("INITIAL TAKE PROFIT LIMIT HAS BEEN GENERATED !!! TICKET #",short_order_ticket_number);
                 }
              }
           }
        }
     }
//----SHORT ORDER GENERATION AND INITIAL STOP LOSS LIMIT GENERATION AND INITIAL TAKE PROFIT LIMIT GENERATION----\\
//----SHORT ORDER GENERATION AND INITIAL STOP LOSS LIMIT GENERATION AND INITIAL TAKE PROFIT LIMIT GENERATION----\\
//----SHORT ORDER GENERATION AND INITIAL STOP LOSS LIMIT GENERATION AND INITIAL TAKE PROFIT LIMIT GENERATION----\\
  }

 

Carl Schreiber
7364
Carl Schreiber  
Use Comment(..) to show all the meaningful values and slow down the tester and enlarge the resolution when you expect the close of your positions.
alexquan
7
alexquan  

the market chart has a MIN VALUE for stoploss and takeprofit...

double initial_stop_loss_limit=0.0010;  

This is a very small stoploss limit , its possible that is under the min. value. 

this is what i use 

               double StopLevel=(MarketInfo(Symbol(),MODE_STOPLEVEL)*Point);   

               double MinStop=(5*Point);

               double BandasuperiorSTOP=(ActualAsk+StopLevel+MinStop);      //formula for .....STOPLOSS for sell

               double BandainferiorSTOP=(ActualBid-StopLevel-MinStop);      //formula for ....STOPLOSS for buy

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

whroeder1
16098
whroeder1  
 double stop_loss_limit=NormalizeDouble(OrderOpenPrice()+initial_stop_loss_limit,Digits);
                  bool order_modified=OrderModify(OrderTicket(),OrderOpenPrice(),stop_loss_limit,OrderTakeProfit(),0,clrMagenta);
You buy at the Ask. You aren't adjusting for the spread. Use OrderClosePrice.
To add comments, please log in or register