MA of DI-(ADX) and Risk and Open just one position each time

Created at 23 Apr 2018, 11:45
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

MA of DI-(ADX) and Risk and Open just one position each time
23 Apr 2018, 11:45


Hello,

I have 3 questions:

1 - how i can put a Simple moving Average in the DI- in the Directional Movement System(ADX)

 

2 - How i do the Risk as Volume by free margin?

the formula is (Account.FreeMargin / 100) * Risk

 

and 

3 - Why in a basic cBot algoritmic is opening all positions at the same time? i just want one position each time, closing in the oposit signal and opening another (buy or sell) closing the the oposite signal.

 

 

Many Thanks!!


@carlosdrcunha
Replies

carlosdrcunha
23 Apr 2018, 12:02

lets say this is the code

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 MAcBot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

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

        [Parameter("MA2", DefaultValue = 10)]
        public int Period2 { get; set; }

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

        private MovingAverage firstMA1;
        private MovingAverage secondMA2;



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

            firstMA1 = Indicators.MovingAverage(Source, Period, MovingAverageType.Simple);
            secondMA2 = Indicators.MovingAverage(Source2, Period2, MovingAverageType.Simple);

        }


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

            var currentSlowMa = firstMA1.Result.Last(0);
            var currentFastMa = secondMA2.Result.Last(0);
            var previousSlowMa = firstMA1.Result.Last(1);
            var previousFastMa = secondMA2.Result.Last(1);



            if (previousSlowMa <previousFastMa && currentSlowMa <currentFastMa )
            {
                CP();
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume);
            }

            if (previousSlowMa > previousFastMa && currentSlowMa > currentFastMa )
            {
                CP();
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume);
            }


        }


        private void CP()
        {
            foreach (var Position in Positions)
            {
                ClosePosition(Position, Volume);
            }
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}


@carlosdrcunha

PanagiotisCharalampous
23 Apr 2018, 12:22 ( Updated at: 21 Dec 2023, 09:20 )

Hi carlosdrcunha,

Thanks for posting in our forum. Regarding your questions.

1) It is not clear what do you mean. Would you like to calculate the simple moving average of the ADX. If yes, see below how you can set the ADX indicator as an input to your SMA indicator

If this in not what you mean, please explain further.

2) I did not understand this question. Can you elaborate a bit more?

3) If you want to restrict your cBot to opening one position at a time, you can use the following condition before executing a market order.

            if (Positions.Count == 0)
            {
                // Execute trading...
            }

Let me know if the above helps,

Best Regards,

Panagiotis


@PanagiotisCharalampous

carlosdrcunha
24 Apr 2018, 21:32

RE: DI- is the red line

​DI- is the red Line in the ADX, how can i put a MA of the red line? or for example as well, how can i put a MA on that Period u got on the ADX?

 

 

Thank you a lot.

 


@carlosdrcunha

carlosdrcunha
24 Apr 2018, 22:25

RE: RE: DI- is the red line

carlosdrcunha said:

 

BTW a MA on the value on the ADX as well, not on the period, on the value given by that period, and on the Red Line as well,

 

The risk, i Want to invest 35% of the free Margin (rounded), for example.

if i have 50k, i want to invest 35%, thats 17.5k in assets.

how can i do it?

 

 

 

Thank you a lot.

 

 


@carlosdrcunha

PanagiotisCharalampous
25 Apr 2018, 09:54

Hi carlosdrcunha,

My example above shows how to get the SMA for an ADX.

Regarding the margin your can use the  Account.FreeMargin property to calculate the amount you want to invest.

I hope this helps.

Best Regards,

Panagiotis


@PanagiotisCharalampous

carlosdrcunha
25 Apr 2018, 17:10

but how can i apply the ADX in the source? just appear Open, High, Close, Low, and i do not eaven know how to have that feature to apply the MA on the ADX.

 

 

thank you.

 


@carlosdrcunha

PanagiotisCharalampous
25 Apr 2018, 17:31

Hi carlosdrcunha,

In order to see ADX in the source list, you first need to add the indicator (Directional Movement System) to the chart.

Best Regards,

Panagiotis


@PanagiotisCharalampous

carlosdrcunha
25 Apr 2018, 18:20

yes, it helped a lot, but how can i apply the indicator from the chart on the code? im just a new starter on coding, just learning.

can you give me an example?

 

thank you Panagiotis,

kind regards


@carlosdrcunha

carlosdrcunha
05 May 2018, 15:03

again, i just can put the MA on the Chart, the code doesnt take the values of the MA i put on the chart to DI- or DI+ or ADX, how can i get the value on the code? because source just give me Open High Close Low.

 

 

Sincerly-

Thank you.


@carlosdrcunha

PanagiotisCharalampous
07 May 2018, 16:27

Hi carlosdrcunha,

See below how you can do the same thing in a cBot

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 NewcBot : Robot
    {

        DirectionalMovementSystem _dms;
        SimpleMovingAverage _sma;
        protected override void OnStart()
        {
            _dms = Indicators.DirectionalMovementSystem(14);
            _sma = Indicators.SimpleMovingAverage(_dms.ADX, 14);
        }

        protected override void OnTick()
        {

        }

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

Best Regards,

Panagiotis


@PanagiotisCharalampous