Gann Hilo

Created at 27 Apr 2022, 11:50
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

Gann Hilo
27 Apr 2022, 11:50


Dear Ahmad, I referred. The robot successfully built, but does not open any position

Please check it again

 

 

 

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

namespace cAlgo.Robots
{
    [Robot()]
    public class GannHiLoRobot : Robot
    {
        private GannHighLow _firstGann, _secondGann, _thirdGann;

        private Position _position;

        [Parameter("1st Gann Period", DefaultValue = 50)]
        public int FirstGannPeriod { get; set; }

        [Parameter("2nd Gann Period", DefaultValue = 10)]
        public int SecondGannPeriod { get; set; }

        [Parameter("3rd Gann Period", DefaultValue = 100)]
        public int ThirdGannPeriod { get; set; }

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

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


        [Parameter(DefaultValue = 0)]
        public int TakeProfit { get; set; }


        [Parameter(DefaultValue = 0)]
        public double SLTrigger { get; set; }

        [Parameter(DefaultValue = 0)]
        public double TrailingStop { get; set; }

        private bool _isTrigerred;


        protected bool UseTrailingStop
        {
            get { return SLTrigger > 0.0 && TrailingStop > 0.0; }
        }

        protected override void OnStart()
        {
            _firstGann = Indicators.GetIndicator<GannHighLow>(FirstGannPeriod);
            _secondGann = Indicators.GetIndicator<GannHighLow>(SecondGannPeriod);
            _thirdGann = Indicators.GetIndicator<GannHighLow>(ThirdGannPeriod);

        }

        protected override void OnTick()
        {

            if (Trade.IsExecuting || _position == null)
                return;

            if (UseTrailingStop)
                Trail();

        }

        /// <summary>
        /// If close price rises above the GannHighLow indicator a buy is triggered and 
        /// if the prices falls below the GannHighLow  indicator a sell is triggered.
        /// </summary>
        protected override void OnBar()
        {
            bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
            bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;


            if (_firstGann.Result.HasCrossedAbove(MarketSeries.Close, 1) && _secondGann.Result.HasCrossedBelow(_thirdGann.Result, 1) && !isShortPositionOpen)
            {
                ClosePosition();
                Sell();
            }
            else if (_firstGann.Result.HasCrossedBelow(MarketSeries.Close, 1) && _secondGann.Result.HasCrossedAbove(_thirdGann.Result, 1) && !isLongPositionOpen)
            {
                ClosePosition();
                Buy();
            }
        }


        /// <summary>
        /// Close the existing position
        /// </summary>
        private void ClosePosition()
        {
            if (_position == null)
                return;

            Trade.Close(_position);
            _position = null;
        }

        /// <summary>
        /// Send a Buy trade request
        /// </summary>
        private void Buy()
        {
            Request request = new MarketOrderRequest(TradeType.Buy, Volume) 
            {
                Label = "Gann HiLo",
                //SlippagePips = 1,
                StopLossPips = StopLoss > 0 ? (int?)StopLoss : null,
                TakeProfitPips = TakeProfit > 0 ? (int?)TakeProfit : null
            };
            Trade.Send(request);
        }

        /// <summary>
        /// Send a Sell trade request
        /// </summary>
        private void Sell()
        {
            Request request = new MarketOrderRequest(TradeType.Sell, Volume) 
            {
                Label = "Gann HiLo",
                //SlippagePips = 1,
                StopLossPips = StopLoss > 0 ? (int?)StopLoss : null,
                TakeProfitPips = TakeProfit > 0 ? (int?)TakeProfit : null
            };
            Trade.Send(request);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            _position = openedPosition;
        }


        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }

        /// <summary>
        /// Trailing Stop
        /// </summary>
        private void Trail()
        {
            if (_position.TradeType == TradeType.Buy)
            {
                double distance = Symbol.Bid - _position.EntryPrice;

                if (distance >= SLTrigger * Symbol.PipSize)
                {
                    if (!_isTrigerred)
                    {
                        _isTrigerred = true;
                        Print("Trailing Stop Loss triggered...");
                    }

                    double newStopLossPrice = Math.Round(Symbol.Bid - TrailingStop * Symbol.PipSize, Symbol.Digits);

                    if (_position.StopLoss == null || newStopLossPrice > _position.StopLoss)
                    {
                        Trade.ModifyPosition(_position, newStopLossPrice, _position.TakeProfit);
                    }
                }
            }
            else
            {
                double distance = _position.EntryPrice - Symbol.Ask;

                if (distance >= SLTrigger * Symbol.PipSize)
                {
                    if (!_isTrigerred)
                    {
                        _isTrigerred = true;
                        Print("Trailing Stop Loss triggered...");
                    }

                    double newStopLossPrice = Math.Round(Symbol.Ask + TrailingStop * Symbol.PipSize, Symbol.Digits);

                    if (_position.StopLoss == null || newStopLossPrice < _position.StopLoss)
                    {
                        Trade.ModifyPosition(_position, newStopLossPrice, _position.TakeProfit);
                    }
                }
            }
        }

    }
}


@hamirady60
Replies

codey
07 Nov 2022, 04:06

@hamirady60,   I am also looking for same Gann HiLo cbot as what you mentioned.

Opening: Cross both Gann 10 and Gann 100

Closing: Cross Gann 10

Did you get this robot?  Please post the code here.   

Thanks


@codey