Crash on start: incorrect parameters count

Created at 12 Jun 2020, 14:59
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

Crash on start: incorrect parameters count
12 Jun 2020, 14:59


The indicator works as intended. On the face of it the parameters look fine. Can someone point out where I have erred?

 

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 LRSqueryIndic : Indicator
    {
        [Parameter("Number of bars for LRS", DefaultValue = 10)]
        public int BarsQ { get; set; }

        [Output("Slope", LineColor = "RoyalBlue")]
        public IndicatorDataSeries LRSslope { get; set; }

        private LinearRegressionSlope slope;

        protected override void Initialize()
        {
            slope = Indicators.LinearRegressionSlope(Bars.ClosePrices, BarsQ);
        }

        public override void Calculate(int index)
        {
            LRSslope[index] = slope.Result[index];
        }
    }
}

 

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 LRSqueryCbot : Robot
    {

        [Parameter("Number of bars for LRS", DefaultValue = 10)]
        public int BarsQ { 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; }

        public string currency = "GBPUSD";

        private LRSqueryIndic slopeDiff;

        protected override void OnStart()
        {
            slopeDiff = Indicators.GetIndicator<LRSqueryIndic>(Bars.ClosePrices, BarsQ);
        }

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


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

            bool condition_long = slopeDiff.LRSslope.Last(1) > 0.0;
            bool condition_short = slopeDiff.LRSslope.Last(1) < 0.0;

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

            //to go long
            if (condition_long)
            {
                if (!PositionOpenByType(TradeType.Buy))
                {
                    CloseOpenPosition(TradeType.Sell);
                    TradeResult result = ExecuteMarketOrder(TradeType.Buy, currency, volume, "", 10, null);
                }
            }
            //to go short
            if (condition_short)
            {
                if (!PositionOpenByType(TradeType.Sell))
                {
                    CloseOpenPosition(TradeType.Buy);
                    TradeResult result = ExecuteMarketOrder(TradeType.Sell, 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 CloseOpenPosition(TradeType type)
        {
            var p = Positions.Find("", currency, type);

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


        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 = Bars.HighPrices.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);
                        }
                    }
                }
            }

            if (PositionOpenByType(TradeType.Sell))
            {
                var position = Positions.Find("");
                var bid = Symbol.Bid;
                var ask = Symbol.Ask;
                var pipSize = Symbol.PipSize;
                var low = Bars.LowPrices.Last(1);

                double distance = position.EntryPrice - bid;
                double newStopLossPrice = low + TrailingStopStep * pipSize;

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

                        if (ask > newStopLossPrice)
                        {
                            ClosePosition(TradeType.Buy);
                        }
                    }
                }
            }
        }
        private void ClosePosition(TradeType type)
        {
            var p = Positions.Find("", currency, type);

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


@lucrum
Replies

PanagiotisCharalampous
12 Jun 2020, 15:02

Hi lucrum,

Your indicator has only one parameter but you are initializing it with two. 

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

lucrum
12 Jun 2020, 16:47

RE:

PanagiotisCharalampous said:

Hi lucrum,

Your indicator has only one parameter but you are initializing it with two. 

Best Regards,

Panagiotis 

Join us on Telegram

 

Thank you. A touch of Fridayitis.

Nick


@lucrum