Break Even does not display in trade panel

Created at 23 Mar 2024, 06:31
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!
L0

L0pg

Joined 16.02.2014

Break Even does not display in trade panel
23 Mar 2024, 06:31


Afternoon,

I have been trying to get break even to populate in the trade options panel, but it does not show up

 

 [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleBreakEven : Robot
    {
        private Symbol _symbolInfo;
        private const string DefaultPositionIdParameterValue = "PID";

        [Parameter("Position Id", DefaultValue = DefaultPositionIdParameterValue)]
        public string PositionId { get; set; }

        [Parameter("Add Pips", DefaultValue = 0.0, MinValue = 0.0)]
        public double AddPips { get; set; }

        [Parameter("Trigger Pips", DefaultValue = 10, MinValue = 1)]
        public double TriggerPips { get; set; }

        protected override void OnStart()
        {
            if (PositionId == DefaultPositionIdParameterValue)
                PrintErrorAndStop("You have to specify \"Position Id\" in cBot Parameters");

            if (TriggerPips < AddPips + 2)
                PrintErrorAndStop("\"Trigger Pips\" must be greater or equal to \"Add Pips\" + 2");

            var position = FindPositionOrStop();
            _symbolInfo = Symbols.GetSymbol(position.SymbolName);

            BreakEvenIfNeeded();
        }

        private void PrintErrorAndStop(string errorMessage)
        {
            Print(errorMessage);
            Stop();

            throw new Exception(errorMessage);
        }

        private Position FindPositionOrStop()
        {
            var position = Positions.FirstOrDefault(p => "PID" + p.Id == PositionId || p.Id.ToString() == PositionId);
            if (position == null)
                PrintErrorAndStop("Position with Id = " + PositionId + " doesn't exist");

            return position;
        }

        protected override void OnTick()
        {
            BreakEvenIfNeeded();
        }

        private void BreakEvenIfNeeded()
        {
            var position = FindPositionOrStop();

            if (position.Pips < TriggerPips)
                return;

            var desiredNetProfitInDepositAsset = AddPips * _symbolInfo.PipValue * position.VolumeInUnits;
            var desiredGrossProfitInDepositAsset = desiredNetProfitInDepositAsset - position.Commissions * 2 - position.Swap;
            var quoteToDepositRate = _symbolInfo.PipValue / _symbolInfo.PipSize;
            var priceDifference = desiredGrossProfitInDepositAsset / (position.VolumeInUnits * quoteToDepositRate);

            var priceAdjustment = GetPriceAdjustmentByTradeType(position.TradeType, priceDifference);
            var breakEvenLevel = position.EntryPrice + priceAdjustment;
            var roundedBreakEvenLevel = RoundPrice(breakEvenLevel, position.TradeType);

            ModifyPosition(position, roundedBreakEvenLevel, position.TakeProfit);

            Print("Stop loss for position PID" + position.Id + " has been moved to break even.");
            Print("Stopping cBot..");
            Stop();
        }

        private double RoundPrice(double price, TradeType tradeType)
        {
            var multiplier = Math.Pow(10, _symbolInfo.Digits);

            if (tradeType == TradeType.Buy)
                return Math.Ceiling(price * multiplier) / multiplier;

            return Math.Floor(price * multiplier) / multiplier;
        }        
        private static double GetPriceAdjustmentByTradeType(TradeType tradeType, double priceDifference)
        {
        if (tradeType == TradeType.Buy)
                return priceDifference;

            return -priceDifference;
        }
       
}

@L0pg
Replies

PanagiotisCharalampous
25 Mar 2024, 06:46

Hi there,

You need to provide more information on your request if you expect to receive any help. Which trade options panel are you referring to? What do you expect to happen and what happens instead?

Best regards,

Panagiotis


@PanagiotisCharalampous