Issue setting breakeven

Created at 11 Feb 2023, 21:04
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!
VE

veenajetwisespisan

Joined 07.02.2023

Issue setting breakeven
11 Feb 2023, 21:04


Hi, can anyone help me with my CBOT?

I have issues with few things.

1. Breakeven. I would like it as such when the stop loss move to stop even, it also covers the commissions. Right now it is not doing so.

2. Is it possible to execute the trade only when it has broken the "high" or "low" of the confirmation candle by 0.5 pips?

3. The cbot seems to execute the trade 1 candle late.

 

Can anyone look into my Cbot can rectify the issues for me?

Thank you very much for the help.

 

 

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 JC3 : Robot
    {

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


        [Parameter("Risk [%]", DefaultValue = 5)]
        public double Risk { get; set; }


        private FractalChaosBands FCB;
        private AwesomeOscillator AWO;
        private MacdCrossOver MACD;


        double Support = 0;
        double Resistance = 0;

        double Lambo = 0.5;
        double Huracan = 0;
        double Aventador = 0;


        protected override void OnStart()
        {

            FCB = Indicators.FractalChaosBands();
            AWO = Indicators.AwesomeOscillator();
            MACD = Indicators.MacdCrossOver(26, 12, 9);


        }



        protected override void OnBar()
        {
            double spread1 = Bars.HighPrices.Last(1) - Bars.LowPrices.Last(1);
            double spread2 = Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2);
            double spread3 = Bars.HighPrices.Last(3) - Bars.LowPrices.Last(3);
            double spread4 = Bars.HighPrices.Last(4) - Bars.LowPrices.Last(4);
            double spread5 = Bars.HighPrices.Last(5) - Bars.LowPrices.Last(5);
            double spread6 = Bars.HighPrices.Last(6) - Bars.LowPrices.Last(6);
            double spread7 = Bars.HighPrices.Last(7) - Bars.LowPrices.Last(7);
            double spread8 = Bars.HighPrices.Last(8) - Bars.LowPrices.Last(8);
            double spread9 = Bars.HighPrices.Last(9) - Bars.LowPrices.Last(9);
            double spread10 = Bars.HighPrices.Last(10) - Bars.LowPrices.Last(10);
            double spread11 = Bars.HighPrices.Last(11) - Bars.LowPrices.Last(11);
            double spread12 = Bars.HighPrices.Last(12) - Bars.LowPrices.Last(12);
            double spread13 = Bars.HighPrices.Last(13) - Bars.LowPrices.Last(13);
            double spread14 = Bars.HighPrices.Last(14) - Bars.LowPrices.Last(14);

            double LargestSpread = Math.Max(spread14, Math.Max(spread13, Math.Max(spread12, Math.Max(spread11, Math.Max(spread10, Math.Max(spread9, Math.Max(spread8, Math.Max(spread7, Math.Max(spread6, Math.Max(spread5, Math.Max(spread4, Math.Max(spread3, Math.Max(spread2, spread1)))))))))))));

            double volume1 = Bars.TickVolumes.Last(1);
            double volume2 = Bars.TickVolumes.Last(2);
            double volume3 = Bars.TickVolumes.Last(3);
            double volume4 = Bars.TickVolumes.Last(4);
            double volume5 = Bars.TickVolumes.Last(5);
            double volume6 = Bars.TickVolumes.Last(6);
            double volume7 = Bars.TickVolumes.Last(7);
            double volume8 = Bars.TickVolumes.Last(8);
            double volume9 = Bars.TickVolumes.Last(9);
            double volume10 = Bars.TickVolumes.Last(10);

            double LargestVolume = Math.Max(volume10, Math.Max(volume9, Math.Max(volume8, Math.Max(volume7, Math.Max(volume6, Math.Max(volume5, Math.Max(volume4, Math.Max(volume3, Math.Max(volume2, volume1)))))))));

            double COspread4 = Math.Abs(Bars.OpenPrices.Last(4) - Bars.ClosePrices.Last(4));
            double COspread3 = Math.Abs(Bars.OpenPrices.Last(3) - Bars.ClosePrices.Last(3));
            double COspread2 = Math.Abs(Bars.OpenPrices.Last(2) - Bars.ClosePrices.Last(2));
            double COspread1 = Math.Abs(Bars.OpenPrices.Last(1) - Bars.ClosePrices.Last(1));




            //Your core logic here
            if (spread1 < LargestSpread && volume1 < LargestVolume && Bars.ClosePrices.Last(1) < Bars.OpenPrices.Last(1) && Bars.ClosePrices.Last(1) < Bars.LowPrices.Last(2) && Bars.ClosePrices.Last(1) < (Bars.HighPrices.Last(1) - (spread1 * 0.699)))
            {
                                              
                                                    if (Bars.OpenPrices.LastValue >= Bars.ClosePrices.Last(1))
                                                    {

                                                        if (PendingOrders.Count != 1)
                                                        {
                                                            Support = Bars.LowPrices.Last(1);
                                                            Resistance = Bars.HighPrices.Last(1);
                                                            double differenceInPips = Math.Abs((Support - Resistance) + (spread1 * 0.2) / Symbol.PipSize);
                                                            double differenceInPips1 = Math.Abs(((Support - Resistance) * 1) / Symbol.PipSize);
                                                            double differenceInPips2 = Math.Abs((Support - Resistance) / Symbol.PipSize);
                                                            DateTime? expiry = Server.Time.AddMinutes(1);
                                                            Huracan = differenceInPips;
                                                            Aventador = differenceInPips2;
                                                            PlaceStopLimitOrder(TradeType.Sell, Symbol, Volume(), Support, 1, "Sell", differenceInPips, differenceInPips1, expiry);
                                                            Lambo = 0;




                    }
                }

            }





            if (Trade.IsExecuting)
                return;

            foreach (var position in Positions)
            {


                if (position.TradeType == TradeType.Sell)
                {

                    var breakevenPrice = position.EntryPrice;
                    var targetbreakeven = position.NetProfit / 2;

                    if (Symbol.Bid < position.EntryPrice - targetbreakeven)
                    {

                        position.ModifyStopLossPrice(breakevenPrice);


                    }
                }
                else
                {
                    var breakevenPrice1 = position.EntryPrice;
                    var targetbreakeven = position.NetProfit / 2;

                    if (Symbol.Bid > position.EntryPrice + targetbreakeven)
                    {

                        position.ModifyStopLossPrice(breakevenPrice1);


                    }
                }
            }
        }



        private double Volume()
        {

            var Volume1 = ((Account.Balance * (Risk * 100)) / (Aventador));

            var Volume2 = Symbol.NormalizeVolumeInUnits(Volume1);

            return Volume2;

        }



    }
}


@veenajetwisespisan