Not opening any positions

 
hi, could you tell me why this code is not opening any positions?
#define EMA_MAGIC 123452413534876

//---

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>

#include <Trade\PositionInfo.mqh>

#include <Trade\AccountInfo.mqh>

//---

input double InpLots          =0.01;    // Lots

input int    InpStopLoss      =50;     // Stop Loss (in pips)

input int    InpTakeToStop    =2;       //Take profit/Stop Loss

input int    InpMAFastPeriod  =50;     // MA trend period

input int    InpMAMedPeriod   =100;    // MA trend period

input int    InpMASlowPeriod  =150;    // MA trend period

//---

int ExtTimeOut=10; // time out in seconds between trade operations



class CSampleExpert

  {

protected:

   double            m_adjusted_point;             // point value adjusted for 3 or 5 points

   CTrade            m_trade;                      // trading object

   CSymbolInfo       m_symbol;                     // symbol info object

   CPositionInfo     m_position;                   // trade position object

   CAccountInfo      m_account;                    // account info wrapper

   //--- indicators

   int               m_handle_fast;

   int               m_handle_med;

   int               m_handle_slow;                 // moving average indicator handle

   //--- indicator buffers

   double            m_buff_FAST[];

   double            m_buff_MED[];

   double            m_buff_SLOW[];                 // EMA indicator buffer

   //--- indicator data for processing

   double            m_fast_current;

   double            m_fast_previous;

   double            m_med_current;

   double            m_med_previous;

   double            m_slow_current;

   double            m_slow_previous;

   //---

   double            m_take_profit;

   double            m_stop_loss;



public:

                     CSampleExpert(void);

                    ~CSampleExpert(void);

   bool              Init(void);

   void              Deinit(void);

   bool              Processing(void);



protected:

   bool              InitCheckParameters(const int digits_adjust);

   bool              InitIndicators(void);

   bool              LongOpened(void);

   bool              ShortOpened(void);

  };

//--- global expert

CSampleExpert ExtExpert;

//+------------------------------------------------------------------+

//| Constructor                                                      |

//+------------------------------------------------------------------+

CSampleExpert::CSampleExpert(void) : m_adjusted_point(0),

                                     m_handle_fast(INVALID_HANDLE),

                                     m_handle_med(INVALID_HANDLE),

                                     m_handle_slow(INVALID_HANDLE),

                                     m_fast_current(0),

                                     m_fast_previous(0),

                                     m_slow_current(0),

                                     m_slow_previous(0),

                                     m_med_current(0),

                                     m_med_previous(0),

                                     m_stop_loss(0),

                                     m_take_profit(0)

  {

   ArraySetAsSeries(m_buff_FAST,true);

   ArraySetAsSeries(m_buff_MED,true);

   ArraySetAsSeries(m_buff_SLOW,true);

  }

//+------------------------------------------------------------------+

//| Destructor                                                       |

//+------------------------------------------------------------------+

CSampleExpert::~CSampleExpert(void)

  {

  }

//+------------------------------------------------------------------+

//| Initialization and checking for input parameters                 |

//+------------------------------------------------------------------+

bool CSampleExpert::Init(void)

  {

//--- initialize common information

   m_symbol.Name(Symbol());                  // symbol

   m_trade.SetExpertMagicNumber(EMA_MAGIC); // magic

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(Symbol());

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)

      digits_adjust=10;

   m_adjusted_point=m_symbol.Point()*digits_adjust;

//--- set default deviation for trading in adjusted points

   m_stop_loss       =InpStopLoss*m_adjusted_point;

   m_take_profit     =InpTakeToStop*m_stop_loss;

//--- set default deviation for trading in adjusted points

   m_trade.SetDeviationInPoints(3*digits_adjust);

//---

   if(!InitCheckParameters(digits_adjust))

      return(false);

   if(!InitIndicators())

      return(false);

//--- succeed

   return(true);

  }

//+------------------------------------------------------------------+

//| Checking for input parameters                                    |

//+------------------------------------------------------------------+

bool CSampleExpert::InitCheckParameters(const int digits_adjust)

  {

//--- initial data checks

   if(InpStopLoss*digits_adjust<m_symbol.StopsLevel())

     {

      printf("Stop Loss must be greater than %d",m_symbol.StopsLevel());

      return(false);

     }

//--- check for right lots amount

   if(InpLots<m_symbol.LotsMin() || InpLots>m_symbol.LotsMax())

     {

      printf("Lots amount must be in the range from %f to %f",m_symbol.LotsMin(),m_symbol.LotsMax());

      return(false);

     }

   if(MathAbs(InpLots/m_symbol.LotsStep()-MathRound(InpLots/m_symbol.LotsStep()))>1.0E-10)

     {

      printf("Lots amount is not corresponding with lot step %f",m_symbol.LotsStep());

      return(false);

     }

//--- succeed

   return(true);

  }

//+------------------------------------------------------------------+

//| Initialization of the indicators                                 |

//+------------------------------------------------------------------+

bool CSampleExpert::InitIndicators(void)

  {

//--- create EMA indicator and add it to collection

   if(m_handle_fast==INVALID_HANDLE)

      if((m_handle_fast=iMA(NULL,0,InpMAFastPeriod,0,MODE_EMA,PRICE_CLOSE))==INVALID_HANDLE)

        {

         printf("Error creating EMA indicator");

         return(false);

        }

   if(m_handle_med==INVALID_HANDLE)

      if((m_handle_med=iMA(NULL,0,InpMAMedPeriod,0,MODE_EMA,PRICE_CLOSE))==INVALID_HANDLE)

        {

         printf("Error creating EMA indicator");

         return(false);

        }

   if(m_handle_slow==INVALID_HANDLE)

      if((m_handle_slow=iMA(NULL,0,InpMASlowPeriod,0,MODE_EMA,PRICE_CLOSE))==INVALID_HANDLE)

        {

         printf("Error creating EMA indicator");

         return(false);

        }      

//--- succeed

   return(true);

  }

//+------------------------------------------------------------------+

//| Check for long position opening                                  |

//+------------------------------------------------------------------+

bool CSampleExpert::LongOpened(void)

  {

   bool res=false;

//--- check for long position (BUY) possibility

   if(m_med_current>m_slow_current)

      if(m_fast_current>m_med_current && m_fast_previous<m_med_previous)

           {

            double price=m_symbol.Ask();

            double tp   =m_symbol.Bid()+m_take_profit;

            double sl   =m_symbol.Bid()-m_stop_loss;

            //--- check for free money

            if(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_BUY,InpLots,price)<0.0)

               printf("We have no money. Free Margin = %f",m_account.FreeMargin());

            else

              {

               //--- open position

               if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,InpLots,price,sl,tp))

                  printf("Position by %s to be opened",Symbol());

               else

                 {

                  printf("Error opening BUY position by %s : '%s'",Symbol(),m_trade.ResultComment());

                  printf("Open parameters : price=%f,TP=%f",price,sl,tp);

                 }

              }

            //--- in any case we must exit from expert

            res=true;

           }

//--- result

   return(res);

  }

//+------------------------------------------------------------------+

//| Check for short position opening                                 |

//+------------------------------------------------------------------+

bool CSampleExpert::ShortOpened(void)

  {

   bool res=false;

//--- check for short position (SELL) possibility

   if(m_med_current<m_slow_current)

      if(m_fast_current<m_med_current && m_fast_previous>m_med_previous)

         {

            double price=m_symbol.Bid();

            double tp   =m_symbol.Ask()-m_take_profit;

            double sl   =m_symbol.Ask()+m_stop_loss;

            //--- check for free money

            if(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_SELL,InpLots,price)<0.0)

               printf("We have no money. Free Margin = %f",m_account.FreeMargin());

            else

              {

               //--- open position

               if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,InpLots,price,sl,tp))

                  printf("Position by %s to be opened",Symbol());

               else

                 {

                  printf("Error opening SELL position by %s : '%s'",Symbol(),m_trade.ResultComment());

                  printf("Open parameters : price=%f,TP=%f",price,sl,tp);

                 }

              }

            //--- in any case we must exit from expert

            res=true;

          }

//--- result

   return(res);

  }

//+------------------------------------------------------------------+

//| main function returns true if any position processed             |

//+------------------------------------------------------------------+

bool CSampleExpert::Processing(void)

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

      return(false);

//--- refresh indicators

   if(BarsCalculated(m_handle_fast)<2 || BarsCalculated(m_handle_med)<2 || BarsCalculated(m_handle_slow)<2)

      return(false);

   if(CopyBuffer(m_handle_fast,0,0,2,m_buff_FAST)       !=2 ||

      CopyBuffer(m_handle_med,0,0,2,m_buff_MED)         !=2 ||

      CopyBuffer(m_handle_slow,0,0,2,m_buff_SLOW)       !=2)

            return(false);

//--- data are put into internal variables

   m_fast_current    =m_buff_FAST[0];

   m_fast_previous   =m_buff_FAST[1];

   m_med_current    =m_buff_MED[0];

   m_med_previous   =m_buff_MED[1];

   m_slow_current    =m_buff_SLOW[0];

   m_slow_previous   =m_buff_SLOW[1];

//--- it is important to enter the market correctly, 

//--- but it is more important to exit it correctly...   

//--- first check if position exists - try to select it

   if(m_position.Select(Symbol()))

     {

      //--- check for long position (BUY) possibility

      if(LongOpened())

         return(true);

      //--- check for short position (SELL) possibility

      if(ShortOpened())

         return(true);

     }

   return(false);

  }

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit(void)

  {

//--- create all necessary objects

   if(!ExtExpert.Init())

      return(INIT_FAILED);

//--- secceed

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert new tick handling function                                |

//+------------------------------------------------------------------+

void OnTick(void)

  {

   static datetime limit_time=0; // last trade processing time + timeout

//--- don't process if timeout

   if(TimeCurrent()>=limit_time)

     {

      //--- check for data

      if(Bars(Symbol(),Period())>2*InpMAFastPeriod)

        {

         //--- change limit time by timeout in seconds if processed

         if(ExtExpert.Processing())

            limit_time=TimeCurrent()+ExtTimeOut;

        }

     }

  }

//+------------------------------------------------------------------+

 

You made a mistake when you copied and pasted the code.

The error is here:

//--- it is important to enter the market correctly,

//--- but it is more important to exit it correctly...

//--- first check if position exists - try to select it

   if(m_position.Select(Symbol()))

     {

      //--- check for long position (BUY) possibility

      if(LongOpened())

         return(true);

      //--- check for short position (SELL) possibility

      if(ShortOpened())

         return(true);

     }

   return(false);

You have not provided a condition for what to do if there is NO POSITION YET in the market.

Reason: