CCI robot

Created at 18 Sep 2012, 14:27
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!
MI

misado

Joined 31.08.2012

CCI robot
18 Sep 2012, 14:27


Hi guys,

tried to create a cci robot (on the basic of rsi robot) but there seems to be an error:

 

ERROR 12:   } expected (but where??)

 

In a next step I will combine the cci with another indi but this comes later.

 

//
//    The "Sample CCI Robot" will create a buy order when the Commodity Channel Index indicator crosses the  level 1, 
//    and a Sell order when the CCI indicator crosses the level -1. The order is closed be either a Stop Loss, defined in 
//    the "Stop Loss" parameter, or by the opposite CCI crossing signal (buy orders close when CCI crosses the -1 level 
//    and sell orders are closed when CCI crosses the 1 level). 
//
//    The robot can generate only one Buy or Sell order at any given time.
//
// -------------------------------------------------------------------------------------------------

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

namespace cAlgo.Robots
{
    [Robot]
    public class CCIRobot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }
        
        [Parameter("Periods", DefaultValue = 21)]
        public int Periods { get; set; }

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

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

        private Position position;        
		private CommodityChannelIndex cci;

        protected override void OnStart()
        {
            cci = Indicators.CommodityChannelIndex(Source, Periods);
        }

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

            if (cci.Result.LastValue < 0 && (position == 1 || position.TradeType == TradeType.Sell)) 
        	{
        		OpenPosition(TradeType.Buy);
        	}

            if (cci.Result.LastValue > 0 && (position == -1 || position.TradeType == TradeType.Buy))
            {
                OpenPosition(TradeType.Sell);
            }
        }

        private void OpenPosition(TradeType command)
        {
            if (position != 1)
            {
                Trade.Close(position);
                position = 1;
            }

        {
            if (position != -1)
            {
                Trade.Close(position);
                position = -1;
            }

            Trade.CreateMarketOrder(command, Symbol, Volume);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;
            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), 1);
        }
		{
            position = openedPosition;
            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), -1);
        }
        private double GetAbsoluteStopLoss(Position position, int stopLossInPips)
        {
            return position.TradeType == TradeType.Buy
                ? position.EntryPrice - Symbol.PipSize * stopLossInPips
                : position.EntryPrice + Symbol.PipSize * stopLossInPips;
        }
    }
}

 


@misado
Replies

sktrader
19 Sep 2012, 12:26

Hi,

I looked at your code and there are a few mistakes.

First of all this is not possible: position = 1. What are you trying to do here?

Here are some corrections:

 

//
//    The "Sample CCI Robot" will create a buy order when the Commodity Channel Index indicator crosses the  level 1, 
//    and a Sell order when the CCI indicator crosses the level -1. The order is closed be either a Stop Loss, defined in 
//    the "Stop Loss" parameter, or by the opposite CCI crossing signal (buy orders close when CCI crosses the -1 level 
//    and sell orders are closed when CCI crosses the 1 level). 
//
//    The robot can generate only one Buy or Sell order at any given time.
//
// -------------------------------------------------------------------------------------------------

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

namespace cAlgo.Robots
{
    [Robot]
    public class CCIRobot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }
        
        [Parameter("Periods", DefaultValue = 21)]
        public int Periods { get; set; }

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

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

        private Position position;        
		private CommodityChannelIndex cci;

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

        protected override void OnTick()
        {
            if (Trade.IsExecuting) return;
            
            // position == 1 ???
            // (position is not int or enum, you cannot compare with 1.)

            if (cci.Result.LastValue < 0 && position.TradeType == TradeType.Sell) 
        	{
        		OpenPosition(TradeType.Buy);
        	}
            //position == -1 same as above
            if (cci.Result.LastValue > 0 &&  position.TradeType == TradeType.Buy)
            {
                OpenPosition(TradeType.Sell);
            }
        }

        private void OpenPosition(TradeType command)
        {
            // THIS IS NOT CORRECT:
//            if (position != 1)
//            {
//                Trade.Close(position);
//                position = 1;
//            }

            // is this what you need?
            if(position != null)
                Trade.Close(position);

//        {     <- this is extra

            // THIS IS NOT CORRECT:
//            if (position != -1)
//            {
//                Trade.Close(position);
//                position = -1;
//            }

            Trade.CreateMarketOrder(command, Symbol, Volume);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;
            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), 1);
        } 

        // DUPLICATE...
//		  {
//            position = openedPosition;
//            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), -1);
//        }

        private double GetAbsoluteStopLoss(Position pos, int stopLossInPips)
        {
            return pos.TradeType == TradeType.Buy
                ? pos.EntryPrice - Symbol.PipSize * stopLossInPips
                : pos.EntryPrice + Symbol.PipSize * stopLossInPips;
        }
    }
}

 

 

 


@sktrader

admin
19 Sep 2012, 14:13 ( Updated at: 23 Jan 2024, 13:11 )

 

Hello misado,

 

Your code needs some modification in order to work properly.

First you need to make sure the position is not null before accessing it.

So, your code could be modified to this:

 

if (cci.Result.LastValue < 0 && (_position == null || _position.TradeType == TradeType.Sell) )
{
    	OpenPosition(TradeType.Buy);
}
if (cci.Result.LastValue > 0 && (_position == null || _position.TradeType == TradeType.Buy))
{
        OpenPosition(TradeType.Sell);
}

 

Please visit this link to understand how to code using Position.

[/docs/reference/calgo/api/position]

 

Also, when you use Trade.ModifyPosition to modify the stop loss and take profit, you need to provide the full take profit or stop loss price not the pips.

In other words this: Trade.ModifyPosition(openedPosition,GetAbsoluteStopLoss(openedPosition,StopLoss),1);

means this: Take Profit Hit will occur when the currency quote will equal 1.

Therefore, if we want Take Profit to be equal to the entry price plus 1 pip, we need 

takeprofit = _position.EntryPrice + Symbol.PipSize

More here: [/docs/reference/calgo/api/internals/itrade/modifyposition]

 

Please visit this link to understand more about using Trade and it's member methods:

[/docs/reference/calgo/api/internals/itrade]

 

 

 


@admin

misado
19 Sep 2012, 22:40

Hi, my attempt is:

Buy when cci >1 and

Sell when cci < -1

I've tried to change some items but still need your help. Its quit strange for me.

Thanks mike


@misado

admin
20 Sep 2012, 15:19

Then you need to modify your if statement to the following:

 


if (cci.Result.LastValue > 1 && (_position == null || _position.TradeType == TradeType.Sell) )
{
	OpenPosition(TradeType.Buy);
}
else if (cci.Result.LastValue < -1 && (_position == null || _position.TradeType == TradeType.Buy))
{
        OpenPosition(TradeType.Sell);
}




@admin

misado
20 Sep 2012, 16:17

I've changed it but still have error 11!

//
//    The "Sample CCI Robot" will create a buy order when the Commodity Channel Index indicator crosses the  level 1, 
//    and a Sell order when the CCI indicator crosses the level -1. The order is closed be either a Stop Loss, defined in 
//    the "Stop Loss" parameter, or by the opposite CCI crossing signal (buy orders close when CCI crosses the -1 level 
//    and sell orders are closed when CCI crosses the 1 level). 
//
//    The robot can generate only one Buy or Sell order at any given time.
//
// -------------------------------------------------------------------------------------------------

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

namespace cAlgo.Robots
{
    [Robot]
    public class CCIRobot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }
        
        [Parameter("Periods", DefaultValue = 21)]
        public int Periods { get; set; }

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

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

        private Position position;        
		private CommodityChannelIndex cci;

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

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

            if (cci.Result.LastValue > 1 && (_position == null || _position.TradeType == TradeType.Sell))
		{
			OpenPosition(TradeType.Buy);
		}
			if (cci.Result.LastValue < -1 && (_position == null || _position.TradeType == TradeType.Buy))
		{
          OpenPosition(TradeType.Sell);
		}
       }

        private void OpenPosition(TradeType command)
        {
            if (position != null)
            {
                Trade.Close(position);
                position = null;
            }

                Trade.CreateMarketOrder(command, Symbol, Volume);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;
            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), 1);
        }
		{
            position = openedPosition;
            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), -1);
        }
        private double GetAbsoluteStopLoss(Position position, int stopLossInPips)
        {
            return position.TradeType == TradeType.Buy
                ? position.EntryPrice - Symbol.PipSize * stopLossInPips
                : position.EntryPrice + Symbol.PipSize * stopLossInPips;
        }
    }
}




@misado

admin
24 Sep 2012, 17:29

The error I am seeing in your code is this: 

 

You are declaring a variable:

private  Position position ;

then you are referencing a variable that does not exist:

_position ==null

position and _position are two different names.

 

The easiest way to fix this is just change the declaration to:

private Position _position


@admin