Sample Trailing Stop

cAlgo_Fanatic's avatar

cAlgo_Fanatic since: 10 Jan 2013;

  26 Apr 2013, 15:44
Sample Trailing Stop
// -------------------------------------------------------------------------------------------------
//
//    The "SampleTrailingbyLabel" cBot will trail an open position given the position Label. 
//    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.
//    Variable "Trailing Stop (pips)" defines the number of pips the Stop Loss trails the spot price by. 
//
// -------------------------------------------------------------------------------------------------

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot("Sample Trailing Stop Robot")]
    public class SampleTrailingbyLabel : Robot
    {

        [Parameter("Position Label", DefaultValue = "My Label")]
        public string MyLabel { get; set; }

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

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

        private bool _isTrigerred;
        
        protected override void OnStart()
        {
            Positions.Opened += PositionsOnOpened;
            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, MyLabel);
        }

        private void PositionsOnOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;
            Print("Position {0} found, waiting for trigger.", position.Label);
        }

        protected override void OnTick()
        {
            var position = Positions.Find(MyLabel);
            
            if (position == null) return;

            if (position.TradeType == TradeType.Buy)
            {
                double distance = Symbol.Bid - position.EntryPrice;

                if (distance >= Trigger * Symbol.PipSize)
                {
                    if (!_isTrigerred)
                    {
                        _isTrigerred = true;
                        Print("Trailing Stop Loss triggered...");
                    }

                    double newStopLossPrice = Math.Round(Symbol.Bid - TrailingStop * Symbol.PipSize, Symbol.Digits);

                    if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                    {
                        ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                    }
                }
            }
            else
            {
                double distance = position.EntryPrice - Symbol.Ask;

                if (distance >= Trigger * Symbol.PipSize)
                {
                    if (!_isTrigerred)
                    {
                        _isTrigerred = true;
                        Print("Trailing Stop Loss triggered...");
                    }

                    double newStopLossPrice = Math.Round(Symbol.Ask + TrailingStop * Symbol.PipSize, Symbol.Digits);

                    if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                    {
                        ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                    }
                }
            }
        }

    }
}
TRADERS FIRST™
aimerdoux's avatar

aimerdoux since: 07 Jun 2015;

  07 Jun 2015, 01:46
RE:

cAlgo_Fanatic said: hello i have a problem using this cbot in my sample code cbot it doesnt allow me to build the bot and shows this problem Error CS1501: Ninguna sobrecarga para el método 'ExecuteMarketOrder' toma '5' argumentos id appreciate your help

// -------------------------------------------------------------------------------------------------
//
//    The "SampleTrailingbyLabel" cBot will trail an open position given the position Label. 
//    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.
//    Variable "Trailing Stop (pips)" defines the number of pips the Stop Loss trails the spot price by. 
//
// -------------------------------------------------------------------------------------------------

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot("Sample Trailing Stop Robot")]
    public class SampleTrailingbyLabel : Robot
    {

        [Parameter("Position Label", DefaultValue = "My Label")]
        public string MyLabel { get; set; }

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

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

        private bool _isTrigerred;
        
        protected override void OnStart()
        {
            Positions.Opened += PositionsOnOpened;
            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, MyLabel);
        }

        private void PositionsOnOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;
            Print("Position {0} found, waiting for trigger.", position.Label);
        }

        protected override void OnTick()
        {
            var position = Positions.Find(MyLabel);
            
            if (position == null) return;

            if (position.TradeType == TradeType.Buy)
            {
                double distance = Symbol.Bid - position.EntryPrice;

                if (distance >= Trigger * Symbol.PipSize)
                {
                    if (!_isTrigerred)
                    {
                        _isTrigerred = true;
                        Print("Trailing Stop Loss triggered...");
                    }

                    double newStopLossPrice = Math.Round(Symbol.Bid - TrailingStop * Symbol.PipSize, Symbol.Digits);

                    if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                    {
                        ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                    }
                }
            }
            else
            {
                double distance = position.EntryPrice - Symbol.Ask;

                if (distance >= Trigger * Symbol.PipSize)
                {
                    if (!_isTrigerred)
                    {
                        _isTrigerred = true;
                        Print("Trailing Stop Loss triggered...");
                    }

                    double newStopLossPrice = Math.Round(Symbol.Ask + TrailingStop * Symbol.PipSize, Symbol.Digits);

                    if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                    {
                        ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                    }
                }
            }
        }

    }
}
Spotware's avatar

Spotware since: 23 Sep 2013;

  10 Jun 2015, 11:38

Dear Trader,

We recompiled the example and we do not get any compilation errors. Could you please provide more details regarding the issue?

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

aimerdoux since: 07 Jun 2015;

  13 Jul 2015, 02:04

the Sample is not totally good written due to the fact that the boolean IsTriggered Works Only once, in the login information you can see that if the trailling stop is activated more than once the Boolean _Istrigerred Only shows the Print information the first time because there is not code to make it go back to the state false;

Spotware's avatar

Spotware since: 23 Sep 2013;

  13 Jul 2015, 11:28

Dear Trader,

You are right. The example is activating only one trailing stop for one specific open position. However you can it as example to enhance your cBot.

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

aimerdoux since: 07 Jun 2015;

  13 Jul 2015, 22:06

I wonder if is too much to ask if u can Show the two lines code that will correct the issue?

Spotware's avatar

Spotware since: 23 Sep 2013;

  14 Jul 2015, 10:36

Dear aimerdoux,

We do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also can contact one of our Partners or post a job in Development Jobs section for coding assistance.

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

aimerdoux since: 07 Jun 2015;

  14 Jul 2015, 17:36
RE:

This post was removed by moderator.

aimerdoux's avatar

aimerdoux since: 07 Jun 2015;

  14 Jul 2015, 17:43
RE:

This post was removed by moderator.

spike.bhv since: 17 Jul 2015;

  17 Jul 2015, 10:23
protected override void OnStart()
        {
            Positions.Opened += PositionsOnOpened;
            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, MyLabel);
            Positions.Closed += PositionsOnClosed;
        }
 
private void PositionsOnOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;
            Print("Position {0} found, waiting for trigger.", position.Label);
        }
private void PositionsOnClosed()
       {
           _isTrigerred = false;
       }

for Example ;)