Error in Modifying Position

Created at 16 Aug 2018, 19:49
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!
RY

ryanoia@gmail.com

Joined 09.03.2018

Error in Modifying Position
16 Aug 2018, 19:49


17/08/2018 00:45:24.724 | → Modifying position PID47206222 (SL: null, TP: 0.95132) FAILED with error "TechnicalError", Position PID47206222

PID47206222 at this time was a Sell AUDCAD at 0.20 lots. However, it happens randomly to different positions. 

 

Error Message says

"Order Execution 

Nothing to Change"

 

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.UTC, AccessRights = AccessRights.None)]
    public class TPforNukedPositions : Robot
    {
        [Parameter("TP for First Nukes", DefaultValue = "20")]
        public double TPforFirstNukes { get; set; }

        [Parameter("TP for First JPY Nukes", DefaultValue = "2000")]
        public double TPforFirstJPYNukes { get; set; }

        [Parameter("TP for High Nukes", DefaultValue = "0")]
        public double TPforHighNukes { get; set; }

        [Parameter("First Nuke Volume", DefaultValue = "10000")]
        public double FirstNukeVolume { get; set; }

        [Parameter("Highly Nuked Volume", DefaultValue = "320000")]
        public double HighlyNukedVolume { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnTick()
        {
            foreach (var position in Positions)
            {
                if (position.TradeType == TradeType.Buy && position.TakeProfit != position.EntryPrice + TPforFirstNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode != "GBPJPY" && position.SymbolCode != "EURJPY" && position.SymbolCode != "USDJPY" && position.SymbolCode != "CADJPY" && position.SymbolCode != "AUDJPY" && position.SymbolCode != "NZDJPY")
                {
                    var takeprofit = position.EntryPrice + TPforFirstNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Buy && position.TakeProfit != position.EntryPrice + TPforHighNukes * Symbol.PipSize && position.VolumeInUnits >= HighlyNukedVolume)
                {
                    var takeprofit = position.EntryPrice + TPforHighNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Sell && position.TakeProfit != position.EntryPrice - TPforFirstNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode != "GBPJPY" && position.SymbolCode != "EURJPY" && position.SymbolCode != "USDJPY" && position.SymbolCode != "CADJPY" && position.SymbolCode != "AUDJPY" && position.SymbolCode != "NZDJPY")
                {
                    var takeprofit = position.EntryPrice - TPforFirstNukes * Symbol.PipSize;
                    ModifyPositionAsync(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Sell && position.TakeProfit != position.EntryPrice - TPforHighNukes * Symbol.PipSize && position.VolumeInUnits >= HighlyNukedVolume)
                {
                    var takeprofit = position.EntryPrice - TPforHighNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Buy && position.TakeProfit != position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "GBPJPY")
                {
                    var takeprofit = position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Buy && position.TakeProfit != position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "EURJPY")
                {
                    var takeprofit = position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Buy && position.TakeProfit != position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "USDJPY")
                {
                    var takeprofit = position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Buy && position.TakeProfit != position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "CADJPY")
                {
                    var takeprofit = position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Buy && position.TakeProfit != position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "AUDJPY")
                {
                    var takeprofit = position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Buy && position.TakeProfit != position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "NZDJPY")
                {
                    var takeprofit = position.EntryPrice + TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Sell && position.TakeProfit != position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "GBPJPY")
                {
                    var takeprofit = position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Sell && position.TakeProfit != position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "EURJPY")
                {
                    var takeprofit = position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Sell && position.TakeProfit != position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "USDJPY")
                {
                    var takeprofit = position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Sell && position.TakeProfit != position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "CADJPY")
                {
                    var takeprofit = position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Sell && position.TakeProfit != position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "AUDJPY")
                {
                    var takeprofit = position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Sell && position.TakeProfit != position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize && position.VolumeInUnits >= FirstNukeVolume && position.VolumeInUnits < HighlyNukedVolume && position.SymbolCode == "NZDJPY")
                {
                    var takeprofit = position.EntryPrice - TPforFirstJPYNukes * Symbol.PipSize;
                    ModifyPosition(position, null, takeprofit);
                }
            }
            // Put your core logic here
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 


@ryanoia@gmail.com
Replies

ryanoia@gmail.com
20 Aug 2018, 07:00 ( Updated at: 21 Dec 2023, 09:20 )

Perhaps my query is complicated. Need to simplify the code. Here's the 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.UTC, AccessRights = AccessRights.None)]
    public class AutoTPforNukedPositions : Robot
    {
        [Parameter("No Nuke TP", DefaultValue = "11")]
        public double NoNukeTP { get; set; }

        [Parameter("No Nuke Volume", DefaultValue = "20000")]
        public double NoNukeVolume { get; set; }
        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnTick()
        {
            foreach (var position in Positions)
            {
                if (position.TradeType == TradeType.Buy && position.TakeProfit != position.EntryPrice + NoNukeTP * Symbol.PipSize && position.VolumeInUnits == NoNukeVolume)
                {
                    var takeprofit = position.EntryPrice + NoNukeTP * Symbol.PipSize;
                    ModifyPositionAsync(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Sell && position.TakeProfit != position.EntryPrice - NoNukeTP * Symbol.PipSize && position.VolumeInUnits == NoNukeVolume)
                {
                    var takeprofit = position.EntryPrice - NoNukeTP * Symbol.PipSize;
                    ModifyPositionAsync(position, null, takeprofit);
                }
                // No Nuke
            }
            // Put your core logic here
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

As previously stated, this is the error:

Error Message says

"Order Execution 

Nothing to Change"

---------

This bot changes TP of any position (long or short) to 11, but the conditions for this bot to execute are:

  1. the volume of the position should be 20K; and
  2. the TP of such position is not equal to 11

However, what happens is, it finds a position with such volume BUT with same TP of 11, AND still it tries to change its TP to 11. However, since the TP was already 11 to begin with, it returns an error - "nothing to change". Then it repeats the process over and over again.

This is what the error looks like:

The question is, why would the bot request to amend the TP of this position when its TP is already 11?


@ryanoia@gmail.com

PanagiotisCharalampous
20 Aug 2018, 10:25

Hi ryanoia@gmail.com,

Can you try using ModifyPosition instead of ModifyPositionAsync and let me know if this still happens?

Best Regards,

Panagiotis


@PanagiotisCharalampous

ryanoia@gmail.com
20 Aug 2018, 10:41

Already did. The first version of this was not async, just modifyposition, as may be observed in the first post above which is lengthy so I understand if nobody read that.

I wouldn't even read that myself again. hehe


@ryanoia@gmail.com

PanagiotisCharalampous
20 Aug 2018, 11:08

Hi ryanoia@gmail.com,

Seems to be a rounding issue. Try the code below

                if (position.TradeType == TradeType.Buy && Math.Round(position.TakeProfit.Value, Symbol.Digits) != Math.Round(position.EntryPrice + NoNukeTP * Symbol.PipSize, Symbol.Digits) && position.VolumeInUnits == NoNukeVolume)
                {
                    var takeprofit = position.EntryPrice + NoNukeTP * Symbol.PipSize;
                    ModifyPositionAsync(position, null, takeprofit);
                }
                if (position.TradeType == TradeType.Sell && Math.Round(position.TakeProfit.Value, Symbol.Digits) != Math.Round(position.EntryPrice - NoNukeTP * Symbol.PipSize, Symbol.Digits) && position.VolumeInUnits == NoNukeVolume)
                {
                    var takeprofit = position.EntryPrice - NoNukeTP * Symbol.PipSize;
                    ModifyPositionAsync(position, null, takeprofit);
                }

Best Regards,

Panagiotis


@PanagiotisCharalampous