Category Other  Published on 12/03/2021

OrglobalFx_BreakEven cBOT

Description

//OrglobalFx_BreakEven cBOT
// orglobalng@gmail.com
// Will move stoploss to breakeven and trail the price.


//OrglobalFx_BreakEven cBOT
// orglobalng@gmail.com
// Will move stoploss to breakeven and trail the price.


using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class OrglobalFx_BreakEven_cBOT : Robot
    {
        [Parameter("Instance Name", DefaultValue = "")]
        public string InstanceName { get; set; }
        [Parameter("Include Break-Even", DefaultValue = true, Group = "Protection")]
        public bool IncludeBreakEven { get; set; }
        [Parameter("Break-Even Trigger (pips)", DefaultValue = 10, MinValue = 1, Group = "Protection")]
        public int BreakEvenPips { get; set; }
        [Parameter("Break-Even Extra (pips)", DefaultValue = 10, MinValue = 1, Group = "Protection")]
        public int BreakEvenExtraPips { get; set; }
        [Parameter("Trail after Break-Even", DefaultValue = true, Group = "Protection")]
        public bool Includetrail { get; set; }
        [Parameter("Partial Position Close", DefaultValue = true, Group = "Protection")]
        public bool partialclose { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here

        }

        protected override void OnTick()
        {
            if (IncludeBreakEven)
            {
                BreakEvenAdjustment();
            }
        }

        #region Break Even
        // code from clickalgo.com
        private void BreakEvenAdjustment()
        {
            var positn = Positions.Find(InstanceName, SymbolName);
            var allPositions = Positions.FindAll(InstanceName, SymbolName);
            foreach (Position position in allPositions)
            {
                var entryPrice = position.EntryPrice;
                var distance = position.TradeType == TradeType.Buy ? Symbol.Bid - entryPrice : entryPrice - Symbol.Ask;

                // move stop loss to break even plus and additional (x) pips
                if (distance >= BreakEvenPips * Symbol.PipSize)
                {
                    if (position.TradeType == TradeType.Buy)
                    {
                        if (position.StopLoss <= position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
                        {
                            if (Includetrail)
                            {
                                ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit, true);
                                Print("Stop Loss to Break Even set for BUY position {0}", position.Id);
                                if (partialclose)
                                    ClosePosition(positn, position.Quantity / 2);
                            }
                            else if (!Includetrail)
                            {
                                ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
                                Print("Stop Loss to Break Even set for BUY position {0}", position.Id);
                                if (partialclose)
                                    ClosePosition(positn, position.Quantity / 2);
                            }
                        }
                    }
                    else
                    {
                        if (position.StopLoss >= position.EntryPrice - (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
                        {
                            if (Includetrail)
                            {
                                ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit, true);
                                Print("Stop Loss to Break Even set for SELL position {0}", position.Id);
                                if (partialclose)
                                    ClosePosition(positn, position.Quantity / 2);
                            }
                            else if (!Includetrail)
                            {
                                ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
                                Print("Stop Loss to Break Even set for SELL position {0}", position.Id);
                                if (partialclose)
                                    ClosePosition(positn, position.Quantity / 2);
                            }
                        }
                    }
                }
            }
        }

        #endregion


    }
}


Orglobalfx01's avatar
Orglobalfx01

Joined on 03.03.2021

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: OrglobalFx_BreakEven_cBOT.algo
  • Rating: 5
  • Installs: 1679
Comments
Log in to add a comment.
AN
Andrew_Parish · 1 year ago

Hey orglobalng,

Would it be possible for you to update this cBot to allow the 'Break-Even Trigger' and the 'Break-Even Extra' settings to allow for fractional pips?

For example, if I attached this cBot to my EURUSD 5min chart, I would like to use a 5.5 pip trigger with a 1.5 pip extra.

Thanks.

NI
ninhdzu2 · 2 years ago

Hello OrglobalFx,
Could you please help me with a reverse code, and guide me where to put it in a Cbot.
Thanks

EX
ex-al · 3 years ago

thank you

Orglobalfx01's avatar
Orglobalfx01 · 3 years ago

Okay...
I'll check that up... Though it might not come quick.
Thanks for you feed back

EX
ex-al · 3 years ago

Hi orglobalng,

yes it works with multiple instances. 

I have an other question. Maybe you can help.  The cbot does trail the price by adding pips to the break even. 
Is it maybe possible to change the code in a way, so that the robot waits for break even to occur and puts the sl x-pips right behind the current price and trails it tick by tick?!?  That would be really trailing the price and in addition, the trailing stop would immediately jump right behind the current price even if the cbot starts with a delay after a trade already is running

Thank you.

Orglobalfx01's avatar
Orglobalfx01 · 3 years ago

Hello ex-al,

For now, I'm not sure if ctrader supports that. 
However, you can add the  multiple instances of pairs to the cbot under the "Automate" tab.

Let me know if this helps.

Thanks.

EX
ex-al · 3 years ago

is there maybe possible to write the code in a way to set the cbot work for all currencies at once/same time or do i have to add the cbot to every chart separately for each currency ?

thank you