Questions from Beginners MQL5 MT5 MetaTrader 5 - page 574

 
Anton Balakirew:
The idea of opening orders on a buy or bear grip signal, how to make it so that there is only one buy/sell trade and one pending order trade, I have one open on every tick of the trade. Help to solve the problem.
Check the number of orders set and the number of open positions before you open a new position or place a new pending order.
 
Artyom Trishkin:

Well, here's another way to do it: matches are written for each candle in the range. In the previous version, the matches were written only for one candle - i.e. no match was written for the one that matched this one.

Artem, thank you. I will look into it.

pako:

The high values may be rounded, e.g. we may round the value 1.23456 to 1.2346.


I see what I mean. I also add a variable in the settings in which you can prescribe the deviation within which the values of high be considered equal. It is necessary because for 4-digit quotes often there is no such a problem, but for five-digit quotes to search for exact matches is infinitely possible.

 
Andrey Koldorkin:

Artem, thank you. I'll look into it.

The rounding is clear. I also put a variable in the settings, in which I will be able to prescribe the deviation value, within which the High values are considered equal. It is needed, because for 4-digit quotes often there is no such a problem, but for five-digit quotes looking for exact matches is endless.

You're welcome. I made an inaccuracy in the second version. Since in the first version we were searching for the matches starting from the next candle from the loop index, the main loop's pass was by the number of candles minus one to compare the two outermost candles. Now, in the second version, for each of the candlesticks we search for matches over the whole range, so in the line

for(int i=0; i<copy_bars-1; i++) {                             // цикл по скопированным данным от начала до "на один меньше размера массива"

you should change the number of bars to

for(int i=0; i<copy_bars; i++) {                             // цикл по скопированным данным от начала до конца диапазона
 
Artyom Trishkin:
Check the number of set orders and the number of open positions before you open a new position or place a new pending order.

i.e. it is necessary to add a condition to check the number of orders and positions in the conditions for a trade?

if (o4>c4&&o3>c3&&l2>l1&&c2<c1&&o2>c2&&o1<c1&&c1>o2&&h1>h2||o2>c1&h2>h1&h1<o2&o1<c1&&l2>l1&&o3>c3&&o4>c4){ //first condition

if (OrdersTotal()==0) //second condition

OrderSend(Symbol(), OP_BUY,Lot,o,10,l1,0); } ................................... and so for each trade?

And which function counts the number of open positions?

I think I've solved the problem:

if (Hour()>=0&Hour()<23){

if (o4>c4&&o3>c3&&l2>l1&&c2<c1&&o2>c2&&o1<c1&&c1>o2&&h1>h2||o2>c1&&h2>h1&&h1<o2&&o1<c1&&l2>l1&&o3>c3&&o4>c4){

if (PositionsTotal()<=1)

OrderSend(Symbol(), OP_BUY,Lot,o,10,l1,0); } //opens Buy

if (o4>c4&&o3>c3&&l2>l1&&c2<c1&&o2>c2&&o1<c1&&c1>o2&&h1>h2||o2>c1&&h2>h1&&h1<o2&&o1<c1&&l2>l1&&o3>c3&&o4>c4){

if (PositionsTotal()==1&OrderType()==0)

OrderSend(Symbol(), OP_SELLSTOP,Lot,l2,10,h2,0);} //Postponed SellStop order

if (o4<c4&&o3<c3&&h2<h1&&c2>c1&&o2<c2&&o1>c1&&c1<o2&&l1<l2||o2<c1&&l2<l1&&l1>o2&&o1>c1&&h2<h1&&o3<c3&&o4<c4){

if (PositionsTotal()<=1)

OrderSend(Symbol(),OP_SELL,Lot,o,10,h1,0); } //opens Sell

if (o4<c4&&o3<c3&&h2<h1&&c2>c1&&o2<c2&&o1>c1&&c1<o2&&l1<l2||o2<c1&&l2<l1&&l1>o2&&o1>c1&&h2<h1&&o3<c3&&o4<c4){

if (PositionsTotal()==1&OrderType()==1)

OrderSend(Symbol(), OP_BUYSTOP,Lot,h2,10,l2,0);} //Postponed BuyStop order

}

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

//Function that returns the total amount of open positions |

// |

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

int PositionsTotal() {

int pos = 0;

for (int i=0; i<OrdersTotal(); i++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if (OrderType() == OP_BUY || OrderType() == OP_SELL)

pos++;

}

return(pos);

}

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

 
Artyom Trishkin:

Well, here's another way to do it: matches are written for each candle in the range. In the previous version, the matches were written only for one candle - i.e. no match was written for the one that matched this one.

Good. But as I see it, if the high of candle 2 coincides with the low of candle 7, then two matches will be found: 2 <=> 7, 7 <=>2. And what about accounting for inverse pairs, but without the extra loop?

Can't you solve the problem in one pass, without using nested for loops:)))?

 
Vasiliy Sokolov:

Gud. However, as I understand it, if the high of candle 2 coincides with the high of candle 7, then two matches will be found: 2 <=> 7, 7 <=>2. And what about accounting for inverse pairs, but without the extra loop?

How about solving the problem in one go, without using nested for loops:)))?

:) Don't try to play meweak;) Of course, I think you can solve the problem with one loop, but I was too lazy to think much longer and did as I thought of it at once.

Naturally, here all the paired or more candles are recorded in the matching data of each candle. Hence the cross-references to each other. I'm just suggesting variants, not knowing what they are for.

And further it is possible to solve the task of optimization of calculations, but I don't need it.

What if you need exactly the data for each candle and all matches, what if, what if, what if ... there is only ....

So I have no time and no desire. I have wasted my time instead of writing my own codes.

SZY, maybe you should also connect Kohonen methods knowing the required input parameter.

 
Artyom Trishkin:

In general - no time and no desire. And so wasted time instead of writing my own codes.

It's a pity, a real pity.
 
Vasiliy Sokolov:
Too bad, too bad.
Well, yes, it's an interesting task, but alas, I don't have the time.
 
Artyom Trishkin:

You're welcome. I made an inaccuracy in the second version. Since the first version searched for matches starting from the next candle from the loop index, the main loop was passed by the number of candles minus one to compare the two outermost candles. Now, in the second version, for each of the candlesticks we search for matches over the whole range, so in the line

you should change the number of bars to

Please tell us the meaning of these lines:

input int Search_Period=10; // Number of candlesticks to copy

int searchPeriod=(Search_Period<1)?1:Search_Period; // what do the "?", ":" symbols mean?

input int Delta=2; // Number of admission points

int delta=(Delta<0)?0:Delta; // what do the symbols "?", ":" mean?

 
Andrey Koldorkin:

Please tell me the meaning of these lines:

input Search_Period=10; // Number of candlesticks to copy

int searchPeriod=(Search_Period<1)?1:Search_Period; // what do the characters "?", ":" mean?

input int Delta=2; // Number of admission points

int delta=(Delta<0)?0:Delta; // what do the symbols "?", ":" mean?

// строку int searchPeriod=(Search_Period<1)?1:Search_Period;
// можно расписать так:

input int Search_Period=10; // Количество копируемых свечей ... эту строку вы видите в настройках
int searchPeriod;           // Сюда будем записывать входной параметр
if(Search_Period<1) searchPeriod=1; // Если во входном параметре ввели ноль или меньше нуля, то параметр будет равен единице
else searchPeriod=Search_Period;    // иначе примем входной параметр

// соответственно и строку int delta=(Delta<0)?0:Delta;
// можно расписать точно так же

From reference:

Conditional operator ?

The general form of a ternary operator looks like this:

expression1? expression2: expression3

As the first operand, "expression1" can be any expression that results in a bool-type value. If the result is true, the operator specified by the second operand, "expression2", is executed .

If the first operand isfalse, the third operand, "expressionZ", is executed. The second and third operands, i.e. "expression2" and "expressionZ", must return values of the same type and must not be of void type. The result of executing the conditional operator is the result of "expression2" or the result of "expression3", depending on the result of "expression1".

//--- renormalize difference between opening and closing prices by the daily range

double true_range = (High==Low)?0:(Close-Open)/(High-Low);

This entry is equivalent to the following

double_range;

if(High==Low)true_range=0;// if High and Low are equal

else true_range=(Close-Open)/(High-Low);// if High is non-zero

Reason: