Is this a bug of StopLoss setting when using PlaceStopOrderAsync()

Created at 23 Jun 2016, 10:46
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!
xjacks's avatar

xjacks

Joined 24.02.2016

Is this a bug of StopLoss setting when using PlaceStopOrderAsync()
23 Jun 2016, 10:46


I use the following statement to place stop order with stoploss:

PlaceStopOrderAsync(TradeType.Buy, Symbol, volume, openPrice, Lable, 5.5, takeProfitPips, Server.Time.AddHours(OrderExpireHour), Comment);

You can see I hardcoded 5.5 pips stoploss in the code. But when I run backtest, the stoploss is never 5.5pips, sometime is 5.0, sometimes is 6.0. Like the snapshot below.

Is this a bug? Or I did something wrong when I call PlaceStopOrderAsync()?

Thanks!


@xjacks
Replies

whis.gg
23 Jun 2016, 12:47

It's not a bug, stop loss and take profit haven't supported decimal places, so it always rounds to whole number. However, there is a workaround - see the sample code below.

using System;
using cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Sample : Robot
    {
        [Parameter(DefaultValue = 5.5)]
        public double StopLossPips { get; set; }

        [Parameter(DefaultValue = 8.1)]
        public double TakeProfitPips { get; set; }

        protected override void OnStart()
        {
            ExecuteMarketOrderAsync(TradeType.Sell, Symbol, Symbol.VolumeMin, "label");
            Positions.Opened += OnPositionsOpened;
        }

        private void OnPositionsOpened(PositionOpenedEventArgs obj)
        {
            var position = obj.Position;
            if (position.Label == "label")
            {
                int direction = position.TradeType == TradeType.Buy ? 1 : -1;

                double stopLoss = position.EntryPrice - StopLossPips * Symbol.PipSize * direction;
                double takeProfit = position.EntryPrice + TakeProfitPips * Symbol.PipSize * direction;

                ModifyPosition(position, stopLoss, takeProfit);
            }
        }
    }
}

 


@whis.gg

xjacks
24 Jun 2016, 06:57

Thank you for your help, it works!

I thought the PlaceStopOrderAsync() method supports stop loss and take profit with decimal because these parameter are all in double? type...Hope these can be improved in the future version.

 


@xjacks