Calculate TP (price) for 2 trades of different lot sizes
-
I have my EA to take profit when TP level is at x pips profit.
However, this TP is made up of 2 trades, on the same instrument, at different prices and lot sizes from a batch of trades. One trade may even be in DD and the other will be in profit). In other words, it closes out trades in drawdown by taking the oldest and newest trades, and closing them when combined, they are in profit by x pips.
It's easy enough to tell fxdreema to TP at x pips profit (I can calculate total lot size, and profit in pips from that), but my end goal is to draw a line on the chart to show me visually when I can expect profit to be taken.
For example, I have 4 trades on the same pair. Let's call them trades A, B, C and D.
Trade A was a sell taken first with 0.5 lot size.
Trade B gets taken when DD is at 15 pips, where the EA takes a 2nd trade for 0.55 pips.
Trade C at 30 pips DD (from trade A at 0.6 lots)
Trade D at 45 pips DD from first entry, at 0.65 lots.
As price returns back into profit on trade D, I wish to know when the oldest and newest trades (trades A and D ONLY, other trades are ignored) reach a total of x pips profit, whereas they close out. So trades A and D close when profit reaches x pips with lot size (0.5 + 0.65 = 1.15 lots).
This is pretty easy to do as currency value (IF profit (A+D) > x pips profit (1.15 lots)), but I can't (I don't know) how to derive the price at which TP would occur so I can draw the line on the chart.
I've been grinding the maths through my brain and I'm sure it's simple when laid out but I've been thinking about it too much now and just tie myself up in knots. Anyone have any idea on how to calculate this? Or how I should be visualising it?
-
I'm looking for something similar and I had to accept I need custom code for sure. If I find the solution I will share it here.
-
ChatGPT has this to say. I'm going to take a look at this closely a little later (it's for a sell):
(Deleted pic as formula was wrong)
-
cumulative profit is a single blue block, no? check unrealized profit - mode pips
-
@TipsyWisdom it is, super easy.
BUT, I would like to display the TP as a line on the chart, and I can only do this (as far as I know) by calculating the price that the TP line should be.
-
Nah, OnTrade.
Id imagine in that tab you can use a pass, connected to a modify variables function, that then defines the value of the variables as at the value of the current TP in price, then do a drawline function, that will draw a line at the new defined variable.
At least that makes sense to me...let me see if I can try to do it. Perhaps some feature is missing that I am not thinking of
-

something like that.
or this

?
-
Never knew you could draw lines straight from TP!
But I don't think that'll work - it works on bucket, right? For bucket you can only filter by direction or manual/automated. By loop you could do it (count buys, then only count every (totaltrades -1)) but then you can't calc the aggregated TP of the 2 trades (remember, this could be the first/last trades of a batch of 10+)
ChatGPT's approach is to proportionally measure each trade's contribution to profit by point, and take into account the starting price of each, which is how it would've been in my head but I had no idea what formula would be.
I've nearly done it but the maths/formula is tripping me up, particularly point size as I want it to work on metals/FX/indices. I'm so close, I have the correct TP now but it's multiplied it by 10 somewhere. I'll post formula when I've cracked it.
-
Also remember the TP in pips is of the first/last trades total lot size. The oldest trade is almost certainly in drawdown when it's closed (but the two together close in profit)
-
This is bizarre.
I have 2 formulas to trace bug on why calc isn't working. One that uses custom pip size (0.0001) and one that uses original (0.00001). Identical formulas bar the 1 decimal place. But fxdreema seems to be unable to calculate the one with the extra decimal place. Just completely ignores it. And it's not the variable, I've tried hardcoding the 0.00001 as well. Very strange.This formula seems to work weirdly I can't get the calculation to work. It simple refuses to multiply by 0.00001. Tried it an MQL5 code block and output to journal. No dice.

It's soooo close. The above is the solution but I have spent so long on this now for some visual flair I think I'll just leave it.
-
A little more on the formula (buys only)

-
Be wary of using chatGPT as it took me a while to get to this formula.
For example, some of the maths was completely out - I gave it 'real life' figures based on watching strategy tester (as it was working with TP based on profit, rather than price). Then it corrected itself.
But you can also just say 'now give it to me as MQL5' and it spits out code for you, so that's pretty handy
double CalculateTakeProfitPriceForBuy(double P_A, double P_D, double x_pips) { double lot_size_A = 0.02; double lot_size_D = 0.06; double pip_size = 0.0001; // Standard pip size for major pairs like EURUSD return (lot_size_A * P_A + lot_size_D * P_D + x_pips * pip_size * (lot_size_A + lot_size_D)) / (lot_size_A + lot_size_D); } // Sample usage: void OnTick() { double P_A = 1.09692; // Opening price of trade A double P_D = 1.09592; // Opening price of trade D double x_pips = 3.0; // Desired profit in pips double TP_Price = CalculateTakeProfitPriceForBuy(P_A, P_D, x_pips); // Use the TP_Price as necessary, e.g., set a take profit order, draw a line on chart, etc. // ... your logic here ... } -
Interesting. Would this formula work like this?:
- I've got three open trades right now. Two buys and one sell.
- Each trade has got different lot size.
- I want a line on the chart showing my expected money profit (inserted as input parameter).
Not sure this formula would do it.
-
@l-andorrà no it will only work for buys, that's a hedged trade, no?
But it might be worth asking ChatGPT as it can come up with a calculation/formula for you
-
Ok. Thanks.
-
........