using a public variable referenced from an indicator in a bot

Created at 16 Feb 2024, 14:27
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!
PJ

pjs.guernsey

Joined 30.03.2021

using a public variable referenced from an indicator in a bot
16 Feb 2024, 14:27


Hi,

I came across the ‘Trend Magic’ indicator, and added it to my indies. It compiles fine. I added a single public double Trend and set that in the code to 1 or -1. I try to reference this from a cBot, but I never get a value other than zero back. Can you tell me what I am doing wrong?  Thanks.

 

Trend magic indie starts like this. The double Trend I added

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TrendMagic : Indicator
    {
        [Parameter("CCI Period", DefaultValue = 20)]
        public int cciPer { get; set; }
        [Parameter("ATR Period", DefaultValue = 5)]
        public int atrPer { get; set; }
        [Parameter("ATR Multiplier", DefaultValue = 1)]
        public int atrMul { get; set; }

        [Output("Trend Magic", LineColor = "Red")]
        public IndicatorDataSeries TM { get; set; }
        [Output("Trend Magic Up", PlotType = PlotType.DiscontinuousLine, LineColor = "Lime", Thickness = 2)]
        public IndicatorDataSeries TMUp { get; set; }
        [Output("Trend Magic Down", PlotType = PlotType.DiscontinuousLine, LineColor = "Red", Thickness = 2)]
        public IndicatorDataSeries TMDown { get; set; }

        private CommodityChannelIndex cci;
        private AverageTrueRange atr;
        private IndicatorDataSeries bufferUp, bufferDown, x, swap;
        public double Trend = 0;

 

I declared it in my Cbot like this

 

        private TrendMagic TM1;
        private double Trend = 0;

 

and in OnStart

TM1 = Indicators.GetIndicator<TrendMagic>(20, 5, 1);

 

Then in Onbar, I am doing this

Trend = TM1.Trend;

it all compiles fine, but ‘Trend’ is always a zero value.

Any pointers would be appreciated (sorry - New to cTrader API and still trying to figure some stuff out!)  Thanks.

 

 

 


@pjs.guernsey
Replies

pjs.guernsey
16 Feb 2024, 16:35

OK, so, I have a workaround, but I do not understand why it does not work

I can use the code per above, and 

Print(Trend.ToString()) - Trend is always ZERO

BUT

Print(Trend.ToString()+"/"+TM1.TMUp[0]+"/"+TM1.TMDown[0])

reference the plot values, and the ‘Trend’ Value starts working! The only code change is to print the value of the plot! Somehow, referencing the plot makes the value work! 


@pjs.guernsey

AlgoCreators
16 Feb 2024, 23:15

Your explanation is a little unclear, but have you checked this out?

  1. Trend = TM1.Trend.LastValue;

@AlgoCreators