knowing when the sum of multiple trades PL will reach 0 (+2more questions)

 

hello, I have 3 question. 


Question 1: 

i'm trying to write a piece of code that answers the following question:

- when having multiple buy trades with different lot sizes(martingale), how do i know at what price will the sum of all of their profit and loss reach zero?

i have worked out a solution. but I am pretty sure people have done this before, so I was looking for the more optimal solution if possible. 


my solution: (to add up the orderopenprices considering the lot sizes and averaging them.)

first there is the assumption that losing trades keep adding to the lot size. (in a buy situation, the upper orders always have less lot size).

steps:

1- find the lowest lot size of all buys. 

2- make a variable to store the sum of order open prices

3- divide every order's lot size by the lowest lot size

4- add (orderopenprice * resultOfStep3) to the variable (for all buys)

5- divide (the sum in the variable) by (the sum of all lot sizes divided by lowest lot size)


this will give me the average open price of all the orders and this open price will be the point where the sum of all of their PL is 0. but there will be a lot of functions and a lot of looping through orders, i'm looking for the elegant way of implementing it. 


Question 2:

how to i make a function run not every tick, but every time an order is closed or opened. or basically any other way just not on every ***** tick! 

there are a lot things that would be using unnecessary resources if i keep calculating them every single tick. 


Question 3: 

how do i draw a line like the one for tp, sl on the chart?

 

Zombie24w:

Question 1: different lot sizes(martingale), how do i know at what price will the sum of all of their profit and loss reach zero?

Question 2: how to i make a function run not every tick, but every time an order is closed or opened. or basically any other way just not on every ***** tick! 

Question 3:  how do i draw a line like the one for tp, sl on the chart?
  1. Lots Weighted Average Price (∑  opi × loti ÷ ∑ loti) This is the break even price of all orders combined.
              OrderOpenPrice question . - MQL4 programming forum
              looking for sample code on how to calculate the price BE for a few Buy and Sell orders simultaneously - Pips - MQL4 programming forum

  2. A) Don't swear at us. That's a good way to get yourself banned. B) You don't (on MT4). Just the next tick, check if OrdersTotal has changed and call your function.
              New candle - MQL4 programming forum #3 2014.04.04

  3. Perhaps you should read the manual. MQL4 ReferenceObject FunctionsObjectCreateENUM_OBJECTOBJ_HLINE - Object Types - Objects Constants - Constants, Enumerations and Structures - MQL4 Reference
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  4. Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum 2015.02.11

    Why it won't work: Calculate Loss from Lot Pips - MQL5 programming forum 2017.07.11

 
William Roeder:
  1. Lots Weighted Average Price (∑  opi × loti ÷ ∑ loti) This is the break even price of all orders combined.
              OrderOpenPrice question . - MQL4 programming forum
              looking for sample code on how to calculate the price BE for a few Buy and Sell orders simultaneously - Pips - MQL4 programming forum

  2. A) Don't swear at us. That's a good way to get yourself banned. B) You don't (on MT4). Just the next tick, check if OrdersTotal has changed and call your function.
              New candle - MQL4 programming forum #3 2014.04.04

  3. Perhaps you should read the manual. MQL4 ReferenceObject FunctionsObjectCreateENUM_OBJECTOBJ_HLINE - Object Types - Objects Constants - Constants, Enumerations and Structures - MQL4 Reference
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  4. Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum 2015.02.11

    Why it won't work: Calculate Loss from Lot Pips - MQL5 programming forum 2017.07.11

Hello and thanks a lot for the answers. 
1- nice formula, u just made it much simpler. thanks a lot
2- sorry for the swearing i didn't mean to. it's just frustrating as you sure know.
3- good, ok.
4- i am aware of how martingle will blow the account eventually, unfortunatly i work for someone who uses martingle in every strategy he wants me to implement...

also thanks for the links, checking them out right away.

Reason: