Skip to main content Skip to footer

StopLossTakeProfitTradeExitSignal - type of Exit Signal that allows to calculate Stop Loss and Take Profit for a strategy at each bar.

The main function inside which a developer should implement all logic of Stop Loss and Take Profit calculation is OnProcessPosition. The below example shows how to create simple Stop Loss signal.

 public class ConstantStopLossExitSignal : StopLossTakeProfitTradeExitSignal
{
        /// <summary>
        /// Distance in points 
        /// </summary>
        [UnitProperty(Name = "Stop loss size (points)", Order = 1, IncludeInTitle = true)]
        [Range(1, int.MaxValue)]

        public int DistancePts { get; set; } = 50;

        #region Overrides of ProtectiveOrderTradeExitSignal

        /// <inheritdoc />
        protected override void OnProcessPosition(Position position, ref double? stopLoss, ref double? takeProfit)
        {
            if (position.IsFlat()) //There is nothing to do with closed position
                return;

            var distance = DistancePts * InstrumentNotNull.PointSize;

            var price = position.ShiftAveragePriceBack(distance);
            if (price.IsNaN())
                return;

            stopLoss = InstrumentNotNull.Definition.RoundToTickSize(price);
        }

        #endregion
}

Cookies Notice

We use cookies to improve your experience, personalize content, and analyze our traffic. By clicking "Accept All Cookies," you agree to the storing of cookies on your device. You can manage your cookie preferences at any time by visiting our Cookie Settings.