Parameter Attribute Issue

Created at 18 Jun 2021, 16:36
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!
KR

kris392001

Joined 06.06.2021

Parameter Attribute Issue
18 Jun 2021, 16:36


Hi All,

I'm new to cTrader and am in the process of building a C# framework to build and test strategies.

I'm having a problem with the Parameter Attributes. I can use a Property in the Robot to set a value in a property in a class instance (Strategy), but as soon as I put a Parameter Attribute on the property in the Robot I get the following error in a backtest : Error #90583624: Can not create instance.

Are there some restrictions on using the Parameter Attribute? Code below and any help would be appreciated.

Cheers

Chris

using cAlgo.API;
using Bodyclock.FX.Baseline;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Baseline : Robot
    {
        //[Parameter("Risk (%)", DefaultValue = 2.0, MinValue = 1.0, MaxValue = 5.0, Step = 1.0, Group = "Risk Management")]
        public double RiskPercentage
        {
            get { return riskPercentage; }
            set { riskPercentage = SetRisk(value); }
        }

        private BaselineStrategy Strategy;
        private double riskPercentage;

        protected override void OnStart()
        {
            Strategy = new BaselineStrategy(this);
            RiskPercentage = 4.0;
        }

        protected override void OnBar()
        {
            Strategy.Execute();
        }

        protected override void OnTick()
        {
            Strategy.Manage();
        }

        private double SetRisk(double value)
        {
            Print("Setting Risk Percentage");
            Strategy.RiskPercentage = value / 100;
            return value;
        }
    }
}

 


@kris392001
Replies

amusleh
18 Jun 2021, 16:57 ( Updated at: 18 Jun 2021, 16:58 )

Hi,

You should not use Print method inside property setter method, this works fine:

using cAlgo.API;


namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Baseline : Robot
    {
        [Parameter("Risk (%)", DefaultValue = 2.0, MinValue = 1.0, MaxValue = 5.0, Step = 1.0, Group = "Risk Management")]
        public double RiskPercentage
        {
            get 
            { 
                return riskPercentage;
            }
            set 
            { 
                riskPercentage = SetRisk(value);
            }
        }


        private double riskPercentage;

        protected override void OnStart()
        {

            RiskPercentage = 4.0;
        }

        protected override void OnBar()
        {

        }

        protected override void OnTick()
        {

        }

        private double SetRisk(double value)
        {
            return value;
        }
    }
}

And Strategy is null when you access it inside setter method, that can also cause this issue, because OnStart is not called yet.


@amusleh

kris392001
18 Jun 2021, 17:07

Thanks, pointed me in the right direction. A null check for the Strategy instance solved the problem.

        private double SetRisk(double value)
        {
            if (Strategy != null)
                Strategy.RiskPercentage = value / 100;
            return value;
        }

 


@kris392001