Problems when using older values of a custom made indicator

Created at 02 Aug 2020, 01:28
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!
PA

patrickzberg

Joined 04.02.2020

Problems when using older values of a custom made indicator
02 Aug 2020, 01:28


Hi together

 

I am trying to use a custom Indicator in my cBot but I somehow struggle to call for a value of the indicator, that is not the most recent one. Do I have to consider something special when calling an older value of a DataSeries from a custom made indicator which I load via references into my Bot?

The build does not fail but the Bot does simply never trade at all.

The difference between short and long position is intentional to show that I tried both...

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HitmanVersuch2 : Robot
    {

        [Parameter("Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Signal Strength")]
        //adx Trigger
        public int adx_trigger { get; set; }

        [Parameter("Sensitivity")]
        //Sensitivity
        public double sens { get; set; }

        [Parameter("Considered Volatility Range")]
        public int vol { get; set; }

        private DirectionalMovementSystem _adx { get; set; }
        private ExponentialMovingAverage _ema { get; set; }
        private HitmanTrueRange _hit { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
            _adx = Indicators.DirectionalMovementSystem(Bars, 14);
            _ema = Indicators.ExponentialMovingAverage(Source, 1);
            _hit = Indicators.GetIndicator<HitmanTrueRange>(Source, 14, sens, vol);

        }

        protected override void OnBar()
        {
            // Put your core logic here
            var Price0 = Bars.ClosePrices.Last(0);
            var Price1 = Bars.ClosePrices.Last(1);

            // )
            //&& (Price0 > _hit.Result.Last(0) && Price1 <= _hit.Result.Last(1)))
            if (_adx.ADX.Last(0) > adx_trigger && Price0 > _hit.Result.Last(0) && Functions.HasCrossedAbove(Bars.ClosePrices, _hit.Result, 0))
            {

                Close(TradeType.Sell);
                Open(TradeType.Buy);


            }

            //   )
            //&& (Price0 < _hit.Result.Last(0) && Price1 >= _hit.Result.Last(1)))
            // && Functions.HasCrossedAbove(Bars.ClosePrices, _hit.Result, 1))
            if (_adx.ADX.Last(0) > adx_trigger && Price0 < _hit.Result.Last(0) && (Price0 < _hit.Result.Last(0) && Price1 > _hit.Result.Last(1))
            {

                Close(TradeType.Buy);
                Open(TradeType.Sell);


            }

        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("Hitman", SymbolName, tradeType))
                ClosePosition(position);
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("Hitman", SymbolName, tradeType);
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);

            if (position == null)
                ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "Hitman");
        }


    }



}



code for the custom made incidator:

// -------------------------------------------------------------------------------------------------
//
//    This code is a cTrader Automate API example.
//    
//    All changes to this file might be lost on the next application update.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
// -------------------------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
    public class HitmanTrueRange : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter(DefaultValue = 14)]
        public int Periods { get; set; }

        [Output("Main", LineColor = "Turquoise")]
        public IndicatorDataSeries Result { get; set; }

        [Parameter("Sensitivity", DefaultValue = 0.05)]
        //Power oder Sensitivity
        public double sens { get; set; }

        [Parameter("ATR", DefaultValue = 10)]
        //Considered Volatility Range
        public int atr2 { get; set; }

        private AverageTrueRange atr;


        protected override void Initialize()
        {
            atr = Indicators.AverageTrueRange(atr2, MovingAverageType.Exponential);

        }


        public override void Calculate(int index)
        {

            if (Result.Count == 0)
            {
                Result[index] = 0;

            }

            if (Result.Count == 1)
            {
                Result[index] = 0;

            }

            if (Bars.ClosePrices.Last(0) > Result.LastValue && Bars.ClosePrices.Last(1) > Result.LastValue)
            {
                if (Result.LastValue > (Bars.ClosePrices.Last(0) - (atr.Result.Last(0) * sens)))
                {
                    Result[index] = Result[index - 1];
                    
                }

                if (Result.LastValue < (Bars.ClosePrices.Last(0) - (atr.Result.Last(0) * sens)))
                {
                    Result[index] = Bars.ClosePrices.Last(0) - (atr.Result.Last(0) * sens);
                }
            }

            else
            {

                if (Bars.ClosePrices.Last(0) < Result.LastValue && Bars.ClosePrices.Last(1) < Result.LastValue)
                {
                    if (Result.LastValue < (Bars.ClosePrices.Last(0) + (atr.Result.Last(0) * sens)))
                    {
                        Result[index] = Result[index - 1];
                        
                    }

                    if (Result.LastValue > (Bars.ClosePrices.Last(0) + (atr.Result.Last(0) * sens)))
                    {
                        Result[index] = Bars.ClosePrices.Last(0) + (atr.Result.Last(0) * sens);
                    }
                }

                else
                {

                    if (Bars.ClosePrices.Last(0) > Result.LastValue && Bars.ClosePrices.Last(1) <= Result.LastValue)
                    {
                        Result[index] = Bars.ClosePrices.Last(0) - atr.Result.Last(0) * sens;
                    }

                    //if (Bars.ClosePrices.Last(0) < Result.Last(1) && Bars.ClosePrices.Last(0) >= Result.Last(1))

                    else
                    {
                        Result[index] = Bars.ClosePrices.Last(0) + (atr.Result.Last(0) * sens);
                    }
                }

            }

        }
    }
}

 


@patrickzberg
Replies

... Deleted by UFO ...