Category Trend  Published on 29/03/2022

OrglobalFx PureRenko cBOT

Description

OrglobalFX Pure Renko cBOT

Trades based on bricks formed.

For example,
If two green bricks appear, take a long position. And otherwise.

Automatic breakeven and trailing stop added.

 

Contact:
Telegram- @orglobalng


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 _OFX_CBOT_123121032022_PURERENKO : Robot
    {
        [Parameter("Volume", DefaultValue = 2000)]
        public double _volume { get; set; }
        [Parameter("Instance Name", DefaultValue = "OrglobalFxPureRenko")]
        public string InstanceName { get; set; }


// Breakeven parameters
        [Parameter("Include Break-Even", DefaultValue = true, Group = "Protection")]
        public bool IncludeBreakEven { get; set; }
        [Parameter("Break-Even Extra (pips)", DefaultValue = 1, MinValue = 0, Group = "Protection")]
        public double BreakEvenExtraPips { get; set; }
        [Parameter("Trail after Break-Even", DefaultValue = true, Group = "Protection")]
        public bool Includetrail { get; set; }

        public AverageTrueRange _atr;
        public double StopLossInPips, BreakEvenPips, trailingstoppips, TakeProfitInPips;

        protected override void OnStart()
        {
            _atr = Indicators.AverageTrueRange(26, MovingAverageType.Simple);
//Label            
            InstanceName = InstanceName + "_" + SymbolName;


        }

        protected override void OnBar()
        {



            if (Bars.ClosePrices.Last(1) > Bars.ClosePrices.Last(2))
            {
                Open(TradeType.Buy, InstanceName);
            }

            if (Bars.ClosePrices.Last(1) < Bars.ClosePrices.Last(2))
            {

                Open(TradeType.Sell, InstanceName);
            }

        }
        protected override void OnTick()
        {

            StopLossInPips = Math.Abs(Bars.OpenPrices.Last(1) - Symbol.Bid);
            StopLossInPips = Math.Round(((StopLossInPips) / Symbol.PipSize), 0);
            TakeProfitInPips = Math.Round((_atr.Result.LastValue / Symbol.PipSize), 0);
            if (IncludeBreakEven)
            {
                BreakEvenAdjustment();
            }
        }

        private void Open(TradeType tradeType, string InstanceName)
        {
//Check there's no existing position before entering a trade
            var position = Positions.Find(InstanceName, SymbolName);
            if (position == null)
            {
                var _volumedv2 = Math.Round(_volume / 2, 0);
                ExecuteMarketOrder(tradeType, SymbolName, _volumedv2, InstanceName, StopLossInPips, TakeProfitInPips);
                ExecuteMarketOrder(tradeType, SymbolName, _volumedv2, InstanceName, StopLossInPips, null);
            }
        }

        #region close trades
//Function for closing trades 
        private void Close(TradeType tradeType, string InstanceName)
        {
            foreach (var position in Positions.FindAll(InstanceName, SymbolName, tradeType))
                ClosePosition(position);
        }
        #endregion

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

//Breakeven @ takeprofit                
                BreakEvenPips = TakeProfitInPips / 2;

//Trailing stop is triggered when winning pips equals the 3 times the takeprofit pips                
                trailingstoppips = TakeProfitInPips * 2;
                Print("trailingstoppips: ", trailingstoppips);
                Print("BreakEvenPips: ", BreakEvenPips);
                Print("TakeProfitInPips: ", TakeProfitInPips);
                Print("StopLossInPips: ", StopLossInPips);



// 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

    }
}


Orglobalfx01's avatar
Orglobalfx01

Joined on 03.03.2021

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: _OFX_CBOT_123121032022_PURERENKO.algo
  • Rating: 0
  • Installs: 1729
  • Modified: 29/03/2022 12:59
Comments
Log in to add a comment.
AN
angel773n · 2 years ago

Hi, would it be possible to modify it to open a trade only after 3 bricks appear after an opposite color?

HA
haint1098 · 2 years ago

Thanks so much for sharing these. I have read a lot of articles with similar topics but none of them are as detailed. I will learn more and practice. 5 letter words