CCI20

Created at 21 Jun 2013, 10:49
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!
EL

elodie

Joined 07.06.2013

CCI20
21 Jun 2013, 10:49


hI
I would like to optimize the results of CCI20
and install "riskpercent"

with this method:

      [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 = 10, MinValue = 0)]
        public int TakeProfit { get; set; }

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

      
        private Position _position;
        private CommodityChannelIndex _cci;

can you help me.
the method will be useful for other robots. (eg martingale is difficult to reproduce)
thank you very much



        
 


 


@elodie
Replies

atrader
21 Jun 2013, 12:06

There is something similar in this robot: /algos/robots/show/271 Is this the one you are saying it's difficult to reproduce?

I haven't tested this but I think it should be something like this:

        protected int getVolume
        {
            get
            {
                var risk = (int) (RiskPct*Account.Balance/100);
                var volumeOnRisk = (int) (risk*Symbol.Ask/(Symbol.PipSize*StopLoss)); // or Bid
               
                double maxVolume = Account.Equity*Leverage*100/101;
                
var vol = Math.Min(volumeOnRisk, maxVolume);
return (int)Math.Truncate(Math.Round(vol) / 10000) * 10000; // round to 10K } }




@atrader

elodie
21 Jun 2013, 12:51

RE:
atrader said:

There is something similar in this robot: /algos/robots/show/271 Is this the one you are saying it's difficult to reproduce?

I haven't tested this but I think it should be something like this:

        protected int getVolume
        {
            get
            {
                var risk = (int) (RiskPct*Account.Balance/100);
                var volumeOnRisk = (int) (risk*Symbol.Ask/(Symbol.PipSize*StopLoss)); // or Bid
               
                double maxVolume = Account.Equity*Leverage*100/101;
                
var vol = Math.Min(volumeOnRisk, maxVolume);
return (int)Math.Truncate(Math.Round(vol) / 10000) * 10000; // round to 10K } }




thank you
but it does not work


@elodie

elodie
24 Jun 2013, 12:10

RE: RE:
elodie said:
atrader said:

There is something similar in this robot: /algos/robots/show/271 Is this the one you are saying it's difficult to reproduce?

I haven't tested this but I think it should be something like this:

        protected int getVolume
        {
            get
            {
                var risk = (int) (RiskPct*Account.Balance/100);
                var volumeOnRisk = (int) (risk*Symbol.Ask/(Symbol.PipSize*StopLoss)); // or Bid
               
                double maxVolume = Account.Equity*Leverage*100/101;
                
var vol = Math.Min(volumeOnRisk, maxVolume);
return (int)Math.Truncate(Math.Round(vol) / 10000) * 10000; // round to 10K } }




thank you
but it does not work

hi

with this method,it allows to adjust the lever :

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

thank you very much


@elodie

atrader
24 Jun 2013, 16:53

I'm not sure what you mean. Do you still need help? Post the code you managed to come up with so far and I will try to correct it.


@atrader

elodie
25 Jun 2013, 14:15

RE:
atrader said:

I'm not sure what you mean. Do you still need help? Post the code you managed to come up with so far and I will try to correct it.

hi I have tried several solutions without success thank you in advance for your help...

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

namespace cAlgo.Robots
{
    [Robot]
    public class CCI_20 : Robot
    {
        [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 = 0, 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; }

        private Position _position;
        private CommodityChannelIndex _cci;

        protected override void OnStart()
        {
            _cci = Indicators.CommodityChannelIndex(Periods);
        }
        protected int getVolume
        {
            get
            {
                var risk = (int) (RiskPct*Account.Balance/100);
                var volumeOnRisk = (int) (risk*Symbol.Ask/(Symbol.PipSize*StopLoss)); // or Bid
               
                double maxVolume = Account.Equity*Leverage*100/101;
                
                var vol = Math.Min(volumeOnRisk, maxVolume);

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

        } 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);

            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;
        }
    }
}


@elodie

cAlgo_Fanatic
26 Jun 2013, 12:48

1. There is an extra curly bracket which will be a reason the program will not build here:

 } protected override void OnBar()

Remove that "}" from the start of the line.

2. The getVolume property is not being used only defined.

One way to use it is by adding this line prior to creating the request object:

Volume = getVolume;

right before this line for instance:

Request request = new MarketOrderRequest(type, Volume)
//...

The getVolume property will return a negative number if SL is not set (if SL = 0) .  So, either modify the code or initialize SL to a value greater than zero.

Usefull C# tutorial: http://www.csharp-station.com/Tutorial.aspx

 


@cAlgo_Fanatic

elodie
26 Jun 2013, 14:49

yes for "}".
c is a copy error
but can you help me to develop fully the "RiskPct" lever 500 ..
malgres all my efforts I n unable to operate "RiskPCT.
I'm still with volume 10000.
whatever starting capital.(backtesting)
thank you very much


@elodie

atrader
26 Jun 2013, 15:52

It's here: /algos/robots/show/307

If you (or anyone else) have any ideas about how to optimize it further, go ahead and let me know I will modify it.


@atrader

elodie
26 Jun 2013, 19:02

RE:
atrader said:

It's here: /algos/robots/show/307

If you (or anyone else) have any ideas about how to optimize it further, go ahead and let me know I will modify it.


great !!

thank you


@elodie

cAlgo_Fanatic
28 Jun 2013, 16:43

You need to reset your global field _position to null when the position gets closed for proper results:

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




@cAlgo_Fanatic

aysos75
29 Jun 2014, 22:04

RE:

 

I tested this robot on h4 between 24.01.2014 and 06.29.2014 and was a monstrous loss of € -20,318 (-41%)! 


@aysos75