CCI 20 Robot (Modified) free

by atrader in category Trend at 26/06/2013
Description

This is a collaborative work  based on a forum post : /forum/cbot-support/1109

Any suggestions welcome. 

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
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;
        }
    }
}
Comments

cAlgo_Fanatic - June 28, 2013 @ 16:41

Please correct your robots (or redownload) to include this lines of code for proper results based on the robots logic: protected override void OnPositionClosed(Position position) { _position = null; }

davidp13 - May 14, 2014 @ 22:39

can you add the following to this robot?

cross of two ema's (slow x fast)and cci cross above 0 then long

cross of two ema's (fast x slow)and cci cross below 0 then short

exit on TP

protect by closing trade when cci crosses 0 in opposite direct than entry or by SL

0