How to determine the breakeven level

 
Hi,

I would like to make an EA to handle opened positions in a specific pair.

I can have many opened orders in both directions, each with different lot size and opened at different levels.

I would like to calculate which is the breakeven level resulting of the current situation.

Anybody who can help ?


I started like this, but I know it is not correct:


for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
RefreshRates();
if (OrderSymbol()==Symbol() && OrderMagicNumber()==Reference)
{

if (OrderType()==OP_BUY)
{
LastBuyPrice=OrderOpenPrice();
ProfitBuy = (Bid - OrderOpenPrice())*OrderLots()*TickValue/TickSize + OrderSwap() + OrderCommission();
TotalBuy = TotalBuy+ProfitBuy;
TotalBuyLots=TotalBuyLots+OrderLots();
LastBuyLots=OrderLots();
LastBuyTime=OrderOpenTime();
OpenOrdersBuy++;
}
}
if (OrderSymbol()==Symbol() && OrderMagicNumber()==Reference)
{

if (OrderType()==OP_SELL)
{
LastSellPrice=OrderOpenPrice();
ProfitSell = (OrderOpenPrice()-Ask)*OrderLots()*TickValue/TickSize + OrderSwap() + OrderCommission();
TotalSell=TotalSell+ProfitSell;
TotalSellLots=TotalSellLots+OrderLots();
LastSellLots=OrderLots();
LastSellTime=OrderOpenTime();
OpenOrdersSell++;
}
}

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ BreakEven Line calculation +
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


if (OpenOrdersSell>0 || OpenOrdersBuy>0)
{
BreakEven =Bid-((TotalBuy +TotalSell)/((TotalBuyLots-TotalSellLots)*TickValue/TickSize));
}
 

When you have both buys and sells:

BEPrice = AverageBuyPrice - (( SumOfSellLots / (SumOfBuyLots+SumOfSellLots)) * ( AverageBuyPrice - AverageSellPrice ))

 
phy:

When you have both buys and sells:

BEPrice = AverageBuyPrice - (( SumOfSellLots / (SumOfBuyLots+SumOfSellLots)) * ( AverageBuyPrice - AverageSellPrice ))


Thank for your answer.

But I doubt it will gives precisely what I am looking for.

Taking the averageBuyprice and the sum of lots will not take care of how they are distributed between the higher and the lower.
As I want to be able to take care of different size for each lot, if all the heavys one are at the top or at the bottom will affect the result
and not figure in the average calculation.

May be in using an arrray, but I am not very familiar in programming them.
 

just a suggestion - why dont you try using equity and balance levels available through the MT4 functions, if they are equal then you are at breakeven level and there's no need to follow all the trades one by one...

thank you

Automated

 

"But I doubt it will gives precisely what I am looking for blah blah blah"

I suggest you carefully work out the formula and try it.

Or not, your choice.

 
Automated:

just a suggestion - why dont you try using equity and balance levels available through the MT4 functions, if they are equal then you are at breakeven level and there's no need to follow all the trades one by one...

thank you

Automated

It would be usefull only when working with only one pair.

These values concern the account in its totality.


Thanks anyway for the suggestion

 
phy:

"But I doubt it will gives precisely what I am looking for blah blah blah"

I suggest you carefully work out the formula and try it.

Or not, your choice.


I will try it.


Just a question:


How you determine the AverageBuyPrice ?


1) AverageBuyPrice = ( HighestBuyPrice+LowestBuyPrice)/2

  • r

2) AverageBuyPrice = (SummOfAllBuyPrice/NumberOfBuyOrders)


Thank again for your help

 
No, here is Average Buy level:
SummBuyLots_Prices=0;
SummLots=0;
for(int i=0;i<OrdersTotal();i++)
{
if (!OrderSelect(i,SELECT_BY_POS)) continue;
if (OrderType()!=OP_BUY || OrderSymbol()!=Symbol() || OrderMagicNumber()!=myMagicNumber) continue;
SummLots+=OrderLots();
SummBuyLots_Prices+=OrderLots()*OrderOpenPrice();
}
if (SummLots>0)AvereageBuyLevel=SummBuyLots_Prices/SummLots;

 
Rosh:
No, here is Average Buy level:
SummBuyLots_Prices=0;
SummLots=0;
for(int i=0;i<OrdersTotal();i++)
{
if (!OrderSelect(i,SELECT_BY_POS)) continue;
if (OrderType()!=OP_BUY || OrderSymbol()!=Symbol() || OrderMagicNumber()!=myMagicNumber) continue;
SummLots+=OrderLots();
SummBuyLots_Prices+=OrderLots()*OrderOpenPrice();
}
if (SummLots>0)AvereageBueLevel=SummBuyLots_Prices/SummLots;


Great!

Thanks a lot!

Reason: