How to use custom indicator in cBot

Created at 25 Sep 2015, 10:42
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!
RO

rotomomo

Joined 22.09.2015

How to use custom indicator in cBot
25 Sep 2015, 10:42


How to refer this indicator in cBot (StochacticCCI)


using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class StochasticCCI : Indicator
    {
        [Output("StochCCI", Color = Colors.Blue)]
        public IndicatorDataSeries StochCCI { get; set; }

        [Output("Trigger", Color = Colors.Red)]
        public IndicatorDataSeries trigger { get; set; }

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

        private CommodityChannelIndex cci;
        private IndicatorDataSeries value1;

        protected override void Initialize()
        {
            cci = Indicators.CommodityChannelIndex(Period);
            value1 = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            double cciL = cci.Result[index];
            double cciH = cci.Result[index];
            for (int i = index - Period + 1; i <= index; i++)
            {
                if (cciH < cci.Result[i])
                {
                    cciH = cci.Result[i];
                }
                if (cciL > cci.Result[i])
                {
                    cciL = cci.Result[i];
                }
            }
            if (cciH != cciL)
            {
                value1[index] = ((cci.Result[index] - cciL) / (cciH - cciL));
            }
            StochCCI[index] = (4 * value1[index] + 3 * value1[index - 1] + 2 * value1[index - 2] + value1[index - 3]) / 10;
            StochCCI[index] = 2 * (StochCCI[index] - 0.5);

            trigger[index] = 0.96 * (StochCCI[index - 1] + 0.02);
        }
    }
}

 


@rotomomo
Replies

ColossusFX
26 Sep 2015, 17:59

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Requests;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class Cci20 : Robot
    {
        private CommodityChannelIndex _cci;
        private Position _position;

        [Parameter(DefaultValue = 1, MinValue = 0, MaxValue = 50)]
        public int RiskPct { get; set; }


        [Parameter(DefaultValue = 500, MinValue = 100, MaxValue = 500)]
        public int Leverage { get; set; }


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


        [Parameter("Stop Loss (pips)", DefaultValue = 10, MinValue = 0)]
        public int StopLoss { get; set; }


        [Parameter("Take Profit (pips)", DefaultValue = 0, MinValue = 0)]
        public int TakeProfit { get; set; }


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


        protected int GetVolume
        {
            get
            {
                var risk = (int) (RiskPct*Account.Balance/100);

                int volumeOnRisk = StopLoss > 0 ? (int) (risk*Symbol.Ask/(Symbol.PipSize*StopLoss)) : Volume;

                double maxVolume = Account.Equity*Leverage*100/101;


                double vol = Math.Min(volumeOnRisk, maxVolume);

                return (int) Math.Truncate(Math.Round(vol)/10000)*10000; // round to 10K
            }
        }

        protected override void OnStart()
        {
            _cci = Indicators.CommodityChannelIndex(Periods);
        }


        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 (_cci.Result.HasCrossedBelow(0.0, 2) && !isShortPositionOpen)
                OpenPosition(TradeType.Sell);
            else if (_cci.Result.HasCrossedAbove(0.0, 2) && !isLongPositionOpen)
                OpenPosition(TradeType.Buy);
        }


        private void OpenPosition(TradeType type)
        {
            if (_position != null)
                Trade.Close(_position);

            Volume = GetVolume;
            Print(Volume);
            Request request = new MarketOrderRequest(type, Volume)
                                  {
                                      Label = "CCI 20",
                                      StopLossPips = StopLoss > 0 ? StopLoss : (int?) null,
                                      TakeProfitPips = TakeProfit > 0 ? TakeProfit : (int?) null,
                                  };
            Trade.Send(request);
        }


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

 


@ColossusFX

ColossusFX
26 Sep 2015, 17:59

That is someone elses CCI bot, not sure if its any help... CCI is not very reliable IMO.


@ColossusFX