What's broken?!?!

Created at 11 Jan 2018, 16:14
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!
DR

Drummond360

Joined 22.12.2017

What's broken?!?!
11 Jan 2018, 16:14


Hi All,

I'm using a simple trailing stop to close each trade taken, here's the code:

  // Trailing stop, first search for positions using this symbol and this robot (label)
            var symbolSearch = Positions.FindAll(label, Symbol);

            foreach (Position position in symbolSearch)
            {
                //Define position direction
                if (position.TradeType == TradeType.Sell)
                {
                    // Calculate distance position has traveled
                    double distance = position.EntryPrice - Symbol.Ask;
                    // Check if distance is greater than trigger to start trailing
                    if (distance >= TrailingTrigger * Symbol.PipSize)
                    {
                        // Calculate new stop loss level
                        double newStopLossPrice = Symbol.Ask + TrailingStopLossDistance * Symbol.PipSize;
                        // Check if new SL level is better than current level
                        if (newStopLossPrice < position.StopLoss)
                        {
                            // Modify SL level and set take profit to null, we only exit when the trailing SL is hit
                            ModifyPosition(position, newStopLossPrice, null);
                            Print("Short Position ID = " + position.Id + " new stop loss = " + newStopLossPrice);

                        }
                    }
                }
                //opposing code for TradeType.Buy
                else
                {

                    double distance = Symbol.Bid - position.EntryPrice;

                    if (distance >= TrailingTrigger * Symbol.PipSize)
                    {

                        double newStopLossPrice = Symbol.Bid - TrailingStopLossDistance * Symbol.PipSize;

                        if (newStopLossPrice > position.StopLoss)
                        {

                            ModifyPosition(position, newStopLossPrice, null);
                            Print("Long Position ID = " + position.Id + " new stop loss = " + newStopLossPrice);
                        }
                    }
                }
            }
        }
    }
}

I'm back testing using Tick data and the result can be seen in the image below... The trade should have locked in a profit of over £50 but instead realized a loss of over £7... 

How can this happen?!?!

Thank you in advance...

 

 


@Drummond360
Replies

Drummond360
11 Jan 2018, 16:15 ( Updated at: 21 Dec 2023, 09:20 )

A better image incase you can't see it...


@Drummond360

PanagiotisCharalampous
12 Jan 2018, 10:57

Hi Drummond360,

The provided information is not enough for us to reach to a conclusion. We need to understand why you expected that the trade should lock in a profit. In order to be able to investigate this, we will all the necessary information to be able to run the backtesting ourselves. So we need

  • The full cBot code.
  • The cBot's initial parameters.
  • All the backtesting parameters.
  • The trade that you believe should be profitable.

If you send us the above, we can have a look and advise further.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Drummond360
12 Jan 2018, 10:59

That's great thank you Panagiotis..

Can I email you all this information?

 


@Drummond360

PanagiotisCharalampous
12 Jan 2018, 11:50

Hi Drummond360,

It is better to put the information in the forum so that other community members can contribute as well and the possible solutions are kept for future reference.

However I understand that you might not want to expose your work to everybody. If this is the case then fill free to contact me at community at spotware dot com.

Best Regards,

Panagiotis


@PanagiotisCharalampous