use of MT5 Library functions

 

I am developing the my use of MT5 and trying to get to grips with the libraries and have two questions:-

1. How do I get the bid and ask price using library SymbolInfo. The following code generates Zero 0.00000 for bid and ask but does appear to give a valid spread

#include <Trade\SymbolInfo.mqh>           //--- The SymbolInfo Class
CSymbolInfo    mysymbol;                  //--- The SymbolInfo Class Object
      //SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   mysymbol.Name(_Symbol);
   double ask         = mysymbol.Ask();
   double bid         = mysymbol.Bid();
   ulong  spread      = mysymbol.Spread(); 
   string msg         = "Start";
   msg = msg + "\nask    = "+DoubleToString(ask,_Digits);
   msg = msg + "\nbid    = "+DoubleToString(bid,_Digits);
   msg = msg + "\nspread = "+DoubleToString(spread,_Digits);
   Comment(msg);
   return(INIT_SUCCEEDED);
}

I am also trying to retrieve the 'Ticket # " following a trade and tried numerous variations / alternatives and appear to fail - i.e. Ticket = 0

            resB[i] = mytrade.Buy (TradingLots,_Symbol,ask,SL,TP,myComment); 
            Ticket  = mytrade.ResultDeal();
            //Ticket  = mytrade.ResultOrder();

the Trade actions and resB is true

I tried the ResultDeal option having seen code in article //https://www.mql5.com/en/articles/138

Any help appreciated

The Use of the MQL5 Standard Trade Class libraries in writing an Expert Advisor
The Use of the MQL5 Standard Trade Class libraries in writing an Expert Advisor
  • www.mql5.com
This article explains how to use the major functionalities of the MQL5 Standard Library Trade Classes in writing Expert Advisors which implements position closing and modifying, pending order placing and deletion and verifying of Margin before placing a trade. We have also demonstrated how Trade classes can be used to obtain order and deal details.
 

I would use copyticks or symbolinfotick.

BTW here is the list of all functions: https://www.mql5.com/en/docs/function_indices.

Documentation on MQL5: Timeseries and Indicators Access / CopyTicks
Documentation on MQL5: Timeseries and Indicators Access / CopyTicks
  • www.mql5.com
CopyTicks - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Carl, thanks for the response.  I assume you suggest using the alternatives for the ASK, BID prices

I have subsequently been using

   ask         = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   bid         = SymbolInfoDouble(_Symbol,SYMBOL_BID);   
   spread      = SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);


This seems to work fine.


I'll take a read of the documentation

 
Peter Williams: I am developing the my use of MT5 and trying to get to grips with the libraries and have two questions:- 1. How do I get the bid and ask price using library SymbolInfo. The following code generates Zero 0.00000 for bid and ask but does appear to give a valid spread I am also trying to retrieve the 'Ticket # " following a trade and tried numerous variations / alternatives and appear to fail - i.e. Ticket = 0 the Trade actions and resB is true

I tried the ResultDeal option having seen code in article //https://www.mql5.com/en/articles/138 Any help appreciated

You set the ".Name()" but did not ".RefreshRates()" afterwards, so the current tick data wasn't available to you.

if( mysymbol.Name( _Symbol ) && mysymbol.RefreshRates() ) {
   double ask    = mysymbol.Ask(),
          bid    = mysymbol.Bid();
   ulong  spread = mysymbol.Spread(); 
};
 
Many thanks
 
Peter Williams #: Many thanks
You are welcome!