Turn indicator to a cBot

Created at 12 Mar 2017, 12:20
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!
PH

phily1_1

Joined 03.03.2017

Turn indicator to a cBot
12 Mar 2017, 12:20


Hi, 

I'm currently learning C# and would like to know if it's possible to turn a indicator to a cBot by only adding functions to the code itself. These functions would be order open , - close, trailingstop, etc. And ofcourse change the namspace to Robot and add Onstart and void to the code itself.

I'm more than happy to provide you with a code that I've been working on as an example if needed.

 

Thank you in advance.

 

 


@phily1_1
Replies

phily1_1
14 Mar 2017, 20:45

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

        public class SampleRobot : Robot
        {

            private DirectionalMovementSystem _dms;

            private double _DIplus;
            private double _DIminus;

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

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

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


            protected override void OnStart()
            {
                _dms = Indicators.DirectionalMovementSystem(Period);

            }

            protected override void OnBar()
            {
                if (Trade.IsExecuting)
                    return;

                bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
                bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;

                if (_dms.ADX.LastValue > 0.0 && _dms.DIPlus.LastValue() && !isLongPositionOpen)
                {
                    ClosePosition();
                    Buy();
                }

                if (_dms.ADX.LastValue < 0.0 && _dms.DIMinus.LastValue() && !isShortPositionOpen)
                {
                    ClosePosition();
                    Sell();
                }
            }

        private void Buy()
        {
            Trade.CreateBuyMarketOrder(Symbol, Volume);
        }

        private void Sell()
        {
            Trade.CreateSellMarketOrder(Symbol, Volume);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            _position = openedPosition;
        }
        
      } 

    }

Like this ???


@phily1_1

... Deleted by UFO ...

phily1_1
16 Mar 2017, 19:25

RE:

lucian said:

You can start with this 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 ADX : Robot
    {




        private DirectionalMovementSystem _dms;


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

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

        [Parameter("DIMinus", DefaultValue = 20)]
        public int DIMinus { get; set; }
        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }
        [Parameter("Label", DefaultValue = "ADX")]
        public string Label { get; set; }

        protected override void OnStart()
        {
            _dms = Indicators.DirectionalMovementSystem(Period);

        }

        protected override void OnBar()
        {





            if (_dms.DIMinus.LastValue > DIMinus && _dms.DIPlus.LastValue < DIPlus && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            {
               
                close(TradeType.Sell);
                trade(TradeType.Buy);
            }

            if (_dms.DIMinus.LastValue < DIMinus && _dms.DIPlus.LastValue > DIPlus && Positions.FindAll(Label, Symbol, TradeType.Sell).Length == 0)
            {
                close(TradeType.Buy);
                trade(TradeType.Sell);
            }

        }
        private void close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(Label, Symbol, tradeType))
                ClosePosition(position);
        }



        private void trade(TradeType tradetype)
        {
            ExecuteMarketOrder(tradetype, Symbol, Volume, Label);
        }





    }

}

Thank you so much ! You truly made my day. 

 


@phily1_1