Category Range  Published on 06/09/2014

AggressiveModifyPositions

Description

Modify all positions to one TP and SL by average price

 

//    Modify all TakeProfit and StopLoss by Average Price. "Aggressive Trading"

//    salr22@hotmail.com, www.borsat.net



// -------------------------------------------------------------------------------
//
//    Modify all TakeProfit and StopLoss by Average Price. "Aggressive Trading"
//    salr22@hotmail.com, www.borsat.net
//    AggressiveModifyPositions.v2
// -------------------------------------------------------------------------------


using System;
using cAlgo.API;
using System.Linq;


namespace cAlgo.Robots
{
    [Robot()]
    public class AggressiveModifyPostions : Robot
    {

        [Parameter("TakeProfit (pips)", DefaultValue = 10.0)]
        public int _takeProfit { get; set; }

        [Parameter("StopLoss (pips)", DefaultValue = 50.0)]
        public int _stopLoss { get; set; }

        [Parameter("TakeProfit_Buy (price)", DefaultValue = 0)]
        public double _takeProfitPrBuy { get; set; }

        [Parameter("StopLoss_Buy (price)", DefaultValue = 0)]
        public double _stopLossPrBuy { get; set; }

        [Parameter("TakeProfit_Sell (price)", DefaultValue = 0)]
        public double _takeProfitPrSell { get; set; }

        [Parameter("StopLoss_Sell (price)", DefaultValue = 0)]
        public double _stopLossPrSell { get; set; }


        protected override void OnTick()
        {
            if (Trade.IsExecuting)
                return;

            double _avrgB = 0;
            double _avrgS = 0;
            double _lotsB = 0;
            double _lotsS = 0;

            double _tpB = 0;
            double _slB = 0;
            double _tpS = 0;
            double _slS = 0;

            // get average 
            foreach (var position1 in Account.Positions)
            {

                if (Symbol.Code == position1.SymbolCode)
                {

                    if (position1.TradeType == TradeType.Buy)
                    {
                        _avrgB = _avrgB + (position1.Volume * position1.EntryPrice);
                        _lotsB = _lotsB + position1.Volume;
                    }

                    if (position1.TradeType == TradeType.Sell)
                    {
                        _avrgS = _avrgS + (position1.Volume * position1.EntryPrice);
                        _lotsS = _lotsS + position1.Volume;
                    }

                }

            }

            _avrgB = Math.Round(_avrgB / _lotsB, Symbol.Digits);
            _avrgS = Math.Round(_avrgS / _lotsS, Symbol.Digits);

            string text = string.Format("{0}", "\n\n" + "Average Buy:  " + _avrgB + "\n\n" + "Average Sell:  " + _avrgS);
            ChartObjects.DrawText("avrg", text.PadLeft(10), StaticPosition.TopLeft, Colors.White);

            if (_takeProfit != 0)
            {
                _tpB = Math.Round(_avrgB + Symbol.PipSize * _takeProfit, Symbol.Digits);
                _tpS = Math.Round(_avrgS - Symbol.PipSize * _takeProfit, Symbol.Digits);
            }
            if (_stopLoss != 0)
            {
                _slB = Math.Round(_avrgB - Symbol.PipSize * _stopLoss, Symbol.Digits);
                _slS = Math.Round(_avrgS + Symbol.PipSize * _stopLoss, Symbol.Digits);
            }

            if (_takeProfitPrBuy != 0)
            {
                _tpB = _takeProfitPrBuy;
            }
            if (_stopLossPrBuy != 0)
            {
                _slB = _stopLossPrBuy;
            }
            if (_takeProfitPrSell != 0)
            {
                _tpS = _takeProfitPrSell;
            }
            if (_stopLossPrSell != 0)
            {
                _slS = _stopLossPrSell;
            }



            // modify 
            foreach (Position position in Account.Positions.Where(position => Symbol.Code == position.SymbolCode))
            {
                double sl = position.TradeType == TradeType.Buy ? _slB : _slS;
                double tp = position.TradeType == TradeType.Buy ? _tpB : _tpS;

                if (position.TakeProfit == tp && position.StopLoss == sl)
                    continue;

                if (position.TakeProfit != tp && position.StopLoss != sl)
                {
                    if (!tp.Equals(0.0) && !sl.Equals(0.0))
                        Trade.ModifyPosition(position, sl, tp);

                    else if (tp.Equals(0.0) && sl.Equals(0.0))
                        Trade.ModifyPosition(position, null, null);

                    else if (tp.Equals(0.0) && !sl.Equals(0.0))
                        Trade.ModifyPosition(position, sl, null);
                    else
                        Trade.ModifyPosition(position, null, tp);
                    continue;
                }
                // md tp
                if (position.TakeProfit != tp && position.StopLoss == sl)
                {
                    if (!tp.Equals(0.0))
                        Trade.ModifyPosition(position, position.StopLoss, tp);
                    else
                        Trade.ModifyPosition(position, position.StopLoss, null);
                    continue;
                }

                //md sl
                if (position.TakeProfit == tp && position.StopLoss != sl)
                    if (!sl.Equals(0.0))
                        Trade.ModifyPosition(position, sl, position.TakeProfit);
                    else if (tp.Equals(0.0) && sl.Equals(0.0))
                        Trade.ModifyPosition(position, null, position.TakeProfit);

            }


        }

    }
}


salr22's avatar
salr22

Joined on 01.10.2013

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: ###AggressiveModifyPostions.v2.algo
  • Rating: 0
  • Installs: 3076
Comments
Log in to add a comment.
aisaac's avatar
aisaac · 9 years ago

the mod.position (price) not work when i set the price es. open buy at 1.32567  and when load the cbot my price position is 1.32467, i set stop loss price at 1.32450 but ctrade send ne error  Request to amend position PID421666 (SL: 1.32450) is REJECTED with error "TRADING_BAD_STOPS".

 

why don't modify my position when open and lost at tot. pips at price ?

i set this price because when price go up  decreases ,my lost profit.

TR
tradermatrix · 9 years ago

merci pour le code
j ai construi un robot et inclus  ton code.(je travaille déja avec un systeme similaire)
il faut noter que les résultats sont bons pour "_takeProfit".
mais il vaut mieux supprimer le _stopLoss,car il provoque de grandes pertes.
@+

thank you for the code 
I have Constructed a robot and included your code. (I work already with a similar system) 
note that the results are good for "_takeProfit." 
but it is better to remove the _stopLoss because it causes great loss. 
@+