CCI20
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
Replies
elodie
21 Jun 2013, 12:51
RE:
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:
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
elodie
25 Jun 2013, 14:15
RE:
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.
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
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
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:
@atrader