robot for CCI

Created at 24 Oct 2018, 13:06
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!
NG

nguyendan81985

Joined 24.10.2018

robot for CCI
24 Oct 2018, 13:06


Hi gents,

im new member for coding Ctrader, i have idea to create robot for automatic trading which is using CCI

1. when CCI crossing above zero line (0), then Buy, when cci >100 then check if CCI crossing below +100 then Close,

2. 1. when CCI crossing above zero line (0), then Sell, when cci < - 100 then check if CCI crossing above - 100 then Close,

 

could you help me to write this code.

thanks


@nguyendan81985
Replies

nguyendan81985
24 Oct 2018, 13:19

my coding as below, but it is not working.

 

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 CCIROBOT : Robot
    {
        [Parameter(DefaultValue = 14)]
        public int cciPeriod { get; set; }


        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }


        private CommodityChannelIndex cci;
        private const string name = "CCIROBOT";

        protected override void OnStart()
        {
            cci = Indicators.CommodityChannelIndex(cciPeriod);

        }

        protected override void OnBar()
        {

            var longPos = Positions.Find(name, Symbol, TradeType.Buy);
            var shortPos = Positions.Find(name, Symbol, TradeType.Sell);

            if ((cci.Result.HasCrossedAbove(0.0, 1) && longPos == null))
            {
                if (shortPos != null)
                {
                    ClosePosition(shortPos);
                    ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, name);
                }

                Print("Open Buy");

                {
                    if (cci.Result.HasCrossedBelow(100.0, 1) && longPos != null)
                        ClosePosition(longPos);

                    Print("Closed Buy");
                }

            }
            else if ((cci.Result.HasCrossedAbove(0.0, 1) && longPos == null))
            {
                if (longPos != null)
                {
                    ClosePosition(longPos);
                    ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, name);
                }

                Print("Open Sell");

                {
                    if (cci.Result.HasCrossedAbove(-100.0, 1) && shortPos != null)
                        ClosePosition(longPos);

                    Print("Closed Sell");
                }
            }
        }


        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }


    }



}

 


@nguyendan81985