Changing TP or SL for all option positions

Created at 06 Aug 2013, 12:03
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!
T4

t4fast

Joined 06.08.2013

Changing TP or SL for all option positions
06 Aug 2013, 12:03


Hi,

 

Is there a script(Robot) to modify the TP or SL for all option postioins to specified price ?

also, Is there a robot that modifies the TP and/or SL for any new potion to a pre-set pip from overall average of position. (averages down/up then modify exisiting open potions TP and SL to be X pips from average entry pice) ?

where can I read the Average entry price ?

 

 

 

 

 


@t4fast
Replies

t4fast
31 Aug 2013, 10:14

should i try the jobs sections ?

I would like to know if it is possible prior posting a job !


@t4fast

cAlgo_Fanatic
03 Sep 2013, 12:03

RE:

t4fast said:

should i try the jobs sections ?

I would like to know if it is possible prior posting a job !

Posting in the jobs section is as easy as posting on the forum.  

  1. Click the "Add New Job" button which will direct you to fill out a simple form. 
  2. Enter the job description, budget and payment method
  3. For the payment method choose from the drop down list either freelancer.com, odesk.com or direct payment.

If you choose freelancer/oDesk enter the URL link of the job post in the respective website, in the Job Listing text box.
The payment method is not handled by cTDN. You will have to choose between using a well known website for freelancers such as freelancer.com or odesk.com, or paying the developer directly.
Paying via freelancer or oDesk gives you the security of disputing the payment if something goes wrong. Certain fees apply. Please visit the respective websites for more details.

The developers will contact you either privately at the email address of your cTDN account or will post a reply.
Please note that anyone who will contact you, will not have access to your email address initially but only after you have replied back to them.  

 


@cAlgo_Fanatic

t4fast
01 Oct 2013, 23:48

HI

the Robot below, modifies the TP+SL and calculates the average price for the trades that are placed manually.

however, if i place orders from CTrader, it doesn't update/modify the open potions , unless, I move/switch the screen to cAlgo !!

is there a fix for it through coding? or it is not designed to do that ?!

 



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


using System;
using cAlgo.API;



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 (var position in Account.Positions)
            {
                if (Symbol.Code == position.SymbolCode && position.TradeType == TradeType.Buy)
                {
                    // md tp
                    if (_tpB != 0 && position.TakeProfit != _tpB)
                    {
                        Trade.ModifyPosition(position, position.StopLoss, _tpB);
                    }
                    if (_tpB == 0 && position.TakeProfit != _tpB)
                    {
                        Trade.ModifyPosition(position, position.StopLoss, null);
                    }

                    //md sl
                    if (_slB != 0 && position.StopLoss != _slB)
                    {
                        Trade.ModifyPosition(position, _slB, position.TakeProfit);
                    }
                    if (_slB == 0 && position.StopLoss != _slB)
                    {
                        Trade.ModifyPosition(position, null, position.TakeProfit);
                    }
                }



                if (Symbol.Code == position.SymbolCode && position.TradeType == TradeType.Sell)
                {

                    // md tp
                    if (_tpS != 0 && position.TakeProfit != _tpS)
                    {
                        Trade.ModifyPosition(position, position.StopLoss, _tpS);
                    }
                    if (_tpS == 0 && position.TakeProfit != _tpS)
                    {
                        Trade.ModifyPosition(position, position.StopLoss, null);
                    }


                    // md sl
                    if (_slS != 0 && position.StopLoss != _slS)
                    {
                        Trade.ModifyPosition(position, _slS, position.TakeProfit);
                    }
                    if (_slS == 0 && position.StopLoss != _slS)
                    {
                        Trade.ModifyPosition(position, null, position.TakeProfit);
                    }

                }

            }


        }

    }
}

 


@t4fast

fzlogic
02 Oct 2013, 17:14

You were probably logged in with different accounts. 


@fzlogic

t4fast
02 Oct 2013, 21:23

RE:

fzlogic said:

You were probably logged in with different accounts. 

 

I am not. 

 


@t4fast

fzlogic
03 Oct 2013, 12:54

Yeah, there seems to be something that is not exactly working. Even though positions opened in cTrader do get modified when I test your code, sometimes not all positions get modified.
Maybe too many requests to modify a position. 

I tried modifying the code to this and it seems to be working better, give it a try and see it the same problem persists:

you also need to include the Linq namespace.

using System.Linq;

...

            // 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);
            }

 


@fzlogic

t4fast
04 Oct 2013, 09:43

Hi !

thanks for your time and effort,
yes, code is working fine now , without any problems.


@t4fast

fzlogic
04 Oct 2013, 10:08

RE:

t4fast said:

Hi !

thanks for your time and effort,
yes, code is working fine now , without any problems.

There is one thing that needs correction in the last line:
 

//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);
// change to this
//md sl
    if (position.TakeProfit == tp && position.StopLoss != sl)
        if (!sl.Equals(0.0))
            Trade.ModifyPosition(position, sl, position.TakeProfit);
        else
            Trade.ModifyPosition(position, null, position.TakeProfit);

 


@fzlogic

HeroHeroHero
25 Jul 2017, 20:01

Hi there,

Is there a way that the above code snippet of t4fast 
"Modify all TakeProfit and StopLoss by Average Price"
to only just Modify all TakeProfit and StopLoss with a predefined static number of pips.

i.e: TakeProfit (pips) is set to 10.  I want all the open positions TP to auto adjust to 10 pips and not by the average price.

 


@HeroHeroHero