Why ModifyPosition does not work ?

Created at 31 Aug 2021, 01:07
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!
VI

vittorioroncagli

Joined 24.04.2021

Why ModifyPosition does not work ?
31 Aug 2021, 01:07


Hello,

I am using the following for change TakeProfit and StopLoss of opened positions, I can't understand why not working.

Many thanks for your help.

Vic

 

var pos_buy = Positions.Find("Buy");

if (pos_buy != null)

{

ModifyPosition(pos_buy, 200, 300);

}

 


@vittorioroncagli
Replies

PanagiotisCharalampous
31 Aug 2021, 08:27

Hi vittorioroncagli,

We need more information in order to help you. Please provide us with the following

  1. Complete cBot code
  2. A detailed explanation describing why do you think it doesn't work and explaining how to reproduce this behavior

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

vittorioroncagli
31 Aug 2021, 21:35

RE:

PanagiotisCharalampous said:

Hi vittorioroncagli,

We need more information in order to help you. Please provide us with the following

  1. Complete cBot code
  2. A detailed explanation describing why do you think it doesn't work and explaining how to reproduce this behavior

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Hello Panagiotis,

many thanks for your replay.

Answering to your questions:

1) The complete example cBot is listed below.

2) I run the cBot in Backtesting and the two values of target profit and stop loss do not change when they should accordingly with the cBot code.

 

using System;

using System.Linq;

using cAlgo.API;

using cAlgo.API.Indicators;

using cAlgo.API.Internals;

using cAlgo.Indicators;

 

namespace cAlgo.Robots

{

    [Robot(TimeZone = TimeZones.EEuropeStandardTime, AccessRights = AccessRights.None)]

    public class BotExample : Robot

    {

        [Parameter()]

        public DataSeries SourceSeries { get; set; }

 

        [Parameter("Volume", DefaultValue = 2, MinValue = 1, MaxValue = 10)]

        public double volume { get; set; }

 

        //MAX BUY

        [Parameter("Max Buy", DefaultValue = 2, MinValue = 1, MaxValue = 5)]

        public int MaxBuy { get; set; }

 

        //MAX SELL

        [Parameter("Max Sell", DefaultValue = 2, MinValue = 1, MaxValue = 5)]

        public int MaxSell { get; set; }

 

        // MACD

        [Parameter("Macd Source", Group = "MACD")]

        public DataSeries MACD { get; set; }

        [Parameter("Macd Period", DefaultValue = 9, Group = "MACD")]

        public int MacdPeriod { get; set; }

        [Parameter("Macd Long Cycle", DefaultValue = 26, Group = "MACD")]

        public int MacdLongCycle { get; set; }

        [Parameter("Macd Short Cycle", DefaultValue = 12, Group = "MACD")]

        public int MacdShortCycle { get; set; }

        private MacdCrossOver _MACD;

 

        // SAR

        [Parameter("SAR Source", Group = "SAR")]

        public DataSeries SAR { get; set; }

        [Parameter("SAR Min AF", DefaultValue = 0.02, MinValue = 0, Group = "SAR")]

        public double MinAF { get; set; }

        [Parameter("SAR Max AF", DefaultValue = 0.2, MinValue = 0, Group = "SAR")]

        public double MaxAF { get; set; }

        private ParabolicSAR _SAR;

 

        // SMA FAST

        [Parameter("SMA Source", Group = "SMA")]

        public DataSeries SMAfast { get; set; }

        [Parameter("SMA Period", DefaultValue = 10, Group = "SMA")]

        public int FastSMAperiod { get; set; }

        private SimpleMovingAverage _SMA;

 

        // CALC

        protected override void OnStart()

        {

            _MACD = Indicators.MacdCrossOver(MacdLongCycle, MacdShortCycle, MacdPeriod);

            _SAR = Indicators.ParabolicSAR(MinAF, MaxAF);

            _SMA = Indicators.SimpleMovingAverage(SMAfast, FastSMAperiod);

        }

 

        protected override void OnTick()

        {

            bool macd_rising = Functions.IsRising(_MACD.MACD);

            bool sma_rising = Functions.IsRising(_SMA.Result);

            bool parab_rising = _SAR.Result.LastValue < Bars.LastBar.Close;

 

            // CLOSE BUY

            if (macd_rising == false | sma_rising == false)

            {

                var pos_B_US30 = Positions.FindAll("Buy");

                foreach (var position in pos_B_US30)

                {

                    if (_SAR.Result.LastValue > Bars.LastBar.Close)

                    {

                        if (position.NetProfit > 0)

                        {

                            position.Close();

                        }

                    }

                }

            }

 

            // CLOSE SELL

            if (macd_rising == true | sma_rising == true)

            {

                var pos_S_US30 = Positions.FindAll("Sell");

                foreach (var position in pos_S_US30)

                {

                    if (_SAR.Result.LastValue < Bars.LastBar.Close)

                    {

                        if (position.NetProfit > 0)

                        {

                            position.Close();

                        }

                    }

                }

            }

 

            // CHANGE TP/SL

            var pos_buy = Positions.Find("Buy");

            if (pos_buy != null)

            {

                if (pos_buy.NetProfit < 0)

                {

                    ModifyPosition(pos_buy, 200, 300);

                }

            }

            var pos_sell = Positions.Find("Sell");

            if (pos_sell != null)

            {

                if (pos_sell.NetProfit < 0)

                {

                    ModifyPosition(pos_sell, 200, 300);

                }

            }

 

            // TRADE

            var pos_b = Positions.FindAll("Buy");

            var pos_s = Positions.FindAll("Sell");

            if (pos_b.Length < MaxBuy && pos_s.Length < MaxSell)

            {

 

                if (macd_rising == true && sma_rising == true && parab_rising == true)

                {

                    var pos_1_US30 = Positions.FindAll("Buy");

                    if (pos_1_US30.Length < MaxBuy)

                    {

                        ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volume, "Buy", 0, 0);

                    }

                }

 

                if (macd_rising == false && sma_rising == false && parab_rising == false)

                {

                    var pos_2_US30 = Positions.FindAll("Sell");

                    if (pos_2_US30.Length < MaxSell)

                    {

                        ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volume, "Sell", 0, 0);

                    }

                }

            }

        }

    }

}

 


@vittorioroncagli

amusleh
01 Sep 2021, 15:43

Hi,

ModifyPosition method requires Stop Loss and Take Profit in absolute Price not Pips.

To change stop loss and take profit based on Pips either you have to calculate the price levels by your self or use the helper ModifyStopLossPips/ModifyTakeProfitPips methods.

Try this:

using System;

using System.Linq;

using cAlgo.API;

using cAlgo.API.Indicators;

using cAlgo.API.Internals;

using cAlgo.Indicators;

namespace cAlgo.Robots

{
    [Robot(TimeZone = TimeZones.EEuropeStandardTime, AccessRights = AccessRights.None)]
    public class BotExample : Robot

    {
        [Parameter()]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Volume", DefaultValue = 2, MinValue = 1, MaxValue = 10)]
        public double volume { get; set; }

        //MAX BUY

        [Parameter("Max Buy", DefaultValue = 2, MinValue = 1, MaxValue = 5)]
        public int MaxBuy { get; set; }

        //MAX SELL

        [Parameter("Max Sell", DefaultValue = 2, MinValue = 1, MaxValue = 5)]
        public int MaxSell { get; set; }

        // MACD

        [Parameter("Macd Source", Group = "MACD")]
        public DataSeries MACD { get; set; }

        [Parameter("Macd Period", DefaultValue = 9, Group = "MACD")]
        public int MacdPeriod { get; set; }

        [Parameter("Macd Long Cycle", DefaultValue = 26, Group = "MACD")]
        public int MacdLongCycle { get; set; }

        [Parameter("Macd Short Cycle", DefaultValue = 12, Group = "MACD")]
        public int MacdShortCycle { get; set; }

        private MacdCrossOver _MACD;

        // SAR

        [Parameter("SAR Source", Group = "SAR")]
        public DataSeries SAR { get; set; }

        [Parameter("SAR Min AF", DefaultValue = 0.02, MinValue = 0, Group = "SAR")]
        public double MinAF { get; set; }

        [Parameter("SAR Max AF", DefaultValue = 0.2, MinValue = 0, Group = "SAR")]
        public double MaxAF { get; set; }

        private ParabolicSAR _SAR;

        // SMA FAST

        [Parameter("SMA Source", Group = "SMA")]
        public DataSeries SMAfast { get; set; }

        [Parameter("SMA Period", DefaultValue = 10, Group = "SMA")]
        public int FastSMAperiod { get; set; }

        private SimpleMovingAverage _SMA;

        // CALC

        protected override void OnStart()

        {
            _MACD = Indicators.MacdCrossOver(MacdLongCycle, MacdShortCycle, MacdPeriod);

            _SAR = Indicators.ParabolicSAR(MinAF, MaxAF);

            _SMA = Indicators.SimpleMovingAverage(SMAfast, FastSMAperiod);
        }

        protected override void OnTick()

        {
            bool macd_rising = Functions.IsRising(_MACD.MACD);

            bool sma_rising = Functions.IsRising(_SMA.Result);

            bool parab_rising = _SAR.Result.LastValue < Bars.LastBar.Close;

            // CLOSE BUY

            if (macd_rising == false | sma_rising == false)

            {
                var pos_B_US30 = Positions.FindAll("Buy");

                foreach (var position in pos_B_US30)

                {
                    if (_SAR.Result.LastValue > Bars.LastBar.Close)

                    {
                        if (position.NetProfit > 0)

                        {
                            position.Close();
                        }
                    }
                }
            }

            // CLOSE SELL

            if (macd_rising == true | sma_rising == true)

            {
                var pos_S_US30 = Positions.FindAll("Sell");

                foreach (var position in pos_S_US30)

                {
                    if (_SAR.Result.LastValue < Bars.LastBar.Close)

                    {
                        if (position.NetProfit > 0)

                        {
                            position.Close();
                        }
                    }
                }
            }

            // CHANGE TP/SL

            var pos_buy = Positions.Find("Buy");

            if (pos_buy != null)

            {
                if (pos_buy.NetProfit < 0)

                {
                    pos_buy.ModifyStopLossPips(200);
                    pos_buy.ModifyTakeProfitPips(300);
                }
            }

            var pos_sell = Positions.Find("Sell");

            if (pos_sell != null)

            {
                if (pos_sell.NetProfit < 0)

                {
                    pos_sell.ModifyStopLossPips(200);
                    pos_sell.ModifyTakeProfitPips(300);
                }
            }

            // TRADE

            var pos_b = Positions.FindAll("Buy");

            var pos_s = Positions.FindAll("Sell");

            if (pos_b.Length < MaxBuy && pos_s.Length < MaxSell)

            {
                if (macd_rising == true && sma_rising == true && parab_rising == true)

                {
                    var pos_1_US30 = Positions.FindAll("Buy");

                    if (pos_1_US30.Length < MaxBuy)

                    {
                        ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volume, "Buy");
                    }
                }

                if (macd_rising == false && sma_rising == false && parab_rising == false)

                {
                    var pos_2_US30 = Positions.FindAll("Sell");

                    if (pos_2_US30.Length < MaxSell)

                    {
                        ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volume, "Sell");
                    }
                }
            }
        }
    }
}

 


@amusleh

vittorioroncagli
02 Sep 2021, 00:50

RE:

amusleh said:

Hi,

ModifyPosition method requires Stop Loss and Take Profit in absolute Price not Pips.

To change stop loss and take profit based on Pips either you have to calculate the price levels by your self or use the helper ModifyStopLossPips/ModifyTakeProfitPips methods.

Try this:

using System;

using System.Linq;

using cAlgo.API;

using cAlgo.API.Indicators;

using cAlgo.API.Internals;

using cAlgo.Indicators;

namespace cAlgo.Robots

{
    [Robot(TimeZone = TimeZones.EEuropeStandardTime, AccessRights = AccessRights.None)]
    public class BotExample : Robot

    {
        [Parameter()]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Volume", DefaultValue = 2, MinValue = 1, MaxValue = 10)]
        public double volume { get; set; }

        //MAX BUY

        [Parameter("Max Buy", DefaultValue = 2, MinValue = 1, MaxValue = 5)]
        public int MaxBuy { get; set; }

        //MAX SELL

        [Parameter("Max Sell", DefaultValue = 2, MinValue = 1, MaxValue = 5)]
        public int MaxSell { get; set; }

        // MACD

        [Parameter("Macd Source", Group = "MACD")]
        public DataSeries MACD { get; set; }

        [Parameter("Macd Period", DefaultValue = 9, Group = "MACD")]
        public int MacdPeriod { get; set; }

        [Parameter("Macd Long Cycle", DefaultValue = 26, Group = "MACD")]
        public int MacdLongCycle { get; set; }

        [Parameter("Macd Short Cycle", DefaultValue = 12, Group = "MACD")]
        public int MacdShortCycle { get; set; }

        private MacdCrossOver _MACD;

        // SAR

        [Parameter("SAR Source", Group = "SAR")]
        public DataSeries SAR { get; set; }

        [Parameter("SAR Min AF", DefaultValue = 0.02, MinValue = 0, Group = "SAR")]
        public double MinAF { get; set; }

        [Parameter("SAR Max AF", DefaultValue = 0.2, MinValue = 0, Group = "SAR")]
        public double MaxAF { get; set; }

        private ParabolicSAR _SAR;

        // SMA FAST

        [Parameter("SMA Source", Group = "SMA")]
        public DataSeries SMAfast { get; set; }

        [Parameter("SMA Period", DefaultValue = 10, Group = "SMA")]
        public int FastSMAperiod { get; set; }

        private SimpleMovingAverage _SMA;

        // CALC

        protected override void OnStart()

        {
            _MACD = Indicators.MacdCrossOver(MacdLongCycle, MacdShortCycle, MacdPeriod);

            _SAR = Indicators.ParabolicSAR(MinAF, MaxAF);

            _SMA = Indicators.SimpleMovingAverage(SMAfast, FastSMAperiod);
        }

        protected override void OnTick()

        {
            bool macd_rising = Functions.IsRising(_MACD.MACD);

            bool sma_rising = Functions.IsRising(_SMA.Result);

            bool parab_rising = _SAR.Result.LastValue < Bars.LastBar.Close;

            // CLOSE BUY

            if (macd_rising == false | sma_rising == false)

            {
                var pos_B_US30 = Positions.FindAll("Buy");

                foreach (var position in pos_B_US30)

                {
                    if (_SAR.Result.LastValue > Bars.LastBar.Close)

                    {
                        if (position.NetProfit > 0)

                        {
                            position.Close();
                        }
                    }
                }
            }

            // CLOSE SELL

            if (macd_rising == true | sma_rising == true)

            {
                var pos_S_US30 = Positions.FindAll("Sell");

                foreach (var position in pos_S_US30)

                {
                    if (_SAR.Result.LastValue < Bars.LastBar.Close)

                    {
                        if (position.NetProfit > 0)

                        {
                            position.Close();
                        }
                    }
                }
            }

            // CHANGE TP/SL

            var pos_buy = Positions.Find("Buy");

            if (pos_buy != null)

            {
                if (pos_buy.NetProfit < 0)

                {
                    pos_buy.ModifyStopLossPips(200);
                    pos_buy.ModifyTakeProfitPips(300);
                }
            }

            var pos_sell = Positions.Find("Sell");

            if (pos_sell != null)

            {
                if (pos_sell.NetProfit < 0)

                {
                    pos_sell.ModifyStopLossPips(200);
                    pos_sell.ModifyTakeProfitPips(300);
                }
            }

            // TRADE

            var pos_b = Positions.FindAll("Buy");

            var pos_s = Positions.FindAll("Sell");

            if (pos_b.Length < MaxBuy && pos_s.Length < MaxSell)

            {
                if (macd_rising == true && sma_rising == true && parab_rising == true)

                {
                    var pos_1_US30 = Positions.FindAll("Buy");

                    if (pos_1_US30.Length < MaxBuy)

                    {
                        ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volume, "Buy");
                    }
                }

                if (macd_rising == false && sma_rising == false && parab_rising == false)

                {
                    var pos_2_US30 = Positions.FindAll("Sell");

                    if (pos_2_US30.Length < MaxSell)

                    {
                        ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volume, "Sell");
                    }
                }
            }
        }
    }
}

 

Thank you for your input.

Using absolute price would be even easier for me.

in such case, if I am correct,  I guess I should use:

pos_buy. ModifyTakeProfitPrice( xxx)

and 

pos_sell. ModifyTakeProfitPrice( xxx)

where xxx is the price I want to set.

 

 


@vittorioroncagli

majidmesbah123456
25 Feb 2022, 22:36 ( Updated at: 25 Feb 2022, 22:38 )

RE:

                    double volume_Less = position_InLoss_Volume / Math.Abs(Position_InLoss_NetProfit) * (position_InProfit_NetProfit);
                    double NewVolume_Loss = position_InLoss_Volume - volume_Less;
                    double NewVolume_Profit = position_InLoss_Volume - volume_Less;

                    if (position_In_Profit.NetProfit >= 200)
                    {
                        ClosePosition(position_In_Profit);
                        ModifyPosition(position_In_Loss, NewVolume_Loss);
                    }

Hello friends I also have a problem with changing the volume for positions, please help

An example of the code that is written is as follows But this code does not change the size of the position

what is the Problem?

 


@majidmesbah123456

amusleh
28 Feb 2022, 09:12

RE: RE:

majidmesbah123456 said:

                    double volume_Less = position_InLoss_Volume / Math.Abs(Position_InLoss_NetProfit) * (position_InProfit_NetProfit);
                    double NewVolume_Loss = position_InLoss_Volume - volume_Less;
                    double NewVolume_Profit = position_InLoss_Volume - volume_Less;

                    if (position_In_Profit.NetProfit >= 200)
                    {
                        ClosePosition(position_In_Profit);
                        ModifyPosition(position_In_Loss, NewVolume_Loss);
                    }

Hello friends I also have a problem with changing the volume for positions, please help

An example of the code that is written is as follows But this code does not change the size of the position

what is the Problem?

 

Hi,

Please check the example #4 at: cAlgo API Reference - Position Interface (ctrader.com)

We can only help you if you post a full sample code that can reproduce the issue.


@amusleh