Creating a multiple sma indicator

Created at 18 Jan 2013, 00:42
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!
Scott's avatar

Scott

Joined 18.01.2013

Creating a multiple sma indicator
18 Jan 2013, 00:42


I am tring to create a sma indicator with 8 sma lines displayed overlayed.

The first sma line shows on the chart but the other 7 don't, Im not sure what I've done wrong I've looked though all the examples and would like a little help.  Also is there away to have sma line have a straight line across as well?

This is what I've got so far

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class sma8 : Indicator
    {
        [Parameter]
        public DataSeries Source { get; set; }

        [Parameter("L1", DefaultValue = 8)]
        public int Periods { get; set; }
        [Parameter("L2", DefaultValue = 11)]
        public int Periods2{ get; set; }
        [Parameter("L3", DefaultValue = 16)]
        public int Periods3{ get; set; }
        [Parameter("L4", DefaultValue = 27)]
        public int Periods4{ get; set; }
        [Parameter("L5", DefaultValue = 43)]
        public int Periods5{ get; set; }
        [Parameter("L6", DefaultValue = 75)]
        public int Periods6{ get; set; }
        [Parameter("L7", DefaultValue = 100)]
        public int Periods7{ get; set; }
        [Parameter("L8", DefaultValue = 144)]
        public int Periods8{ get; set; }
        
        [Output("Level1", Color = Colors.Green, Thickness = 5)]
        public IndicatorDataSeries Result { get; set; }
        [Output("Level2", Color = Colors.Yellow, Thickness = 5)]
        public IndicatorDataSeries Result2{ get; set; }
        [Output("Level3", Color = Colors.White, Thickness = 5)]
        public IndicatorDataSeries Result3{ get; set; }
        [Output("Level4", Color = Colors.Aqua, Thickness = 5)]
        public IndicatorDataSeries Result4{ get; set; }        
        [Output("Level5", Color = Colors.Gray, Thickness = 5)]
        public IndicatorDataSeries Result5{ get; set; }
        [Output("Level6", Color = Colors.Orange, Thickness = 5)]
        public IndicatorDataSeries Result6{ get; set; }
        [Output("Level7", Color = Colors.Red, Thickness = 5)]
        public IndicatorDataSeries Result7{ get; set; }
        [Output("Level8", Color = Colors.Purple, Thickness = 5)]
        public IndicatorDataSeries Result8{ get; set; }        
      
        
             public override void Calculate(int index)   
        {
            double sum = 0.0;

            for (int i = index - Periods + 1; i <= index; i++)
            {
                sum += Source[i];
            }
            Result[index] = sum / Periods;
        }
        
    }
}

Thanks Scott


@Scott
Replies

admin
18 Jan 2013, 11:59

You can use the build in Indicator "SimpleMovingAverage"

 

private SimpleMovingAverage _simpleMovingAverage1;
private SimpleMovingAverage _simpleMovingAverage2;
private SimpleMovingAverage _simpleMovingAverage3;
// ... etc.

protected override void Initialize()
{
     _simpleMovingAverage1 = Indicators.SimpleMovingAverage(Source, Period1);
     _simpleMovingAverage2 = Indicators.SimpleMovingAverage(Source, Period2);
     _simpleMovingAverage3 = Indicators.SimpleMovingAverage(Source, Period3);
      //...etc
}
public override void Calculate(int index)
{       
     Result[index] = _simpleMovingAverage1.Result[index];
     Result2[index] = _simpleMovingAverage2.Result[index];
     //...etc
}

See more examples with build in Indicators at the API Reference

 

 

 

 

 

 


@admin

Scott
18 Jan 2013, 23:26 ( Updated at: 21 Dec 2023, 09:20 )

RE: sma
admin said:

You can use the build in Indicator "SimpleMovingAverage"

 

private SimpleMovingAverage _simpleMovingAverage1;
private SimpleMovingAverage _simpleMovingAverage2;
private SimpleMovingAverage _simpleMovingAverage3;
// ... etc.

protected override void Initialize()
{
     _simpleMovingAverage1 = Indicators.SimpleMovingAverage(Source, Period1);
     _simpleMovingAverage2 = Indicators.SimpleMovingAverage(Source, Period2);
     _simpleMovingAverage3 = Indicators.SimpleMovingAverage(Source, Period3);
      //...etc
}
public override void Calculate(int index)
{       
     Result[index] = _simpleMovingAverage1.Result[index];
     Result2[index] = _simpleMovingAverage2.Result[index];
     //...etc
}

See more examples with build in Indicators at the API Reference

 

 

 

 

 

 

Thanks for getting back to me, I've put the changes in but 1 error has come up

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class sma8 : Indicator
    {
        [Parameter]
        public DataSeries Source { get; set; }

       private SimpleMovingAverage _simpleMovingAverage1;
       private SimpleMovingAverage _simpleMovingAverage2;
       private SimpleMovingAverage _simpleMovingAverage3;


protected override void Initialize()
{
     _simpleMovingAverage1 = Indicators.SimpleMovingAverage(Source, Period1);
     _simpleMovingAverage2 = Indicators.SimpleMovingAverage(Source, Period2);
     _simpleMovingAverage3 = Indicators.SimpleMovingAverage(Source, Period3);

}
public override void Calculate(int index)
{       
     Result[index] = _simpleMovingAverage1.Result[index];
     Result2[index] = _simpleMovingAverage2.Result[index];
     Result2[index] = _simpleMovingAverage3.Result[index];
}
      
        
             public override void Calculate(int index)   
        {
            double sum = 0.0;

            for (int i = index - Periods + 1; i <= index; i++)
            {
                sum += Source[i];
            }
            Result[index] = sum / Periods;
        }
        
    }
} 


@Scott

hichem
19 Jan 2013, 08:43 ( Updated at: 21 Dec 2023, 09:20 )

RE: RE: sma
Scott said:
admin said:

You can use the build in Indicator "SimpleMovingAverage"

 

private SimpleMovingAverage _simpleMovingAverage1;
private SimpleMovingAverage _simpleMovingAverage2;
private SimpleMovingAverage _simpleMovingAverage3;
// ... etc.

protected override void Initialize()
{
     _simpleMovingAverage1 = Indicators.SimpleMovingAverage(Source, Period1);
     _simpleMovingAverage2 = Indicators.SimpleMovingAverage(Source, Period2);
     _simpleMovingAverage3 = Indicators.SimpleMovingAverage(Source, Period3);
      //...etc
}
public override void Calculate(int index)
{       
     Result[index] = _simpleMovingAverage1.Result[index];
     Result2[index] = _simpleMovingAverage2.Result[index];
     //...etc
}

See more examples with build in Indicators at the API Reference

 

 

 

 

 

 

Thanks for getting back to me, I've put the changes in but 1 error has come up

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class sma8 : Indicator
    {
        [Parameter]
        public DataSeries Source { get; set; }

       private SimpleMovingAverage _simpleMovingAverage1;
       private SimpleMovingAverage _simpleMovingAverage2;
       private SimpleMovingAverage _simpleMovingAverage3;


protected override void Initialize()
{
     _simpleMovingAverage1 = Indicators.SimpleMovingAverage(Source, Period1);
     _simpleMovingAverage2 = Indicators.SimpleMovingAverage(Source, Period2);
     _simpleMovingAverage3 = Indicators.SimpleMovingAverage(Source, Period3);

}
public override void Calculate(int index)
{       
     Result[index] = _simpleMovingAverage1.Result[index];
     Result2[index] = _simpleMovingAverage2.Result[index];
     Result2[index] = _simpleMovingAverage3.Result[index];
}
      
        
             public override void Calculate(int index)   
        {
            double sum = 0.0;

            for (int i = index - Periods + 1; i <= index; i++)
            {
                sum += Source[i];
            }
            Result[index] = sum / Periods;
        }
        
    }
} 

You implement the Calculate() method twice. You should remove this bloc of code:

public override void Calculate(int index)   
        {
            double sum = 0.0;

            for (int i = index - Periods + 1; i <= index; i++)
            {
                sum += Source[i];
            }
            Result[index] = sum / Periods;
        }




@hichem

Scott
21 Jan 2013, 00:41 ( Updated at: 21 Dec 2023, 09:20 )

RE: RE: RE: sma

 

Hi thanks, its still not working I removed the block of code you said too and it had the same problem below for the result,results2,results3, I added the Output as I want to be able to control color and line type.  Also how can I set the line width I put it in the Parameters

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class sma8 : Indicator
    {
        [Parameter]
        public DataSeries Source { get; set; }

       private SimpleMovingAverage _simpleMovingAverage1;
       private SimpleMovingAverage _simpleMovingAverage2;
       private SimpleMovingAverage _simpleMovingAverage3; 


protected override void Initialize()
{
     _simpleMovingAverage1 = Indicators.SimpleMovingAverage(Source, Period1);
     _simpleMovingAverage2 = Indicators.SimpleMovingAverage(Source, Period2);
     _simpleMovingAverage3 = Indicators.SimpleMovingAverage(Source, Period3);

}
public override void Calculate(int index)
{       
     Result[index] = _simpleMovingAverage1.Result[index];
     Result2[index] = _simpleMovingAverage2.Result[index];
     Result2[index] = _simpleMovingAverage3.Result[index];
}
       [Output("Period1", Color = Colors.Green)]
              public IndicatorDataSeries Result { get; set; }
     
       [Output("Period2", Color = Colors.Yellow)]
              public IndicatorDataSeries Result2 { get; set; }         
     
       [Output("Period3", Color = Colors.White)]
              public IndicatorDataSeries Result3 { get; set; }         
        
    }
}             
      


@Scott

admin
21 Jan 2013, 12:03

Hello Scott,

You need to declare Period1, Period2, etc. Like you had in your original code as input parameter for example.

Below is the declaration of Period1. It is of type integer (int) and it is taged as an input parameter. 

 [Parameter("L1", DefaultValue = 8)]
 public int Period1 { get; set; }


Useful links : c# Tutorials

Your complete code should be:

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class sma8 : Indicator
    {
        [Parameter]
        public DataSeries Source { get; set; }

        [Parameter("L1", DefaultValue = 8)]
        public int Periods { get; set; }
        [Parameter("L2", DefaultValue = 11)]
        public int Periods2 { get; set; }
        [Parameter("L3", DefaultValue = 16)]
        public int Periods3 { get; set; }
        [Parameter("L4", DefaultValue = 27)]
        public int Periods4 { get; set; }
        [Parameter("L5", DefaultValue = 43)]
        public int Periods5 { get; set; }
        [Parameter("L6", DefaultValue = 75)]
        public int Periods6 { get; set; }
        [Parameter("L7", DefaultValue = 100)]
        public int Periods7 { get; set; }
        [Parameter("L8", DefaultValue = 144)]
        public int Periods8 { get; set; }

        [Output("Level1", Color = Colors.Green, Thickness = 5)]
        public IndicatorDataSeries Result { get; set; }
        [Output("Level2", Color = Colors.Yellow, Thickness = 5)]
        public IndicatorDataSeries Result2 { get; set; }
        [Output("Level3", Color = Colors.White, Thickness = 5)]
        public IndicatorDataSeries Result3 { get; set; }
        [Output("Level4", Color = Colors.Aqua, Thickness = 5)]
        public IndicatorDataSeries Result4 { get; set; }
        [Output("Level5", Color = Colors.Gray, Thickness = 5)]
        public IndicatorDataSeries Result5 { get; set; }
        [Output("Level6", Color = Colors.Orange, Thickness = 5)]
        public IndicatorDataSeries Result6 { get; set; }
        [Output("Level7", Color = Colors.Red, Thickness = 5)]
        public IndicatorDataSeries Result7 { get; set; }
        [Output("Level8", Color = Colors.Purple, Thickness = 5)]
        public IndicatorDataSeries Result8 { get; set; }


        private SimpleMovingAverage _simpleMovingAverage1;
        private SimpleMovingAverage _simpleMovingAverage2;
        private SimpleMovingAverage _simpleMovingAverage3;        
        private SimpleMovingAverage _simpleMovingAverage4;
        private SimpleMovingAverage _simpleMovingAverage5;
        private SimpleMovingAverage _simpleMovingAverage6;
        private SimpleMovingAverage _simpleMovingAverage7;
        private SimpleMovingAverage _simpleMovingAverage8;

        protected override void Initialize()
        {
            _simpleMovingAverage1 = Indicators.SimpleMovingAverage(Source, Periods);
            _simpleMovingAverage2 = Indicators.SimpleMovingAverage(Source, Periods2);
            _simpleMovingAverage3 = Indicators.SimpleMovingAverage(Source, Periods3);
            _simpleMovingAverage4 = Indicators.SimpleMovingAverage(Source, Periods4);
            _simpleMovingAverage5 = Indicators.SimpleMovingAverage(Source, Periods5);
            _simpleMovingAverage6 = Indicators.SimpleMovingAverage(Source, Periods6);
            _simpleMovingAverage7 = Indicators.SimpleMovingAverage(Source, Periods7);
            _simpleMovingAverage8 = Indicators.SimpleMovingAverage(Source, Periods8);

        }
        public override void Calculate(int index)
        {
            Result[index] = _simpleMovingAverage1.Result[index];
            Result2[index] = _simpleMovingAverage2.Result[index];
            Result3[index] = _simpleMovingAverage3.Result[index];
            Result4[index] = _simpleMovingAverage4.Result[index];
            Result5[index] = _simpleMovingAverage5.Result[index];
            Result6[index] = _simpleMovingAverage6.Result[index];
            Result7[index] = _simpleMovingAverage7.Result[index];
            Result8[index] = _simpleMovingAverage8.Result[index];            

        }

    }
}




@admin

monkeykingfx
30 May 2015, 10:45

I want to know if I need to write code for it ALERT warning, however.


@monkeykingfx

kursitamu
02 Jun 2015, 16:16

Re : Creating a multiple sma indicator

This post was removed by moderator.


@kursitamu

Spotware
10 Jun 2015, 14:45

RE:

monkeyking28 said:

I want to know if I need to write code for it ALERT warning, however.

Dear Trader,

Could you please provide us more information regarding your question?


@Spotware