How can I fix My cci bot to run properly.
How can I fix My cci bot to run properly.
22 Sep 2015, 11:13
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 cciBot : Robot { private CommodityChannelIndex _cci; private const string label = "cci"; private Position _position; [Parameter("Periods", DefaultValue = 20, MinValue = 1)] public int Periods { get; set; } [Parameter(DefaultValue = "cci")] public string cBotLabel { get; set; } [Parameter(DefaultValue = 3)] public int MaxPositions { get; set; } [Parameter("Tralling Stop (pips)", DefaultValue = 1)] public int StopLoss { get; set; } protected double Quantity { get { double Quantity = Account.Balance / 1000; return (Quantity); } } protected long VolumeInUnits { get { return Symbol.QuantityToVolume(Quantity); } } public bool IsLastBar { get; set; } [Parameter("MinLoss", DefaultValue = -0.1)] public double MinLoss { get; set; } [Parameter("Take Profit (pips)", DefaultValue = 100)] public int TakeProfit { get; set; } protected override void OnStart() { _cci = Indicators.CommodityChannelIndex(Periods); Positions.Opened += PositionsOnOpened; Positions.Closed += PositionsOnClosed; // Put your initialization logic here } protected override void OnBar() { var cBotPositions = Positions.FindAll(cBotLabel); if (cBotPositions.Length > MaxPositions) { return; } var longPosition = Positions.Find(label, Symbol, TradeType.Buy); var shortPosition = Positions.Find(label, Symbol, TradeType.Sell); bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy; bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell; if (_cci.Result.HasCrossedAbove(0, 20) && !isLongPositionOpen) { if (IsLastBar) { ClosePosition(shortPosition); ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, cBotLabel, StopLoss, TakeProfit); } } if (_cci.Result.HasCrossedBelow(0, 20) && !isShortPositionOpen) { if (IsLastBar) { ClosePosition(longPosition); ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, cBotLabel, StopLoss, TakeProfit); } } } private void PositionsOnOpened(PositionOpenedEventArgs obj) { Position openedPosition = obj.Position; if (openedPosition.Label != cBotLabel) return; Print("position opened at {0}", openedPosition.EntryPrice); } private void PositionsOnClosed(PositionClosedEventArgs obj) { Position closedPosition = obj.Position; if (closedPosition.Label != cBotLabel) return; Print("position closed with {0} gross profit", closedPosition.GrossProfit); } protected override void OnPositionOpened(Position openedPosition) { _position = openedPosition; } protected override void OnPositionClosed(Position position) { _position = null; } protected override void OnTick() { var cBotPositions = Positions.FindAll(cBotLabel); if (cBotPositions.Length > MaxPositions) { return; foreach (var position in Positions) { //tralling stop if (position.GrossProfit <= 0) continue; if (position.TradeType == TradeType.Sell) { double profit1 = position.GrossProfit + position.Commissions - StopLoss * 10 * position.Volume / 100000.0; if (profit1 > 0.0 && position.TradeType == TradeType.Sell) { double? stopLossPrice = Symbol.Bid + StopLoss * Symbol.PipSize; if (StopLoss != 0 && stopLossPrice < position.StopLoss && stopLossPrice < position.EntryPrice) { if (stopLossPrice - Symbol.Bid > 0) { ModifyPosition(position, stopLossPrice, position.TakeProfit); } } } } else { double profit2 = position.GrossProfit + position.Commissions - StopLoss * 10 * position.Volume / 100000.0; if (profit2 > 0.0 && position.TradeType == TradeType.Buy) { double? stopLossPrice = Symbol.Ask - StopLoss * Symbol.PipSize; if (StopLoss != 0 && stopLossPrice > position.StopLoss && stopLossPrice > position.EntryPrice) { if (stopLossPrice - Symbol.Ask < 0) { ModifyPosition(position, stopLossPrice, position.TakeProfit); } } } } } } } } }
PS. My algorithms are to buy when the cci cross above the zero line at the next bar of the candlestick and sell when cci cross under the zero line at the next bar. I want to add t/p , trailing stop lost and stop lost. I am a Newbie for C# . So thank you for your help.
Replies
Spotware
22 Sep 2015, 22:03
Dear Trader,
We would like to inform you that we do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also can contact one of our Partners or post a job in Development Jobs section for further coding assistance.
@Spotware
rotomomo
22 Sep 2015, 11:22
RE:
*** I Mean wait until the cci cross above the zero line and wait until this bar is close then buy at the next bar.
@rotomomo