Sample Trailing Stop

Created at 26 Apr 2013, 15:44
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!
cAlgo_Fanatic's avatar

cAlgo_Fanatic

Joined 10.01.2013

Sample Trailing Stop
26 Apr 2013, 15:44


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

    }
}

 


@cAlgo_Fanatic
Replies

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

    }
}

 

 


@aimerdoux

Spotware
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?


@Spotware

aimerdoux
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;


@aimerdoux

Spotware
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.


@Spotware

aimerdoux
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?


@aimerdoux

Spotware
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.

 


@Spotware

aimerdoux
14 Jul 2015, 17:36

RE:

This post was removed by moderator.


@aimerdoux

aimerdoux
14 Jul 2015, 17:43

RE:

This post was removed by moderator.


@aimerdoux

spike.bhv
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 ;)


@spike.bhv

spike.bhv
17 Jul 2015, 10:25

private void PositionsOnClosed(PositionClosedEventArgs args)
       {
           _isTrigerred = false;
       }

 


@spike.bhv

aimerdoux
17 Jul 2015, 19:11

RE:

spike.bhv said:

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

thanks mate


@aimerdoux

freshforex05
14 Apr 2016, 15:19

This post was removed by moderator.


freshforex05
27 Apr 2016, 16:16

This post was removed by moderator.


... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...