why can'ti place orders.....please coders

 
//+------------------------------------------------------------------+
//|                                                    Strategy1.mq5 |
//|                                                              UJE |
//|                                        udohjeremiah231@gmail.com |
//+------------------------------------------------------------------+
#property copyright "UJE"
#property link      "udohjeremiah231@gmail.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Included files                                                   |
//+------------------------------------------------------------------+
#include  <Trade\Trade.mqh>
CTrade mytrade;
//+------------------------------------------------------------------+
//|Input files                                                       |
//+------------------------------------------------------------------+
input int vol = 1;      //Lot size
//+------------------------------------------------------------------+
//|Class and function definitions                                    |
//+------------------------------------------------------------------+
class Trade
{
   public:
   bool openposition;      //Is there a position opened?
   long postype;           //What kind of position is opened?
   ENUM_ORDER_TYPE signal; //Is the char giving any signal and what signal is it giving?
   
   //---Check for buy signal
   void BuySignal(void)
   {
      //---Position and signal details
      openposition = PositionSelect(_Symbol);
      postype      = PositionGetInteger(POSITION_TYPE);
      signal       = WRONG_VALUE;
      
      //---Array to hold the bars data
      MqlRates bar[];
      CopyRates(_Symbol,_Period,0,7,bar);
      
      //Check buy signal
      if((bar[3].open < bar[2].open && bar[3].close < bar[2].close)&&
         (bar[2].open < bar[1].open && bar[2].close < bar[1].close)&&
         (bar[1].open < bar[0].open && bar[1].close < bar[0].close))
            signal = true;
            
       if(signal == true && openposition == false)
       {
         double price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         double sl    = bar[2].low;
         double tp    = price + ((price - sl) * 3);
         mytrade.Buy(vol,_Symbol,price,sl,tp,"Long");
       }
    }
   //---Check for sell signal
   void SellSignal(void)
   {
      //---Position and signal details
      openposition = PositionSelect(_Symbol);
      postype      = PositionGetInteger(POSITION_TYPE);
      signal       = WRONG_VALUE;
      
      //---Array to hold the bars data
      MqlRates bar[];
      CopyRates(_Symbol,_Period,0,7,bar);
      
      if((bar[3].open > bar[2].open && bar[3].close > bar[2].close)&&
         (bar[2].open > bar[1].open && bar[2].close > bar[1].close)&&
         (bar[1].open > bar[0].open && bar[1].close > bar[0].close))
            signal = true;
            
       if(signal == true && openposition == false)
       {
         double price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
         double sl    = bar[2].high;
         double tp    = price + ((sl - price) * 3);
         mytrade.Sell(vol,_Symbol,price,sl,tp,"Short"); 
       }
    }
};
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---Place trade
   Trade now;           //Define an object to the Trade class
   now.BuySignal();       //Buy when there is a buy signal
   now.SellSignal();      //Sell when there is a sell signal
  }
//+------------------------------------------------------------------+

the strategy codes without compilation errors but won't place orders in live or strategy tester... pls someone help

 

New code:

//+------------------------------------------------------------------+
//|                                                    Strategy1.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
#include  <Trade\Trade.mqh>
//---
CTrade         m_trade;                      // object of CTrade class
//--- input parameters
input double   InpVolume   = 1.0;            // Lot size
//---
//+------------------------------------------------------------------+
//|Class and function definitions                                    |
//+------------------------------------------------------------------+
class CMyTrade : public CObject
  {
public:
                     CMyTrade();
                    ~CMyTrade();
   //--- check BUY Signal and open BUY position
   void              BuySignal(void);
   //--- check SELL Signal and open SELL position
   void              SellSignal(void);

  };
//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CMyTrade::CMyTrade()
  {
  }
//+------------------------------------------------------------------+
//| Dectructor                                                       |
//+------------------------------------------------------------------+
CMyTrade::~CMyTrade()
  {
  }
//+------------------------------------------------------------------+
//| Buy Signal                                                       |
//+------------------------------------------------------------------+
void CMyTrade::BuySignal(void)
  {
//--- position and signal details
   if(PositionSelect(Symbol()))
      return;
//--- array to hold the bars data
   MqlRates bar[];
   CopyRates(Symbol(),Period(),0,7,bar);
//--- check buy signal
   bool buy_signal=((bar[3].open < bar[2].open && bar[3].close < bar[2].close)&&
                    (bar[2].open < bar[1].open && bar[2].close < bar[1].close)&&
                    (bar[1].open < bar[0].open && bar[1].close < bar[0].close));

   if(buy_signal)
     {
      double price = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
      double sl    = bar[2].low;
      double tp    = price + ((price - sl) * 3);
      m_trade.Buy(InpVolume,Symbol(),price,sl,tp,"Long");
     }
  }
//+------------------------------------------------------------------+
//| Sell Signal                                                      |
//+------------------------------------------------------------------+
void CMyTrade::SellSignal(void)
  {
//--- position and signal details
   if(PositionSelect(Symbol()))
      return;
//--- array to hold the bars data
   MqlRates bar[];
   CopyRates(Symbol(),Period(),0,7,bar);
//--- check sell signal
   bool sell_signal=((bar[3].open > bar[2].open && bar[3].close > bar[2].close)&&
                     (bar[2].open > bar[1].open && bar[2].close > bar[1].close)&&
                     (bar[1].open > bar[0].open && bar[1].close > bar[0].close));

   if(sell_signal)
     {
      double price = SymbolInfoDouble(Symbol(),SYMBOL_BID);
      double sl    = bar[2].high;
      double tp    = price + ((sl - price) * 3.0);
      m_trade.Sell(InpVolume,Symbol(),price,sl,tp,"Short");
     }
  }
//---
CMyTrade       m_mytrade;                    // object of CMyTrade class
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

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

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   m_mytrade.BuySignal();
   m_mytrade.SellSignal();
  }
//+------------------------------------------------------------------+
Files:
Strategy1.mq5  5 kb
 

i appreciate man...but the strategy still does not place trades

 
udoh.jeremiah.emem :

i appreciate man...but the strategy still does not place trades

The code that I have provided is working.

He definitely opens up BUY positions.

And as for the SELL positions, you need to slightly change the signal parameter.

And in general - learn to use a strategy tester before saying: "Something does not work for me" :)


Add: SELL positions Open, but very rarely.

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 

One more note: you get OHLC data into the bar array, but you completely forget to expand the array

ArraySetAsSeries(bar,true);

 Version 1.001:

//+------------------------------------------------------------------+
//|                                                    Strategy1.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.001"
//---
#include  <Trade\Trade.mqh>
//---
CTrade         m_trade;                      // object of CTrade class
//--- input parameters
input double   InpVolume   = 1.0;            // Lot size
//---
//+------------------------------------------------------------------+
//|Class and function definitions                                    |
//+------------------------------------------------------------------+
class CMyTrade : public CObject
  {
public:
                     CMyTrade();
                    ~CMyTrade();
   //--- check BUY Signal and open BUY position
   void              BuySignal(void);
   //--- check SELL Signal and open SELL position
   void              SellSignal(void);

  };
//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CMyTrade::CMyTrade()
  {
  }
//+------------------------------------------------------------------+
//| Dectructor                                                       |
//+------------------------------------------------------------------+
CMyTrade::~CMyTrade()
  {
  }
//+------------------------------------------------------------------+
//| Buy Signal                                                       |
//+------------------------------------------------------------------+
void CMyTrade::BuySignal(void)
  {
//--- position and signal details
   if(PositionSelect(Symbol()))
      return;
//--- array to hold the bars data
   MqlRates bar[];
   ArraySetAsSeries(bar,true);
   CopyRates(Symbol(),Period(),0,7,bar);
//--- check buy signal
   bool buy_signal=((bar[3].open < bar[2].open && bar[3].close < bar[2].close)&&
                    (bar[2].open < bar[1].open && bar[2].close < bar[1].close)&&
                    (bar[1].open < bar[0].open && bar[1].close < bar[0].close));

   if(buy_signal)
     {
      double price = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
      double sl    = bar[2].low;
      double tp    = price + ((price - sl) * 3);
      m_trade.Buy(InpVolume,Symbol(),price,sl,tp,"Long");
     }
  }
//+------------------------------------------------------------------+
//| Sell Signal                                                      |
//+------------------------------------------------------------------+
void CMyTrade::SellSignal(void)
  {
//--- position and signal details
   if(PositionSelect(Symbol()))
      return;
//--- array to hold the bars data
   MqlRates bar[];
   ArraySetAsSeries(bar,true);
   CopyRates(Symbol(),Period(),0,7,bar);
//--- check sell signal
   bool sell_signal=((bar[3].open > bar[2].open && bar[3].close > bar[2].close)&&
                     (bar[2].open > bar[1].open && bar[2].close > bar[1].close)&&
                     (bar[1].open > bar[0].open && bar[1].close > bar[0].close));

   if(sell_signal)
     {
      double price = SymbolInfoDouble(Symbol(),SYMBOL_BID);
      double sl    = bar[2].high;
      double tp    = price + ((sl - price) * 3.0);
      m_trade.Sell(InpVolume,Symbol(),price,sl,tp,"Short");
     }
  }
//---
CMyTrade       m_mytrade;                    // object of CMyTrade class
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

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

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   m_mytrade.BuySignal();
   m_mytrade.SellSignal();
  }
//+------------------------------------------------------------------+
Files:
Strategy1.mq5  5 kb
 
ok man thanks...but if i may ask how do u use the strategy tester on this EA?
 
udoh.jeremiah.emem :
ok man thanks...but if i may ask how do u use the strategy tester on this EA?

I run this code from the MetaEditor.

My settings


My actions:


 
udoh.jeremiah.emem:

You have been warned about duplicating topics. You can expect a ban if you continue to do so.