stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Calculate break even for a basket?
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=2145
Page 1 of 1
Author:  hopfi2k [ Thu May 16, 2013 9:30 am ]
Post subject:  Calculate break even for a basket?

Hello guru-coders :)

Does anyone have a code-snippet to calculate a basket break even price (1..n buy/sell trades with different lot sizes, including commissions and swap)?

I'm not good enough at maths to figure it out... :oops:

Any help is highly appreciated!

Best Regards,
Andy
Author:  mobthehop [ Thu May 16, 2013 9:55 am ]
Post subject:  Re: Calculate break even for a basket?

Hope this helps - attached working indicator as proof of concept

Code: Select all

int    nOrders       = 0;
   double dOpenPrice    = 0.0;
   double dProfit       = 0.0;
   
   for (int i = 0; i <= OrdersTotal(); i++) 
   { 
      if(OrderSelect(i,SELECT_BY_POS))
      { 
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_BUY || OrderType() == OP_SELL)
            { 
               dOpenPrice += OrderOpenPrice();
               dProfit+= OrderProfit() + OrderSwap();
               nOrders++;
            }
         } 
      }
   }   
   
   if(nOrders > 0)
   {
      double dAvgEntryPrice = dOpenPrice/nOrders;
Author:  hopfi2k [ Thu May 16, 2013 10:11 am ]
Post subject:  Re: Calculate break even for a basket?

Hey mobthehop,

Thanks for the speedy reply.

I went through your code, and some questions remain:

Code: Select all

dProfit+= OrderProfit() + OrderSwap();
Why are you summing up profits and don't make any further reference/use to/of it?

Furthermore, I don't see, how different lot size of the individual trades are taken into account? Or am I missing something complete here?

Cheers,
Andy
mobthehop wrote:Hope this helps - attached working indicator as proof of concept

Code: Select all

int    nOrders       = 0;
   double dOpenPrice    = 0.0;
   double dProfit       = 0.0;
   
   for (int i = 0; i <= OrdersTotal(); i++) 
   { 
      if(OrderSelect(i,SELECT_BY_POS))
      { 
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_BUY || OrderType() == OP_SELL)
            { 
               dOpenPrice += OrderOpenPrice();
               dProfit+= OrderProfit() + OrderSwap();
               nOrders++;
            }
         } 
      }
   }   
   
   if(nOrders > 0)
   {
      double dAvgEntryPrice = dOpenPrice/nOrders;
Author:  mobthehop [ Thu May 16, 2013 10:40 am ]
Post subject:  Re: Calculate break even for a basket?

Hi Andy, mmhhh...... Please have a look at the complete code in indicator

a) dProfit is used to draw the profit / loss rectangle on chart....
b) different lot sizes are considered with OrderProfit()

Think of it more of a hint how to do it, I hacked this up 2 years ago to visually represent open PL of baskets, probably not 100% what you need

Cheers

Mop
Author:  gaheitman [ Thu May 16, 2013 4:55 pm ]
Post subject:  Re: Calculate break even for a basket?

hopfi2k wrote:Hello guru-coders :)

Does anyone have a code-snippet to calculate a basket break even price (1..n buy/sell trades with different lot sizes, including commissions and swap)?

I'm not good enough at maths to figure it out... :oops:

Any help is highly appreciated!

Best Regards,
Andy
I don't do any basket trading, so this may be a stupid question, but the heck is a break even "price" for an entire basket?

George
Author:  MrLong [ Sat May 18, 2013 5:31 pm ]
Post subject:  Re: Calculate break even for a basket?

Hi,

// Global
extern bool useAverageBE = false; // Use average breakeven closure.

//Put in start
if (RunningTrades(Symbol()) > 1 && useAverageBE && countProfit(Symbol()) <= 0)
{
CloseTrades(Symbol(), 10, MagicNumber);
Alert("Orders, ", symbol, ", closed at breakeven.");

}

int RunningTrades(string symbol)
{
int trades = 0;
for (int i = 0; i < OrdersTotal(); i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
continue;
if (OrderMagicNumber() == MagicNumber && OrderSymbol() == symbol && OrderType() == OP_BUY || OrderType() == OP_SELL)
{
trades++;
}
}
return(trades);
}

//+------------------------------------------------------------------+
//| expert Get profit/loss for all trades for this symbol function
//+------------------------------------------------------------------+
double countProfit(string symbol)
{
double cash = 0;

for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
if ((OrderSymbol() == symbol) && OrderMagicNumber() == MagicNumber)
{
cash += (OrderProfit() + OrderSwap() + OrderCommission());
}
}
return(cash);
}



//+------------------------------------------------------------------+
//| Close All Button Function |
//+------------------------------------------------------------------+
void CloseTrades(string symbol, int Slippage, int MagicNumber)
{
bool closed = false;
for (int i = OrdersTotal(); i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
while (IsTradeContextBusy())
Sleep(100);
RefreshRates();
if (OrderType() == OP_BUY && (MagicNumber == OrderMagicNumber() && OrderSymbol() == symbol))
{
closed = OrderClose(OrderTicket(), OrderLots(),
NormalizeDouble(MarketInfo(OrderSymbol(), MODE_BID), MarketInfo(OrderSymbol(), MODE_DIGITS)), Slippage, CLR_NONE);
refreshGrid();
}
if (OrderType() == OP_SELL && (MagicNumber == OrderMagicNumber() && OrderSymbol() == symbol))
{
closed = OrderClose(OrderTicket(), OrderLots(),
NormalizeDouble(MarketInfo(OrderSymbol(), MODE_ASK), MarketInfo(OrderSymbol(), MODE_DIGITS)), Slippage, CLR_NONE);
refreshGrid();
}
}
}


Best Regards

Andy
All times are UTC Page 1 of 1