RSI triggers not sending alerts or orders

 

Hi everyone. I have been working on a simple RSI expert advisor and I can't get it to ever actually trigger the alerts that I've coded. I'm thinking I've made a mistake somewhere, but I've gone through this like a million times line-by-line and I can't figure out where the problem is. If any of you get a spare moment, I would appreciate it more than you know if you could just glance over this for obvious errors. I promise I've tried to figure it out myself first.


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

  int o = OrdersTotal();
  int r = (iRSI(NULL,NULL,14,PRICE_CLOSE,1));

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if((r > 70) && (o = 0))
      {
      Alert("High RSI detected. New position suggested.");
      }
   else if((r < 30) && (o = 0))
      {
      Alert("Low RSI detected. New position suggested.");
      }
  }
//+------------------------------------------------------------------+
 
John Corum:

Hi everyone. I have been working on a simple RSI expert advisor and I can't get it to ever actually trigger the alerts that I've coded. I'm thinking I've made a mistake somewhere, but I've gone through this like a million times line-by-line and I can't figure out where the problem is. If any of you get a spare moment, I would appreciate it more than you know if you could just glance over this for obvious errors. I promise I've tried to figure it out myself first.


Try this;

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

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
double r = iRSI(NULL,Period(),14,PRICE_CLOSE,1);
//---
   if((r > 70) && (OrdersTotal() < 1))
      {
      Alert("High RSI detected. New position suggested.");
      }
   else if((r < 30) && (OrdersTotal() < 1))
      {
      Alert("Low RSI detected. New position suggested.");
      }
  }
//+------------------------------------------------------------------
 
  int o = OrdersTotal();
  int r = (iRSI(NULL,NULL,14,PRICE_CLOSE,1));

Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

    Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
John Corum:

Hi everyone. I have been working on a simple RSI expert advisor and I can't get it to ever actually trigger the alerts that I've coded. I'm thinking I've made a mistake somewhere, but I've gone through this like a million times line-by-line and I can't figure out where the problem is. If any of you get a spare moment, I would appreciate it more than you know if you could just glance over this for obvious errors. I promise I've tried to figure it out myself first.


void OnTick()
  {
  double r = iRSI(Symbol(),PERIOD_CURRENT,14,PRICE_CLOSE,1);
  char o = OrdersTotal();

  
   if (r > 70 && o ==0)
      {
      Alert("in ",Symbol()," High RSI detected. New position suggested.");
      }
   else if (r <30 && o ==0)
      {
      Alert("in ",Symbol()," Low RSI detected. New position suggested.");
      }

try this one

Reason: