Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 176

 
mila.com:

Reading, but would never have guessed to open the file before the loop )

Maybe I didn't read it carefully )
There's even an example of reading a file line by line from the beginning to the end...
 

There is an Expert Advisor, which is guided by the Pinbar when trading. The robot works correctly on currencies and is totally unpredictable on CFDs.

Can you please tell me how this can happen?

 
RichLux:

I have written a robot which is based on Pinbar when trading. The robot works correctly on currencies and completely unpredictable on CFDs.

Can you please tell me how this can happen?

Here is everything for your case.
 

Here is the Pinbar definition function

int Pin() { 
  double Close1 = iClose (_Symbol,_Period,1); 
  double Open1 = iOpen (_Symbol,_Period,1); 
  double Low1 = iLow(_Symbol,_Period,1); 
  double High1 = iHigh (_Symbol,_Period,1); 
  double Close2 = iClose (_Symbol,_Period,2); 
  double Open2 = iOpen (_Symbol,_Period,2); 
  double Low2 = iLow(_Symbol,_Period,2); 
  double High2 = iHigh (_Symbol,_Period,2); 
  if (MathAbs(Close1-Open1)/(High1-Low1)<= BodyPercent) 
    { 
       if(Close1>High1 - BodyLocaut*(High1-Low1)&&Open1>High1-BodyLocaut*(High1-Low1)&&Low1<Low2-nose*_Point && High2>High1) 
          return (1); 
       if(Close1<Low1 + BodyLocaut*(High1-Low1)&&Open1<Low1+BodyLocaut*(High1-Low1)&&High1>High2+nose*_Point && Low2<Low1) 
          return (-1); 
    } 
  return(0); 
}

When I insert it into the Expert Advisor, the function works fine (i.e. the Expert Advisor sets deals on the next bar after the bar corresponding to the conditions of the function) for currency pairs. However, trades on CFD are opened everywhere.

What is the difference between CFDs and currency pairs for this function?

 
RichLux:

Here is the Pinbar definition function

When I insert it into the Expert Advisor, the function works fine (i.e. the Expert Advisor sets deals on the next bar after the bar corresponding to the conditions of the function) for currency pairs. However, trades on CFD are opened everywhere.

What is the difference between CFDs and currency pairs for this function?

First of all, replace all && with {} so that each of the conditions is in its own block - then you can unprint the tested result in each block - see what values you get in the log.
 
Artyom Trishkin:
First of all replace all && with {} so that each of conditions was in its own block - then you can in each block unwind tested result - see in log what values you get.

Thanks, I followed the advice and understood where I was going wrong. Because my EA was moving in 5 digits and CFD is only 2 digits and I expected the EA to react to 50 pips minimum and got the reaction from 5 pips. I messed up.

Please help me with this aspect.

There is such a part in the code:

MathAbs(Close1-Open1)/(High1-Low1)

Sometimes it happens that High1=Low1. The tester then generates the critical error saying that it cannot divide by zero.

How to work around it?

 
RichLux:

Thanks, I followed the advice and understood where I was going wrong. Because my EA was moving in 5 digits and CFD is only 2 digits and I expected the EA to react to 50 pips minimum and got the reaction from 5 pips. I messed up.

Please help me with this aspect.

There is such a part in the code:

Sometimes it happens that High1=Low1. The tester then generates the critical error saying that it cannot divide by zero.

How to work around it?

You can do it without thinking:

MathAbs((Close1-Open1)/(High1-Low1>0?High1-Low1:0.00001))
 

Hello, encountered a problem during compilation:


void OnTick()

{

double minprice=999999, mp, maxprice=-999999;

for(int i=0; i<10; i++)

{

mp = iLow(Simbol(), PERIOD_CURRENT, i);

if (mp < minprice)

minprice = mp;

}


for(int i=0; i<10; i++)

{

mp = iHigh(Simbol(), PERIOD_CURRENT, i);

if (mp > maxprice)

maxprice = mp;

}

Comment("Minprice: " + DoubleToString(minprice, 5) + "\n "+

"Maxprice: " + DoubleToString(maxprice, 5));

}


The source code contains functions the compiler swears by iLow and iHigh and variable Simbol() there in string mode, but it solves another problem in the example. What do I need to fix in the code above. I'm stuck and can't compile the loop to check it.

FROM THE TEXTBOOK

If you want to get the value corresponding to the current incomplete bar, then

you can use the first form of the call specifying start_pos=0 and count=1.

Example:

#property copyright "2009, MetaQuotes Software Corp."

#property link "https://www.mql5.com"

#property version "1.00"

#property description "Example output of High[i] and Low[i] values"

#property description "for randomly chosen bars"

double High[],Low[];

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

//| Get Low for a given bar number |

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

double iLow(string symbol,ENUM_TIMEFRAMES timeframe,int index)

{

double low=0;

ArraySetAsSeries(low,true);

int copied=CopyLow(symbol,timeframe,0,Bars(symbol,timeframe),Low);

if(copied>0 && index<copied) low=Low[index];

return(low);

}

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

//| Get High for the given bar number |

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

double iHigh(string symbol,ENUM_TIMEFRAMES timeframe,int index)

{

double high=0;

ArraySetAsSeries(high,true);

int copied=CopyHigh(symbol,timeframe,0,Bars(symbol,timeframe),High);

if(copied>0 && index<copied) high=High[index];

return(high);

}

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

Автоматический трейдинг и тестирование торговых стратегий
Автоматический трейдинг и тестирование торговых стратегий
  • www.mql5.com
MQL5: язык торговых стратегий для MetaTrader 5, позволяет писать собственные торговые роботы, технические индикаторы, скрипты и библиотеки функций
 
geratdc:

Hello, encountered a problem during compilation:


void OnTick()

{

double minprice=999999, mp, maxprice=-999999;

for(int i=0; i<10; i++)

{

mp = iLow(Simbol(), PERIOD_CURRENT, i);

if (mp < minprice)

minprice = mp;

}


for(int i=0; i<10; i++)

{

mp = iHigh(Simbol(), PERIOD_CURRENT, i);

if (mp > maxprice)

maxprice = mp;

}

Comment("Minprice: " + DoubleToString(minprice, 5) + "\n "+

"Maxprice: " + DoubleToString(maxprice, 5));

}


The source code contains functions the compiler swears by iLow and iHigh b variable Simbol() is in string mode there, but it solves another problem in the example. What do I need to fix in the code above. I'm stuck and can't compile the loop to check it.

FROM THE TEXTBOOK

If you need to get the value corresponding to the current incomplete bar, then

you can use the first form of the call, specifying start_pos=0 and count=1.

Example:

#property copyright "2009, MetaQuotes Software Corp."

#property link "https://www.mql5.com"

#property version "1.00"

#property description "Example output of High[i] and Low[i] values"

#property description "for randomly chosen bars"

double High[],Low[];

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

//| Get Low for a given bar number |

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

double iLow(string symbol,ENUM_TIMEFRAMES timeframe,int index)

{

double low=0;

ArraySetAsSeries(low,true);

int copied=CopyLow(symbol,timeframe,0,Bars(symbol,timeframe),Low);

if(copied>0 && index<copied) low=Low[index];

return(low);

}

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

//| Get High for the given bar number |

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

double iHigh(string symbol,ENUM_TIMEFRAMES timeframe,int index)

{

double high=0;

ArraySetAsSeries(high,true);

int copied=CopyHigh(symbol,timeframe,0,Bars(symbol,timeframe),High);

if(copied>0 && index<copied) high=High[index];

return(high);

}

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


there's a mistake in this wordSimbol() you should write Symbol() or _Symbol
 
Sergey Gritsay:

You have an error in this wordSimbol() - Symbol() or _Symbol should be written

Thank you, the error on Simbol has been corrected by spelling Symbol correctly. I have something wrong with iLow and iHigh functions.

Thedifference in % between MQL4 and MQL5 ? otherwise I will have to scratch my head every time I go through MQL4 video tutorials.

Files:
1.png  64 kb