Sell Trailing Stop

Created at 05 Oct 2012, 16:46
admin's avatar

Spotware Support

Joined 30.09.2011

Sell Trailing Stop
05 Oct 2012, 16:46


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);
                }
            }
        }
    }
}

 


@admin
Replies

vobr
12 Mar 2014, 16:06

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


@vobr

abdalah hacid
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.


@aysos75

cTrader Team
05 Aug 2014, 10:58

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


@Spotware

nguyendan81985
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


@nguyendan81985

Panagiotis Charalampous
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


@PanagiotisCharalampous