PositionClose() on MT5 not working

 

Hi all,

I am an MT5 student and my PositionClose() has refused to work. I have tried various options and alternatives but failed and I decided to seek help. Please point to me what I could be doing wrong.

Thank you.

//+------------------------------------------------------------------+
//|                                            Code_From_Scratch.mq5 |
//|                        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.005"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>
//---
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
CAccountInfo   m_account;                    // object of CAccountInfo class
CTrade         m_trade;                      // object of CTrade class
CPositionInfo  m_position;                   // trade position object
//---
int handleTrendFastMA;
int handleTrendSlowMA;

input ulong    InpMagic             =  1111;   // Magic number
//---
bool     m_need_close_buys          = false;    // close all BUY positions
bool     m_need_close_sells         = false;    // close all SELL positions
//---
datetime m_prev_bars       = 0;              // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   handleTrendFastMA=iMA(_Symbol,PERIOD_M30,12,0,MODE_EMA,PRICE_CLOSE);
   handleTrendSlowMA=iMA(_Symbol,PERIOD_M30,21,0,MODE_EMA,PRICE_CLOSE);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   datetime time_0=iTime(Symbol(),PERIOD_M30,0);
   if(time_0==m_prev_bars)
      return;
   m_prev_bars=time_0;
//---
   string m_array_symbols[4]  = {"EURUSD","NZDUSD"};
   int total = ArraySize(m_array_symbols);
   for(int i=0; i<total; i++)
     {
      double Ask=SymbolInfoDouble(m_array_symbols[i],SYMBOL_ASK);
      double Bid=SymbolInfoDouble(m_array_symbols[i],SYMBOL_BID);

      double maTrendFast[];
      double maTrendSlow[];
      CopyBuffer(handleTrendFastMA,0,0,1,maTrendFast);
      CopyBuffer(handleTrendSlowMA,0,0,1,maTrendSlow);
      MqlRates rates[];
      ArraySetAsSeries(rates,true);
      int copied=CopyRates(m_array_symbols[i],PERIOD_M30,0,5,rates);

      //---Calculations for new positions
      if(PositionsTotal()<4)
        {
         if(maTrendFast[0]>maTrendSlow[0])
           {
            if(rates[1].high>rates[2].high && rates[1].close > rates[1].open && rates[2].close > rates[2].open && rates[3].open > rates[3].close && rates[2].close-rates[2].open >= (rates[3].open-rates[3].close)/2)
              {
               if(!PositionSelect(m_array_symbols[i]))
                  m_trade.Buy(0.01,m_array_symbols[i],Ask,(Ask-3000*_Point),(Ask+3000*_Point));
               CheckTrailingStop(Ask);
              }
           }
         else
            if(maTrendFast[0]<maTrendSlow[0])
              {
               if(rates[1].low<rates[2].low && rates[1].close<rates[1].open && rates[2].close<rates[2].open && rates[3].open < rates[3].close && rates[2].open-rates[2].close >= (rates[3].close-rates[3].open)/2)
                 {
                  if(!PositionSelect(m_array_symbols[i]))
                     m_trade.Sell(0.01,m_array_symbols[i],Bid,(Bid+3000*_Point),(Bid-3000*_Point));
                  CheckTrailingStop1(Bid);
                 }
              }
         return;
        }

      //---
      for(int j=PositionsTotal()-1; j>=0; j--) // returns the number of open positions
         if(m_position.SelectByIndex(j))
            if(m_position.Symbol()==Symbol() && m_position.Magic()==InpMagic)
              {
               if(m_position.PositionType()==POSITION_TYPE_BUY) // long position is opened
                 {
                  if(rates[1].close < maTrendFast[0])
                    {
                     m_trade.PositionClose(m_position.Ticket()); // close position
                     return; // exit
                    }

                 }
               else
                 {
                  if(m_position.PositionType()==POSITION_TYPE_SELL) // long position is opened
                    {
                     if(rates[1].close > maTrendFast[0])
                       {
                        m_trade.PositionClose(m_position.Ticket()); // close position
                        return; // exit
                       }
                    }
                 }
              }
      return;
     }


  }
//+------------------------------------------------------------------+
//|           ------Trailing Stops-------                            |
//+------------------------------------------------------------------+
void CheckTrailingStop(double Ask)
  {
//set the desired stop loss to 150 points
   double SL=NormalizeDouble(Ask-500*_Point,_Digits);
//Check all the current open positions for the current symbol
   for(int i=PositionsTotal()-1; i>=0; i--) // Count all currency pair positions
     {
      string symbol=PositionGetSymbol(i);
      if(_Symbol==symbol)  //If chart symbol equals position symbol
        {
         if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY) //IF WE HAVE A sell position
           {
            //Get the ticket number
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
            //Get the current Stop Loss
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);
            //if current stop loss is below 150 points from Ask price
            if(CurrentStopLoss<SL)
              {
               //Modify the stop loss by 10 points
               m_trade.PositionModify(PositionTicket,(CurrentStopLoss+10*_Point),0);
              }
           }//End Symbol in loop
        }//End for loop
     }//End trailing stop
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CheckTrailingStop1(double Bid)
  {
   double SL=NormalizeDouble(Bid+500*_Point,_Digits);//Set the stop loss to 150 points
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      string symbol=PositionGetSymbol(i); //get the symbol position
      if(_Symbol==symbol)//if the currency pair is equal
         if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL) //IF WE HAVE A sell position
           {
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);//calculate the current stop loss
            double CurrentStopLoss = PositionGetDouble(POSITION_SL);//Get the ticket type
            if(CurrentStopLoss>SL) //If the current top loss is more than 180
              {
               m_trade.PositionModify(PositionTicket,(CurrentStopLoss-10*_Point),0);
              }
           }//End of Loop
     }//End of For Loop
  }
//+----------------------------
//+------------------------------------------------------------------+
Files:
RoughWork.ex5  65 kb
Reason: