Sell Trailing Stop

admin's avatar

admin since: 30 Nov -0001;

  05 Oct 2012, 16:46
Sell Trailing Stop

UPDATED

This robot is intended to be used as a sample and does not guarantee any particular outcome or profit of any kind. Use it at your own risk.

// -------------------------------------------------------------------------------------------------
//  In "Sample Sell Trailing" Robot the user can set the variable "Trigger (pips)" 
//  which defines the point where the Stop Loss will start trailing the order. 
//  When the profit in pips is above or equal to "Trigger (pips)" the stop loss will start trailing the spot price.
//  Until this condition is met the user can set a normal Stop Loss using the "Stop Loss (pips)" variable. 
//  Variable "Trailing Stop (pips)" defines the number of pips the Stop Loss trails the spot price by. 
//  The user can select to also set a take profit in his order with parameter "Take Profit (pips)". 
//
// -------------------------------------------------------------------------------------------------

using cAlgo.API;

namespace cAlgo.Robots.Samples
{
    [Robot("Sample Sell Trailing Robot", TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleSellTrailing : Robot
    {
        [Parameter(DefaultValue = 10000)]
        public int Volume { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { get; set; }

        [Parameter("Trigger (pips)", DefaultValue = 20)]
        public int Trigger { get; set; }

        [Parameter("Trailing Stop (pips)", DefaultValue = 10)]
        public int TrailingStop { get; set; }

        protected override void OnStart()
        {
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SampleSellTrailing", StopLoss, TakeProfit);
        }

        protected override void OnTick()
        {
            var position = Positions.Find("SampleSellTrailing");

            if (position == null)
            {
                Stop();
                return;
            }

            var distance = Symbol.Ask - position.EntryPrice;
            if (distance >= Trigger * Symbol.PipSize)
            {
                double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;
                if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                }
            }
        }
    }
}
TRADERS FIRST™

vobr since: 12 Mar 2014;

  12 Mar 2014, 16:06

What I need to change, to make Buy Trailing Stop?)

aysos75's avatar

aysos75 since: 28 Sep 2013;

  05 Aug 2014, 00:00
RE:

admin said:

UPDATED

This robot is intended to be used as a sample and does not guarantee any particular outcome or profit of any kind. Use it at your own risk.

// -------------------------------------------------------------------------------------------------
//  In "Sample Sell Trailing" Robot the user can set the variable "Trigger (pips)" 
//  which defines the point where the Stop Loss will start trailing the order. 
//  When the profit in pips is above or equal to "Trigger (pips)" the stop loss will start trailing the spot price.
//  Until this condition is met the user can set a normal Stop Loss using the "Stop Loss (pips)" variable. 
//  Variable "Trailing Stop (pips)" defines the number of pips the Stop Loss trails the spot price by. 
//  The user can select to also set a take profit in his order with parameter "Take Profit (pips)". 
//
// -------------------------------------------------------------------------------------------------

using cAlgo.API;

namespace cAlgo.Robots.Samples
{
    [Robot("Sample Buy Trailing Robot", TimeZone = TimeZones.UTC)]
    public class SampleSellTrailing : Robot
    {
        [Parameter(DefaultValue = 10000)]
        public int Volume { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { get; set; }

        [Parameter("Trigger (pips)", DefaultValue = 20)]
        public int Trigger { get; set; }

        [Parameter("Trailing Stop (pips)", DefaultValue = 10)]
        public int TrailingStop { get; set; }

        protected override void OnStart()
        {
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SampleSellTrailing", StopLoss, TakeProfit);
        }

        protected override void OnTick()
        {
            var position = Positions.Find("SampleSellTrailing");

            if (position == null)
            {
                Stop();
                return;
            }

            double distance = position.EntryPrice - Symbol.Ask;

            if (distance >= Trigger * Symbol.PipSize)
            {
                double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;
                if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                }
            }
        }
    }
}

This code is bad!

because the test 

distance >= Trigger * Symbol.PipSize

is alway true!

and 

newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;

is away from the initial stop.

Voir les choses du point de vue des choses elles mêmes.
Spotware's avatar

Spotware since: 23 Sep 2013;

  05 Aug 2014, 10:58

Abdallah_Hacid, thank you for the issue report. Code is updated.

TRADERS FIRST™ Vote for your favorite features: https://ctrader.com/forum/suggestions

nguyendan81985 since: 24 Oct 2018;

  26 Dec 2020, 15:34
RE:

admin said:

UPDATED

This robot is intended to be used as a sample and does not guarantee any particular outcome or profit of any kind. Use it at your own risk.

// -------------------------------------------------------------------------------------------------
//  In "Sample Sell Trailing" Robot the user can set the variable "Trigger (pips)" 
//  which defines the point where the Stop Loss will start trailing the order. 
//  When the profit in pips is above or equal to "Trigger (pips)" the stop loss will start trailing the spot price.
//  Until this condition is met the user can set a normal Stop Loss using the "Stop Loss (pips)" variable. 
//  Variable "Trailing Stop (pips)" defines the number of pips the Stop Loss trails the spot price by. 
//  The user can select to also set a take profit in his order with parameter "Take Profit (pips)". 
//
// -------------------------------------------------------------------------------------------------

using cAlgo.API;

namespace cAlgo.Robots.Samples
{
    [Robot("Sample Sell Trailing Robot", TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleSellTrailing : Robot
    {
        [Parameter(DefaultValue = 10000)]
        public int Volume { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { get; set; }

        [Parameter("Trigger (pips)", DefaultValue = 20)]
        public int Trigger { get; set; }

        [Parameter("Trailing Stop (pips)", DefaultValue = 10)]
        public int TrailingStop { get; set; }

        protected override void OnStart()
        {
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SampleSellTrailing", StopLoss, TakeProfit);
        }

        protected override void OnTick()
        {
            var position = Positions.Find("SampleSellTrailing");

            if (position == null)
            {
                Stop();
                return;
            }

            var distance = Symbol.Ask - position.EntryPrice;
            if (distance >= Trigger * Symbol.PipSize)
            {
                double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;
                if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                }
            }
        }
    }
}

how can i use this code in my robot? can you help me?, i just newer. thanks

PanagiotisCharalampous's avatar

PanagiotisCharalampous since: 13 Jan 2017;

  28 Dec 2020, 09:04

Hi nguyendan81985,

What kind of help do you need? cTrader Automate help files can be found here.

Best Regards,

Panagiotis 

Join us on Telegram

Head of Community Management at cTrader