Gann Hilo free

by hamirady60 in category Trend at 25/04/2022
Description

Hello, can anyone help me?
I want to add a condition to this indicator but I do not know how to code
I want this  indicator to have another Gan Hilo
For example,  two GannHilo 10 and 100, and the position opens when the price crosses both, but when the price returns from the Gun 10, the position closes.
My language is not English so I hope you understand what I mean
And thank you very much for helping me

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
//#reference: ..\Indicators\GannHighLow.algo
// -------------------------------------------------------------------------------
//
//    This is a Robot based on the GannHighLow Indicator. 
//    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.
//    There is only one position open at a time. So, if a buy is triggered and a sell is open
//    the sell will be closed.
//
//    The default parameters use no SL/TP (Default to zero)
//    If SLTrigger and TrailingStop are greater than zero then Trailing stop is used
//
// -------------------------------------------------------------------------------

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

namespace cAlgo.Robots
{
    [Robot()]
    public class GannHiLoRobot : Robot
    {
        private GannHighLow _gannHighLowIndicator;
        private Position _position;

        [Parameter("Period", DefaultValue = 50)]
        public int Period { 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()
        {
            _gannHighLowIndicator = Indicators.GetIndicator<GannHighLow>(Period);
        }

        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 (_gannHighLowIndicator.Result.HasCrossedAbove(MarketSeries.Close, 1) && !isShortPositionOpen)
            {
                ClosePosition();
                Sell();
            }
            else if (_gannHighLowIndicator.Result.HasCrossedBelow(MarketSeries.Close, 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);
                    }
                }
            }
        }

    }
}
Comments

TraderC - April 26, 2022 @ 16:51

Let me know if what do you want is something like "breakeven" to close a position before it cross back the " Gun 10"...

hamirady60 - April 26, 2022 @ 20:11

Not exactly, I want the position to close after the price crosses the 10 gann
Let me put it that way
Opening position: Passing both gann 10 and 100  
close position: Cross the gun 10

prestigedecanter6 - April 28, 2022 @ 11:52

I read this article. I think You put a great deal of exertion to How Much Do Veneers Cost make this article. I like your work.

faulknernolan6320702 - April 28, 2022 @ 18:44

Hope you will find a satisfactory answer soon. A new day of learning and working effectively. Sorry for not being able to reply to this post for you. But I want to dedicate the fall guys game, hope it helps you relax ^^

0