Breakeven - keeps executing

Created at 21 Oct 2019, 11:45
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!
NY

NyanHtet

Joined 14.02.2017

Breakeven - keeps executing
21 Oct 2019, 11:45


Hi,

The breakeven adjustment works fine. 

but the modifying does not stop. it keeps executing. 

It seems like I need another condition in  "IF"

but not sure how to do so.. can anyone help me with this? 

 

 

   private void BreakEvenAdjustment()
        {
            var allPositions = Positions.FindAll(label, SymbolName);

            foreach (Position position in allPositions)
            {
                //   if (position.StopLoss != null)
                //      return;

                var entryPrice = position.EntryPrice;
                var distance = position.TradeType == TradeType.Buy ? Symbol.Bid - entryPrice : entryPrice - Symbol.Ask;

                // move stop loss to break even plus and additional (x) pips
                if (distance >= BreakEvenPips * Symbol.PipSize)
                {
                    if (position.TradeType == TradeType.Buy)
                    {
                        if (position.StopLoss <= position.EntryPrice + (Symbol.PipSize * breakevenextrapips) || position.StopLoss == null)
                        {
                            ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * breakevenextrapips), position.TakeProfit);
                            Print("Stop Loss to Break Even set for BUY position {0}", position.Id);
                        }
                    }
                    else
                    {
                        if (position.StopLoss >= position.EntryPrice - (Symbol.PipSize * breakevenextrapips) || position.StopLoss == null)
                        {
                            ModifyPosition(position, entryPrice - (Symbol.PipSize * breakevenextrapips), position.TakeProfit);
                            Print("Stop Loss to Break Even set for SELL position {0}", position.Id);
                        }
                    }
                }
            }
        }


@NyanHtet
Replies

PanagiotisCharalampous
21 Oct 2019, 12:01

Hi NyanHtet,

Since we do not have the complete cBot code to reproduce the problem, we can only make guesses. Try rounding your calculated values to 5 decimal points and let us know if it resolves the problem. Example

position.StopLoss <= Math.Round(position.EntryPrice + (Symbol.PipSize * breakevenextrapips),5)

Else please post the complete cBot code and steps to reproduce the behavior.

Best Regards,

Panagiotis


@PanagiotisCharalampous

NyanHtet
21 Oct 2019, 12:07

it seem like i fixed it. 

added another loop inside.

testing now.

 

 

  if (distance >= BreakEvenPips * Symbol.PipSize)
                {
                    if (position.TradeType == TradeType.Buy)
                    {
                        if (position.StopLoss <= position.EntryPrice + (Symbol.PipSize * breakevenextrapips) || position.StopLoss == null)
                        {

                            //Fixed _21oct19
                            if (position.StopLoss == position.EntryPrice + (Symbol.PipSize * breakevenextrapips))
                            {
                                break;
                            }

                            else
                            {
                                ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * breakevenextrapips), position.TakeProfit);
                                Print("Stop Loss to Break Even set for BUY position {0}", position.Id);
                            }

                        }
                    }


@NyanHtet