calculate tp for a basket of positions

 

Hi, I am trying to calculate the price level for take profit for a basket/group of positions. It is for a martingale system. The tp should be placed at the price level where cumulative profit of all buy orders is xx in account currency. It seems my calculation is wrong. Can someone see what I am doing wrong? 


double BuyTP_Price()
  {
   double tickValue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
   double BuyMoneyProfitClose = inpProfitToCloseBasket ;

   double BuyTp = NormalizeDouble(AvaregeBuyEntryPrice + _Point*(BuyMoneyProfitClose/tickValue/TotalBuyLotsOpen())
                                  + _Point*inpBrokerCommisionPerLot*TotalBuyLotsOpen(),_Digits);

   return (BuyTp);
  }
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
Account Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
  1. Lots weighted average price is your open price. (Op)

    Lots Weighted Average Price = ∑  op(i) × lot(i) ÷ ∑ lot(i). This is the break even price of all orders combined.
              Figuring out how to reopen several positions after forcibly closing them - Expert Advisors and Automated Trading - MQL5 programming forum #1 (2022)

    Your profit is ( Cp - Op) * Lots * Tv / Ts

    Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
              Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)


  2. Hedging, grid trading, same as Martingale.
              Martingale, Hedging and Grid : MHG - General - MQL5 programming forum (2016)

    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)

    Why it won't work:
              Calculate Loss from Lot Pips - MQL5 programming forum (2017)
              THIS Trading Strategy is a LIE... I took 100,000 TRADES with the Martingale Strategy - YouTube (2020)

 
William Roeder #:
  1. Lots weighted average price is your open price. (Op)

    Your profit is ( Cp - Op) * Lots * Tv / Ts


  2. Hedging, grid trading, same as Martingale.
              Martingale, Hedging and Grid : MHG - General - MQL5 programming forum (2016)

    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)

    Why it won't work:
              Calculate Loss from Lot Pips - MQL5 programming forum (2017)
              THIS Trading Strategy is a LIE... I took 100,000 TRADES with the Martingale Strategy - YouTube (2020)

Thank you for your reply. I don't quite understand what you mean by " Your profit is (  Cp -  Op) * Lots *  Tv /  Ts". Would you care to show what you mean in mq5 language? That would be highly appreciated.  

I tried using your formula for lots weighted avarage in this way: 

double BuyTp = NormalizeDouble((AvaregeBuyEntryPrice * TotalBuyLotsOpen())/TotalBuyLotsOpen(),_Digits);
I must have used your formula wrong, as with this the output becomes the average price of open orders and does not adjust for varying lot sizes of the different orders. So the tp level is not the break even point of all orders, but merely the average entry point of all orders.. Would appreciate it if you can show me how you would write the function in mq5. 
 
jestester #: I don't quite understand what you mean by " Your profit is (  Cp -  Op) * Lots *  Tv /  Ts".
  • Cp = Close price of position
  • Op = Open price of position
  • Lots = Volume of position
  • Tv = Tick value
  • Ts = Tick size
 
jestester #: I tried using your formula for lots weighted avarage in this way:

See also the following:

Forum on trading, automated trading systems and testing trading strategies

Any great idea about HEDGING positions welcome here

Fernando Carreiro, 2018.09.17 21:17

For those of you that want to learn to see the "light" and not fall into the trap of "hedging" and Grid strategies, here is the basic math:

How to Calculate the Net Resulting Equivalent Order:


Learn to do your research properly! Do not be blinded by false promises. Do the math!


 
Fernando Carreiro #:

See also the following:


Thank you Fernando, but I don't know what the variables on that sheet are referring to. Can you please explain?

I have been working on solving this issue for 5 hours using the formulas presented by William Roeder, but still I have no success in getting either the cummilative break even price or the cummilative tp price based on a money profit value. 

If anyone would share how they would go about it in mq5 language that would be highly appreciated. 

Thanks

 
jestester #: Thank you Fernando, but I don't know what the variables on that sheet are referring to. Can you please explain? I have been working on solving this issue for 5 hours using the formulas presented by William Roeder, but still I have no success in getting either the cummilative break even price or the cummilative tp price based on a money profit value. If anyone would share how they would go about it in mq5 language that would be highly appreciated. 

There is no exact MQL code to give you, because it varies depending on the strategy and what information needs to be calculated. That is why I gave you the math instead for you to implement depending on your requirements.

The math is self explanatory, but if you are having difficulty with it, then I doubt you will be able to implement it in code, but here are the variables anyway:

  • vi = volume of individual position
  • oi = open price of individual position
  • ci = close price of individual position
  • Vn = total volume for a basket of positions
  • On = net mean open price for a basket of positions
  • Cn = net mean close price for a basket of positions
  • PLn = profit/loss for a basket of positions
The rest is in the math calculations.
     
    Fernando Carreiro #:

    There is no exact MQL code to give you, because it varies depending on the strategy and what information needs to be calculated. That is why I gave you the math instead for you to implement depending on your requirements.

    The math is self explanatory, but if you are having difficulty with it, then I doubt you will be able to implement it in code, but here are the variables anyway:

    • vi = volume of individual position
    • oi = open price of individual position
    • ci = close price of individual position
    • Vn = total volume for a basket of positions
    • On = net mean open price for a basket of positions
    • Cn = net mean close price for a basket of positions
    • PLn = profit/loss for a basket of positions
    The rest is in the math calculations.
      Perfect, thank you!!
       
      Fernando Carreiro #:

      See also the following:


      hi! great formula thanks.

      but is there a way to make it work for all positions and no just one for each  direction?

      P = (C-O).V 
      

      This only works for buys 

      sells would be 

      P = (O-C).V 

      so is there a way to make these formlar to work for both positions at once?

       
      Yup. Figured it out. I will publish it when I’m free. 

      In case I don’t, to anyone reading this in the future, it’s not hard 

      You just have to do some calculations and get the prices for which buy profit + sell loss (or vice versa) is equal to your target