Debugging my very first MTQ5 EA - page 2

 
Bob Matthews #As an aside, if, as I believe the only MT4 aspect of this MT5 EA is the 'MT4Orders' include, how difficult would it be for me to update the EA so that it works on MT5 order-handling?

MT4Orders is a class that "hides" the way trade transactions work on MT5, by making them look like how they would work in the older MT4.

However, MT5's trade handling is completely different and much more complex, because it offers a whole lot of other functionality and features that were simply not available nor even possible on MT4.

Switching between the two trade handling methods can be quite daunting. That is why I recommended that you start with fresh eyes on MT5 without being misdirected by the older MT4 way.

To learn about native MT5 trade handling, start with the following article, while referencing the MQL5 Documentation and the MQL5 programming book for traders. You can also look into the Trade class of the Standard Library if you prefer OOP to Procedural/Functional programming.

Articles

Orders, Positions and Deals in MetaTrader 5

MetaQuotes, 2011.02.01 16:13

Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
 

Here's a concise example to go with:

Forum on trading, automated trading systems and testing trading strategies

How to start with MQL5

Vladimir Karputov, 2020.12.19 05:33

Example: short trade request form and full trade request form

Code: Buy Short code and  Full code.mq5

//+------------------------------------------------------------------+
//|                                Buy Short code and  Full code.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.00"
//---
#include <Trade\Trade.mqh>
CTrade         m_trade;                      // trading object
//---
#property script_show_inputs
//--- input parameters
input group             "Trading settings"
input uint     InpStopLoss          = 150;         // Stop Loss
input uint     InpTakeProfit        = 460;         // Take Profit
input group             "Additional features"
input ulong    InpDeviation         = 10;          // Deviation
input ulong    InpMagic             = 200;         // Magic number
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(Symbol());
   m_trade.SetDeviationInPoints(InpDeviation);
//--- Short code
   m_trade.Buy(0.01);
//--- Full code
   MqlTick last_tick;
   ResetLastError();
   if(SymbolInfoTick(Symbol(),last_tick))
     {
      Print(Symbol()," ",last_tick.time,": Bid = ",last_tick.bid," Ask = ",last_tick.ask);
     }
   else
     {
      Print("SymbolInfoTick() failed, error = ",GetLastError());
      return;
     }
//---
   double sl=(InpStopLoss==0)?0.0:last_tick.ask-InpStopLoss*Point();
   double tp=(InpTakeProfit==0)?0.0:last_tick.ask+InpTakeProfit*Point();
   m_trade.Buy(0.02,Symbol(),last_tick.ask,sl,tp,"Full code");
  }
//+------------------------------------------------------------------+


Result