zig ziag

Created at 06 May 2022, 10:17
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!
HA

hamirady60

Joined 25.04.2022

zig ziag
06 May 2022, 10:17


 

Hello 

i need help
I want to make changes to this Robot
If the price breaks it previous trend, the position will open
Rule:

Buy- Wherever high was seen and then low. Wait for the price to close above that high
Sell - wherever low was seen and then high. Wait for the price to close below the previous lowest level

*

*

*

*

*

using cAlgo.API;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot()]
    public class ZigZagCycleBot : Robot
    {
        private Position _position;
        private ZigZag _zigZag;
        private double _prevValue;


        [Parameter(DefaultValue = 12)]
        public int ZzDepth { get; set; }

        [Parameter(DefaultValue = 550)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 5)]
        public int ZzDeviation { get; set; }

        [Parameter(DefaultValue = 3)]
        public int ZzBackStep { get; set; }

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


        protected override void OnStart()
        {
            _zigZag = Indicators.GetIndicator<ZigZag>(ZzDepth, ZzDeviation, ZzBackStep);
        }

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

            bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
            bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;


            double lastValue = _zigZag.Result.LastValue;

            if (!double.IsNaN(lastValue))
            {

                // Buy                
                if (lastValue < _prevValue && !isLongPositionOpen)
                {
                    ClosePosition();
                    Buy();
                }
                // Sell
                else if (lastValue > _prevValue && _prevValue > 0.0 && !isShortPositionOpen)
                {
                    ClosePosition();
                    Sell();
                }

                _prevValue = lastValue;
            }
        }

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

        private void ClosePosition()
        {
            if (_position == null)
                return;
            Trade.Close(_position);
            _position = null;
        }

        private void Buy()
        {
            Trade.CreateBuyMarketOrder(Symbol, Volume);
        }

        private void Sell()
        {
            Trade.CreateSellMarketOrder(Symbol, Volume);
        }

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

    }
}


@hamirady60