Errors, bugs, questions - page 804

 

ilunga:

P.S. Am I correct in assuming that in my (and your) code bar_info[1] is the maximum of the current bar?

Nobody said anything about current bar. :)

I just re-did your example. If you want exactly current bar, you have to:

1. define direction of series of array (0 bar - current or highest).

If, as in our case, the array is moved out, you can do it once in the initialization block. If we hide the array in a function, we define it in place.

2. If the array is declared as a series, the current bar will be zero, and the one closed before it will be 1.

At least as I remember.

 

I may have missed something, but in order to open on the current bar (taking into account all additions) it should be like this.

Move the array to the function!

//Function BUY_pending
bool BUY_pending(string symbol,ENUM_TIMEFRAMES period,double volume,ulong magic = 0)
{
//----------------------------------------------------------------------------//
//Work variables
double price = 0, sl = 0, tp = 0; //Prices: Open, Sell stop, Take profit
int ResCopy = -1; //Result of copying the data into an array
int Dig     = 0;  //Digits

double bar_info[1];

bool Result = true; //Returned importance
//----------------------------------------------------------------------------//

ResetLastError();

//Checking the signal to stopping the trading system
  if(IsStopped()) return(false);
//Preparation of array
ArraySetAsSeries(bar_info,true);
//Preparation of structures
ZeroMemory(TradeRequest);
ZeroMemory(TradeResult);
ZeroMemory(CheckResult);
//Copying the data into an array
ResCopy = CopyHigh(symbol,period,0,1,bar_info);

  if(ResCopy==-1)return(false); 
//Calculations
Dig   = (int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);

price = NormalizeDouble(bar_info[0] + 500*_Point,Dig);
sl    = NormalizeDouble(price - 200*_Point,Dig);
tp    = NormalizeDouble(price + 1000*_Point,Dig);
//Preparation of request
TradeRequest.type_filling = ORDER_FILLING_FOK;
TradeRequest.action       = TRADE_ACTION_PENDING;
TradeRequest.type         = ORDER_TYPE_BUY_STOP; 
TradeRequest.deviation    = 10;
TradeRequest.symbol = symbol;
TradeRequest.magic  = magic;
TradeRequest.volume = volume;
TradeRequest.price  = price;
TradeRequest.sl     = sl;
TradeRequest.tp     = tp;
//Checking
Result = OrderCheck(TradeRequest,CheckResult);

  if(!Result)
  //Print message for user
  {
  PrintFormat("retcode=%d",CheckResult.retcode);

  PrintFormat("%s %s at %G Ask=%G  Bid=%G  ",
              EnumToString(TradeRequest.type),symbol,TradeRequest.price,SymbolInfoDouble(symbol,SYMBOL_ASK),
              SymbolInfoDouble(symbol,SYMBOL_BID));                  
  Print("------------");
  }

  if((!Result)||(CheckResult.retcode!=0))return(false);
//OrderSend
Result = OrderSend(TradeRequest,TradeResult);
//Checking for presence of the errors
  if(_LastError!=0){Result = false;}
//----------------------------------------------------------------------------//
return(Result);
//----------------------------------------------------------------------------//
}
 
Interesting:

And no one said anything about the current one. :)

I just reworked your example. If you need current one, you have to:

1. Define direction of array series (0 bar - current or the latest).

If, as in our case, the array is moved out, you can do it once in the initialization block. If we hide the array in a function, we define it in place.

2. If the array is declared as a series, the current bar will be zero, and the bar closed before that will be 1.

At least as I remember it.

I expanded the array to 3 elements. I got a mismatch between the current price and what's in it. This is most likely the reason.

#include <Trade\SymbolInfo.mqh>
double bar_info[3];

CSymbolInfo m_sym1;
CSymbolInfo m_sym2;

bool a;
int OnInit()
{
   SymbolSelect("EURUSD",true);
   SymbolSelect("GBPUSD",true);
   a = false;   
   return(0);
}

void OnTick()
{
   if (a) return;
   a = true;
   int ResCopy = CopyHigh("EURUSD",PERIOD_D1,0,3,bar_info);
   if(ResCopy==-1)return;
   Print("bar_info[0] = " + DoubleToString(bar_info[0]));
   Print("bar_info[1] = " + DoubleToString(bar_info[1]));
   Print("bar_info[2] = " + DoubleToString(bar_info[2]));
   PrintFormat("Ask=%G  Bid=%G  ", SymbolInfoDouble("EURUSD",SYMBOL_ASK), SymbolInfoDouble("EURUSD",SYMBOL_BID));
}

Results (testing was done on GBPUSD):

FR      0       test3 (GBPUSD,H1)       13:12:59        2012.01.02 09:00:00   bar_info[0] = 1.29591000
LH      0       test3 (GBPUSD,H1)       13:12:59        2012.01.02 09:00:00   bar_info[1] = 1.29987000
OF      0       test3 (GBPUSD,H1)       13:12:59        2012.01.02 09:00:00   bar_info[2] = 1.29220000
QN      0       test3 (GBPUSD,H1)       13:12:59        2012.01.02 09:00:00   Ask=1.29722  Bid=1.29709  

We get that both Ask and Bid are greater than the maximum of zero and greater than the maximum of the second bar


If we run the test on EURUSD, everything is OK:

FL      0       test3 (EURUSD,H1)       13:21:09        2012.01.02 09:00:00   bar_info[0] = 1.29591000
LJ      0       test3 (EURUSD,H1)       13:21:09        2012.01.02 09:00:00   bar_info[1] = 1.29987000
OP      0       test3 (EURUSD,H1)       13:21:09        2012.01.02 09:00:00   bar_info[2] = 1.29220000
CO      0       test3 (EURUSD,H1)       13:21:09        2012.01.02 09:00:00   Ask=1.29241  Bid=1.2922  


I have a feeling that when testing "not my" pair the situation "information on bars has been updated, but the tick is absent yet".

 
ilunga:

I have expanded the array to 3 elements. I get inconsistency between the current price and the array contents. That's most likely the reason.

Results:

We obtain that Ask and Bid are both greater than the zero bar maximum and greater than the second bar maximum

You don't consider the seriality of the arrays, in your code. No one can guarantee that with this outcome 0 bar will not be in say the year 2000.

I gave the code above it is suitable for all TFs in the array variant.

This code is there for a reason.

//Preparation of array
ArraySetAsSeries(bar_info,true);

If you need only a daily timeframe (D1) to identify the bar maximum you don't need to deal with the array at all, just change the first part of the function to this one

//Function BUY_pending
bool BUY_pending(string symbol,double volume,ulong magic = 0)
{
//----------------------------------------------------------------------------//
//Work variables
double price = 0, sl = 0, tp = 0; //Prices: Open, Sell stop, Take profit
double High  = 0; //The maximum value of bid for the current day

int Dig     = 0; //Digits

bool Result = true; //Returned importance
//----------------------------------------------------------------------------//

ResetLastError();

//Checking the signal to stopping the trading system
  if(IsStopped()) return(false);
//Preparation of structures
ZeroMemory(TradeRequest);
ZeroMemory(TradeResult);
ZeroMemory(CheckResult);
//Calculations
High = SymbolInfoDouble(symbol,SYMBOL_BIDHIGH);
Dig  = (int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);

price = NormalizeDouble(High + 500*_Point,Dig);
sl    = NormalizeDouble(price - 200*_Point,Dig);
tp    = NormalizeDouble(price + 1000*_Point,Dig);
 
Interesting:

You don't consider the seriality of arrays in your code. No one can guarantee that at this outcome 0 bar will not be in say 2000.

I gave the code above it is suitable for all TFs in the array variant.

This code is there for a reason

ArraySetAsSeries only for dynamic arrays?
Документация по MQL5: Основы языка / Типы данных / Объект динамического массива
Документация по MQL5: Основы языка / Типы данных / Объект динамического массива
  • www.mql5.com
Основы языка / Типы данных / Объект динамического массива - Документация по MQL5
 
Interesting:

You don't consider the seriality of arrays in your code. No one can guarantee that at this outcome 0 bar will not be in say the year 2000.

I gave the code above it is suitable for all TFs in the array variant.

This code is there for a reason.

Ok, make the array dynamic.

#include <Trade\SymbolInfo.mqh>
double bar_info[];

CSymbolInfo m_sym1;
CSymbolInfo m_sym2;

bool a;
int OnInit()
{
   ArrayResize(bar_info, 3);
   ArraySetAsSeries(bar_info,ххх);
   SymbolSelect("EURUSD",true);
   SymbolSelect("GBPUSD",true);
   a = false;   
   return(0);
}

void OnTick()
{
   if (a) return;
   a = true;
   int ResCopy = CopyHigh("EURUSD",PERIOD_D1,0,3,bar_info);
   if(ResCopy==-1)return;
   Print("bar_info[0] = " + DoubleToString(bar_info[0]));
   Print("bar_info[1] = " + DoubleToString(bar_info[1]));
   Print("bar_info[2] = " + DoubleToString(bar_info[2]));
   PrintFormat("Ask=%G  Bid=%G  ", SymbolInfoDouble("EURUSD",SYMBOL_ASK), SymbolInfoDouble("EURUSD",SYMBOL_BID));
}

Instead of xxx we put true and false.

The results:

FF      0       test3 (GBPUSD,H1)       13:25:47        2012.01.02 09:00:00   bar_info[0] = 1.29220000
GL      0       test3 (GBPUSD,H1)       13:25:47        2012.01.02 09:00:00   bar_info[1] = 1.29987000
OJ      0       test3 (GBPUSD,H1)       13:25:47        2012.01.02 09:00:00   bar_info[2] = 1.29591000
FR      0       test3 (GBPUSD,H1)       13:25:47        2012.01.02 09:00:00   Ask=1.29722  Bid=1.29709  

и

JP      0       test3 (GBPUSD,H1)       13:26:07        2012.01.02 09:00:00   bar_info[0] = 1.29591000
PN      0       test3 (GBPUSD,H1)       13:26:07        2012.01.02 09:00:00   bar_info[1] = 1.29987000
KD      0       test3 (GBPUSD,H1)       13:26:07        2012.01.02 09:00:00   bar_info[2] = 1.29220000
MP      0       test3 (GBPUSD,H1)       13:26:07        2012.01.02 09:00:00   Ask=1.29722  Bid=1.29709  

The order in the array is changed, the result is not. Bid is greater than the maximum [0]-th bar of the array

 
ilunga:
ArraySetAsSeries only for dynamic arrays?

I don't remember to be honest. But the result of the code I cited above coincides with the one we get (for EUR the open price is 1.24516 for GBP 1.56721)

High = SymbolInfoDouble(symbol,SYMBOL_BIDHIGH);

Although yes, it works fine without ArraySetAsSeries at this point

//+------------------------------------------------------------------+
//Function BUY_pending
bool BUY_pending(string symbol,ENUM_TIMEFRAMES period,double volume,ulong magic = 0)
{
//----------------------------------------------------------------------------//
//Work variables
double price = 0, sl = 0, tp = 0; //Prices: Open, Sell stop, Take profit
int ResCopy = -1; //Result of copying the data into an array
int Dig     = 0;  //Digits

double bar_info[1];

bool Result = true; //Returned importance
//----------------------------------------------------------------------------//

ResetLastError();

//Checking the signal to stopping the trading system
  if(IsStopped()) return(false);
//Preparation of structures
ZeroMemory(TradeRequest);
ZeroMemory(TradeResult);
ZeroMemory(CheckResult);
//Copying the data into an array
ResCopy = CopyHigh(symbol,period,0,1,bar_info);

  if(ResCopy==-1)return(false); 
//Calculations
Dig   = (int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);

price = NormalizeDouble(bar_info[0] + 500*_Point,Dig);

PS

That is, if you want to get the current bar, you needlessly copy three bars into the array on this line.

//Это не правильно
int ResCopy = CopyHigh("EURUSD",PERIOD_D1,0,3,bar_info);
//так правильно
int ResCopy = CopyHigh("EURUSD",PERIOD_D1,0,1,bar_info);

//если PERIOD_D1 не меняется вот идеальный вариант
 High = SymbolInfoDouble(symbol,SYMBOL_BIDHIGH);
 
Interesting:

PS

I.e. if you need to get the current bar you absolutely in vain copy three bars in this line to the array

Thank you! This option works without errors and is fully sufficient in this case.


However, the question left hanging just above is how the Bid can be greater than the maximum =(

 

ilunga:

However, the question left hanging above is how the Bid can be greater than the maximum =(

First of all, we need to determine for which period this maximum is taken.

If the array is declared as double bar_info[n] then the current bar in it will be the largest index.

if n = 2 this code will work with yesterday's daily bar

int ResCopy = CopyHigh("EURUSD",PERIOD_D1,0,2,bar_info);
price = NormalizeDouble(bar_info[0] + 500*_Point,Dig);

and this one with the current bar

int ResCopy = CopyHigh("EURUSD",PERIOD_D1,0,2,bar_info);
price = NormalizeDouble(bar_info[1] + 500*_Point,Dig);

I.e., if you copy several bars, you should arrange something like this to get the current bar (though you may need additional checks of how many bars have been copied into the array)

price = NormalizeDouble(bar_info[ResCopy-1] + 500*_Point,Dig);
Документация по MQL5: Доступ к таймсериям и индикаторам / Bars
Документация по MQL5: Доступ к таймсериям и индикаторам / Bars
  • www.mql5.com
Доступ к таймсериям и индикаторам / Bars - Документация по MQL5
 
Interesting:

The first thing to do is to define the period over which this maximum is taken.

If the array is declared as double bar_info[n] then the current bar in it will be the largest index.

Thus, there is a code on the previous page where there is an array of 3 elements. When outputting it with Prints, we get that Bid = 1.29709, while bar_info[n-1] stores 1.29220
Reason: