Close all positions with profitable takeprofit

Created at 09 Dec 2013, 15:18
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!
NI

nirin

Joined 09.12.2013

Close all positions with profitable takeprofit
09 Dec 2013, 15:18


Hi,

How can i close all the positions as soon as their sum is profitable?

Eg: 1k sell = -45

      2k sell = -90

      4k buy = 180

Thanks for your help!


@nirin
Replies

daemon
09 Dec 2013, 17:34

something like this:

        double sum;

        protected override void OnTick()
        {
            sum = 0;

            foreach (var position in Positions)
            {
                sum += position.NetProfit;
            }

            if (sum > 0)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }
        }

 


@daemon

hichem
09 Dec 2013, 17:37

RE:
sum += position.NetProfit;

should be

sum += position.NetProfit + position.Commisions + positions.Swap ;

daemon said:

something like this:

        double sum;

        protected override void OnTick()
        {
            sum = 0;

            foreach (var position in Positions)
            {
                sum += position.NetProfit;
            }

            if (sum > 0)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }
        }

 

 


@hichem

daemon
09 Dec 2013, 18:04

thanks :)


@daemon

daemon
09 Dec 2013, 18:06

Wait, Shouldn't it be:

sum += position.NetProfit - position.Commisions - positions.Swap;

 


@daemon

Cerunnos
09 Dec 2013, 18:56

Shouldn't be:

sum += position.NetProfit - position.Swap

or

sum += position.GrossProfit - position.Commissions - position.Swap


Or not?

 


@Cerunnos

daemon
10 Dec 2013, 09:29

RE:

it should be the way hichem said. Commisions and swap are negative.

 

Cerunnos said:

Shouldn't be:

sum += position.NetProfit - position.Swap

or

sum += position.GrossProfit - position.Commissions - position.Swap


Or not?

 

 


@daemon

Cerunnos
10 Dec 2013, 09:58

But I'm the opinion that position.Netprofit includes already commissions...


@Cerunnos

atrader
10 Dec 2013, 10:15

RE:

It includes commissions one way.

Cerunnos said:

But I'm the opinion that position.Netprofit includes already commissions...

 


@atrader

nirin
10 Dec 2013, 10:16

Hi,

But the robot doesnt contain any close position, only GetAveragePrice(TradeType.Buy or Sell) +- TakeProfit * Symbol.PointSize;

Is it possible?

Thanks!


 


@nirin

Cerunnos
10 Dec 2013, 10:57

RE: RE:

atrader said:

It includes commissions one way.

Cerunnos said:

But I'm the opinion that position.Netprofit includes already commissions...

 

Thanks :-)


@Cerunnos

jeex
10 Dec 2013, 11:22

RE: RE: RE:

Cerunnos said:

atrader said:

It includes commissions one way.

Cerunnos said:

But I'm the opinion that position.Netprofit includes already commissions...

 

Thanks :-)

Yes, you must double the commission to calculate total costs of the trade.


@jeex

Spotware
10 Jan 2014, 12:12

RE: RE:

hichem said:

sum += position.NetProfit;

should be

sum += position.NetProfit + position.Commisions + positions.Swap ;

daemon said:

something like this:

        double sum;

        protected override void OnTick()
        {
            sum = 0;

            foreach (var position in Positions)
            {
                sum += position.NetProfit;
            }

            if (sum > 0)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }
        }

UPDATE:

The NetProfit property of a Position type variable has been updated to include closing commissions. 

Until now the NetProfit property of a Position type variable included commissions one way, that is only the commissions charged upon creating an order. Now, they will include the commissions that will be charged when the position is closed as well.
So, if you had any cBots calculating net profit and subtracted the closing commissions you will need to modify the code otherwise commissions will be subtracted twice.


@Spotware