Break Even Isn't Working!

Created at 27 Mar 2017, 10:55
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!
20

201058436

Joined 27.03.2017

Break Even Isn't Working!
27 Mar 2017, 10:55


I tried everything I could think of before coming here, however.. the Break Even code below still isn't working in either a back test or with a demo account.

I've tried a range of examples from other source codes including the one from the sample Spotware provided.. but none seem to work. As far as I can tell, it should be.

Essentially and be clear.. what I wanted it to do was, once the pending order that was previously created was hit and turned into a position, then, wait for symbol price in pips to hit the target trigger in pips and then modify that position with an updated stop loss.


I don't normally ask for help and would appreciate any that anyone can spare.

 

--Thank you.

 


        //Break Even Settings
        [Parameter("Move SL To Break Even", DefaultValue = true)]
        public bool MoveToBreakEven { get; set; }
        [Parameter("Trigger when gaining:", DefaultValue = 5, MinValue = 0, Step = 0.25)]
        public double BETriggerWhenGaining { get; set; }
        [Parameter("Additional pips to move SL:", DefaultValue = 0.75, MinValue = 0, Step = 0.25)]
        public double AdditionalAfterBE { get; set; }


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


        private void StopLossOnBreakEven()
        //Set Stop Loss To Break Even + Desired Pips If Entry Price + Pips Is Hit
        {
            foreach (var LongPosition in Positions.FindAll(OpenLong, Symbol, TradeType.Buy))
            foreach (var ShortPosition in Positions.FindAll(OpenShort, Symbol, TradeType.Sell))
                {
                    if (LongPosition != null)
                    {
                        double BEStopLossLong = (LongPosition.EntryPrice + (Symbol.PipSize * AdditionalAfterBE));
                        double BETakeProfitLong = (LongPosition.EntryPrice + (Symbol.PipSize * TakeProfitAllPips));
                        {
                            if (MoveToBreakEven == true & LongPosition.TradeType == TradeType.Buy & ((LongPosition.EntryPrice + LongPosition.Pips) >= ((LongPosition.EntryPrice) + (Symbol.PipSize * BETriggerWhenGaining))))
                                ModifyPosition(LongPosition, BEStopLossLong, BETakeProfitLong);
                        }
                    }
                    else if (ShortPosition != null)
                    {
                        double BEStopLossShort = (ShortPosition.EntryPrice - (Symbol.PipSize * AdditionalAfterBE));
                        double BETakeProfitShort = (ShortPosition.EntryPrice - (Symbol.PipSize * TakeProfitAllPips));
                        {
                            if (MoveToBreakEven == true & ShortPosition.TradeType == TradeType.Sell & ((ShortPosition.EntryPrice + ShortPosition.Pips) <= ((ShortPosition.EntryPrice) - (Symbol.PipSize * BETriggerWhenGaining))))
                            ModifyPosition(ShortPosition, BEStopLossShort, BETakeProfitShort);
                        }
                    }
                }
            }
            

@201058436
Replies

201058436
28 Mar 2017, 11:30

RE:

AthenasGuidance said:

I've solved this problem, the updated code below works:

        protected override void OnTick()
        {
            {
            try {
                if (MoveToBreakEven == true)
                    {
                    
                    var PositionLong = Positions.Find("OpenLong");
                    var PositionPriceLong = ((PositionLong.EntryPrice * Symbol.PipSize) + (PositionLong.Pips * Symbol.PipSize));
                    var BEStopLossTriggerLong = ((PositionLong.EntryPrice * Symbol.PipSize) + (BETriggerWhenGaining * Symbol.PipSize));
                    var BEStopLossLong = (PositionLong.EntryPrice + AdditionalAfterBE);
                    var BETakeProfitLong = (PositionLong.EntryPrice + TakeProfitAllPips);
                    
                    if (PositionPriceLong >= BEStopLossTriggerLong)
                        {
                            ModifyPositionAsync(PositionLong, BEStopLossLong, BETakeProfitLong);
                        }
                    }
                } catch (Exception e)
                {
                    Print("{0} ", e.StackTrace);
                }
            }
            //Break Even Short
            {
            try {
                if (MoveToBreakEven == true)
                    {
                    
                    var PositionShort = Positions.Find("OpenShort");
                    var PositionPriceShort = ((PositionShort.EntryPrice * Symbol.PipSize) - (PositionShort.Pips * Symbol.PipSize));
                    var BEStopLossTriggerShort = ((PositionShort.EntryPrice * Symbol.PipSize) - (BETriggerWhenGaining * Symbol.PipSize));
                    var BEStopLossShort = (PositionShort.EntryPrice - AdditionalAfterBE);
                    var BETakeProfitShort = (PositionShort.EntryPrice - TakeProfitAllPips);
                    
                    if (PositionPriceShort <= BEStopLossTriggerShort)
                        {
                            ModifyPositionAsync(PositionShort, BEStopLossShort, BETakeProfitShort);
                        }
                    }
                } catch (Exception e)
                {
                    Print("{0} ", e.StackTrace);
                }
            }
        }

 


@201058436