[Help] Problem while creating multiple SMAs and trying to use it as source in Standard Deviation in Robot

Created at 15 Oct 2023, 03:47
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!
JI

JINGXUAN

Joined 23.08.2023

[Help] Problem while creating multiple SMAs and trying to use it as source in Standard Deviation in Robot
15 Oct 2023, 03:47


Hi guys, I found a problem while creating multiple SMAs and trying to use it as source in standard deviation in robot it’s not possible to add up these SMAs, it says operator ‘+’ cannot be applied to operands of type ’SimpleMoving Average’ and ’SimpleMoving Average’, can anyone help me with the problem?

eg:

var sma1 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 50);

var sma2 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 100);

var sma3 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 150);

var sma4 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 200);

var sma_1234 = (((sma1+sma2)/2) + ((sma3+sma4)/2))/2;


var sd = Indicators.StandardDeviation(sma_1234.Result,50, MovingAverageType.Simple);

 

var sma_1234 = (((sma1+sma2)/2) + ((sma3+sma4)/2))/2 this is the part where it cannot do it in robot it has to be done it alone in an new indicator calculate and return a single result as output, but I am too new to code, could anyone please help with the indicator code? Much appreciated !

 

This is my SMA code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = true, AccessRights = AccessRights.None)]
    public class SMAsExample : Indicator
    {
        //SMA
        [Parameter("SMA Source")]
        public DataSeries SMASource { get; set; }
        
        [Parameter(DefaultValue = 10)]
        public int SMAPeriods10 { get; set; }

        [Parameter(DefaultValue = 20)]
        public int SMAPeriods20 { get; set; }

        [Parameter(DefaultValue = 50)]
        public int SMAPeriods50 { get; set; }

        [Parameter(DefaultValue = 100)]
        public int SMAPeriods100 { get; set; }

        [Parameter(DefaultValue = 150)]
        public int SMAPeriods150 { get; set; }

        [Parameter(DefaultValue = 200)]
        public int SMAPeriods200 { get; set; }
        
        [Output("SMA10"             , LineColor = "White"           , PlotType = PlotType.Line  , Thickness = 1)]
        public IndicatorDataSeries SMA10 { get; set; }                       
                                                                                    
        [Output("SMA20"             , LineColor = "White"           , PlotType = PlotType.Line  , Thickness = 1)]
        public IndicatorDataSeries SMA20 { get; set; }                      
        
        [Output("SMA_1_line"        , LineColor = "Yellow"          , PlotType = PlotType.Line  , Thickness = 2)]
        public IndicatorDataSeries SMA_1_line { get; set; }                       
                                                                                    
        [Output("SMA_2_line"        , LineColor = "Orange"          , PlotType = PlotType.Line  , Thickness = 2)]
        public IndicatorDataSeries SMA_2_line { get; set; }                       
                                                                                    
        [Output("SMA_3_line"        , LineColor = "Red"             , PlotType = PlotType.Line  , Thickness = 2)]
        public IndicatorDataSeries SMA_3_line { get; set; }                       
                                                                                    
        [Output("SMA_4_line"        , LineColor = "Blue"            , PlotType = PlotType.Line  , Thickness = 2)]
        public IndicatorDataSeries SMA_4_line { get; set; }                       
                                                                                    
        [Output("SMA_12_line"            , LineColor = "Green"      , PlotType = PlotType.Line  , Thickness = 2)]
        public IndicatorDataSeries SMA_12_line { get; set; }                      
                                                                                    
        [Output("SMA_23_line"            , LineColor = "Purple"     , PlotType = PlotType.Line  , Thickness = 2)]
        public IndicatorDataSeries SMA_23_line { get; set; }

        [Output("SMA_1234_line"          , LineColor = "Cyan"       , PlotType = PlotType.Points, Thickness = 1)]
        public IndicatorDataSeries SMA_1234_line { get; set; }
        
        public SimpleMovingAverage _sma10           ;
        public SimpleMovingAverage _sma20           ;
        public SimpleMovingAverage _sma1            ;
        public SimpleMovingAverage _sma2            ;
        public SimpleMovingAverage _sma3            ;
        public SimpleMovingAverage _sma4            ;
        public IndicatorDataSeries _sma1234         ;
        
        protected override void Initialize()
        {
            _sma10   = Indicators.SimpleMovingAverage(SMASource, SMAPeriods10)   ;
            _sma20   = Indicators.SimpleMovingAverage(SMASource, SMAPeriods20)  ;
            _sma1    = Indicators.SimpleMovingAverage(SMASource, SMAPeriods50)  ;
            _sma2    = Indicators.SimpleMovingAverage(SMASource, SMAPeriods100)  ;
            _sma3    = Indicators.SimpleMovingAverage(SMASource, SMAPeriods150) ;
            _sma4    = Indicators.SimpleMovingAverage(SMASource, SMAPeriods200) ;
        }

        public override void Calculate(int index)
        {
            SMA10[index]            = _sma10.Result[index]  ;
            SMA20[index]            = _sma20.Result[index]  ;
            SMA_1_line[index]       = _sma1.Result[index]   ;
            SMA_2_line[index]       = _sma2.Result[index]   ;
            SMA_3_line[index]       = _sma3.Result[index]   ;
            SMA_4_line[index]       = _sma4.Result[index]   ;

            
            // Calculate additional SMAs
            SMA_12_line[index]    = (_sma1.Result[index]+_sma2.Result[index])/2;
            SMA_23_line[index]    = (_sma3.Result[index]+_sma4.Result[index])/2;
            SMA_1234_line[index]  = (((_sma1.Result[index]+_sma2.Result[index])/2) + ((_sma3.Result[index]+_sma4.Result[index])/2))/2;
        }
    }
}

 

Please help, thanks in advanced!


@JINGXUAN
Replies

PanagiotisChar
16 Oct 2023, 04:18

Hi there, 

I don't see what the problem is. The indicator is working fine.

Best regards,

cTrader Team


@PanagiotisChar