How to get bull/bear candle? - page 2

 
Mohammed Tijjani #:

I have edited your post. In future please use the CODE button when you insert code.

Code button in editor

 
Fernando Carreiro #:

I have edited your post. In future please use the CODE button when you insert code.

You know am new member that why
Thank you sir
 
Fernando Carreiro #:

That is MQL4 code, not MQL5. MQL5 does not support predefined array variables for timeseries rates data (Open[], Close[], etc.).

Those predefined variables don't exist in MQL5. You will have to convert all of your code into proper MQL5.

Or define them. Using global arrays like Open[], Close[], Low[], High[] in custom indicator for MetaTrader 5 - MQL5 programming forum (2023)

 
Mohammed Tijjani #:
Is not
Working because your parameter never gets updated. You need to placed it inside OnTick() 
 
I got it 
Thank you so much
 
#include <Trade\Trade.mqh>

bool bullish = iOpen(Symbol(),_Period,1) < iClose(Symbol(),_Period,1) ? true : false;

CTrade trade;

int barsTotal;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void sell()
  {

// if(OrdersTotal() == 0)
     {
      double bid =NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID),_Digits);
      trade.Sell(0.1,_Symbol,bid);
     }
  }



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void buy()
  {
// if(OrdersTotal() == 0)

     {
      double ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      trade.Buy(0.1,_Symbol,ask);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   close_opration();


   int bars = iBars(_Symbol,PERIOD_CURRENT);
   if(barsTotal != bars)
     {
      barsTotal = bars;

      if(iOpen(Symbol(),_Period,1) < iClose(Symbol(),_Period,1))
        {
         bullish = true;
        }
      else
        {
         bullish = false;
        }


      if(bullish)
        {
          buy();
        }
      else
         if(!bullish) //Negating the bullish hence making it false.
           {
            sell();
           }
     }
  }
An amendment in the code @
 
  1. Asomah Prince #:
    bool bullish = iOpen(Symbol(),_Period,1) < iClose(Symbol(),_Period,1) ? true : false;

    That is not an assignment; it's initialization of a common (globally declared), or static variable(s) 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 indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), 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. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  2.    int bars = iBars(_Symbol,PERIOD_CURRENT);
       if(barsTotal != bars)
         {
          barsTotal = bars;

    You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed, and that tick could arrive almost at the end of a bar's duration.

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              MT4: New candle - MQL4 programming forum #3 (2014)
              MT5: Accessing variables - MQL4 programming forum #3 (2022)

    I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

  3. Your code
          if(iOpen(Symbol(),_Period,1) < iClose(Symbol(),_Period,1))
            {
             bullish = true;
            }
          else
            {
             bullish = false;
            }
    Simplified.
    Increase Order after stoploss - MQL4 programming forum #1.3 (2017)
          bullish = iOpen(Symbol(),_Period,1) < iClose(Symbol(),_Period,1);