Strategy Tester : no trades at all

 

Hello,


I have tested this code on a live session and it 'works' well (it opens trades):


#property copyright "Aymeric"
#property link      "https://www.mysite.com"
#property version   "1.00"
#property strict

#include <Trade\Trade.mqh>
CTrade trade;

input int RSI_Period = 14;
input double OverBought = 92.51;
input double OverSold = 7.49;

input double TakeProfitLevel = 300; // TP in points = 30 pips
input double StopLossLevel = 50; // SL in points = 5 pips


enum ENUM_MARGIN_LOTS_MODE {
   MARGIN_LOTS_ALL,
   MARGIN_LOTS_CALL,
   MARGIN_LOTS_STOPOUT
};

input ENUM_MARGIN_LOTS_MODE themode = MARGIN_LOTS_CALL;


// returns the number of lots tradable based on free margin
double MarginLotsFree(string symbol, ENUM_MARGIN_LOTS_MODE mode) {

   double freeMargin = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   
   double equity = AccountInfoDouble(ACCOUNT_BALANCE); // only used for the CALL & STOPOUT lvls
   
   ENUM_ACCOUNT_STOPOUT_MODE stopoutMode = 
   (ENUM_ACCOUNT_STOPOUT_MODE)AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE);
   
   double marginCall = AccountInfoDouble(ACCOUNT_MARGIN_SO_CALL);
   double marginStopOut = AccountInfoDouble(ACCOUNT_MARGIN_SO_SO);
   
   // value (in our account currenry) at which the broker will make a call (saying that we must refill)
   double callValue = (stopoutMode==ACCOUNT_STOPOUT_MODE_PERCENT) ? equity * marginCall/100 : marginCall;
   
   // value (in our account currenry) at which the broker will start closing the trades
   double stopoutValue = (stopoutMode==ACCOUNT_STOPOUT_MODE_PERCENT) ? equity*marginStopOut/100 : marginStopOut;
   
   // Adjust available margin based on mode 
   double availableMargin = freeMargin;
   switch(mode) {
      case MARGIN_LOTS_CALL:
         availableMargin = freeMargin - callValue;
         break;
      case MARGIN_LOTS_STOPOUT:
         availableMargin = freeMargin - stopoutValue;
         break;
   }
   
   // = how much margin required to trade 1 lot of that symbol
   //double marginPerLot = SymbolInfoDouble(_Symbol, SYMBOL_MARGIN_INITIAL);
   double marginPerLot = MarginRequired(_Symbol);
   
   PrintFormat("Available margin = %.2f", availableMargin);
   PrintFormat("Current marginPerLot = %.2f", marginPerLot);
   double lots = availableMargin/marginPerLot;
   lots = lots - 0.01;
   PrintFormat("Resulting LOT size = %.2f", lots);
   return NormalizeDouble(lots, 2);
 
}



double MarginRequired(string symbol) {

   double rate = 0;
   ENUM_ORDER_TYPE type = ORDER_TYPE_BUY;
   double price = SymbolInfoDouble(symbol, SYMBOL_ASK);
   if(!OrderCalcMargin(type, symbol, 1.0, price, rate)) {
      return(0.0);
   }
   return rate;

}

// Global variable to store the time of the last bar
datetime lastBarTime = 0;


void OnTick()
{


   //double freeMargin1 = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   //PrintFormat("freeMargin = %.2f",freeMargin1);
  
   // Get the current time of the last bar
   datetime currentBarTime = iTime(_Symbol, 0, 0);
   
   // If a new bar has not opened, return
   if (currentBarTime == lastBarTime)
     return;
   
   // Update the time of the last bar
   lastBarTime = currentBarTime;

  if (!isMarketOpen()) return;
  
  
  double rsi = CalculateRSI();
 
  PrintFormat("rsi = %.2f",rsi);
  PrintFormat("OverBought = %.2f",OverBought);
  PrintFormat("PositionsTotal = %.2f",PositionsTotal());
  
  Print(rsi > OverBought);
  if (rsi > OverBought && PositionsTotal() == 0)
  {
    Print("SHOULD SELL");
    OpenSell();
  }
  else if (rsi < OverSold && PositionsTotal() == 0)
  {
    Print("SHOULD BUY");
    OpenBuy();
  }
}

double CalculateRSI()
{
  int handle = iRSI(_Symbol, 0, RSI_Period, PRICE_CLOSE);
  double rsi[1];
  CopyBuffer(handle, 0, 1, 1, rsi);
  return rsi[0];
}


bool isMarketOpen() 
{
   datetime current_time = TimeCurrent();
   
   MqlDateTime str1;
   
   TimeToStruct(current_time,str1);;
   
   int day_of_week = str1.day_of_week;
   int hour = str1.hour;
  
   
   // Forex market is closed on Saturday and Sunday
   if(day_of_week >= 5) 
   {
      return false;
   }
   if (hour > 17 || hour < 7) {
      return false;
   }
   
   return true;
}



//...
void OpenBuy()
{
   
  //double volume = MarginLotsFree(_Symbol, MARGIN_LOTS_ALL);
  //double volume = MarginLotsFree(_Symbol, MARGIN_LOTS_CALL);
  double volume = MarginLotsFree(_Symbol, themode);
  
  //Print(volume);
  double Ask, Bid;
  SymbolInfoDouble(_Symbol, SYMBOL_ASK, Ask);
  SymbolInfoDouble(_Symbol, SYMBOL_BID, Bid);
  
  long minstoplevel = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);
  double sl = NormalizeDouble(Ask - ((StopLossLevel + minstoplevel) * _Point), _Digits);
  double tp = NormalizeDouble(Ask + ((TakeProfitLevel + minstoplevel) * _Point), _Digits);
  
  
  PrintFormat("SL = %.2f",sl);
  PrintFormat("TP = %.2f",tp);
  PrintFormat("Volume to buy = %.2f",volume);
  
  if(!trade.Buy(volume, _Symbol, Ask, sl, tp)) {
    Print("Failed to open Buy order: ", GetLastError());
  }
}

void OpenSell()
{
  //double volume = MarginLotsFree(_Symbol, MARGIN_LOTS_ALL);
  //double volume = MarginLotsFree(_Symbol, MARGIN_LOTS_CALL);
  double volume = MarginLotsFree(_Symbol, themode);
  
  double Ask, Bid;
  
  
  SymbolInfoDouble(_Symbol, SYMBOL_ASK, Ask);
  SymbolInfoDouble(_Symbol, SYMBOL_BID, Bid);
  
  long minstoplevel = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);
  double sl = NormalizeDouble(Bid + ((StopLossLevel + minstoplevel) * _Point), _Digits);
  double tp = NormalizeDouble(Bid - ((TakeProfitLevel + minstoplevel) * _Point), _Digits);
  
  
  PrintFormat("SL = %.2f",sl);
  PrintFormat("TP = %.2f",tp);
  PrintFormat("Volume to sell = %.2f",volume);
  
  
  if(!trade.Sell(volume, _Symbol, Bid, sl, tp)) {
    Print("Failed to open Sell order: ", GetLastError());
  }
}
//...


But when I test if with the strategy tester:


No trades never opens during the testing:




Do you see any reason why ?


Thanks


[EDIT] I also removed the 'themode' input and fixed the value and I also arranged the values for the RSI threshold (OverBought and OverSold) so that it should be triggered easily



But still does not work...

 
AYMERIC75:

Hello,


I have tested this code on a live session and it 'works' well (it opens trades):



But when I test if with the strategy tester:


No trades never opens during the testing:




Do you see any reason why ?


Thanks


[EDIT] I also removed the 'themode' input and fixed the value and I also arranged the values for the RSI threshold (OverBought and OverSold) so that it should be triggered easily



But still does not work...

was a problem of having a 1:1 leverage in the settings, I set it to 1:100 and it works
Reason: