wht i need to code to do not open one position each candle?

Created at 04 Sep 2018, 23:51
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!
CA

carlosdrcunha

Joined 23.04.2018

wht i need to code to do not open one position each candle?
04 Sep 2018, 23:51


what do i need to code to do not open one position each candle?? like my cBot is opening one position each new candle, how can i prevent it??

 

 

Thank you.


@carlosdrcunha
Replies

PanagiotisCharalampous
05 Sep 2018, 12:09

Hi carlosdrcunha,

Can you share your cBot code and explain to us what would you expect the cBot to do?

Best Regards,

Panagiotis


@PanagiotisCharalampous

carlosdrcunha
05 Sep 2018, 16:24

its here

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

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

 


        [Parameter("Moving_Average")]
        public DataSeries MA { get; set; }

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

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("MA1", DefaultValue = 14)]
        public int Period { get; set; }

 

        [Parameter("SL", DefaultValue = 14)]
        public int iSL { get; set; }

        [Parameter("TP", DefaultValue = 14)]
        public int iTP { get; set; }

 

 


  
        private MovingAverage MA1;
        private BollingerBands BB;
     

        private const string label = "EMA";

        private Position longPosition;
        private Position shortPosition;
     

 

        protected override void OnStart()
        {
            // Put your initialization logic here

            MA1 = Indicators.SimpleMovingAverage(MA, Period);

            BB = Indicators.BollingerBands(Source, 20, 2, MovingAverageType.Exponential);

 

        }

 

 

        protected override void OnTick()
        {
            // Put your core logic here}
        }

        protected override void OnBar()
        {
            var cBotPositions = Positions.FindAll(label);

            longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            if (cBotPositions.Length >= 1)
                return;

 

 

 

 

 

 

 

            if ( MA1.Result.Last(0) > BB.Main.Last(0) && longPosition == null)
            {

                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume);
            }
            if (MA1.Result.Last(0) < BB.Main.Last(0))
            {
                CP();
            }

 

 

            if (MA1.Result.Last(0) < BB.Main.Last(0) && shortPosition == null)
            {

                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume);
            }
            if (MA1.Result.Last(0) < BB.Main.Last(0))
            {
                CP();
            }

 

 


        }

 

 

 

 

        private void CP()
        {
            foreach (var Position in Positions)
            {

                if (Position.GrossProfit > 0)
                {
                    ClosePosition(Position, Volume);
                }
            }
        }


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

 

 

 

 

 

So i want is only open one position for each signal.


@carlosdrcunha

PanagiotisCharalampous
05 Sep 2018, 16:29

Hi carlosdrcunha,

It seems that you have a condition for this in the code

            if (cBotPositions.Length >= 1)
                return;

but you do not add a label when executing an order.

ExecuteMarketOrder(TradeType.Buy, Symbol, Volume);

If you add the label, you will probably solve your problem.

Best Regards,

Panagiotis


@PanagiotisCharalampous

carlosdrcunha
05 Sep 2018, 19:03

hello again

hi agan Panagiotis,

 

thank you for ur reply,

 

but if i do this code now without your the stuff i was telling u about do the same thing, buys every new candle on this code as well

 

 

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

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




        [Parameter("Moving_Average")]
        public DataSeries MA { get; set; }

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

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("MA1", DefaultValue = 14)]
        public int Period { get; set; }



        [Parameter("SL", DefaultValue = 14)]
        public int iSL { get; set; }

        [Parameter("TP", DefaultValue = 14)]
        public int iTP { get; set; }

        private MovingAverage MA1;
        private BollingerBands BB;


        private const string label = "EMA";

        protected override void OnStart()
        {
            // Put your initialization logic here

            MA1 = Indicators.SimpleMovingAverage(MA, Period);

            BB = Indicators.BollingerBands(Source, 20, 2, MovingAverageType.Exponential);


        }

        protected override void OnTick()
        {
            // Put your core logic here}
        }

        protected override void OnBar()
        {

            if (MA1.Result.Last(0) > BB.Main.Last(0))
            {

                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume);
            }
            if (MA1.Result.Last(0) < BB.Main.Last(0))
            {
                CP();
            }
            if (MA1.Result.Last(0) < BB.Main.Last(0))
            {

                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume);
            }
            if (MA1.Result.Last(0) < BB.Main.Last(0))
            {
                CP();
            }

        }
        private void CP()
        {
            foreach (var Position in Positions)
            {

                if (Position.GrossProfit > 0)
                {
                    ClosePosition(Position, Volume);
                }
            }
        }


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


@carlosdrcunha

carlosdrcunha
05 Sep 2018, 19:54

understood, but so i tried to add the label on ExecuteMarketOrder((Tradetype.Sell,Symbol,Volume"String"); and nothing happened


@carlosdrcunha

carlosdrcunha
05 Sep 2018, 23:08

hello again

Hello again Panagiotis,

 

so i did ExecuteMarketOrder(TradeType.Buy, Symbol, Volume,"string"); is this wht i should do? no other affect than the past one, can u send me the code solved? if possible?

 

 

thank you,

Carlos cunha.


@carlosdrcunha

carlosdrcunha
06 Sep 2018, 02:03

hello again Panagiotis,

 

Problem solved, Thank you a lot,

 

can i ask you another question?

 

if now i want to allow n number of trades at the same time? like 5 trades at the same time in diferent signals? how can i code that?

 

 

Thank you,

Sincerly,

Carlos Cunha.


@carlosdrcunha

PanagiotisCharalampous
06 Sep 2018, 10:43

Hi Carlos,

You can check if Positions.Count < 5 before proceeding to executing an order.

Best Regards,

Panagiotis


@PanagiotisCharalampous

carlosdrcunha
06 Sep 2018, 13:25

hi again,

 

 

it was very useful

thank you very much Panagiotis,

 

Sincerly,

Carlos Cunha.


@carlosdrcunha

carlosdrcunha
06 Sep 2018, 15:16

RE: another question

Panagiotis Charalampous said:

Hi Carlos,

You can check if Positions.Count < 5 before proceeding to executing an order.

Best Regards,

Panagiotis

 

 

 

Another question, why this code is not giving trades?

 

 

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

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

        [Parameter("K_Period", DefaultValue = 12)]
        public int K_Period { get; set; }

        [Parameter("K_Slowing", DefaultValue = 3)]
        public int Slowing { get; set; }

        [Parameter("D_Period", DefaultValue = 3)]
        public int D_Period { get; set; }

        [Parameter("Moving_Average")]
        public DataSeries MA { get; set; }

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

        [Parameter("Trailing Stop", DefaultValue = 10)]
        public double TrailStop { get; set; }

        [Parameter("Risk", DefaultValue = 35)]
        public double Risk { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("MA1", DefaultValue = 3)]
        public int Period { get; set; }

        [Parameter("PSar min", DefaultValue = 14)]
        public double MinA { get; set; }

        [Parameter("PSar max", DefaultValue = 14)]
        public double MaxA { get; set; }

        [Parameter("RSI", DefaultValue = 14)]
        public int RSIPeriod { get; set; }

        [Parameter("SL", DefaultValue = 14)]
        public int iSL { get; set; }

        [Parameter("TP", DefaultValue = 14)]
        public int iTP { get; set; }


        [Parameter("2nd MA", DefaultValue = 20)]
        public int a { get; set; }





        private ParabolicSAR PSar;
        private StochasticOscillator Stoch;
        private MovingAverage MA1;
        private RelativeStrengthIndex RSI;
        private MovingAverage MA2;


        private const string label = "EMA";

        private Position longPosition;
        private Position shortPosition;
        public double balance;


        protected override void OnStart()
        {
            // Put your initialization logic here
            Stoch = Indicators.StochasticOscillator(K_Period, Slowing, D_Period, MovingAverageType.Simple);
            MA1 = Indicators.SimpleMovingAverage(Stoch.PercentD, Period);
            PSar = Indicators.ParabolicSAR(MinA, MaxA);
            RSI = Indicators.RelativeStrengthIndex(Source, RSIPeriod);
            MA2 = Indicators.SimpleMovingAverage(MA1.Result, a);

            balance = Account.FreeMargin;
        }





        protected override void OnTick()
        {
            // Put your core logic here}
        }

        protected override void OnBar()
        {
            var cBotPositions = Positions.FindAll(label);

            longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            if (cBotPositions.Length >= 1)
                return;




            double Risk = 0.01 * balance;





            if (Stoch.PercentK.Last(0) < MA1.Result.Last(1) && Stoch.PercentK.Last(0) < 20 && longPosition == null)
            {
                CP();

                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
            }

            if (Stoch.PercentK.Last(1) > MA1.Result.Last(0) && Stoch.PercentK.Last(0) > 80 && shortPosition == null)
            {
                CP();

                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }
        }









        private void CP()
        {
            foreach (var Position in Positions)
            {



                ClosePosition(Position, Volume);

            }
        }
    }
}


@carlosdrcunha

carlosdrcunha
12 Sep 2018, 13:07

RE: RE: another question

carlosdrcunha said:

 

This is a good cBot, but why this is not giving trades?

can anyone tell me why does it not giving trades?

Thank you

 

 

 

 

 

 

 

 

Panagiotis Charalampous said:

Hi Carlos,

You can check if Positions.Count < 5 before proceeding to executing an order.

Best Regards,

Panagiotis

 

 

 

Another question, why this code is not giving trades?

 

 

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

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

        [Parameter("K_Period", DefaultValue = 12)]
        public int K_Period { get; set; }

        [Parameter("K_Slowing", DefaultValue = 3)]
        public int Slowing { get; set; }

        [Parameter("D_Period", DefaultValue = 3)]
        public int D_Period { get; set; }

        [Parameter("Moving_Average")]
        public DataSeries MA { get; set; }

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

        [Parameter("Trailing Stop", DefaultValue = 10)]
        public double TrailStop { get; set; }

        [Parameter("Risk", DefaultValue = 35)]
        public double Risk { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("MA1", DefaultValue = 3)]
        public int Period { get; set; }

        [Parameter("PSar min", DefaultValue = 14)]
        public double MinA { get; set; }

        [Parameter("PSar max", DefaultValue = 14)]
        public double MaxA { get; set; }

        [Parameter("RSI", DefaultValue = 14)]
        public int RSIPeriod { get; set; }

        [Parameter("SL", DefaultValue = 14)]
        public int iSL { get; set; }

        [Parameter("TP", DefaultValue = 14)]
        public int iTP { get; set; }


        [Parameter("2nd MA", DefaultValue = 20)]
        public int a { get; set; }





        private ParabolicSAR PSar;
        private StochasticOscillator Stoch;
        private MovingAverage MA1;
        private RelativeStrengthIndex RSI;
        private MovingAverage MA2;


        private const string label = "EMA";

        private Position longPosition;
        private Position shortPosition;
        public double balance;


        protected override void OnStart()
        {
            // Put your initialization logic here
            Stoch = Indicators.StochasticOscillator(K_Period, Slowing, D_Period, MovingAverageType.Simple);
            MA1 = Indicators.SimpleMovingAverage(Stoch.PercentD, Period);
            PSar = Indicators.ParabolicSAR(MinA, MaxA);
            RSI = Indicators.RelativeStrengthIndex(Source, RSIPeriod);
            MA2 = Indicators.SimpleMovingAverage(MA1.Result, a);

            balance = Account.FreeMargin;
        }





        protected override void OnTick()
        {
            // Put your core logic here}
        }

        protected override void OnBar()
        {
            var cBotPositions = Positions.FindAll(label);

            longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            if (cBotPositions.Length >= 1)
                return;




            double Risk = 0.01 * balance;





            if (Stoch.PercentK.Last(0) < MA1.Result.Last(1) && Stoch.PercentK.Last(0) < 20 && longPosition == null)
            {
                CP();

                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
            }

            if (Stoch.PercentK.Last(1) > MA1.Result.Last(0) && Stoch.PercentK.Last(0) > 80 && shortPosition == null)
            {
                CP();

                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }
        }









        private void CP()
        {
            foreach (var Position in Positions)
            {



                ClosePosition(Position, Volume);

            }
        }
    }
}

 


@carlosdrcunha