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;
        }
    }
}
Replies
                     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

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:
And Strategy is null when you access it inside setter method, that can also cause this issue, because OnStart is not called yet.
@amusleh