Replies

steel.export
24 May 2022, 16:17

RE:

amusleh said:

Hi,

Try this:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SuperTrendReference_cBot : Robot
    {
        [Parameter(DefaultValue = "SuperTrendReference_cBot")]
        public string cBotLabel { get; set; }

        [Parameter("Serial Key", DefaultValue = "12")]
        public string SerialKey { get; set; }

        [Parameter("Trade Volume,in lots:", DefaultValue = 0.5, MinValue = 0.001)]
        public double Lots { get; set; }

        [Parameter("Martingale - Yes/No:", DefaultValue = true)]
        public bool UseMartingale { get; set; }

        [Parameter("Multiplication Factor:", DefaultValue = 1.5)]
        public double MultiplicationFactor { get; set; }

        [Parameter("Tolerance - Yes/No:", DefaultValue = true)]
        public bool UseTolerance { get; set; }

        [Parameter("Tolerance in pips:", DefaultValue = 10.0)]
        public double TolerancePips { get; set; }

        [Parameter("ATR Period", Group = ("Super Trend Indicator Inputs"), DefaultValue = 14)]
        public int ST_Period { get; set; }

        [Parameter("ATR Multiplier", Group = ("Super Trend Indicator Inputs"), DefaultValue = 2)]
        public double ST_atrMulti { get; set; }

        [Parameter("ATR Source", Group = ("Super Trend Indicator Inputs"), DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType ST_AtrSource { get; set; }

        [Parameter("Trading Start Hour:0-23", Group = ("Trading Hours"), DefaultValue = 11, MinValue = 0, MaxValue = 23, Step = 1)]
        public int StartTimeHour { get; set; }

        [Parameter("Trading Start Minute:0-59", Group = ("Trading Hours"), DefaultValue = 0, MinValue = 0, MaxValue = 59, Step = 1)]
        public int StartTimeMinute { get; set; }

        [Parameter("Trading End Hour:0-23", Group = ("Trading Hours"), DefaultValue = 17, MinValue = 0, MaxValue = 23, Step = 1)]
        public int EndTimeHour { get; set; }

        [Parameter("Trading End Minute:0-59", Group = ("Trading Hours"), DefaultValue = 0, MinValue = 0, MaxValue = 59, Step = 1)]
        public int EndTimeMinute { get; set; }

        [Parameter("Max Drawdown,in %:", DefaultValue = 10.0)]
        public double MaxDDPrct { get; set; }

        [Parameter("Coffee Break - Yes/No:", DefaultValue = true)]
        public bool UseCoffeeBreak { get; set; }

        [Parameter("Profit:", DefaultValue = 500.0)]
        public double CBProfit { get; set; }

        [Parameter("RSI Signal bar", DefaultValue = 1, MinValue = 0)]
        public int SignalBar { get; set; }

        private SuperTrendExperto SuperTrend_;
        private double currentTradeLots;

        private DateTime StartTimeHour_;
        private DateTime EndTimeHour_;
        private DateTime StartTimeMinute_;
        private DateTime EndTimeMinute_;

        private double InitialDDAccountBalance;
        private double CurrentClosedPosBal;

        protected override void OnStart()
        {
            if (SerialKey != "0")
            {
                Print("Serial Key is NOT valid!");
                Stop();
            }

            SuperTrend_ = Indicators.GetIndicator<SuperTrendExperto>(ST_Period, ST_atrMulti, ST_atrMulti);

            if (UseMartingale == true)
            {
                currentTradeLots = Lots;
            }

            StartTimeHour_ = Server.Time.Date.AddHours(StartTimeHour);
            EndTimeHour_ = Server.Time.Date.AddHours(EndTimeHour);
            StartTimeMinute_ = Server.Time.Date.AddMinutes(StartTimeMinute);
            EndTimeMinute_ = Server.Time.Date.AddMinutes(EndTimeMinute);

            InitialDDAccountBalance = Account.Balance;
            CurrentClosedPosBal = 0.0;
        }

        protected override void OnTick()
        {
            if (IsReach_DDPrct(MaxDDPrct) == true)
            {
                CloseAllPositions(TradeType.Buy);
                CloseAllPositions(TradeType.Sell);

                Print("DD is Hit!");
                InitialDDAccountBalance = Account.Balance;
                CurrentClosedPosBal = 0.0;
            }
        }

        protected override void OnBar()
        {
            if (IsTradingHours() == true)
            {
                if (SuperTrend_.LowLine.Last(SignalBar) > 0.0 && CalculatePositionsQnty(TradeType.Buy) == 0)
                {
                    CloseAllPositions(TradeType.Sell);
                    if (UseMartingale == true)
                        UpdateTradeVolume(TradeType.Sell);
                    if (UseMartingale == true)
                        OpenMarketOrder(TradeType.Buy, currentTradeLots);
                    else
                        OpenMarketOrder(TradeType.Buy, Lots);
                }
                else if (SuperTrend_.HighLine.Last(SignalBar) > 0.0 && CalculatePositionsQnty(TradeType.Sell) == 0)
                {
                    CloseAllPositions(TradeType.Buy);
                    if (UseMartingale == true)
                        UpdateTradeVolume(TradeType.Buy);
                    if (UseMartingale == true)
                        OpenMarketOrder(TradeType.Sell, currentTradeLots);
                    else
                        OpenMarketOrder(TradeType.Sell, Lots);
                }
            }
        }

        private void OpenMarketOrder(TradeType tradeType, double dLots)
        {
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(dLots);
            volumeInUnits = Symbol.NormalizeVolumeInUnits(volumeInUnits, RoundingMode.Down);

            //in final version need add attempts counter
            var result = ExecuteMarketOrder(tradeType, Symbol.Name, volumeInUnits, cBotLabel, null, null);

            if (!result.IsSuccessful)
            {
                Print("Execute Market Order Error: {0}", result.Error.Value);

                OnStop();
            }
        }

        private int CalculatePositionsQnty(TradeType tradeType)
        {
            return Positions.FindAll(cBotLabel, Symbol.Name, tradeType).Length;
        }

        private void CloseAllPositions(TradeType tradeType)
        {
            bool isClosed = false;
            foreach (var position in Positions.FindAll(cBotLabel, Symbol.Name, tradeType))
            {
                var result = ClosePosition(position);
                if (!result.IsSuccessful)
                {
                    Print("Closing market order error: {0}", result.Error);
                }
                else
                {
                    isClosed = true;
                }
            }
            
            if (isClosed)
            {
                Stop();
            }
        }

        private bool IsReach_DDPrct(double DDPrctValue)
        {
            return ((CalculatePL() + CurrentClosedPosBal) <= (-1.0 * InitialDDAccountBalance * DDPrctValue / 100.0));
        }

        private double CalculatePL()
        {
            double CurrentPL = 0.0;
            foreach (var position in Positions.FindAll(cBotLabel, Symbol.Name))
            {
                CurrentPL += position.NetProfit;
            }
            return CurrentPL;
        }

        private bool IsTradingHours()
        {
            var currentTimeHour = Server.Time.TimeOfDay.Hours;
            var currentTimeMinute = Server.Time.TimeOfDay.Minutes;
            if ((StartTimeHour_.Hour < currentTimeHour && EndTimeHour_.Hour > currentTimeHour) || (StartTimeHour_.Hour == currentTimeHour && StartTimeMinute_.Minute <= currentTimeMinute) || (EndTimeHour_.Hour == currentTimeHour && EndTimeMinute_.Minute >= currentTimeMinute))
                return true;

            return false;
        }

        private void UpdateTradeVolume(TradeType tradeType)
        {
            RefreshData();
            HistoricalTrade lastClosedPosition = History.FindLast(cBotLabel, Symbol.Name, tradeType);
            if (lastClosedPosition == null)
                return;
            CurrentClosedPosBal += lastClosedPosition.NetProfit;

            if ((UseTolerance == false && lastClosedPosition.NetProfit > 0.0) || (UseTolerance == true && lastClosedPosition.Pips > TolerancePips))
            {
                currentTradeLots = Lots;
            }
            else
            {
                if (UseTolerance == false || (UseTolerance == true && lastClosedPosition.Pips < (-1.0 * TolerancePips)))
                {
                    currentTradeLots *= MultiplicationFactor;
                }
                else
                {
                    //same lot size
                }
            }

            if (UseCoffeeBreak == true && lastClosedPosition.NetProfit > CBProfit)
            {
                Print("Coffee Break is Hit!");
                Stop();
            }
        }

        protected override void OnStop()
        {
            Stop();
        }
    }
}

I don't have the referenced indicator inside your cBot so I can't test it.

Dear Mr. Ahmad,

It is working correctly. Thanks a lot for your help.

Thanks & Regards,

Altaf


@steel.export

steel.export
24 May 2022, 14:28

RE: How to Stop the cBot after each Trade

amusleh said:

Hi,

Try this:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SuperTrendReference_cBot : Robot
    {
        [Parameter(DefaultValue = "SuperTrendReference_cBot")]
        public string cBotLabel { get; set; }

        [Parameter("Serial Key", DefaultValue = "12")]
        public string SerialKey { get; set; }

        [Parameter("Trade Volume,in lots:", DefaultValue = 0.5, MinValue = 0.001)]
        public double Lots { get; set; }

        [Parameter("Martingale - Yes/No:", DefaultValue = true)]
        public bool UseMartingale { get; set; }

        [Parameter("Multiplication Factor:", DefaultValue = 1.5)]
        public double MultiplicationFactor { get; set; }

        [Parameter("Tolerance - Yes/No:", DefaultValue = true)]
        public bool UseTolerance { get; set; }

        [Parameter("Tolerance in pips:", DefaultValue = 10.0)]
        public double TolerancePips { get; set; }

        [Parameter("ATR Period", Group = ("Super Trend Indicator Inputs"), DefaultValue = 14)]
        public int ST_Period { get; set; }

        [Parameter("ATR Multiplier", Group = ("Super Trend Indicator Inputs"), DefaultValue = 2)]
        public double ST_atrMulti { get; set; }

        [Parameter("ATR Source", Group = ("Super Trend Indicator Inputs"), DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType ST_AtrSource { get; set; }

        [Parameter("Trading Start Hour:0-23", Group = ("Trading Hours"), DefaultValue = 11, MinValue = 0, MaxValue = 23, Step = 1)]
        public int StartTimeHour { get; set; }

        [Parameter("Trading Start Minute:0-59", Group = ("Trading Hours"), DefaultValue = 0, MinValue = 0, MaxValue = 59, Step = 1)]
        public int StartTimeMinute { get; set; }

        [Parameter("Trading End Hour:0-23", Group = ("Trading Hours"), DefaultValue = 17, MinValue = 0, MaxValue = 23, Step = 1)]
        public int EndTimeHour { get; set; }

        [Parameter("Trading End Minute:0-59", Group = ("Trading Hours"), DefaultValue = 0, MinValue = 0, MaxValue = 59, Step = 1)]
        public int EndTimeMinute { get; set; }

        [Parameter("Max Drawdown,in %:", DefaultValue = 10.0)]
        public double MaxDDPrct { get; set; }

        [Parameter("Coffee Break - Yes/No:", DefaultValue = true)]
        public bool UseCoffeeBreak { get; set; }

        [Parameter("Profit:", DefaultValue = 500.0)]
        public double CBProfit { get; set; }

        [Parameter("RSI Signal bar", DefaultValue = 1, MinValue = 0)]
        public int SignalBar { get; set; }

        private SuperTrendExperto SuperTrend_;
        private double currentTradeLots;

        private DateTime StartTimeHour_;
        private DateTime EndTimeHour_;
        private DateTime StartTimeMinute_;
        private DateTime EndTimeMinute_;

        private double InitialDDAccountBalance;
        private double CurrentClosedPosBal;

        protected override void OnStart()
        {
            if (SerialKey != "0")
            {
                Print("Serial Key is NOT valid!");
                Stop();
            }

            SuperTrend_ = Indicators.GetIndicator<SuperTrendExperto>(ST_Period, ST_atrMulti, ST_atrMulti);

            if (UseMartingale == true)
            {
                currentTradeLots = Lots;
            }

            StartTimeHour_ = Server.Time.Date.AddHours(StartTimeHour);
            EndTimeHour_ = Server.Time.Date.AddHours(EndTimeHour);
            StartTimeMinute_ = Server.Time.Date.AddMinutes(StartTimeMinute);
            EndTimeMinute_ = Server.Time.Date.AddMinutes(EndTimeMinute);

            InitialDDAccountBalance = Account.Balance;
            CurrentClosedPosBal = 0.0;
        }

        protected override void OnTick()
        {
            if (IsReach_DDPrct(MaxDDPrct) == true)
            {
                CloseAllPositions(TradeType.Buy);
                CloseAllPositions(TradeType.Sell);

                Print("DD is Hit!");
                InitialDDAccountBalance = Account.Balance;
                CurrentClosedPosBal = 0.0;
            }
        }

        protected override void OnBar()
        {
            if (IsTradingHours() == true)
            {
                if (SuperTrend_.LowLine.Last(SignalBar) > 0.0 && CalculatePositionsQnty(TradeType.Buy) == 0)
                {
                    CloseAllPositions(TradeType.Sell);
                    if (UseMartingale == true)
                        UpdateTradeVolume(TradeType.Sell);
                    if (UseMartingale == true)
                        OpenMarketOrder(TradeType.Buy, currentTradeLots);
                    else
                        OpenMarketOrder(TradeType.Buy, Lots);
                }
                else if (SuperTrend_.HighLine.Last(SignalBar) > 0.0 && CalculatePositionsQnty(TradeType.Sell) == 0)
                {
                    CloseAllPositions(TradeType.Buy);
                    if (UseMartingale == true)
                        UpdateTradeVolume(TradeType.Buy);
                    if (UseMartingale == true)
                        OpenMarketOrder(TradeType.Sell, currentTradeLots);
                    else
                        OpenMarketOrder(TradeType.Sell, Lots);
                }
            }
        }

        private void OpenMarketOrder(TradeType tradeType, double dLots)
        {
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(dLots);
            volumeInUnits = Symbol.NormalizeVolumeInUnits(volumeInUnits, RoundingMode.Down);

            //in final version need add attempts counter
            var result = ExecuteMarketOrder(tradeType, Symbol.Name, volumeInUnits, cBotLabel, null, null);

            if (!result.IsSuccessful)
            {
                Print("Execute Market Order Error: {0}", result.Error.Value);

                OnStop();
            }
        }

        private int CalculatePositionsQnty(TradeType tradeType)
        {
            return Positions.FindAll(cBotLabel, Symbol.Name, tradeType).Length;
        }

        private void CloseAllPositions(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(cBotLabel, Symbol.Name, tradeType))
            {
                var result = ClosePosition(position);
                if (!result.IsSuccessful)
                {
                    Print("Closing market order error: {0}", result.Error);
                }
            }

            Stop();
        }

        private bool IsReach_DDPrct(double DDPrctValue)
        {
            return ((CalculatePL() + CurrentClosedPosBal) <= (-1.0 * InitialDDAccountBalance * DDPrctValue / 100.0));
        }

        private double CalculatePL()
        {
            double CurrentPL = 0.0;
            foreach (var position in Positions.FindAll(cBotLabel, Symbol.Name))
            {
                CurrentPL += position.NetProfit;
            }
            return CurrentPL;
        }

        private bool IsTradingHours()
        {
            var currentTimeHour = Server.Time.TimeOfDay.Hours;
            var currentTimeMinute = Server.Time.TimeOfDay.Minutes;
            if ((StartTimeHour_.Hour < currentTimeHour && EndTimeHour_.Hour > currentTimeHour) || (StartTimeHour_.Hour == currentTimeHour && StartTimeMinute_.Minute <= currentTimeMinute) || (EndTimeHour_.Hour == currentTimeHour && EndTimeMinute_.Minute >= currentTimeMinute))
                return true;

            return false;
        }

        private void UpdateTradeVolume(TradeType tradeType)
        {
            RefreshData();
            HistoricalTrade lastClosedPosition = History.FindLast(cBotLabel, Symbol.Name, tradeType);
            if (lastClosedPosition == null)
                return;
            CurrentClosedPosBal += lastClosedPosition.NetProfit;

            if ((UseTolerance == false && lastClosedPosition.NetProfit > 0.0) || (UseTolerance == true && lastClosedPosition.Pips > TolerancePips))
            {
                currentTradeLots = Lots;
            }
            else
            {
                if (UseTolerance == false || (UseTolerance == true && lastClosedPosition.Pips < (-1.0 * TolerancePips)))
                {
                    currentTradeLots *= MultiplicationFactor;
                }
                else
                {
                    //same lot size
                }
            }

            if (UseCoffeeBreak == true && lastClosedPosition.NetProfit > CBProfit)
            {
                Print("Coffee Break is Hit!");
                Stop();
            }
        }

        protected override void OnStop()
        {
            Stop();
        }
    }
}

 

Dear Mr. Ahmad,

Thanks for your reply.

In the code you have sent the cBot is not executing any trade and is Stopping without executing any trade.

Thanks & Best Regards,

Altaf


@steel.export

steel.export
24 May 2022, 11:59

RE: How to Stop the cBot after each Trade

amusleh said:

Hi,

Try this:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SuperTrendReference_cBot : Robot
    {
        [Parameter(DefaultValue = "SuperTrendReference_cBot")]
        public string cBotLabel { get; set; }

        [Parameter("Serial Key", DefaultValue = "12")]
        public string SerialKey { get; set; }

        [Parameter("Trade Volume,in lots:", DefaultValue = 0.5, MinValue = 0.001)]
        public double Lots { get; set; }

        [Parameter("Martingale - Yes/No:", DefaultValue = true)]
        public bool UseMartingale { get; set; }

        [Parameter("Multiplication Factor:", DefaultValue = 1.5)]
        public double MultiplicationFactor { get; set; }

        [Parameter("Tolerance - Yes/No:", DefaultValue = true)]
        public bool UseTolerance { get; set; }

        [Parameter("Tolerance in pips:", DefaultValue = 10.0)]
        public double TolerancePips { get; set; }

        [Parameter("ATR Period", Group = ("Super Trend Indicator Inputs"), DefaultValue = 14)]
        public int ST_Period { get; set; }

        [Parameter("ATR Multiplier", Group = ("Super Trend Indicator Inputs"), DefaultValue = 2)]
        public double ST_atrMulti { get; set; }

        [Parameter("ATR Source", Group = ("Super Trend Indicator Inputs"), DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType ST_AtrSource { get; set; }

        [Parameter("Trading Start Hour:0-23", Group = ("Trading Hours"), DefaultValue = 11, MinValue = 0, MaxValue = 23, Step = 1)]
        public int StartTimeHour { get; set; }

        [Parameter("Trading Start Minute:0-59", Group = ("Trading Hours"), DefaultValue = 0, MinValue = 0, MaxValue = 59, Step = 1)]
        public int StartTimeMinute { get; set; }

        [Parameter("Trading End Hour:0-23", Group = ("Trading Hours"), DefaultValue = 17, MinValue = 0, MaxValue = 23, Step = 1)]
        public int EndTimeHour { get; set; }

        [Parameter("Trading End Minute:0-59", Group = ("Trading Hours"), DefaultValue = 0, MinValue = 0, MaxValue = 59, Step = 1)]
        public int EndTimeMinute { get; set; }

        [Parameter("Max Drawdown,in %:", DefaultValue = 10.0)]
        public double MaxDDPrct { get; set; }

        [Parameter("Coffee Break - Yes/No:", DefaultValue = true)]
        public bool UseCoffeeBreak { get; set; }

        [Parameter("Profit:", DefaultValue = 500.0)]
        public double CBProfit { get; set; }

        [Parameter("RSI Signal bar", DefaultValue = 1, MinValue = 0)]
        public int SignalBar { get; set; }

        private SuperTrendExperto SuperTrend_;
        private double currentTradeLots;

        private DateTime StartTimeHour_;
        private DateTime EndTimeHour_;
        private DateTime StartTimeMinute_;
        private DateTime EndTimeMinute_;

        private double InitialDDAccountBalance;
        private double CurrentClosedPosBal;

        protected override void OnStart()
        {
            if (SerialKey != "0")
            {
                Print("Serial Key is NOT valid!");
                Stop();
            }

            SuperTrend_ = Indicators.GetIndicator<SuperTrendExperto>(ST_Period, ST_atrMulti, ST_atrMulti);

            if (UseMartingale == true)
            {
                currentTradeLots = Lots;
            }

            StartTimeHour_ = Server.Time.Date.AddHours(StartTimeHour);
            EndTimeHour_ = Server.Time.Date.AddHours(EndTimeHour);
            StartTimeMinute_ = Server.Time.Date.AddMinutes(StartTimeMinute);
            EndTimeMinute_ = Server.Time.Date.AddMinutes(EndTimeMinute);

            InitialDDAccountBalance = Account.Balance;
            CurrentClosedPosBal = 0.0;
        }

        protected override void OnTick()
        {
            if (IsReach_DDPrct(MaxDDPrct) == true)
            {
                CloseAllPositions(TradeType.Buy);
                CloseAllPositions(TradeType.Sell);

                Print("DD is Hit!");
                InitialDDAccountBalance = Account.Balance;
                CurrentClosedPosBal = 0.0;
            }
        }

        protected override void OnBar()
        {
            if (IsTradingHours() == true)
            {
                if (SuperTrend_.LowLine.Last(SignalBar) > 0.0 && CalculatePositionsQnty(TradeType.Buy) == 0)
                {
                    CloseAllPositions(TradeType.Sell);
                    if (UseMartingale == true)
                        UpdateTradeVolume(TradeType.Sell);
                    if (UseMartingale == true)
                        OpenMarketOrder(TradeType.Buy, currentTradeLots);
                    else
                        OpenMarketOrder(TradeType.Buy, Lots);
                }
                else if (SuperTrend_.HighLine.Last(SignalBar) > 0.0 && CalculatePositionsQnty(TradeType.Sell) == 0)
                {
                    CloseAllPositions(TradeType.Buy);
                    if (UseMartingale == true)
                        UpdateTradeVolume(TradeType.Buy);
                    if (UseMartingale == true)
                        OpenMarketOrder(TradeType.Sell, currentTradeLots);
                    else
                        OpenMarketOrder(TradeType.Sell, Lots);
                }
            }
        }

        private void OpenMarketOrder(TradeType tradeType, double dLots)
        {
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(dLots);
            volumeInUnits = Symbol.NormalizeVolumeInUnits(volumeInUnits, RoundingMode.Down);

            //in final version need add attempts counter
            var result = ExecuteMarketOrder(tradeType, Symbol.Name, volumeInUnits, cBotLabel, null, null);

            if (!result.IsSuccessful)
            {
                Print("Execute Market Order Error: {0}", result.Error.Value);
            }

            Stop();
        }

        private int CalculatePositionsQnty(TradeType tradeType)
        {
            return Positions.FindAll(cBotLabel, Symbol.Name, tradeType).Length;
        }

        private void CloseAllPositions(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(cBotLabel, Symbol.Name, tradeType))
            {
                var result = ClosePosition(position);
                if (!result.IsSuccessful)
                {
                    Print("Closing market order error: {0}", result.Error);
                }
            }
        }

        private bool IsReach_DDPrct(double DDPrctValue)
        {
            return ((CalculatePL() + CurrentClosedPosBal) <= (-1.0 * InitialDDAccountBalance * DDPrctValue / 100.0));
        }

        private double CalculatePL()
        {
            double CurrentPL = 0.0;
            foreach (var position in Positions.FindAll(cBotLabel, Symbol.Name))
            {
                CurrentPL += position.NetProfit;
            }
            return CurrentPL;
        }

        private bool IsTradingHours()
        {
            var currentTimeHour = Server.Time.TimeOfDay.Hours;
            var currentTimeMinute = Server.Time.TimeOfDay.Minutes;
            if ((StartTimeHour_.Hour < currentTimeHour && EndTimeHour_.Hour > currentTimeHour) || (StartTimeHour_.Hour == currentTimeHour && StartTimeMinute_.Minute <= currentTimeMinute) || (EndTimeHour_.Hour == currentTimeHour && EndTimeMinute_.Minute >= currentTimeMinute))
                return true;

            return false;
        }

        private void UpdateTradeVolume(TradeType tradeType)
        {
            RefreshData();
            HistoricalTrade lastClosedPosition = History.FindLast(cBotLabel, Symbol.Name, tradeType);
            if (lastClosedPosition == null)
                return;
            CurrentClosedPosBal += lastClosedPosition.NetProfit;

            if ((UseTolerance == false && lastClosedPosition.NetProfit > 0.0) || (UseTolerance == true && lastClosedPosition.Pips > TolerancePips))
            {
                currentTradeLots = Lots;
            }
            else
            {
                if (UseTolerance == false || (UseTolerance == true && lastClosedPosition.Pips < (-1.0 * TolerancePips)))
                {
                    currentTradeLots *= MultiplicationFactor;
                }
                else
                {
                    //same lot size
                }
            }

            if (UseCoffeeBreak == true && lastClosedPosition.NetProfit > CBProfit)
            {
                Print("Coffee Break is Hit!");
                Stop();
            }
        }

        protected override void OnStop()
        {
            Stop();
        }
    }
}

 

Dear Mr. Ahmed,

Thanks for your reply.

In the code you have sent the Robot Stops as soon as it starts the Trade and we have to Stop the Trade manually.

But I want to start the Robot Manually and it will Stop Automatically after it finishes the trade as per the SuperTrent Indicator (that means It stops Automatically when the Trend is over). And it does not start a new Trade in opposite direction. So we can manually start the Robot for next Trade.

Thanks & Best Regards,

Altaf


@steel.export

steel.export
24 May 2022, 10:53

RE: How to Stop the cBot after each Trade

amusleh said:

Hi,

What do you mean by stop? do you mean stopping the cBot itself?

Dear Mr. Ahmad,

Thanks for your prompt reply.

Yes, Stopping the cBot itself after each trade.

Thanks & Regards,

Altaf


@steel.export

steel.export
15 May 2022, 07:18

RE:

amusleh said:

Hi,

You have to use outputs instead of IndicatorDataSeries fields, then you will be able to use your indicator on a cBot by referencing.

Also you can put the whole indicator code on a cBot, example:

using System;
using cAlgo.API;
using cAlgo.API.Internals;

namespace NewcBot2
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot2 : Robot
    {
        [Parameter("Show High,Low", DefaultValue = true)]
        public bool showHL { get; set; }

        private IndicatorDataSeries _haOpen;
        private IndicatorDataSeries _haClose;
        private IndicatorDataSeries _haHigh;
        private IndicatorDataSeries _haLow;

        protected override void OnStart()
        {
            _haOpen = CreateDataSeries();
            _haClose = CreateDataSeries();
            _haHigh = CreateDataSeries();
            _haLow = CreateDataSeries();
        }

        protected override void OnBar()
        {
            var index = Bars.Count - 2;

            var open = Bars.OpenPrices[index];
            var high = Bars.HighPrices[index];
            var low = Bars.LowPrices[index];
            var close = Bars.ClosePrices[index];

            var haClose = (open + high + low + close) / 4;

            if (double.IsNaN(_haOpen[index - 1]))
            {
                _haOpen[index - 1] = Bars.OpenPrices[index - 1];
            }

            var haOpen = (index > 0) ? (_haOpen[index - 1] + _haClose[index - 1]) / 2 : (open + close) / 2;
            var haHigh = Math.Max(Math.Max(high, haOpen), haClose);
            var haLow = Math.Min(Math.Min(low, haOpen), haClose);

            _haOpen[index] = haOpen;
            _haHigh[index] = haHigh;
            _haLow[index] = haLow;
            _haClose[index] = haClose;

            if (index < 4) return;

            var position = Positions.Find("MyBot");

            if (_haClose[index] > _haOpen[index] && _haClose[index - 1] < _haOpen[index - 1])
            {
                if (position != null && position.TradeType == TradeType.Sell) ClosePosition(position);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, "MyBot");
            }
            else if (_haClose[index] < _haOpen[index] && _haClose[index - 1] > _haOpen[index - 1])
            {
                if (position != null && position.TradeType == TradeType.Buy) ClosePosition(position);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, Symbol.VolumeInUnitsMin, "MyBot");
            }
        }
    }
}

The above cBot opens a buy position on start of green line and a sell position on start of red line, it closes the buy position on start of red line and the sell position on start of green line.

 

Dear Mr. Ahmed,

Thanks for posting the above Sample cBot that can be used by Referencing an Indicator, and it opens a buy position on start of green line and a sell position on start of red line, it closes the buy position on start of red line and the sell position on start of green line.

Kindly also advice how to Add Parameters such as Volume/Lot Size,  ATR Period, and  ATR Multiplier to this cBot, if we are Referencing it with SuperTrend Indicator.

Thanks & Best Regards,

Altaf


@steel.export

steel.export
06 May 2022, 11:39

RE: Custom Renko Chart (Non-Overlay)

amusleh said:

Hi,

We fixed the issue, thanks for reporting.

 

Dear Mr. Ahmad,

Thanks for Fixing the Indicator.

Please also advice if it possible to have Candlestick Chart on Top, and Non-Overlay Renko chart at the bottom on the same page ?

Thanks & Best Regards,

Altaf


@steel.export

steel.export
06 May 2022, 08:53

RE: RE: RE: RE: ADX INDICATOR ALERT ON TELEGRAM

amusleh said:

steel.export said:

Dear Mr. Ahmad,

Thanks for your reply.

This time Built is giving the below 2 Errors.

 

Error CS0029 Cannot implicitly convert type 'cAlgo.API.TimeFrame' to 'string' ADXwithAlert.

Error CS0029 Cannot implicitly convert type 'cAlgo.API.Internals.Symbol' to 'string' ADXwithAlert.

 

Please Advice.

Thanks & Best Regards,

Altaf

Hi,

You have to call the ToString method on them:

using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Alert;
using cAlgo.API.Alert.Models;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class ADXwithAlert : Indicator
    {
        private AverageDirectionalMovementIndexRating _adx;
        private int _lastAlertBarIndex;

        [Parameter(DefaultValue = 14, Group = "ADX")]
        public int Periods { get; set; }

        [Parameter("Level", DefaultValue = 25, Group = "Alert")]
        public double AlertLevel { get; set; }

        [Parameter("Setup", DefaultValue = 25, Group = "Alert")]
        public bool AlertSetup { get; set; }

        [Output("ADX", LineColor = "Blue")]
        public IndicatorDataSeries Adx { get; set; }

        [Output("ADXR", LineColor = "Yellow")]
        public IndicatorDataSeries AdxR { get; set; }

        [Output("DI+", LineColor = "Green")]
        public IndicatorDataSeries DiPlus { get; set; }

        [Output("DI-", LineColor = "Red")]
        public IndicatorDataSeries DiMinus { get; set; }

        protected override void Initialize()
        {
            _adx = Indicators.AverageDirectionalMovementIndexRating(Periods);

            if (AlertSetup)
            {
                Notifications.ShowPopup();
            }
        }

        public override void Calculate(int index)
        {
            Adx[index] = _adx.ADX[index];
            AdxR[index] = _adx.ADXR[index];
            DiPlus[index] = _adx.DIPlus[index];
            DiMinus[index] = _adx.DIMinus[index];

            if (IsLastBar && _lastAlertBarIndex != index && Adx[index] > AlertLevel)
            {
                _lastAlertBarIndex = index;

                var type = string.Format("ADX above {0}", AlertLevel);

           AlertModel alert = new AlertModel
            {
                TimeFrame = TimeFrame.ToString(),
                Symbol = SymbolName,
                Price = Adx[index],
                TriggeredBy = "ADX with Alert",
                Type = type,
                Time = Server.Time
            };

                Notifications.TriggerAlert(alert);
            }
        }
    }
}

 

Dear Mr. Ahmat,

It is now working well. Thanks a lot for your help.

Thanks & Best Regards,

Altaf


@steel.export

steel.export
04 May 2022, 14:36

RE: RE: ADX INDICATOR ALERT ON TELEGRAM

amusleh said:

steel.export said:

Dear Mr. Ahmad,

Thanks for your reply.

At the time of Built it is giving the below error

The type or namespace name 'AlertModel' could not be found (are you missing a using directive or an assembly reference?)

Hi,

Add the using cAlgo.API.Alert.Models:

using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Alert;
using cAlgo.API.Alert.Models;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class ADXwithAlert : Indicator
    {
        private AverageDirectionalMovementIndexRating _adx;
        private int _lastAlertBarIndex;

        [Parameter(DefaultValue = 14, Group = "ADX")]
        public int Periods { get; set; }

        [Parameter("Level", DefaultValue = 25, Group = "Alert")]
        public double AlertLevel { get; set; }

        [Parameter("Setup", DefaultValue = 25, Group = "Alert")]
        public bool AlertSetup { get; set; }

        [Output("ADX", LineColor = "Blue")]
        public IndicatorDataSeries Adx { get; set; }

        [Output("ADXR", LineColor = "Yellow")]
        public IndicatorDataSeries AdxR { get; set; }

        [Output("DI+", LineColor = "Green")]
        public IndicatorDataSeries DiPlus { get; set; }

        [Output("DI-", LineColor = "Red")]
        public IndicatorDataSeries DiMinus { get; set; }

        protected override void Initialize()
        {
            _adx = Indicators.AverageDirectionalMovementIndexRating(Periods);

            if (AlertSetup)
            {
                Notifications.ShowPopup();
            }
        }

        public override void Calculate(int index)
        {
            Adx[index] = _adx.ADX[index];
            AdxR[index] = _adx.ADXR[index];
            DiPlus[index] = _adx.DIPlus[index];
            DiMinus[index] = _adx.DIMinus[index];

            if (IsLastBar && _lastAlertBarIndex != index && Adx[index] > AlertLevel)
            {
                _lastAlertBarIndex = index;

                var type = string.Format("ADX above {0}", AlertLevel);

           AlertModel alert = new AlertModel
            {
                TimeFrame = TimeFrame,
                Symbol = Symbol,
                Price = Adx[index],
                TriggeredBy = "ADX with Alert",
                Type = type,
                Time = Server.Time
            };

                Notifications.TriggerAlert(alert);
            }
        }
    }
}

 

 

Dear Mr. Ahmad,

Thanks for your reply.

This time Built is giving the below 2 Errors.

 

Error CS0029 Cannot implicitly convert type 'cAlgo.API.TimeFrame' to 'string' ADXwithAlert.

Error CS0029 Cannot implicitly convert type 'cAlgo.API.Internals.Symbol' to 'string' ADXwithAlert.

 

Please Advice.

Thanks & Best Regards,

Altaf


@steel.export

steel.export
21 Apr 2022, 14:25

ADX INDICATOR ALERT ON TELEGRAM

Dear Mr. Ahmad,

Thanks for your reply.

At the time of Built it is giving the below error

The type or namespace name 'AlertModel' could not be found (are you missing a using directive or an assembly reference?)


@steel.export

steel.export
20 Apr 2022, 07:51

ADX INDICATOR ALERT ON TELEGRAM

Dear Mr. Ahmed,

Thanks for your reply.

The ADX Alert onTelegram is working fine, but along with the Telegram Alert it also shows a Pop-up Alert on screen on ctrader Platform. Is there any way to Disable the Pop-up Alerts on Screen, and only receive Alert on Telegram?

Awaiting your early reply.

Thanks and Best Regards,

Altaf


@steel.export

steel.export
08 Feb 2022, 14:47

Combination of One Execution Per Bar & Multiple Executions.

Dear Mr. Ahmad Norman Musleh,

Thanks for your Reply & Help.


@steel.export

steel.export
03 Feb 2022, 10:27

RE: ADX GRADIENT LEVEL

amusleh said:

Hi,

Do you mean something like this:

        private bool CheckADX()
        {
            if (adxType == ADXType.None)
            {
                return true;
            }

            double adxLastLevel = _adx.ADX.Last(1);
            double adxPreviousLevel = _adx.ADX.Last(2);

            if (adxType == ADXType.Gradient)
            {
                // it will return true if ADX is raising and it's above 25
                if (adxLastLevel > adxPreviousLevel && adxLastLevel > 25)
                {
                    return true;
                }
            }
            else
            {
                if (adxLastLevel > adxLevel)
                {
                    return true;
                }
            }

            return false;
        }

You can add more conditions on an if statement by using && (and) or || (or),

Dear Mr. Ahmad Musleh,

Thanks a lot for your Help and Support. 

Can you please send your email or contact me on altaf.rehmatwala@gmail.com.  I tried to reach you on your gmail, but no reply.

Thanks & Best Regards,

Altaf

 


@steel.export

steel.export
22 Jan 2021, 09:03 ( Updated at: 22 Jan 2021, 09:07 )

hi


@steel.export