Category Trend  Published on 21/03/2022

OrglobalFx BreakEven cBOT v2.0

Description

Updated


//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.EasternStandardTime, AccessRights = AccessRights.None)]
    public class _OFX_CBOT_032421032022_BREAKEVEN_v2 : 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 = 100, MinValue = 1, Group = "Protection")]
        public int BreakEvenPips { get; set; }
        [Parameter("Break-Even Extra (pips)", DefaultValue = 100, MinValue = 1, Group = "Protection")]
        public int BreakEvenExtraPips { get; set; }
        [Parameter("Trail after Break-Even", DefaultValue = true, Group = "Protection")]
        public bool Includetrail { get; set; }
        [Parameter("Trailing Stop (pips)", DefaultValue = 100, MinValue = 1, Group = "Protection")]
        public int trailingstoppips { 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)
                        {
                            // && position.Pips >= trailingstoppips)
                            if (Includetrail)
                            {
                                //ModifyPosition(position, position.EntryPrice);
                                position.ModifyStopLossPrice(position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips));
                                Print("Stop Loss to Break Even set for BUY position {0}", position.Id);

                                if (position.Pips >= trailingstoppips)
                                    position.ModifyTrailingStop(true);

                            }
                            else if (!Includetrail)
                            {
                                //ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
                                position.ModifyStopLossPrice(position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips));
                                Print("Stop Loss to Break Even set for BUY position {0}", position.Id);
                            }
                        }
                    }
                    else
                    {
                        if (position.StopLoss >= position.EntryPrice - (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
                        {
                            // && position.Pips >= trailingstoppips)
                            if (Includetrail)
                            {
                                ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
                                Print("Stop Loss to Break Even set for SELL position {0}", position.Id);

                                if (position.Pips >= trailingstoppips)
                                    position.ModifyTrailingStop(true);
                            }
                            else if (!Includetrail)
                            {
                                ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
                                Print("Stop Loss to Break Even set for SELL position {0}", position.Id);

                            }
                        }
                    }

                }
            }
        }

        #endregion

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }


    }
}


Orglobalfx01's avatar
Orglobalfx01

Joined on 03.03.2021

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: _OFX_CBOT_032421032022_BREAKEVEN_v2.algo
  • Rating: 0
  • Installs: 1349
  • Modified: 21/03/2022 14:28
Comments
Log in to add a comment.
No comments found.