close a same type of position

Created at 03 May 2015, 15:20
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!
TR

tradermatrix

Joined 24.07.2012

close a same type of position
03 May 2015, 15:20


hello
several current trades I would close the trades of the same type (buy or sell) when they add a total profit.
for example;
if total Positions.Buy = 100 euros
close all buy positions .... but leave open the Sell positions ...
and vice versa.
I want to use this method in place of stoploss and takeprofit.
Does it exist a solution ..?
thank you.


@tradermatrix
Replies

mindbreaker
03 May 2015, 19:56

double earn = 0; foreach (var pos in Positions){ if (pos.TradeType == TradeType. Buy){ earn+= pos.GrosProfit; } } if (earn> 100){ foreach (var posin Positions){ ClosePosition (pos); } }
@mindbreaker

tradermatrix
03 May 2015, 21:22

RE:

mindbreaker said:

double earn = 0; foreach (var pos in Positions){ if (pos.TradeType == TradeType. Buy){ earn+= pos.GrosProfit; } } if (earn> 100){ foreach (var posin Positions){ ClosePosition (pos); } }

thank you
I find this error; GrosProfit;
 

   {


            double earn = 0;
            
            foreach (var pos in Positions)
            {
                if (pos.TradeType == TradeType.Buy)
                {
                    earn += pos.GrosProfit;

                }
            }
            if (earn > 100)
            {
                foreach (var pos in Positions)
                {
                    ClosePosition(pos);
                }
            }

        }

Error CS1061: 'cAlgo.API.Position' does not contain a definition for 'GrosProfit' and no extension method 'GrosProfit' accepting a first argument of type 'cAlgo.API.Position' could be found (are you missing a using directive or an assembly reference?)

have you an idea of the problem?
 thank you in advance

 

 


@tradermatrix

mindbreaker
04 May 2015, 09:57

RE: RE:
        protected override void OnTick()
        {

            double earn = 0;

            foreach (var pos in Positions)
            {
                if (pos.TradeType == TradeType.Buy)
                {
                    // if You write pos and hit dot you have got tips for positions in calgo                    
                    //pos.NetProfit;
                    //pos.Pips;
                    //...
                    earn += pos.GrossProfit;
                }
            }
            if (earn > 100)
            {
                foreach (var pos in Positions)
                {
                    if (pos.TradeType == TradeType.Buy)
                    {
                        ClosePosition(pos);
                    }
                }
            }

        }

 


@mindbreaker

tradermatrix
04 May 2015, 13:06

RE: RE: RE:

mindbreaker said:

        protected override void OnTick()
        {

            double earn = 0;

            foreach (var pos in Positions)
            {
                if (pos.TradeType == TradeType.Buy)
                {
                    // if You write pos and hit dot you have got tips for positions in calgo                    
                    //pos.NetProfit;
                    //pos.Pips;
                    //...
                    earn += pos.GrossProfit;
                }
            }
            if (earn > 100)
            {
                foreach (var pos in Positions)
                {
                    if (pos.TradeType == TradeType.Buy)
                    {
                        ClosePosition(pos);
                    }
                }
            }

        }

 

thank you very much mindbreaker
the error was; GrosProfit .. !!! GrossProfit.
for the info I have written;
 

 

 [Parameter(DefaultValue = 15)]
        public double totalBuy { get; set; }

        [Parameter(DefaultValue = 15)]
        public double totalSell { get; set; }

 

 

 protected override void OnTick()
        {
            gainsBuy();
            gainsSell();

}

  private void gainsBuy()
        {

double earn = 0;



            foreach (var pos in Positions)
            {
                if (pos.TradeType == TradeType.Buy)
                {
                    earn += pos.NetProfit;
                }

            }


            if (earn > totalBuy)
            {
                foreach (var pos in Positions)

                    if (pos.TradeType == TradeType.Buy)
                    {
                        ClosePosition(pos);
                    }
            }

        }

        private void gainsSell()
        {

double earn = 0;


            foreach (var pos in Positions)
            {
                if (pos.TradeType == TradeType.Sell)
                {
                    earn += pos.NetProfit;
                }

            }


            if (earn > totalSell)
            {
                foreach (var pos in Positions)

                    if (pos.TradeType == TradeType.Sell)
                    {
                        ClosePosition(pos);
                    }
            }
        }



bons trades


@tradermatrix