max trades open per cbot

Created at 07 May 2019, 14:58
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
NE

nelson.pmf.sc

Joined 07.02.2019

max trades open per cbot
07 May 2019, 14:58


Hello guys.... Please, I would like to request some help for the following robot indicator. (I just want to add a simple solution ---- stop oppening positions when it reaches 20 positions opened at the total count; and automatically reopen when the total opened positions count < 20.....  thanks:))

 

using System.Linq;

using cAlgo.API;

using System.IO;

using System.Collections;

using System.Collections.Generic;

using cAlgo.API.Indicators;

using cAlgo.API.Internals;

using cAlgo.Indicators;

 

namespace cAlgo

{

    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]

    public class MACD : Robot

    {

 

        [Parameter("Volume", DefaultValue = 1000)]

        public int volume { get; set; }

 

        [Parameter(DefaultValue = 52, MinValue = 1)]

        public int StopLoss { get; set; }

 

        [Parameter(DefaultValue = 58, MinValue = 1)]

        public int TakeProfit { get; set; }

 

 

        [Parameter("MACD LongCycle", DefaultValue = 43, MinValue = 1)]

        public int LongCycle { get; set; }

 

        [Parameter("MACD ShortCycle", DefaultValue = 10, MinValue = 1)]

        public int ShortCycle { get; set; }

 

        [Parameter("MACD Period", DefaultValue = 10, MinValue = 1)]

        public int MACDPeriod { get; set; }

 

        private MacdCrossOver _MACD;

        protected override void OnBar()

        {

            var dailySeries = MarketData.GetSeries(TimeFrame.Daily);

 

            _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod);

            if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)

            {

                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);

            }

 

            if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)

            {

                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);

            }

        }

        protected override double GetFitness(GetFitnessArgs args)

        {

            //maximize count of winning trades and minimize count of losing trades

            return args.WinningTrades / args.LosingTrades;


@nelson.pmf.sc
Replies

PanagiotisCharalampous
07 May 2019, 17:45

Hi nelson.pmf.sc,

Thanks for posting in our forum. See below

protected override void OnBar()
        {
            if (Positions.Count < 20)
            {
                var dailySeries = MarketData.GetSeries(TimeFrame.Daily);

                _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod);

                if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);
                }

                if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);
                }
            }
        }

Best Regards,

Panagiotis


@PanagiotisCharalampous

nelson.pmf.sc
07 May 2019, 18:50

RE:

Panagiotis Charalampous said:

Hi nelson.pmf.sc,

Thanks for posting in our forum. See below

protected override void OnBar()
        {
            if (Positions.Count < 20)
            {
                var dailySeries = MarketData.GetSeries(TimeFrame.Daily);

                _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod);

                if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);
                }

                if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);
                }
            }
        }

Best Regards,

Panagiotis

Thanks a lot Panagiotis. I already tested and worked perfectly.

You´re the best <:-)


@nelson.pmf.sc

nelson.pmf.sc
13 May 2019, 21:10

RE:

Panagiotis Charalampous said:

Hi nelson.pmf.sc,

Thanks for posting in our forum. See below

protected override void OnBar()
        {
            if (Positions.Count < 20)
            {
                var dailySeries = MarketData.GetSeries(TimeFrame.Daily);

                _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod);

                if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);
                }

                if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);
                }
            }
        }

Best Regards,

Panagiotis

Panagiotis... My Cbot is great but If possible, I'd like a little more help with it.

So, the question is....  by now It´s coded as below....

using System.Linq;

using cAlgo.API;

using System.IO;

using System.Collections;

using System.Collections.Generic;

using cAlgo.API.Indicators;

using cAlgo.API.Internals;

using cAlgo.Indicators;

 

namespace cAlgo

{

    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]

    public class MACD : Robot

    {

 

        [Parameter("Volume", DefaultValue = 1000)]

        public int volume { get; set; }

 

        [Parameter(DefaultValue = 52, MinValue = 1)]

        public int StopLoss { get; set; }

 

        [Parameter(DefaultValue = 58, MinValue = 1)]

        public int TakeProfit { get; set; }

 

 

        [Parameter("MACD LongCycle", DefaultValue = 43, MinValue = 1)]

        public int LongCycle { get; set; }

 

        [Parameter("MACD ShortCycle", DefaultValue = 10, MinValue = 1)]

        public int ShortCycle { get; set; }

 

        [Parameter("MACD Period", DefaultValue = 10, MinValue = 1)]

        public int MACDPeriod { get; set; }

 

        private MacdCrossOver _MACD;

       

protected override void OnBar()

        {

            if (Positions.Count < 20)

            {

                var dailySeries = MarketData.GetSeries(TimeFrame.Daily);

 

                _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod);

 

                if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)

                {

                    ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);

                }

 

                if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)

                {

                    ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);

                }

            }

        }

        protected override double GetFitness(GetFitnessArgs args)

        {

            //maximize count of winning trades and minimize count of losing trades

            return args.WinningTrades / args.LosingTrades;

 

So... even  using the MACD indicator in the Daily timeframe; I would like to use the stochastic oscillator indicator(9;3;9, simple) in the Hour timeframe (in order that it does not allow to open negotiations when the K line is below the level 20 of the indicator)
MIght you help me, please.


@nelson.pmf.sc

PanagiotisCharalampous
14 May 2019, 09:17

Hi nelson.pmf.sc,

See below how to declare a stochastic oscillator

var _stochastic = Indicators.StochasticOscillator(9,3,9, MovingAverageType.Simple);

The parameter you need to check is

_stochastic.PercentK

Best Regards,

Panagiotis


@PanagiotisCharalampous

nelson.pmf.sc
14 May 2019, 17:07

RE:

Panagiotis Charalampous said:

Hi nelson.pmf.sc,

See below how to declare a stochastic oscillator

var _stochastic = Indicators.StochasticOscillator(9,3,9, MovingAverageType.Simple);

The parameter you need to check is

_stochastic.PercentK

Best Regards,

Panagiotis

Ok.

Thanks a lot again.

I`ll test it.

I´m just affraid if I´d achieve to put it working in the right timeframe.

Working on this now.

BEst regards

Nelson


@nelson.pmf.sc

nelson.pmf.sc
29 May 2019, 17:52

RE:

Panagiotis Charalampous said:

Hi nelson.pmf.sc,

Thanks for posting in our forum. See below

protected override void OnBar()
        {
            if (Positions.Count < 20)
            {
                var dailySeries = MarketData.GetSeries(TimeFrame.Daily);

                _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod);

                if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);
                }

                if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);
                }
            }
        }

Best Regards,

Panagiotis

 Hello .... Could you help me once more?

The subject is .... when I added written above: 

 {
            if (Positions.Count < 20)
            {

all bots stop.

 

And I would like that only the "EUR/USD C"  stopped.

Please, How could I fix it??

thanks in advance!

Nelson


@nelson.pmf.sc

PanagiotisCharalampous
30 May 2019, 09:37

Hi nelson.pmf.sc,

Try the below

if (Positions.Count(x => x.Label == "EUR/USD C") < 20)

Best Regards,

Panagiotis


@PanagiotisCharalampous

nelson.pmf.sc
30 May 2019, 15:34

RE:

Panagiotis Charalampous said:

Hi nelson.pmf.sc,

Try the below

if (Positions.Count(x => x.Label == "EUR/USD C") < 20)

Best Regards,

Panagiotis

 

Thanks Panagiotis. It was very helpful and worked perfectly.

Best regards.

Nelson


@nelson.pmf.sc

nelson.pmf.sc
30 Jul 2019, 22:38

RE:

Panagiotis Charalampous said:

Hi nelson.pmf.sc,

See below how to declare a stochastic oscillator

var _stochastic = Indicators.StochasticOscillator(9,3,9, MovingAverageType.Simple);

The parameter you need to check is

_stochastic.PercentK

Best Regards,

Panagiotis

Hello buddy,

once again I come here to ask for your help.I would like to change the stochastic indicator for the Money Flow index.I tried to make a simple substitution in the parameter line, as for example .... if (Positions.Count(x => x.Label == "EUR/USD C") < 4)
            {
                var dailySeries = MarketData.GetSeries(TimeFrame.Daily);
                var _moneyFlow = Indicators.MoneyFlowIndex 


                _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod);
                if (_MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0  && __moneyFlow.Level.LastValue < 75)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);
                }

but it did not work.Could you help me as I could put the Money FLow Index (period = 14) indicator to open long position when it was below the 75 level??

** And i ´d like that it works in the time frame selected in the chart (not daily) as stochatic always worked.

** Only MACD must be in the timeframe daily.

Your help will be extremely important!!!

 

 

 

I`m editing the message... i`ve made another try as written below... (but didn´t work)

using System.Linq;
using cAlgo.API;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class MACD : Robot
    {

        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter(DefaultValue = 54, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 57, MinValue = 1)]
        public int TakeProfit { get; set; }


        [Parameter("MACD LongCycle", DefaultValue = 48, MinValue = 1)]
        public int LongCycle { get; set; }

        [Parameter("MACD ShortCycle", DefaultValue = 10, MinValue = 1)]
        public int ShortCycle { get; set; }

        [Parameter("MACD Period", DefaultValue = 20, MinValue = 1)]
        public int MACDPeriod { get; set; }

        private MoneyFlowIndex _moneyFlow;
        [Parameter("Period", DefaultValue = 14)]
        public int Period { get; set; }
        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }
        private MacdCrossOver _MACD;
        protected override void OnBar()
        {

            if (Positions.Count(x => x.Label == "EUR/USD C") < 4)
            {
                var dailySeries = MarketData.GetSeries(TimeFrame.Daily);


                _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod);
                if (_MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0 && _moneyFlow.Result.LastValue < 75)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "EUR/USD C", StopLoss, TakeProfit, 5);
                }

                         }
        }
        protected override double GetFitness(GetFitnessArgs args)
        {
            //maximize count of winning trades and minimize count of losing trades
            return args.WinningTrades / args.LosingTrades;
        }

    }

}


@nelson.pmf.sc