Accessing custom indicator from cBot

Created at 06 Aug 2019, 15:07
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!
LU

lucrum

Joined 06.08.2019

Accessing custom indicator from cBot
06 Aug 2019, 15:07


How do I access the result of a custom indicator? As it stands the cBot doesn't build; it fails at "condition_UpSignal". A copy of the custom indicator is included. The indicator has been referenced. Any help is greatfully appreciated.


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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.WEuropeStandardTime, AccessRights = AccessRights.None)]
    public class CallingCustomIndic : Robot
    {
        [Parameter("Start trading (hour) 1", DefaultValue = 7)]
        public int StartHour { get; set; }

        [Parameter("Stop trading (hour) 1", DefaultValue = 23)]
        public int StopHour { get; set; }

        [Parameter("Trading hours", DefaultValue = true)]
        public bool condition_tradeHours { get; set; }

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

        [Parameter("Include Trailing Stop", DefaultValue = true)]
        public bool hasTrailingStop { get; set; }

        [Parameter("Trailing Stop Trigger (pips)", DefaultValue = 0)]
        public int TrailingStopTrigger { get; set; }

        [Parameter("Trailing Stop Step (pips)", DefaultValue = 5)]
        public int TrailingStopStep { get; set; }

        [Parameter("Pip size multiplier", DefaultValue = 20)]
        public int pipSizeMultiplier { get; set; }

        [Parameter("Periods", DefaultValue = 10)]
        public int periods { get; set; }

        public string currency = "EURUSD";

        private HighToHighMA highToHigh;

        protected override void OnStart()
        {
            highToHigh = Indicators.GetIndicator<HighToHighMA>(periods);
        }

        protected override void OnTick()
        {
            ManageStop();
        }

        protected override void OnBar()
        {
            var longPosition = Positions.FindAll("", currency, TradeType.Buy);
            var position = Positions.FindAll("", currency, TradeType.Sell);

            bool condition_tradeHours = (Server.Time.Hour > StartHour) && (Server.Time.Hour < StopHour);
            bool condition_UpSignal = highToHigh.LastValue < 0.0001;

            var openPosition = Positions.Find("");
            double volume = Symbol.QuantityToVolumeInUnits(0.1);

            //to go long
            if (condition_UpSignal && condition_tradeHours)
            {
                if (!PositionOpenByType(TradeType.Buy))
                {
                    TradeResult result = ExecuteMarketOrder(TradeType.Buy, currency, volume, "", 10, null);
                }
            }
        }

        protected override void OnStop()
        {
        }

        private bool PositionOpenByType(TradeType type)
        {
            var p = Positions.FindAll("", currency, type);

            if (p.Length >= 1)
            {
                return true;
            }

            return false;
        }


        private void ManageStop()
        {
            if (PositionOpenByType(TradeType.Buy))
            {

                var position = Positions.Find("");
                var bid = Symbol.Bid;
                var ask = Symbol.Ask;
                var pipSize = Symbol.PipSize;
                var high = MarketSeries.High.Last(1);
                double distance = ask - position.EntryPrice;

                double newStopLossPrice = high - TrailingStopStep * pipSize;

                if (distance > TrailingStopTrigger * pipSize)
                {
                    if (newStopLossPrice > position.StopLoss)
                    {
                        ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                        if (bid < newStopLossPrice)
                        {
                            ClosePosition(TradeType.Sell);
                        }
                    }
                }
            }
        }
        private void ClosePosition(TradeType type)
        {
            var p = Positions.Find("", currency, type);

            if (p != null)
            {
                ClosePosition(p);
            }
        }
    }
}

The custom indicator:

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

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

        [Parameter("Periods", DefaultValue = 10)]
        public int periods { get; set; }

        [Output("H to H MA")]
        public IndicatorDataSeries highToHigh { get; set; }

        private MovingAverage diffsAve;
        private IndicatorDataSeries highTradesDiff;


        protected override void Initialize()
        {
            highTradesDiff = CreateDataSeries();
            diffsAve = Indicators.MovingAverage(highTradesDiff, periods, MovingAverageType.Simple);
        }

        public override void Calculate(int index)
        {
            highTradesDiff[index] = MarketSeries.High[index - 1] - MarketSeries.High[index - 2];
            highToHigh[index] = diffsAve.Result[index];
        }
    }
}

 


@lucrum
Replies

PanagiotisCharalampous
06 Aug 2019, 15:17

Hi Nick,

Thanks for posting in our forum. Try changing the following line

bool condition_UpSignal = highToHigh.LastValue < 0.0001;

to the following

bool condition_UpSignal = highToHigh.highToHigh.LastValue < 0.0001;

Best Regards,

Panagiotis


@PanagiotisCharalampous

lucrum
06 Aug 2019, 16:47

 

Thank's Panagiotis. What is the logic behind repeating 'highToHigh'?

 

Nick


@lucrum

PanagiotisCharalampous
06 Aug 2019, 16:54

Hi Nick,

The first highToHigh is the object you defined here

highToHigh = Indicators.GetIndicator<HighToHighMA>(periods);

The second highToHigh is its property

        [Output("H to H MA")]
        public IndicatorDataSeries highToHigh { get; set; }

So if you have named you indicator as below

NicksIndicator = Indicators.GetIndicator<HighToHighMA>(periods);

then the code would change to

bool condition_UpSignal = NicksIndicator.highToHigh.LastValue < 0.0001;

Best Regards,

Panagiotis


@PanagiotisCharalampous

lucrum
08 Aug 2019, 20:44

Thank you for the explanation. I am getting a similar problem with accessing the value of horizontal lines from an indicator. At this stage all I want to do is print the value in the bot.

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

namespace cAlgo
{

    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ActiveAreas : Indicator
    {
        public Color _textColor;

        [Parameter("High Color", DefaultValue = "DarkOrange")]
        public string ColorH { get; set; }

        [Parameter("Low Color", DefaultValue = "Orange")]
        public string ColorL { get; set; }

        [Parameter("Low Lookback", DefaultValue = "0")]
        public int LowLookback { get; set; }

        [Parameter("High Lookback", DefaultValue = "2")]
        public int HighLookback { get; set; }

        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            var series = MarketData.GetSeries(TimeFrame.Daily);
            var highPrice = series.High.Last(HighLookback);
            var lowPrice = series.Low.Last(LowLookback);

            Chart.DrawHorizontalLine("Active Area High", highPrice, ColorH, 2, LineStyle.Solid);
            Chart.DrawHorizontalLine("Active Area Low", lowPrice, ColorL, 2, LineStyle.Solid);
            Print("High trade = {0}", highPrice);

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




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

        public string currency = "EURUSD";
        //Create an object (same as an instance) of the Class HighToHighMA (the indicator). In this example the object name is 'linesIndicator'.
        private ActiveAreas linesIndicator;

        protected override void OnStart()
        {
            //This defines what the object does.
            linesIndicator = Indicators.GetIndicator<ActiveAreas>();
        }

        protected override void OnTick()
        {

        }

        protected override void OnBar()
        {
            Print("Weekly high is {0}", linesIndicator.highPrice);
        }


    }
}

 

 


@lucrum

PanagiotisCharalampous
09 Aug 2019, 09:14

Hi Nick,

You are trying to access properties that do not exist in the indicator. There is no highPrice defined, only some local variables. The property needs to be global and public to be accessible.

Best Regards,

Panagiotis


@PanagiotisCharalampous