RSI Issues when trying to code my own EA

 


I am trying to practice my coding EA skills and created an EA around RSI levels. 

When i try to back test the ea, it opens a position up on RSI level between 50-60 (range) and the example input variables are based on 70 and 30. Is this a coding issue or something to do with the back test / days off / holidays or something i am missing completely? I tried to add a print of the RSI level which showed once at 20 but not for anything else (wouldnt show, said 4756 error or something)

(2) When i back test and try to strategy optimize it also adds in rsi_buy and rsi_sell levels above 100 - i assume this is a known error or is it a cause from the code below?

(3) Also when i try to add a decimal (.) it wont let me under rsi level say 35.99 or 65.99, it wont let me add in a decimal in strategy tester?

Appreciate any assistance! ***I just notice i forgot to add in code to only open 1 position ** please ignore this factor - adding in

input double RSI_Buy = 35;
input double RSI_Sell = 65;
input int StopLoss = 250;
input int TakeProfit = 500;
input double Lots = 0.10;
input double RSI_Buy = 19.99;
input double RSI_Sell = 80.01;

bool positionOpened = false;

int OnInit()
{
   // Perform initialization tasks if needed
   return(INIT_SUCCEEDED);
}

void OnTick()
{
   double rsi = iRSI(_Symbol, PERIOD_M15, 14, POSITION_PRICE_CURRENT);

   // Check for Buy condition
   if (!positionOpened && rsi < RSI_Buy)
   {
      // Print RSI values for debugging
      Print("RSI (M15): ", rsi);

      // Open a Buy position
      OpenPosition(ORDER_TYPE_BUY);
   }

   // Check for Sell condition
   if (!positionOpened && rsi > RSI_Sell)
   {
      // Print RSI values for debugging
      Print("RSI (M15): ", rsi);

      // Open a Sell position
      OpenPosition(ORDER_TYPE_SELL);
   }
}

void OpenPosition(int orderType)
{
   MqlTradeRequest request;
   MqlTradeResult result;

   ZeroMemory(request);

   request.action = TRADE_ACTION_DEAL;
   request.symbol = _Symbol;
   request.volume = Lots;
   request.type_filling = ORDER_FILLING_FOK;
   request.type = (ENUM_ORDER_TYPE)orderType;
   request.price = orderType == ORDER_TYPE_BUY ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) : SymbolInfoDouble(_Symbol, SYMBOL_BID);
   request.sl = orderType == ORDER_TYPE_BUY ? NormalizeDouble(request.price - StopLoss * _Point, _Digits) : NormalizeDouble(request.price + StopLoss * _Point, _Digits);
   request.tp = orderType == ORDER_TYPE_BUY ? NormalizeDouble(request.price + TakeProfit * _Point, _Digits) : NormalizeDouble(request.price - TakeProfit * _Point, _Digits);

   if (OrderSend(request, result) > 0)
   {
      Print("Order placed successfully: ", orderType, ", ", Lots, " lots at ", request.price);
      positionOpened = true;  // Set the flag to indicate that a position is opened
   }
   else
   {
      Print("OrderSend failed with error: ", GetLastError());
   }
}

void OnDeinit(const int reason)
{
   // Perform deinitialization tasks if needed
}
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 
Delete, figured it out