Sample SMA

Created at 15 Nov 2023, 10:12
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!
HU

humza.jelani

Joined 21.02.2023

Sample SMA
15 Nov 2023, 10:12


Hi,

I'm trying to configure a trade entry logic using the sample sma.

Instead of IsRising() and IsFalling() conditions I want to enter into a trade when the Bars.OpenPrices.Last(1) is greater than the Sample SMA.

What do I need to do to execute this cBot properly?

I have initialized the indicator using the OnStart() function.

Here is the code:

        [Parameter(DefaultValue = 10)]
        public int Periods { get; set; }
        
        SampleSMA _sma;
        
        #endregion

        #region Methods
        protected override void OnStart()
        {
            _sma = Indicators.GetIndicator<SampleSMA>(Bars.ClosePrices, Periods);

   // Two consecutive up bars
                        if (Bars.ClosePrices.Last(1) > Bars.OpenPrices.Last(1) 
                        && Bars.ClosePrices.Last(2) > Bars.OpenPrices.Last(2)
                        && Bars.OpenPrices.Last(1) > _sma.Result.LastValue)
                        && Bars.OpenPrices.Last(2) > _sma.Result.LastValue))

Please advise.


@humza.jelani
Replies

humza.jelani
15 Nov 2023, 10:17

The Problem

I am unable to reference sma values using bars ago function as possible using NinjaTrader 8.

Bars.ago made it really simple to reference indicator values certain bars ago - there is an indicator value attached to each bar on the chart in NT8.

Please advise.


@humza.jelani

PanagiotisCharalampous
15 Nov 2023, 10:48

RE: Sample SMA

Hi Humza,

Just use Last() method as you do with ClosePrices and OpenPrices

                        if (Bars.ClosePrices.Last(1) > Bars.OpenPrices.Last(1) 
                        && Bars.ClosePrices.Last(2) > Bars.OpenPrices.Last(2)
                        && Bars.OpenPrices.Last(1) > _sma.Result.Last(1))
                        && Bars.OpenPrices.Last(2) > _sma.Result.Last(2)))

@PanagiotisCharalampous