Strategy Tester - Not doing any trades

 

Hi Forum,

I have the following code, but for some reason when I run the strategy tester, it does not do any trades - so my results page is just blank.. 

(Sorry, wrong formatted code before)

// Input parameters
input double RiskPercentage = 1.0; // Risk percentage per trade
input int TakeProfit = 300; // Take profit in pips

// Define indicators
input int FastEMA_Period = 20;
input int SlowEMA_Period = 50;
input int RSI_Period = 14;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   double lotSize;
   double stopLoss;
   double takeProfit;
   int ticket;

   // Calculate lot size based on risk percentage
   lotSize = CalculateLotSize(RiskPercentage);

   // Check for short trade conditions
   if (IsShortTradeConditionsMet())
     {
      // Calculate stop loss and take profit levels
      stopLoss = iClose("XAUUSD",PERIOD_H4,1); // Previous green candle close
      takeProfit = Bid - TakeProfit * Point;

      // Open short trade
      ticket = OrderSend("XAUUSD", OP_SELL, lotSize, Bid, 3, stopLoss, takeProfit, "Short Trade", 0, clrNONE);
     }

   // Check for long trade conditions
   if (IsLongTradeConditionsMet())
     {
      // Calculate stop loss and take profit levels
      stopLoss = iClose("XAUUSD",PERIOD_H4,1); // Previous red candle close
      takeProfit = Ask + TakeProfit * Point;

      // Open long trade
      ticket = OrderSend("XAUUSD", OP_BUY, lotSize, Ask, 3, stopLoss, takeProfit, "Long Trade", 0, clrNONE);
     }
  }
//+------------------------------------------------------------------+

bool IsShortTradeConditionsMet()
  {
   // Check if EMA conditions are met
   bool emaCondition = iMA("XAUUSD", PERIOD_H4, FastEMA_Period, 0, MODE_EMA, PRICE_CLOSE, 0) > iMA("XAUUSD", PERIOD_H4, SlowEMA_Period, 0, MODE_EMA, PRICE_CLOSE, 0);

   // Check if candle conditions are met
   bool candleCondition = Close[1] > iMA("XAUUSD", PERIOD_H4, FastEMA_Period, 1, MODE_EMA, PRICE_CLOSE, 1);

   // Check if RSI condition is met
   bool rsiCondition = iRSI("XAUUSD", PERIOD_H4, RSI_Period, PRICE_CLOSE, 0) > 70;

   // Check if previous candle is green
   bool prevCandleCondition = Close[2] > Open[2] && Close[1] < Open[1];

   return emaCondition && candleCondition && rsiCondition && prevCandleCondition;
  }

bool IsLongTradeConditionsMet()
  {
   // Check if EMA conditions are met
   bool emaCondition = iMA("XAUUSD", PERIOD_H4, FastEMA_Period, 0, MODE_EMA, PRICE_CLOSE, 0) < iMA("XAUUSD", PERIOD_H4, SlowEMA_Period, 0, MODE_EMA, PRICE_CLOSE, 0);

   // Check if candle conditions are met
   bool candleCondition = Close[1] < iMA("XAUUSD", PERIOD_H4, FastEMA_Period, 1, MODE_EMA, PRICE_CLOSE, 1);

   // Check if RSI condition is met
   bool rsiCondition = iRSI("XAUUSD", PERIOD_H4, RSI_Period, PRICE_CLOSE, 0) < 30;

   // Check if previous candle is red
   bool prevCandleCondition = Close[2] < Open[2] && Close[1] > Open[1];

   return emaCondition && candleCondition && rsiCondition && prevCandleCondition;
  }

double CalculateLotSize(double riskPercentage)
  {
   double accountBalance = AccountBalance();
   double lotSize = (accountBalance * riskPercentage) / 100 / MarketInfo("XAUUSD",MODE_MARGINREQUIRED);

   return lotSize;
  }

Improperly formatted code removed by moderator.

 

It is working for me:

caution:

1. make sure to use symbol XAUUSD in strategy tester. MT4 stratester cannot open trades other than the symbol you are testing.

2. make sure that your symbol name is exactly XAUUSD. This EA is designed only for symbol XAUUSD.

 
Yashar Seyyedin #:

It is working for me:

caution:

1. make sure to use symbol XAUUSD in strategy tester. MT4 stratester cannot open trades other than the symbol you are testing.

2. make sure that your symbol name is exactly XAUUSD. This EA is designed only for symbol XAUUSD.

Hmm, I seem to be doing everything correctly.. I doesn’t open any trades at all.
 Can I ser ypur setting?

 
Markise Mikeli #:
Hmm, I seem to be doing everything correctly.. I doesn’t open any trades at all.
 Can I ser ypur setting?

My symbol is "XAUUSD.I"
So I had to modify the symbol in the source code. 
What is your exact symbol name for gold?

 
Yashar Seyyedin #:
My symbol is "XAUUSD.I"
So I had to modify the symbol in the source code. 
What is your exact symbol name for gold?

See attached picture. The code i linked in my original comment gave me some results, however, it seems like they didn't change when i changed parameters, period etc. 


I then tried to change the name in the code to "XAUUSD, Gold", then i get no results at all

 
Markise Mikeli #:

See attached picture. The code i linked in my original comment gave me some results, however, it seems like they didn't change when i changed parameters, period etc. 


I then tried to change the name in the code to "XAUUSD, Gold", then i get no results at all

Your time frame is hard coded: h4.
Changes to stratester won't affect.
 

Because tester can only use the symbol and period you've selected, you could use the chart variables 

int period=_Period;
string sym=_Symbol;

/*
then, throughout your code...

 stopLoss = iClose(sym,period,1)
 etc

*/

Reason: