QuickStart - Break Even

Created at 15 Jan 2014, 16:21
Spotware's avatar

cTrader Team

Joined 23.09.2013

QuickStart - Break Even
15 Jan 2014, 16:21


using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot]
    public class SampleBreakEven:Robot
    {
        private TradeType _tradeType;

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

        [Parameter("Buy", DefaultValue = true)]
        public bool Buy { get; set; }

        [Parameter("Volume", DefaultValue = 10000, MinValue = 10000)]
        public int Volume { get; set; }

        [Parameter(DefaultValue = 10)]
        public double TakeProfitPips { get; set; }

        [Parameter(DefaultValue = 10)]
        public double StopLossPips { get; set; }

        [Parameter(DefaultValue = "Break Even cBot")]
        public string MyLabel { get; set; }

        protected override void OnStart()
        {            
            _tradeType = Buy ? TradeType.Buy : TradeType.Sell;
            var result = ExecuteMarketOrder(_tradeType, Symbol, Volume,
                MyLabel, StopLossPips, TakeProfitPips);
            if(!result.IsSuccessful)
            {
                Print("Stopping cBot due to failure executing market order");
                Stop();
            }
        }

        protected override void OnTick()
        {
            var position = Positions.Find(MyLabel, Symbol, _tradeType);
            if(position == null)
            {
                Print("Position not found. Stopping cBot");
                Stop();
                return;
            }
            
            var entryPrice = position.EntryPrice;
            var distance = _tradeType == TradeType.Buy
                               ? Symbol.Bid - entryPrice
                               : entryPrice - Symbol.Ask;

            if (distance >= Trigger*Symbol.PipSize)
            {
                ModifyPosition(position, entryPrice, position.TakeProfit);
                Print("Stop Loss to Break Even set for position {0}", position.Id);
                Stop();
            }
        }

    }
}

 


@Spotware
Replies

hgorski
17 Jan 2014, 08:42

Thanks,

 

And if I wanted to close, let's say 50k of 100k position in the same time to moving stops to BE? How should I modify this example code?

 


@hgorski

aimerdoux
09 Jul 2015, 00:55

RE:

hgorski said:

Thanks,

 

And if I wanted to close, let's say 50k of 100k position in the same time to moving stops to BE? How should I modify this example code?

 

you have to modify the method Close to receive the amount of volume you want to close when the breakeven is hitted then use a bool to notify the method that the breakeven is activated 

 

private void Close1(TradeType tradeType, long volume)
        {
            foreach (var position in Positions.FindAll("ace1", Symbol, tradeType))
                ClosePosition(position, long Volume);
        }


@aimerdoux

kricka
22 May 2017, 21:51

Maybe the free download and use of Market Order 2.0 or 3.0 will give a hint what can be accomplished.

Totally free to download and use at RMMRobot.com


@kricka

Omega
30 Jul 2017, 11:51

Break even

Is there a way to set my ctrader so that my trades go to break even after a set amount of points?


@Omega

Shares4us
14 Oct 2022, 10:32

RE:

Spotware said:

                 ModifyPosition(position, entryPrice, position.TakeProfit);

This is not setting a position to breakeven!!!!

To set a position to Breakeven you have set the StopLoss to the Entryprice AND take into account the commission & spread  
 


@Shares4us

... Deleted by UFO ...