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

 
Artyom Trishkin:
Go through all open positions in the account in the loop, filter them by wizard and type.
If the type of position is right, then increase the size of the time array by 1 and record the opening price of this position in it.
When the loop is complete, then you will have an array with opening prices of all necessary positions.
In the loop, add up all of the values in the array, and at the end of the loop, divide the resulting value by the size of the array.
That is it - you have a calculated price. Now add the required number of points*Point() to it.
And furthermore, if the resulting price turns out to be lower than the opening price of the position (Buy), it will not be a Take Profit, but a Stop Loss.
And in addition to all of the above, you can add: do not take into account commissions and swaps)
 
A(i+1)=A(i)+x(i+1)/(i+1), A is the arithmetic average, x is the current value. At the opening of each new order, count.
 
Vitaly Muzichenko:
And to add to all this: it will not take into account commissions and swaps)

What do commissions and swaps have to do with it? A person wants to set a take profit total for all open positions. That is all.
 
Алексей Тарабанов:

What do commissions and swaps have to do with it? The man wants to set the take profit total for all open positions. That's all.
Right, he wants to add N points to the total price of all open positions.
 
Vitaly Muzichenko:
That's right, from the total price of all the open ones it wants to add N points.

I think he wants to add N points from not open ones as well. Do you think TP is an indicator of trader's greed, or a parameter of his trading system?
 
Алексей Тарабанов:

I think we should calculate the profit level from not opened prices as well. Do you think TP is an indicator of trader's greed or a parameter of his trading system?

If positions stay in the market for a long time, they incur swaps, and most often they are negative (Wednesday=x*3). Plus you need to take into account the commissions if the account is ECN, because when you close on take profit it seems to be in the plus, but in fact it is a minus, we have not considered costs.

On the screenshot, the first option that appears, for 5 positions - 4 days of swap and if you planned to take 10pp, then in a simple calculation we take only 6pp profit and that's without taking into account commissions (if any), if we take into account all expenses, then we close just at zero, because one opening price is not enough for the calculation.

That is why I suggest to calculate the profit level for all from breakeven price + add N-points to it.

 
Vitaly Muzichenko:

If the positions have been in the market for a long time, they are subject to swaps, and most of the time they are negative. Plus you need to take into account commissions if you have an ECN account, because if you close on take profit, you are actually at a disadvantage because you have not taken costs into account.

On the screenshot, the first option we saw, for 5 positions - 4 days of swap, and if we planned to take 10pp, then in a simple calculation we take only 6pp profit, and that without taking into account the spread and commissions (if any), if we include all expenses, we close just at zero, because one opening price is not enough for the calculation.



I work a little differently. But, it's not about us. He just asked how to average the TP :)
 
Алексей Тарабанов:

I work a little differently. But, it's not about us. He just asked how to average TP :)
Right, only TC will ask a little later how to average correctly :)
 
Vitaly Muzichenko:
Right, only TC will ask a little later how to average correctly :)

There will be a day ...
 

Guys, let's live together. :)

This is a piece of my TS, where all open trade prices on the BAY side should be summed, divided by the number of trades and added 200 pips. And in the direction of sell subtract 200 pips.

Who may help me write MQL4 code?

X1 - X open trades

Y - number of open trades

The formula should look something like this: X1 + X2 + X3 + X4 + X... / Y+200

For example, if I have 5 open trades in BAI, I should sum them up, divide by 5 and add 200. And 8 open trades in sell should be summed, divided by 8 and subtract 200.


Please send me the code in MQL4.


Thank you very much!

Reason: