OrglobalFx_BreakEven cBOT free

by orglobalng in category Other at 12/03/2021
Description

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

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
//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


    }
}
Comments
0