Please help me with this lot calculation logic

 

Hi guys, first of all thank you very much for making this forum so alive and rich of informations..i`ve been learning a lot from all the gurus and moderators...

now lets go to my problem...

im trying to create a function that will return the value for my next order in this logic: lets not consider the spread for a second, my expert got the trigger(from whatever strategy) to open an order size 0.1, this order go negative of 30 pips, at this point another trigger iin the opposite direction is generated(always from the strategy and not because a certain number of negative pips is reached) now the next order is placed in a martingale fashion but instead of a fixed increment(logarithmic, exponential...) we follow this formula:

S2 = Distance x S1 / Target

Where:

S2 is the new size for the new order

Distance is the amount of negative pips of the previously order

S1 is the size of the previously order

Target is a fixed number of pips that the new order will require at the new size to reach break even

so following the example and assuming we have target at 20 pips we have

S2 = ((30 x 0.1) / 20)

new size is 0.15...so 20 pips(target) at size 0.15 will get me to break even

i hope is clear, now i came with this function but for some reason it dont return the correct lot...please have a look

double CalcLots()

{
double lot;
double l_ord_open_price = 0;
double dist;
double ordelot;




    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
            

    if (OrderType() == OP_BUY  && OrderSymbol() == Symbol() && OrderMagicNumber() == 1  )
      l_ord_open_price = OrderOpenPrice();
      ordelot = OrderLots();
    {
      dist = (l_ord_open_price_4 - Ask );
      lot = ((dist * ordelot) / (Ask + BuyTakeprofit12*PipValue*Point));
    }  
      
  
   
        if   (OrderType() == OP_SELL  && OrderSymbol() == Symbol() && OrderMagicNumber() == 1  )
      l_ord_open_price = OrderOpenPrice();
      ordelot = OrderLots();
      {
      dist = (Bid - l_ord_open_price_4 ) ;
      lot = ((dist * ordelot) / (Bid - SellTakeprofit11*PipValue*Point));
      
      }  
  
   
   return (lot);
   
   }
   
    }

after this at the time to open the new order i call the function

double lot = CalcLots() ;

and use the lot variable for the new order...but is not working

I hope is clear and someone can give me a hand...thanks

 
I think you need the last closed order in your orderhistory to get the correct calculation or are you leaving the order open?
 
Ickyrus:
I think you need the last closed order in your orderhistory to get the correct calculation or are you leaving the order open?

Thanks for the replay, yes i leave the last order open
 
The goal is : decrease the target will increase the risk but it will get to break even sooner and vice versa
 

Frirst some Math!

OP is the orderopenprice OP1 is one order and OP2 is another order

CP is the currentprice of the order Bid or Ask for Buy and Sell

L1 is the Lots of OP1 and L2 is the Lots of OP2

Therefore the current profit/loss would be

(OP1 -CP)*L1 + (OP2-CP)*L2 =ProfitLoss

find price for ProfitLoss to equal zero we get CP=(OP1*L1 +OP2*L2) /(L1+L2)

Now you are trying to add an OP3 with lots L3 and work out a value for L3 i.e

(OP1 -(CP+Target))*L1 + (OP2-(CP+Target))*L2 + (OP3-(CP+Target))*L3=0 Solve for L3

L3= - ( (OP1 -(CP+Target))*L1 + (OP2-(CP+Target))*L2 ) / (OP3-(CP+Target))

Although unless your silppage is zero meeting your Known Openprice value is doubtful. and you will have to round up L3 to 2 decimal places.

 
Ickyrus:

Frirst some Math!

OP is the orderopenprice OP1 is one order and OP2 is another order

CP is the currentprice of the order Bid or Ask for Buy and Sell

L1 is the Lots of OP1 and L2 is the Lots of OP2

Therefore the current profit/loss would be

(OP1 -CP)*L1 + (OP2-CP)*L2 =ProfitLoss

find price for ProfitLoss to equal zero we get CP=(OP1*L1 +OP2*L2) /(L1+L2)

Now you are trying to add an OP3 with lots L3 and work out a value for L3 i.e

(OP1 -(CP+Target))*L1 + (OP2-(CP+Target))*L2 + (OP3-(CP+Target))*L3=0 Solve for L3

L3= - ( (OP1 -(CP+Target))*L1 + (OP2-(CP+Target))*L2 ) / (OP3-(CP+Target))

Although unless your silppage is zero meeting your Known Openprice value is doubtful. and you will have to round up L3 to 2 decimal places.


What about this:

dist = (MathAbs(currentprice - ord_open_price ) / (Point)); this will give me the number of pips

lott = ((dist * ordelot1) / target(in PIPS)); this will give me the lot2 required to hit break even at target

lot2 = NormalizeDouble(lott,2); this will normalize the var to consider only 2 digit after the decimal

the problem is that when i call the function it give me weird numbers...

 

1 lot buy goes negative 30 pips

open 1.5 lot sell when breakEven ?

.

Price have to go down more to become in profit

again 30 pips down

Buy 60 pips * 1 = 60 loss ===> 60 pips = 30 pips + x pips =

Sell 30 pips * 1.5 = 45 profit ===> 30 pips = x pips

.

At BreakEven both same (30 pips + x pips) * 1 = x pips * 1.5

30 pips + x pips = 1.5 * x pips

30 pips = 0.5 * x pips

x = 60 ANSWER

.

.

Check

Buy 30 + 60 =90 loss

sell 1.5 * 60 = 90 profit

 
deVries:

1 lot buy goes negative 30 pips

open 1.5 lot sell when breakEven ?

.

Price have to go down more to become in profit

again 30 pips down

Buy 60 pips * 1 = 60 loss ===> 60 pips = 30 pips + x pips =

Sell 30 pips * 1.5 = 45 profit ===> 30 pips = x pips

.

At BreakEven both same (30 pips + x pips) * 1 = x pips * 1.5

30 pips + x pips = 1.5 * x pips

30 pips = 0.5 * x pips

x = 60 ANSWER

.

.

Check

Buy 30 + 60 =90 loss

sell 1.5 * 60 = 90 profit

Ok guys i got it! thanks for your help !!you are the best!
 

HI, 

do you perhaps have some code you have used . I also try to find a soultion to that problem.

I would like to give my EA a  

extern bool  UseBreakEvenLots = true;

extern int BreakEvenPips = 50;

extern int AutoBreakEvenLevel = 4; 

 so now I would like to get a function with that the EA will auto calculate how many lots it

has to buy on level 4 of my trades to get to breakeven in 50 Pips. I am using a grid so the 4 level.

 

I am trying to code but am not a Guru ;) so if you would have some code already I could modifiy
it would be great.

thanks 

Reason: