Calculate net profit/loss at price

Created at 29 Aug 2016, 22:16
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
BR

brunoamancio.ti

Joined 20.08.2016

Calculate net profit/loss at price
29 Aug 2016, 22:16


Is there something implemented to calculate net profit at specific price?

I mean, if I buy 1k EURUSD at 1,10000 and want to calculate net profit I will have if price reaches 1.10100 ?


@brunoamancio.ti
Replies

... Deleted by UFO ...

brunoamancio.ti
30 Aug 2016, 02:19

RE:

lucian said:

Try  Profit Calculated from :http://www.calgocbots.com/profit-calculated

Do you have the source code? I'd like to be able to see and edit it.


@brunoamancio.ti

kricka
30 Aug 2016, 02:30

Net profit

It all depends on what the net profit is calculatet on. If it includes both the commission and the spread it can become somewhat complicated. Spread has to be calculated on the current spread on the pair, while commission has to be calculated from the history of an earlier trade made on the pair. Then it is possible to get the right net profit before closing a trade.

Market Order 3.0 information shows this in a cBot.


@kricka

brunoamancio.ti
30 Aug 2016, 02:49

It should take into consideration:

- Base price (either ask or bid, depending on what you want)

- Target price

- Commission per million units

With that it should be possible to calculate. I wanted to know if something has a snippet with something similar or I have to code it, but I haven't got much time this week. =)


@brunoamancio.ti

tradermatrix
31 Aug 2016, 11:49

RE:
  [Parameter("Net Profit", DefaultValue = 100)]
        public double totalgains { get; set; }

////////////////////////////////////////////////////////////////////////////////////


   protected override void OnTick()
        {
 NETPROFIT();
}


////////////////////////////////////////////////////////////////////////////////////
  private void NETPROFIT()
        {


            var netProfit = 0.0;

            if (totalgains > 0)


                foreach (var openedPosition in Positions)
                {

                    netProfit += openedPosition.NetProfit;

                }

            if (netProfit >= totalgains && totalgains != 0)
            {


                foreach (var openedPosition in Positions)
                {
                    ClosePosition(openedPosition);
                }
            }
        }

brunoamancio.ti said:

It should take into consideration:

- Base price (either ask or bid, depending on what you want)

- Target price

- Commission per million units

With that it should be possible to calculate. I wanted to know if something has a snippet with something similar or I have to code it, but I haven't got much time this week. =)

 


@tradermatrix

brunoamancio.ti
31 Aug 2016, 14:00

That doesn't do what I proposed. It just checks the current net profit of all positions and closes all positions if current net profit > specified value.

I want to calculate the net profit at a price different from the current.


@brunoamancio.ti

galafrin
01 Sep 2016, 19:39

It is not possible to exact profit when trading outside account currency , albeit this may help and match Symbol.UnrealizedNetProfit and Symbol.UnrealizedGrossProfit ;

            double 
            trade = ( volume < 0 ? 1 : -1 ) , 
            commission = 35.0 / 1000000.0 ;
            
            double netProfit = Math.Round ( ( openPrice * ( 1.0 - commission * trade ) - closePrice * ( 1.0 + commission * trade ) ) * Math.Abs ( volume ) / Symbol.TickSize * Symbol.TickValue  * trade , 2 ) ; 
            double comm      = Math.Round ( ( openPrice + closePrice ) * commission / Symbol.TickSize * Symbol.TickValue * Math.Abs ( volume ) , 2 ) ; 

 


@galafrin

galafrin
01 Sep 2016, 19:49

RE:
double
trade = ( volume < 0 ? 1 : -1 ) , 
commission = 35.0 / 1000000.0 ;
netProfit = Math.Round ( ( oPrice * ( 1.0 - commission * trade ) - cPrice * ( 1.0 + commission * trade ) ) * Math.Abs ( volume ) / Symbol.TickSize * Symbol.TickValue  * trade , 2 ) , 
comm      = Math.Round ( ( oPrice + cPrice ) * commission * Math.Abs ( volume ) / Symbol.TickSize * Symbol.TickValue , 2 ) ;

 


@galafrin

croucrou
01 Sep 2016, 23:59

var position= Positions.Find("label");

(targetPrice - position.EntryPrice) * Symbol.PipValue * position.Volume - Symbol.Spread - position.Commisions - positions.Swap

 


@croucrou

croucrou
02 Sep 2016, 02:52

 Forget the previous one. See this:

var position= Positions.Find("label");

Math.Abs(targetPrice - position.EntryPrice) * Symbol.PipSize * Symbol.PipValue * position.Volume - Symbol.Spread - position.Commisions - positions.Swap

@croucrou

brunoamancio.ti
03 Sep 2016, 15:52

Thanks for your replies =)

 

On top of the others suggestions, I wrote another possibility in the form of an extension method for Positions. Like this it is possible to call this method directly for any position object.

 

    public static class PositionExtension
    {
        public static double GetNetProfitAt(this Position position, double price)
        {
            // 3 USD per 100K units traded
            const double commission = 3/100000d;
            var openComission = position.EntryPrice * position.Volume * commission;
            var closeComission = price * position.Volume * commission;

            var calculation = position.TradeType == TradeType.Buy ? price - position.EntryPrice : position.EntryPrice - price;
            calculation += openComission + closeComission;
            return position.Volume * calculation;
        }
     }

 


@brunoamancio.ti

brunoamancio.ti
03 Sep 2016, 15:57

RE:

There is no way to edit answers, so, there's a mistake in the code above, but you must find it yourself =)

 

brunoamancio.ti said:

Thanks for your replies =)

 

On top of the others suggestions, I wrote another possibility in the form of an extension method for Positions. Like this it is possible to call this method directly for any position object.

 

    public static class PositionExtension
    {
        public static double GetNetProfitAt(this Position position, double price)
        {
            // 3 USD per 100K units traded
            const double commission = 3/100000d;
            var openComission = position.EntryPrice * position.Volume * commission;
            var closeComission = price * position.Volume * commission;

            var calculation = position.TradeType == TradeType.Buy ? price - position.EntryPrice : position.EntryPrice - price;
            calculation += openComission + closeComission;
            return position.Volume * calculation;
        }
     }

 

 


@brunoamancio.ti

croucrou
10 Sep 2016, 16:02

Of course I should have written:

/ Symbol.PipSize

and not:

* Symbol.PipSize

Couldn't edit here.


@croucrou